Skip to content

Commit

Permalink
random find assginment, assign specific num for float and int type in…
Browse files Browse the repository at this point in the history
… WVAV
  • Loading branch information
li-chi committed Mar 11, 2015
1 parent d62e622 commit 1ce4ec5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
72 changes: 54 additions & 18 deletions WVAV/WVAV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "llvm/Support/raw_ostream.h"
#include "clang/Lex/lexer.h"
#include "clang/Lex/Token.h"

#include <stdlib.h>

using namespace clang;
using namespace clang::ast_matchers;
Expand All @@ -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<clang::BinaryOperator>("bo");
const VarDecl *vd = Result.Nodes.getNodeAs<clang::VarDecl>("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: " <<str<< std::endl;


Rewrite.InsertTextAfter(des, str);

Expand All @@ -63,8 +91,9 @@ class MyASTConsumer : public ASTConsumer {
MyASTConsumer(Rewriter &R) : VD(R) {


Matcher.addMatcher(varDecl(hasName(varName)).bind("vd"),&VD);
//ignoringImpCasts(declRefExpr(to(functionDecl().bind("fun2"))));
Matcher.addMatcher(binaryOperator(hasOperatorName("="),
hasLHS(ignoringParenImpCasts(declRefExpr(to(
varDecl().bind("vd")))))).bind("bo"),&VD);
}

void HandleTranslationUnit(ASTContext &Context) override {
Expand All @@ -83,6 +112,7 @@ class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {}
void EndSourceFileAction() override {
if(counting) return;
TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID())
.write(llvm::outs());
}
Expand All @@ -98,12 +128,18 @@ class MyFrontendAction : public ASTFrontendAction {
};

int main(int argc, const char **argv) {
/*
std::cout << "Enter variable name: " << std::endl;
std::getline (std::cin,varName);
std::cout << "Enter new value: " << std::endl;
std::getline (std::cin,varValue);
*/
CommonOptionsParser op(argc, argv, MatcherSampleCategory);
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
Tool.run(newFrontendActionFactory<MyFrontendAction>().get());

counting = false;
srand(time(NULL));
choose = rand() % count + 1;
count = 0;

return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}
7 changes: 6 additions & 1 deletion doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)…

0 comments on commit 1ce4ec5

Please sign in to comment.