-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutant_Gen.cpp
More file actions
349 lines (276 loc) · 14.3 KB
/
Mutant_Gen.cpp
File metadata and controls
349 lines (276 loc) · 14.3 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <string>
#include <sstream>
#include <istream>
#include <iostream>
#include <fstream>
#include <iterator>
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/AST/AST.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/raw_os_ostream.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory ToolingSampleCategory("Matcher Sample");
int no_of_operations = 0;
int preprocessing_done = false;
std::vector< std::string > pathList;
StringRef File_Path;
//std::string newFile;
template <typename T>
static std::string getText(const SourceManager &SourceManager, const T &Node) {
SourceLocation StartSpellingLocation =
SourceManager.getSpellingLoc(Node.getLocStart());
SourceLocation EndSpellingLocation =
SourceManager.getSpellingLoc(Node.getLocEnd());
if (!StartSpellingLocation.isValid() || !EndSpellingLocation.isValid()) {
return std::string();
}
bool Invalid = true;
const char *Text =
SourceManager.getCharacterData(StartSpellingLocation, &Invalid);
if (Invalid) {
return std::string();
}
std::pair<FileID, unsigned> Start =
SourceManager.getDecomposedLoc(StartSpellingLocation);
std::pair<FileID, unsigned> End =
SourceManager.getDecomposedLoc(Lexer::getLocForEndOfToken(
EndSpellingLocation, 0, SourceManager, LangOptions()));
if (Start.first != End.first) {
// Start and end are in different files.
return std::string();
}
if (End.second < Start.second) {
// Shuffling text with macros may cause this.
return std::string();
}
return std::string(Text, End.second - Start.second);
}
static std::string getNextFile(StringRef currFileName, std::string newFileExtension){
std::string newFile = currFileName.str();
std::string ext = newFileExtension+"GlobalID"+std::to_string(no_of_operations++);
/* size_t pos_dir = newFile.find(pathList[0]);
std::string dir="mutants"+newFile[pos_dir-1];
newFile.insert(pos_dir, dir);*/
size_t pos = newFile.find(".cpp");
newFile.insert(pos, ext);
std::ifstream src(pathList[0], std::ios::binary);
std::ofstream dst(newFile, std::ios::binary);
dst << src.rdbuf();
return newFile;
}
static std::vector<clang::BinaryOperator::Opcode> viableBinMutantOpcodes(clang::BinaryOperator::Opcode Operator){
std::vector<clang::BinaryOperator::Opcode> OpCode;
StringRef Operat_str = clang::BinaryOperator::getOpcodeStr(Operator);
if(clang::BinaryOperator::isComparisonOp(Operator)){
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_LT)) != true)
OpCode.push_back(clang::BO_LT);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_GT)) != true)
OpCode.push_back(clang::BO_GT);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_LE)) != true)
OpCode.push_back(clang::BO_LE);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_GE)) != true)
OpCode.push_back(clang::BO_GE);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_EQ)) != true)
OpCode.push_back(clang::BO_EQ);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_NE)) != true)
OpCode.push_back(clang::BO_NE);
}
else if((clang::BinaryOperator::isMultiplicativeOp(Operator)) || (clang::BinaryOperator::isAdditiveOp(Operator))){
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Mul)) != true)
OpCode.push_back(clang::BO_Mul);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Div)) != true)
OpCode.push_back(clang::BO_Div);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Rem)) != true)
OpCode.push_back(clang::BO_Rem);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Sub)) != true)
OpCode.push_back(clang::BO_Sub);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Add)) != true)
OpCode.push_back(clang::BO_Add);
}
if(clang::BinaryOperator::isShiftOp (Operator)){
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Shl)) != true)
OpCode.push_back(clang::BO_Shl);
if(Operat_str.equals(clang::BinaryOperator::getOpcodeStr(clang::BO_Shr)) != true)
OpCode.push_back(clang::BO_Shr);
}
return OpCode;
}
class BinaryOperatorHandler : public MatchFinder::MatchCallback {
public:
BinaryOperatorHandler(Replacements *Replace) : Replace(Replace) {}
virtual void run(const MatchFinder::MatchResult &Result) {
if (const BinaryOperator *binOp = Result.Nodes.getNodeAs<clang::BinaryOperator>("binaryOperator")) {
// llvm::outs()<<"\nisInSystemHeader = \n"<<Result.SourceManager->isInSystemHeader(binOp->getExprLoc())<<"\n";
if((Result.SourceManager->isInMainFile(binOp->getExprLoc())==true) &&
(Result.SourceManager->isInSystemHeader(binOp->getExprLoc())==0)){
StringRef currFileName =Result.SourceManager->getFilename(binOp->getExprLoc());
llvm::outs()<<"\nCurrFile: "<<currFileName.str()<<"\n";
if(currFileName.find(pathList[0])==std::string::npos)
return ;
std::string temp;
temp = getText(*(Result.SourceManager),*binOp);
llvm::outs()<<"\nBinaryOperator: "<<temp<<"\n";
StringRef operand = binOp->getOpcodeStr(binOp->getOpcode () );
// llvm::outs()<<"Opcode: "<<operand.str()<<"\n";
clang::BinaryOperator::Opcode binOpCode = binOp->getOpcode ();
std::vector<clang::BinaryOperator::Opcode> mutant_Ops = viableBinMutantOpcodes(binOpCode);
Expr * lhs =binOp->getLHS () ;
Expr * rhs =binOp->getRHS () ;
std::string new_replacement;
std::string newFileExtension= "_Line"+std::to_string(Result.SourceManager->getSpellingLineNumber(binOp->getExprLoc()))
+"Column"+std::to_string(Result.SourceManager->getSpellingColumnNumber (binOp->getExprLoc()));
for(std::vector<clang::BinaryOperator::Opcode>::iterator op_it = mutant_Ops.begin();op_it != mutant_Ops.end(); ++op_it){
new_replacement = getText(*(Result.SourceManager),*lhs)+" "+clang::BinaryOperator::getOpcodeStr(*op_it).str()
+" " +getText(*(Result.SourceManager),*rhs);
std::string newFile =getNextFile(currFileName,newFileExtension);
Replacement Rep(*(Result.SourceManager), binOp, new_replacement);
Replacement Rep1(newFile, Rep.getOffset (), Rep.getLength(),new_replacement);
Replace->insert(Rep1);
new_replacement.erase();
}
if(binOp->isComparisonOp ()){
llvm::outs()<<"Comparison Opcode: "<<operand.str()<<"\n";
}
if(binOp->isMultiplicativeOp ()){
llvm::outs()<<"Multiplicative Opcode: "<<operand.str()<<"\n";
}
if(binOp->isAdditiveOp ()){
llvm::outs()<<"Additive Opcode: "<<operand.str()<<"\n";
}
if(binOp->isShiftOp ()){
llvm::outs()<<"Shift Opcode: "<<operand.str()<<"\n";
}
if(binOp->isBitwiseOp ()){
llvm::outs()<<"Bitwise Opcode: "<<operand.str()<<"\n";
}
if(binOp->isRelationalOp ()){
llvm::outs()<<"Relational Opcode: "<<operand.str()<<"\n";
}
if(binOp->isEqualityOp ()){
llvm::outs()<<"Equality Opcode: "<<operand.str()<<"\n";
}
if(binOp->isLogicalOp ()){
llvm::outs()<<"Logical Opcode: "<<operand.str()<<"\n";
}
if(binOp->isAssignmentOp ()){
llvm::outs()<<"Assignment Opcode: "<<operand.str()<<"\n";
}
if(binOp->isCompoundAssignmentOp ()){
llvm::outs()<<"CompoundAssignment Opcode: "<<operand.str()<<"\n";
}
if(binOp->isShiftAssignOp ()){
llvm::outs()<<"ShiftAssign Opcode: "<<operand.str()<<"\n";
}
}
}
}
private:
Replacements *Replace;
};
class unaryOperatorHandler : public MatchFinder::MatchCallback {
public:
unaryOperatorHandler(Replacements *Replace) : Replace(Replace) {}
virtual void run(const MatchFinder::MatchResult &Result) {
if (const UnaryOperator *unOp = Result.Nodes.getNodeAs<clang::UnaryOperator>("unaryOperator")) {
if((Result.SourceManager->isInMainFile(unOp->getExprLoc())==true) &&
(Result.SourceManager->isInSystemHeader(unOp->getExprLoc())==0)){
StringRef currFileName =Result.SourceManager->getFilename(unOp->getExprLoc());
llvm::outs()<<"\nCurrFile: "<<currFileName.str()<<"\n";
if(currFileName.find(pathList[0])==std::string::npos)
return ;
std::string temp;
temp = getText(*(Result.SourceManager),*unOp);
llvm::outs()<<"\nUnaryOperator\n"<<temp<<"\n";
//llvm::outs()<<"Opcode: "<<operand.str()<<"\n";
clang::UnaryOperator::Opcode UnOpCode = unOp->getOpcode ();
std::vector<clang::UnaryOperator::Opcode> mutant_Ops;
Expr * subExpr = unOp->getSubExpr ();
StringRef Operat_str = clang::UnaryOperator::getOpcodeStr(UnOpCode);
if(clang::UnaryOperator::isIncrementOp(UnOpCode) || clang::UnaryOperator::isDecrementOp(UnOpCode) ){
if((Operat_str.equals(clang::UnaryOperator::getOpcodeStr(clang::UO_PostInc)) != true) || (clang::UnaryOperator::isPrefix(UnOpCode)))
mutant_Ops.push_back(clang::UO_PostInc);
if((Operat_str.equals(clang::UnaryOperator::getOpcodeStr(clang::UO_PostDec)) != true) || (clang::UnaryOperator::isPrefix(UnOpCode)))
mutant_Ops.push_back(clang::UO_PostDec);
if((Operat_str.equals(clang::UnaryOperator::getOpcodeStr(clang::UO_PreInc)) != true) || (clang::UnaryOperator::isPostfix(UnOpCode)))
mutant_Ops.push_back(clang::UO_PreInc);
if((Operat_str.equals(clang::UnaryOperator::getOpcodeStr(clang::UO_PreDec)) != true) || (clang::UnaryOperator::isPostfix(UnOpCode)))
mutant_Ops.push_back(clang::UO_PreDec);
std::string new_replacement;
std::string newFileExtension= "_Line"+std::to_string(Result.SourceManager->getSpellingLineNumber(unOp->getExprLoc()))
+"Column"+std::to_string(Result.SourceManager->getSpellingColumnNumber (unOp->getExprLoc()));
for(std::vector<clang::UnaryOperator::Opcode>::iterator op_it = mutant_Ops.begin();op_it != mutant_Ops.end(); ++op_it){
if(clang::UnaryOperator::isPostfix(*op_it)){
new_replacement = clang::UnaryOperator::getOpcodeStr(*op_it).str()
+getText(*(Result.SourceManager),*subExpr);
}
else{
new_replacement = getText(*(Result.SourceManager),*subExpr) + clang::UnaryOperator::getOpcodeStr(*op_it).str();
}
std::string newFile =getNextFile(currFileName,newFileExtension);
Replacement Rep(*(Result.SourceManager), unOp, new_replacement);
Replacement Rep1(newFile, Rep.getOffset (), Rep.getLength(),new_replacement);
Replace->insert(Rep1);
new_replacement.erase();
}
if(unOp->isIncrementOp ()){
llvm::outs()<<"Increment Opcode: "<<Operat_str.str()<<"\n";
}
else if(unOp->isDecrementOp ()){
llvm::outs()<<"Decrement Opcode: "<<Operat_str.str()<<"\n";
}
else if(unOp->isIncrementDecrementOp ()){
llvm::outs()<<"IncrementDecrement Opcode: "<<Operat_str.str()<<"\n";
}
else if(unOp->isArithmeticOp ()){
llvm::outs()<<"Arithmetic Opcode: "<<Operat_str.str()<<"\n";
}
}
}
}
}
private:
Replacements *Replace;
};
int main(int argc, const char **argv) {
CommonOptionsParser op(argc, argv, ToolingSampleCategory);
pathList = op.getSourcePathList();
llvm::outs() << "Input Path:"<<pathList[0]<<"\n";
RefactoringTool Tool(op.getCompilations(), op.getSourcePathList());
// Set up AST matcher callbacks.
// IfStmtHandler HandlerForIf(&Tool.getReplacements());
BinaryOperatorHandler HandlerForbinOp(&Tool.getReplacements());
unaryOperatorHandler HandlerForUnaryOp(&Tool.getReplacements());
MatchFinder Finder;
Finder.addMatcher(binaryOperator().bind("binaryOperator"), &HandlerForbinOp);
Finder.addMatcher(unaryOperator().bind("unaryOperator"), &HandlerForUnaryOp);
// Run the tool and collect a list of replacements. We could call runAndSave,
// which would destructively overwrite the files with their new contents.
// However, for demonstration purposes it's interesting to show the
// replacements.
if (int Result = Tool.runAndSave(newFrontendActionFactory(&Finder).get())) {
return Result;
}
//createAndWrite2File();
preprocessing_done = true;
llvm::outs() << "Replacements collected by the tool:\n";
for (auto &r : Tool.getReplacements()) {
llvm::outs() << r.toString() << "\n";
}
return 0;
}