From dbbbb3eebcb99116b9afd768967e7492882737a9 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Tue, 21 Apr 2020 21:21:50 +0430 Subject: work in progress --- cgrep.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) (limited to 'cgrep.cpp') diff --git a/cgrep.cpp b/cgrep.cpp index 4391e5c..0a0888c 100644 --- a/cgrep.cpp +++ b/cgrep.cpp @@ -7,7 +7,27 @@ * */ /***********************************************************************************************/ /*included modules*/ -#include "./pch.hpp" +#include "./cfe-extra/cfe_extra.h" +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/LLVM.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Lex/Lexer.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "clang/Tooling/CommonOptionsParser.h" +#include "clang/Tooling/Tooling.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include +#include +#include +#include +#include +#include /***********************************************************************************************/ /*used namespaces*/ using namespace llvm; @@ -205,7 +225,7 @@ bool regex_handler(std::string rx_str, std::string identifier_name) { * @param SR source range for the matched result * @param SM sourcemanager * @param isdecl is the matched result a delaration - * @param DTN the matched result cast to dynamically typed node + * @param DTN the matched result cast to a dynamically typed node */ void output_handler(const MatchFinder::MatchResult &MR, SourceRange SR, SourceManager &SM, bool isdecl, @@ -724,7 +744,7 @@ public: if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void(); auto NameRef = VD->getName(); - if (!NameRef.empty()) { + if (CO_TRACE == NameRef.str()) { SubMatcher.setND(VD->getCanonicalDecl()->getUnderlyingDecl()); Matcher.addMatcher(declRefExpr(to(varDecl(hasName(VD->getName())))) .bind("tracevardeclrefexpr"), @@ -741,6 +761,29 @@ private: TraceVarHandlerSub SubMatcher; }; /***********************************************************************************************/ +class SubDynamicMatcher : public MatchFinder::MatchCallback { + public: + explicit SubDynamicMatcher(Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) override {} + + private: + MatchFinder Matcher; + Rewriter &Rewrite [[maybe_unused]]; +}; +/***********************************************************************************************/ +class DynamicMatcher : public MatchFinder::MatchCallback { + public: + explicit DynamicMatcher(Rewriter &Rewrite) : Rewrite(Rewrite), SubDynamicHandler(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) override {} + + private: + MatchFinder Matcher; + Rewriter &Rewrite [[maybe_unused]]; + SubDynamicMatcher SubDynamicHandler; +}; +/***********************************************************************************************/ class PPInclusion : public PPCallbacks { public: explicit PPInclusion(SourceManager *SM, Rewriter *Rewrite) @@ -857,6 +900,20 @@ private: MatchFinder Matcher; }; /***********************************************************************************************/ +class DynamicASTConsumer : public ASTConsumer { +public: + explicit DynamicASTConsumer(Rewriter &R) : DynamicHandler(R) { + } + + void HandleTranslationUnit(ASTContext &Context) override { + Matcher.matchAST(Context); + } + +private: + DynamicMatcher DynamicHandler; + MatchFinder Matcher; +}; +/***********************************************************************************************/ class CgrepASTConsumer : public ASTConsumer { public: explicit CgrepASTConsumer(Rewriter &R) -- cgit v1.2.3