diff --git a/WVAV/WVAV.cpp b/WVAV/WVAV.cpp index 3fb5839..9eb2582 100644 --- a/WVAV/WVAV.cpp +++ b/WVAV/WVAV.cpp @@ -13,7 +13,7 @@ #include "llvm/Support/raw_ostream.h" #include "clang/Lex/lexer.h" #include "clang/Lex/Token.h" - +#include using namespace clang; using namespace clang::ast_matchers; @@ -22,30 +22,58 @@ using namespace clang::tooling; static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample"); -std::string varName; -std::string varValue; +//std::string varName; +bool counting = true; +int count = 0; +int choose; class VarDeclHandler : public MatchFinder::MatchCallback { public: VarDeclHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} virtual void run(const MatchFinder::MatchResult &Result) { + const BinaryOperator *bo = Result.Nodes.getNodeAs("bo"); const VarDecl *vd = Result.Nodes.getNodeAs("vd"); - SourceLocation sl = vd->getSourceRange().getBegin(); - - SourceLocation des = sl; - while (Lexer::findLocationAfterToken(des, tok::semi, Rewrite.getSourceMgr(), + std::string str; + + if(vd->getType()->isIntegerType()) { + if(counting) { + count++; + return; + } else { + count++; + if(count != choose) { + return; + } + } + str = vd->getNameAsString() + " = 10;//wrong value\n//"; + + } else if (vd->getType()->isFloatingType()) { + if(counting) { + count++; + return; + } else { + count++; + if(count != choose) { + return; + } + } + str = vd->getNameAsString() + " = 15.3;//wrong value\n//"; + } else { + return; + } + + SourceLocation sl = bo->getSourceRange().getBegin(); + + SourceLocation des = sl; + while (Lexer::findLocationAfterToken(des, tok::semi, Rewrite.getSourceMgr(), Rewrite.getLangOpts(), true).isInvalid()) { //des = Lexer::getLocForEndOfToken(sl, 0, Rewrite.getSourceMgr(), Rewrite.getLangOpts()); des = des.getLocWithOffset(-1); - } - des = Lexer::findLocationAfterToken(des, tok::semi, Rewrite.getSourceMgr(), + } + des = Lexer::findLocationAfterToken(des, tok::semi, Rewrite.getSourceMgr(), Rewrite.getLangOpts(), true); - std::string str = vd->getType().getAsString() + " " + vd->getNameAsString() + - " = " + varValue + "; // init var \n" - + "// Missing init var " + vd->getNameAsString() + ": "; - - //std::cout << "fun: " <().get()); + counting = false; + srand(time(NULL)); + choose = rand() % count + 1; + count = 0; + return Tool.run(newFrontendActionFactory().get()); } diff --git a/doc.txt b/doc.txt index 3d5ab6d..a7c8d0d 100644 --- a/doc.txt +++ b/doc.txt @@ -22,8 +22,13 @@ MIEB -> if(a) b else (c) d,only leave d; see above MLC -> if(a&&b),去掉&&b; + MLPA -> 随便去掉一句赋值语句等不会出compile错的句子; -WVAV -> 给随机的赋值语句改的数; + +WVAV -> Assignment a random num in assignment; +only works for int and float; + WPFV -> change, 有symbol table!! 去table随机找个var; + WAEP -> f(a+b) —>> f(a) or f(a-b)…