Skip to content

Commit

Permalink
ast matcher with libtooling
Browse files Browse the repository at this point in the history
  • Loading branch information
Cissakind committed Mar 18, 2022
1 parent 282e761 commit ab02f25
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
metadata.

- `clang-plugins`: suite of examples of clang plugins.


- `ast-matcher`: AST matcher implementation with LibTooling
11 changes: 11 additions & 0 deletions ast-matcher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD "20")
project(forMatcher)

set(CMAKE_BUILD_TYPE RelWithDebInfo)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(lib)
8 changes: 8 additions & 0 deletions ast-matcher/input/for1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int main()
{
int sum = 10;
for (int i = 0; i < 10; i++)
{
sum += i;
}
}
8 changes: 8 additions & 0 deletions ast-matcher/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

find_package(Clang REQUIRED)
include_directories(${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

add_executable(forMatcher forMatcher.cpp)

target_link_libraries(forMatcher clangTooling)
74 changes: 74 additions & 0 deletions ast-matcher/lib/forMatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;
using namespace clang::tooling;


static llvm::cl::OptionCategory MatcherCategory("matcher options");

StatementMatcher forCond = forStmt(
hasLoopInit(
declStmt(
hasSingleDecl(
varDecl(
hasInitializer(
integerLiteral(
equals(0)))).bind("init_var")))));

class MatchPrinter: public MatchFinder::MatchCallback {

public:
virtual void run(const MatchFinder::MatchResult &Result) {
if (const clang::VarDecl *var = Result.Nodes.getNodeAs<clang::VarDecl>("init_var"))
llvm::outs() << var-> getNameAsString();
}
};


int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MatcherCategory);
ClangTool Matcher(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());

MatchPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(traverse(clang::TK_IgnoreUnlessSpelledInSource, forCond), &Printer);
Matcher.run(newFrontendActionFactory(&Finder).get());

return 0;
}


// StatementMatcher forCond = forStmt(hasCondition(
// binaryOperator(
// hasOperatorName("<"),
// hasLHS(ignoringParenImpCasts(declRefExpr(
// to(varDecl(hasType(isInteger())).bind("condVarName"))))),
// hasRHS(
// expr(
// hasType(
// isInteger())).bind("condValue")))));







// int main(int argc, const char **argv) {
// CommonOptionsParser OptionsParser(argc, argv, MatcherCategory);
// ClangTool Matcher(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());

// LoopPrinter Printer;
// MatchFinder Finder;
// Finder.addMatcher(traverse(clang::TK_IgnoreUnlessSpelledInSource, forCond), &Printer);
// Matcher.run(newFrontendActionFactory(&Finder).get());

// return 0;
// }
24 changes: 24 additions & 0 deletions ast-matcher/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env zsh
# =========================================================================== #

llvmBuildDir="$HOME/repos/llvm-project/build"

buildDir=$(realpath -L ./build)

cc=$llvmBuildDir/bin/clang
cxx=$llvmBuildDir/bin/clang++

cmake -S . -B $buildDir -G "Ninja" \
-DLLVM_DIR=${llvmBuildDir}/lib/cmake/llvm \
-DClang_DIR=${llvmBuildDir}/lib/cmake/clang \
-DCMAKE_C_COMPILER=$cc \
-DCMAKE_CXX_COMPILER=$cxx \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON


# --------------------------------------------------------------------------- #

ninja -C $buildDir -j$(nproc)


# =========================================================================== #

0 comments on commit ab02f25

Please sign in to comment.