-
Notifications
You must be signed in to change notification settings - Fork 0
/
WVAV.cpp
149 lines (123 loc) · 4.44 KB
/
WVAV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <string>
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#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;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample");
//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");
std::string str;
if(vd->getType()->isIntegerType()) {
if(counting) {
count++;
return;
} else {
count++;
if(count != choose) {
return;
}
}
srand(time(NULL));
int r = rand() % 1000;
str = vd->getNameAsString() + " = " + std::to_string(r) + ";//wrong value\n//";
} else if (vd->getType()->isFloatingType()) {
if(counting) {
count++;
return;
} else {
count++;
if(count != choose) {
return;
}
}
srand(time(NULL));
int r = rand() & 1000;
str = vd->getNameAsString() + " = " + std::to_string(r) + ".0;//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(),
Rewrite.getLangOpts(), true);
Rewrite.InsertTextAfter(des, str);
}
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 {
public:
MyASTConsumer(Rewriter &R) : VD(R) {
Matcher.addMatcher(binaryOperator(hasOperatorName("="),
hasLHS(ignoringParenImpCasts(declRefExpr(to(
varDecl().bind("vd")))))).bind("bo"),&VD);
}
void HandleTranslationUnit(ASTContext &Context) override {
// Run the matchers when we have the whole TU parsed.
Matcher.matchAST(Context);
}
private:
MatchFinder Matcher;
VarDeclHandler VD;
};
// For each source file provided to the tool, a new FrontendAction is created.
class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {}
void EndSourceFileAction() override {
if(counting) return;
TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID())
.write(llvm::outs());
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return llvm::make_unique<MyASTConsumer>(TheRewriter);
}
private:
Rewriter TheRewriter;
};
int main(int argc, const char **argv) {
/*
std::cout << "Enter variable name: " << std::endl;
std::getline (std::cin,varName);
*/
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());
}