diff options
author | bloodstalker <thabogre@gmail.com> | 2016-09-02 14:32:28 +0000 |
---|---|---|
committer | bloodstalker <thabogre@gmail.com> | 2016-09-02 14:32:28 +0000 |
commit | 6d4fdb747e3c31c01bbfe3c6db31c4ec67ee9380 (patch) | |
tree | edc5da51bfcec2f651059cb49ec8359f06a25907 /mutator.cpp | |
parent | some minor changes. (diff) | |
download | mutator-6d4fdb747e3c31c01bbfe3c6db31c4ec67ee9380.tar.gz mutator-6d4fdb747e3c31c01bbfe3c6db31c4ec67ee9380.zip |
some minor changes
Diffstat (limited to '')
-rw-r--r-- | mutator.cpp | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/mutator.cpp b/mutator.cpp index a53dc8a..f799809 100644 --- a/mutator.cpp +++ b/mutator.cpp @@ -4,6 +4,7 @@ /*included modules*/ /*standard library*/ #include <string> +#include <iostream> /*LLVM-libs*/ #include "clang/AST/AST.h" #include "clang/AST/ASTConsumer.h" @@ -17,12 +18,9 @@ #include "clang/Rewrite/Core/Rewriter.h" #include "llvm/Support/raw_ostream.h" #include "llvm/IR/Function.h" -/*adding the -- deafult option is not a good choice since we need extra flags to compile code or else the parsers not gonna parse all of the target file.*/ -#if 0 -#include "llvm/support/CommandLine.h" -#endif /**********************************************************************************************************************/ /*used namespaces*/ +using namespace llvm; using namespace clang; using namespace clang::ast_matchers; using namespace clang::driver; @@ -32,37 +30,51 @@ using namespace clang::tooling; static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample"); - /**********************************************************************************************************************/ -/*matcher callback for 'if' and 'else if'.*/ +/*matcher callback for something.*/ class FunctionHandler : public MatchFinder::MatchCallback { public: FunctionHandler (Rewriter &Rewrite) : Rewrite(Rewrite) {} virtual void run(const MatchFinder::MatchResult &MR) { - /*dev*/ + if (MR.Nodes.getNodeAs<clang::BinaryOperator>("binopeq") != nullptr) + { + /*underdev*/ + /*get the matched node.*/ + const BinaryOperator *BinOp = MR.Nodes.getNodeAs<clang::BinaryOperator>("binopeq"); + + /*get the sourceloation.*/ + SourceLocation BinOpSL = BinOp->getLocStart(); + + /*does the sourcelocation include a macro expansion?*/ + if ( BinOpSL.isMacroID() ) + { + /*get the expansion range which is startloc and endloc*/ + std::pair <SourceLocation, SourceLocation> expansionRange = Rewrite.getSourceMgr().getImmediateExpansionRange(BinOpSL); + + /*get the startloc.*/ + BinOpSL = expansionRange.first; + } + + /*replace it.*/ + Rewrite.ReplaceText(BinOpSL, 2U , "XXX"); + } } private: Rewriter &Rewrite; }; /**********************************************************************************************************************/ - /**********************************************************************************************************************/ -// Implementation of the ASTConsumer interface for reading an AST produced -// by the Clang parser. It registers a couple of matchers and runs them on -// the AST. class MyASTConsumer : public ASTConsumer { -//friend class CalleeHandler; public: MyASTConsumer(Rewriter &R) : HandlerForFunction(R) { - Matcher.addMatcher (functionDecl(), &HandlerForFunction); + Matcher.addMatcher(binaryOperator(hasOperatorName("==")).bind("binopeq"), &HandlerForFunction); } void HandleTranslationUnit(ASTContext &Context) override { - // Run the matchers when we have the whole TU parsed. Matcher.matchAST(Context); } @@ -71,7 +83,6 @@ private: MatchFinder Matcher; }; /**********************************************************************************************************************/ -// For each source file provided to the tool, a new FrontendAction is created. class MyFrontendAction : public ASTFrontendAction { public: MyFrontendAction() {} |