diff options
author | bloodstalker <thabogre@gmail.com> | 2016-11-04 22:56:26 +0000 |
---|---|---|
committer | bloodstalker <thabogre@gmail.com> | 2016-11-04 22:56:26 +0000 |
commit | 45924890c1153c078137038237720de50041c1ef (patch) | |
tree | d3352274ad9785d58aac90c574c911bbafac8621 | |
parent | now builds lvl mutation source file (diff) | |
download | mutator-45924890c1153c078137038237720de50041c1ef.tar.gz mutator-45924890c1153c078137038237720de50041c1ef.zip |
some minor fixes
-rw-r--r-- | mutator.cpp | 79 |
1 files changed, 51 insertions, 28 deletions
diff --git a/mutator.cpp b/mutator.cpp index 59d3e1b..c1d00f9 100644 --- a/mutator.cpp +++ b/mutator.cpp @@ -278,62 +278,85 @@ private: Rewriter &Rewrite; }; /**********************************************************************************************************************/ -class IfFixer : public MatchFinder::MatchCallback +class IfElseFixer : public MatchFinder::MatchCallback { public: - IfFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {} + IfElseFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {} - /*adds curly braces to ifstmts that dont have it.*/ virtual void run(const MatchFinder::MatchResult &MR) { - if (MR.Nodes.getNodeAs<clang::IfStmt>("mrif") != nullptr) + /*underdev*/ + if (MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse") != nullptr) { - const IfStmt *MRIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrif"); + const IfStmt *ElseIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse"); + //const IfStmt *LastIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse"); - SourceLocation FFSL = MRIf->getThen()->getLocStart(); - FFSL = Devi::SourceLocationHasMacro(FFSL, Rewrite, "start"); - SourceLocation FFSLE = MRIf->getThen()->getLocEnd(); - FFSLE = Devi::SourceLocationHasMacro(FFSLE, Rewrite, "end"); + SourceLocation IFESL = ElseIf->getLocStart(); + IFESL = Devi::SourceLocationHasMacro(IFESL, Rewrite, "start"); + SourceLocation IFESLE = ElseIf->getLocEnd(); + IFESLE = Devi::SourceLocationHasMacro(IFESLE, Rewrite, "end"); + SourceRange SR; + SR.setBegin(IFESL); + SR.setEnd(IFESLE); - /*we're getting the endloc with an offset of 2 to accomodate unary operators like '++'.*/ - /*line-terminating semicolons are not included in the matches.*/ - Rewrite.InsertText(FFSL, "{\n", "true", "true"); - Rewrite.InsertTextAfterToken(FFSLE.getLocWithOffset(2U), "\n}"); + clang::Rewriter::RewriteOptions opts; + + int RangeSize = Rewrite.getRangeSize(SR, opts); + + //std::cout << IFESLE.printToString(*MR.SourceManager) << "\n" << std::endl; + +#if 0 + //Rewrite.InsertText(ElseIf->getThen()->getLocStart(), "{\n", "true", "true"); + Rewrite.InsertTextAfterToken(IFESL.getLocWithOffset(RangeSize + 1U), "else\n{/*intentionally left blank*/\n}\n"); +#endif } else { - std::cout << "matcher -- returned nullptr." << std::endl; + std::cout << "matcher -mrifelse- returned nullptr." << std::endl; } } + private: Rewriter &Rewrite; }; /**********************************************************************************************************************/ -class IfElseFixer : public MatchFinder::MatchCallback +class IfFixer : public MatchFinder::MatchCallback { public: - IfElseFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {} + IfFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {} + /*adds curly braces to ifstmts that dont have it.*/ virtual void run(const MatchFinder::MatchResult &MR) { - /*underdev*/ - if (MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse") != nullptr) + if (MR.Nodes.getNodeAs<clang::IfStmt>("mrif") != nullptr) { - const IfStmt *ElseIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse"); + const IfStmt *MRIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrif"); - std::cout << "yolo" << std::endl; + SourceLocation FFSL = MRIf->getThen()->getLocStart(); + FFSL = Devi::SourceLocationHasMacro(FFSL, Rewrite, "start"); + SourceLocation FFSLE = MRIf->getThen()->getLocEnd(); + FFSLE = Devi::SourceLocationHasMacro(FFSLE, Rewrite, "end"); - //Rewrite.InsertText(ElseIf->getThen()->getLocStart(), "{\n", "true", "true"); - Rewrite.InsertTextAfterToken(ElseIf->getLocEnd().getLocWithOffset(2U), "\nelse\n{\n}"); + SourceRange SR; + SR.setBegin(FFSL); + SR.setEnd(FFSLE); + Rewriter::RewriteOptions opts; + int RangeSize = Rewrite.getRangeSize(SR, opts); + + /*we're getting the endloc with an offset of 2 to accomodate unary operators like '++'.*/ + /*line-terminating semicolons are not included in the matches.*/ +#if 1 + Rewrite.InsertText(FFSL, "{\n", "true", "true"); + Rewrite.InsertTextAfterToken(FFSL.getLocWithOffset(RangeSize), "\n}"); +#endif } else { - std::cout << "matcher -mrifelse- returned nullptr." << std::endl; + std::cout << "matcher -- returned nullptr." << std::endl; } } - private: Rewriter &Rewrite; }; @@ -342,7 +365,7 @@ private: class MyASTConsumer : public ASTConsumer { public: - MyASTConsumer(Rewriter &R) : HandlerForFunction(R), HandlerForIfTrap(R), HandlerForStmtTrap(R), HandlerForStmtRet(R), HandlerForFixer(R), HandlerForWhile(R), HandlerForIfFixer(R), HandlerForIfElse(R) { + MyASTConsumer(Rewriter &R) : HandlerForFunction(R), HandlerForIfTrap(R), HandlerForStmtTrap(R), HandlerForStmtRet(R), HandlerForFixer(R), HandlerForWhile(R), HandlerForIfElse(R), HandlerForIfFixer(R) { Matcher.addMatcher(binaryOperator(hasOperatorName("==")).bind("binopeq"), &HandlerForFunction); Matcher.addMatcher(ifStmt(hasCondition(anything())).bind("iftrap"), &HandlerForIfTrap); @@ -355,9 +378,9 @@ public: Matcher.addMatcher(whileStmt(unless(hasDescendant(compoundStmt()))).bind("mrwhile"), &HandlerForWhile); - Matcher.addMatcher(ifStmt(unless(hasDescendant(compoundStmt()))).bind("mrif"), &HandlerForIfFixer); + Matcher.addMatcher(ifStmt(allOf(hasElse(ifStmt()), unless(hasAncestor(ifStmt())), unless(hasDescendant(ifStmt(hasElse(unless(ifStmt()))))))).bind("mrifelse"), &HandlerForIfElse); - Matcher.addMatcher(ifStmt(allOf(hasElse(anything()), hasDescendant(ifStmt()), unless(hasAncestor(ifStmt())))).bind("mrifelse"), &HandlerForIfElse); + Matcher.addMatcher(ifStmt(unless(hasDescendant(compoundStmt()))).bind("mrif"), &HandlerForIfFixer); } void HandleTranslationUnit(ASTContext &Context) override { @@ -371,8 +394,8 @@ private: StmtRet HandlerForStmtRet; ForFixer HandlerForFixer; WhileFixer HandlerForWhile; - IfFixer HandlerForIfFixer; IfElseFixer HandlerForIfElse; + IfFixer HandlerForIfFixer; MatchFinder Matcher; }; /**********************************************************************************************************************/ |