-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
# =========================================================================== # |