From dbbbb3eebcb99116b9afd768967e7492882737a9 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Tue, 21 Apr 2020 21:21:50 +0430 Subject: work in progress --- CONTRIBUTORS.md | 5 ++-- README.md | 6 ++--- cgrep.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++--- compile_commands.json | 7 +++++- makefile | 11 ++++----- testscript/main.py | 4 +++- 6 files changed, 78 insertions(+), 18 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 9373abc..7364ddb 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,6 +1,5 @@ - ## Contributors The list is in chronological order:
- * bloodstalker - * Yeger +* bloodstalker +* Yeger diff --git a/README.md b/README.md index 4ecd206..e251032 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ and it will match your regex against all function declarations, and will output ## Features -* It's basically Clang regexing it's way through your C-family source-code. You have all the context you can ever need. -* Can output whether to print the declaration of a match even if the match itself is not a declaration along with the matched result. -* Can output matches in a script-friendly format which could be used in turn by a secondary script. + * It's basically Clang regexing it's way through your C-family source-code. You have all the context you can ever need. + * Can output whether to print the declaration of a match even if the match itself is not a declaration along with the matched result. + * Can output matches in a script-friendly format which could be used in turn by a secondary script. ### Will cgrep try to implement all of the grep switches? The answer is no. The main distinction is that `cgrep` is only meant to work on C-family source files not text files. Most of `grep`'s switches don't apply to the usecase or provide almost no benefits at all.
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) diff --git a/compile_commands.json b/compile_commands.json index b74aadf..598f647 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -1,7 +1,12 @@ [ { - "command": "c++ -c -include-pch pch.hpp.gch -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -std=c++14 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++17 -fexceptions -o cgrep.o cgrep.cpp", + "command": "c++ -c -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -std=c++14 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++17 -fexceptions -o cgrep.o cgrep.cpp", "directory": "/home/bloodstalker/extra/cgrep", "file": "/home/bloodstalker/extra/cgrep/cgrep.cpp" + }, + { + "command": "c++ -c -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -std=c++14 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++17 -fexceptions -o cfe-extra/cfe_extra.o cfe-extra/cfe_extra.cpp", + "directory": "/home/bloodstalker/extra/cgrep", + "file": "/home/bloodstalker/extra/cgrep/cfe-extra/cfe_extra.cpp" } ] \ No newline at end of file diff --git a/makefile b/makefile index cff732a..94fd556 100644 --- a/makefile +++ b/makefile @@ -84,7 +84,7 @@ LD_FLAGS+=$(EXTRA_LD_FLAGS) .PHONY:all clean help ASM SO TAGS -all: pch.hpp.gch $(TARGET) +all: $(TARGET) everything:$(TARGET) A ASM SO $(TARGET)-dbg TAGS $(TARGET)-cov @@ -98,18 +98,15 @@ depend:.depend -include ./.depend -%.o:%.cpp pch.hpp.gch - $(CXX) -include-pch pch.hpp.gch $(CXX_FLAGS) -c $< -o $@ +%.o:%.cpp + $(CXX) $(CXX_FLAGS) -c $< -o $@ %.odbg:%.cpp $(CXX) $(CXX_FLAGS) -g -c $< -o $@ -%.ocov:%.cpp pch.hpp.gch +%.ocov:%.cpp $(CXX) $(CXX_FLAGS) $(COV_CXX) -c $< -o $@ -pch.hpp.gch: pch.hpp - $(CXX) $(CXX_FLAGS) -c $< -o $@ - ./cfe-extra/cfe_extra.o:./cfe-extra/cfe_extra.cpp $(CXX) $(CXX_FLAGS) -c $< -o $@ diff --git a/testscript/main.py b/testscript/main.py index 4990d10..013ab99 100755 --- a/testscript/main.py +++ b/testscript/main.py @@ -2,13 +2,15 @@ # _*_ coding=utf-8 _*_ import argparse +import subprocess +import os +import sys class Argparser(object): def __init__(self): parser = argparse.ArgumentParser() parser.add_argument("--string", type=str, help="string") parser.add_argument("--bool", action="store_true", help="bool", default=False) - parser.add_argument("--dbg", action="store_true", help="debug", default=False) self.args = parser.parse_args() def main(): -- cgit v1.2.3