aboutsummaryrefslogtreecommitdiffstats
path: root/mutator.cpp
blob: 114bfeafd78fb2bbf934626f8c832ee4d379e214 (plain) (blame)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*first line intentionally left blank.*/
/*code structure inspired by Eli Bendersky's tutorial on Rewriters.*/
/**********************************************************************************************************************/
/*FIXME-all classes should use replacements.*/
/**********************************************************************************************************************/
/*included modules*/
/*project headers*/
#include "mutator_aux.h"
/*standard headers*/
#include <string>
#include <iostream>
#include <cassert>
/*LLVM headers*/
#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/Lex/Lexer.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Function.h"
/**********************************************************************************************************************/
/*used namespaces*/
using namespace llvm;
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
/**********************************************************************************************************************/
/*global vars*/
/*the variable that holds the previously-matched SOC for class StmtTrap.*/
std::string g_linenoold;

static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample");
/**********************************************************************************************************************/
/*matcher callback for something.*/
class FunctionHandler : public MatchFinder::MatchCallback {
public:
  FunctionHandler (Rewriter &Rewrite) : Rewrite(Rewrite) {}

  virtual void run(const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::BinaryOperator>("binopeq") != nullptr)
    {
      /*get the matched node.*/
      const BinaryOperator *BinOp = MR.Nodes.getNodeAs<clang::BinaryOperator>("binopeq");

      /*get the sourceloation.*/
      SourceLocation BinOpSL = BinOp->getLocStart();

      BinOpSL = Devi::SourceLocationHasMacro(BinOpSL, Rewrite, "start");
      /*does the sourcelocation include a macro expansion?*/

      /*replace it.*/
#if 0
      Rewrite.ReplaceText(BinOpSL, 2U , "XXX");
#endif
    }
    else
    {
      std::cout << "the macther -binopeq- returned nullptr!" << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class StmtTrapIf : public MatchFinder::MatchCallback {
public:
  StmtTrapIf (Rewriter &Rewrite) : Rewrite(Rewrite) {}

  virtual void run (const MatchFinder::MatchResult &MR)
  {
    /*just in case*/
    if (MR.Nodes.getNodeAs<clang::IfStmt>("iftrap") != nullptr)
    {
      /*getting the matched ifStmt.*/
      const IfStmt *TrapIf = MR.Nodes.getNodeAs<clang::IfStmt>("iftrap");

      /*getting the condition of the matched ifStmt.*/
      const Expr *IfCond = TrapIf->getCond();

      /*getting the sourcerange of the condition of the trapped ifstmt.*/
      SourceLocation TrapIfCondSL = IfCond->getLocStart();
      TrapIfCondSL = Devi::SourceLocationHasMacro(TrapIfCondSL, Rewrite, "start");
      SourceLocation TrapIfCondSLEnd = IfCond->getLocEnd();
      TrapIfCondSLEnd = Devi::SourceLocationHasMacro(TrapIfCondSLEnd, Rewrite, "end");
      SourceRange TrapIfCondSR;
      TrapIfCondSR.setBegin(TrapIfCondSL);
      TrapIfCondSR.setEnd(TrapIfCondSLEnd);

      /*replacing the condition with the utility trap function.*/
#if 0
      Rewrite.ReplaceText(TrapIfCondSR, "C_Trap_P()");
#endif
    }
    else
    {
      std::cout << "the matcher -iftrap- returned nullptr!" << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class StmtTrap : public MatchFinder::MatchCallback {
public:
  StmtTrap (Rewriter &Rewrite) : Rewrite(Rewrite) {}

  /*if there are more than 2 statements in the traditional sense in one line, the behavior is undefined.*/
  /*for now this class only finds an SOC. later to be used for something useful.*/
  virtual void run (const MatchFinder::MatchResult &MR)
  {
    /*out of paranoia*/
    if (MR.Nodes.getNodeAs<clang::Stmt>("stmttrap") != nullptr)
    {
      const Stmt *StmtTrapMR = MR.Nodes.getNodeAs<clang::Stmt>("stmttrap");

      SourceLocation STSL = StmtTrapMR->getLocStart();
      STSL = Devi::SourceLocationHasMacro(STSL, Rewrite, "start");
      SourceLocation STSLE = StmtTrapMR->getLocEnd();
      STSLE = Devi::SourceLocationHasMacro(STSLE, Rewrite, "end");

      /*just getting the SOC of the matched statement out of its sourcelocation.*/
      /*this only works since we are guaranteed the same and known matching pattern by MatchFinder.*/
      size_t startloc = 0U;
      size_t endloc = 0U;
      std::string lineno;
      startloc = STSL.printToString(*MR.SourceManager).find(":", 0U);
      endloc = STSLE.printToString(*MR.SourceManager).find(":", startloc + 1U);
      lineno = STSL.printToString(*MR.SourceManager).substr(startloc, endloc - startloc);

      /*just prints out the sourcelocations for diagnostics.*/
#if 0
      std::cout << STSL.printToString(*MR.SourceManager) << std::endl;
      std::cout << STSLE.printToString(*MR.SourceManager) << std::endl;
      std::cout << startloc << "---" << endloc << "---" << lineno << std::endl;
#endif

      /*have we matched a new SOC? if yes:*/
      if (lineno != g_linenoold)
      {
        SourceRange SR;
        SR.setBegin(StmtTrapMR->getLocStart());
        SR.setEnd(StmtTrapMR->getLocEnd());

#if 0
        Rewrite.InsertText(StmtTrapMR->getLocStart(), "XXX", "true", "true");
#endif
      }
      else
      {
        /*intentionally left blank.*/
      }

      /*set the string representing the old SOC line number to the one matched now.*/
      g_linenoold = lineno;
    }
    else
    {
      std::cout << "the matcher -stmttrap- returned nullptr!" << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class StmtRet : public MatchFinder::MatchCallback
{
public:
  StmtRet (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  virtual void run (const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::ReturnStmt>("stmtret") != nullptr)
    {
      /*getting the returnstmt.*/
      const ReturnStmt *RetStmtMR = MR.Nodes.getNodeAs<clang::ReturnStmt>("stmtret");

      /*getting the sourcerange of the matched returnstmt.*/
      SourceLocation RSMR = RetStmtMR->getLocStart();
      RSMR = Devi::SourceLocationHasMacro(RSMR, Rewrite, "start");
      SourceLocation RSMRE = RetStmtMR->getLocEnd();
      RSMRE = Devi::SourceLocationHasMacro(RSMRE, Rewrite, "end");
      SourceRange RSMRSR;
      RSMRSR.setBegin(RSMR);
      RSMRSR.setEnd(RSMRE);

#if 0
      Rewrite.ReplaceText(RSMRSR, "C_Trap_P()");
#endif
    }
    else
    {
      std::cout << "matcher -stmtret- returned nullptr." << std::endl;
    }
  }

private:
  Rewriter &Rewrite;

};
/**********************************************************************************************************************/
class ForFixer : public MatchFinder::MatchCallback
{
public:
  ForFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  /*adds curly braces for forstmts that don't have it.*/
  virtual void run(const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::ForStmt>("mrfor") != nullptr)
    {
      const ForStmt *MRFor = MR.Nodes.getNodeAs<clang::ForStmt>("mrfor");

      SourceLocation MRForSL = MRFor->getBody()->getLocStart();
      MRForSL = Devi::SourceLocationHasMacro(MRForSL, Rewrite, "start");
      SourceLocation MRForSLE = MRFor->getBody()->getLocEnd();
      MRForSLE = Devi::SourceLocationHasMacro(MRForSLE, Rewrite, "end");

      Rewrite.InsertText(MRForSL, "{\n", true, false);
      /*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.InsertTextAfterToken(MRForSLE.getLocWithOffset(2U), "\n}");
    }
    else
    {
      std::cout << "matcher -mrfor- returned nullptr." << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class WhileFixer : public MatchFinder::MatchCallback
{
public:
  WhileFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  /*adds curly braces for whilestmts that don't have it.*/
  virtual void run (const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::WhileStmt>("mrwhile") != nullptr)
    {
      const WhileStmt *MRWhile = MR.Nodes.getNodeAs<clang::WhileStmt>("mrwhile");

#if 0
      std::cout << MRWhile->getBody()->getLocStart().printToString(*MR.SourceManager) << std::endl;
      std::cout << MRWhile->getBody()->getLocEnd().printToString(*MR.SourceManager) << std::endl;
#endif

      SourceLocation WFSL = MRWhile->getBody()->getLocStart();
      WFSL = Devi::SourceLocationHasMacro(WFSL, Rewrite, "start");
      SourceLocation WFSLE = MRWhile->getBody()->getLocEnd();
      WFSLE = Devi::SourceLocationHasMacro(WFSLE, Rewrite, "end");

      /*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(WFSL, "{\n", true, true);
      Rewrite.InsertTextAfterToken(WFSLE.getLocWithOffset(2U), "\n}");
    }
    else
    {
#if 1
      std::cout << "matcher -mrwhile- returned nullptr." << std::endl;
#endif
    }
  }


private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class IfElseFixer : public MatchFinder::MatchCallback
{
public:
  IfElseFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  virtual void run(const MatchFinder::MatchResult &MR)
  {
    /*underdev*/
    if (MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse") != nullptr)
    {
      const IfStmt *ElseIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse");
      //const IfStmt *LastIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrifelse");

      SourceLocation IFESL = ElseIf->getElse()->getLocStart();
      IFESL = Devi::SourceLocationHasMacro(IFESL, Rewrite, "start");
      SourceLocation IFESLE = ElseIf->getElse()->getLocEnd();
      IFESLE = Devi::SourceLocationHasMacro(IFESLE, Rewrite, "end");
      SourceRange SR;
      SR.setBegin(IFESL);
      SR.setEnd(IFESLE);

      clang::Rewriter::RewriteOptions opts;

      int RangeSize = Rewrite.getRangeSize(SR, opts);

      //std::cout << IFESLE.printToString(*MR.SourceManager) << "\n" << std::endl;

#if 1
      Rewrite.InsertText(IFESL, "{\n", true, true);
      Rewrite.InsertTextAfterToken(IFESL.getLocWithOffset(RangeSize), "\n}");
#endif
    }
    else
    {
      std::cout << "matcher -mrifelse- returned nullptr." << std::endl;
    }
  }


private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class IfFixer : public MatchFinder::MatchCallback
{
public:
  IfFixer (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)
    {
      const IfStmt *MRIf = MR.Nodes.getNodeAs<clang::IfStmt>("mrif");

      SourceLocation FFSL = MRIf->getThen()->getLocStart();
      FFSL = Devi::SourceLocationHasMacro(FFSL, Rewrite, "start");
      SourceLocation FFSLE = MRIf->getThen()->getLocEnd();
      FFSLE = Devi::SourceLocationHasMacro(FFSLE, Rewrite, "end");

      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 -mrif- returned nullptr." << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class SwitchFixer : public MatchFinder::MatchCallback
{
public:
  SwitchFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  virtual void run(const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::CaseStmt>("bubba-hotep") != nullptr)
    {
      const CaseStmt *CS = MR.Nodes.getNodeAs<clang::CaseStmt>("bubba-hotep");

      const Stmt *SB = CS->getSubStmt();

      SourceLocation SBSL = SB->getLocStart();
      SBSL = Devi::SourceLocationHasMacro(SBSL, Rewrite, "start");
      SourceLocation SBSLE = SB->getLocEnd();
      SBSLE = Devi::SourceLocationHasMacro(SBSLE, Rewrite, "end");

      SourceLocation SL = CS->getLocStart();
      SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
      SourceLocation SLE = CS->getLocEnd();
      SLE = Devi::SourceLocationHasMacro(SLE, Rewrite, "end");

      SourceRange SR;
      SR.setBegin(SL);
      SR.setEnd(SLE);
      Rewriter::RewriteOptions opts;
      int RangeSize = Rewrite.getRangeSize(SR, opts);

#if 0
      Rewrite.InsertText(SBSL, "{\n", "true", "true");
      Rewrite.InsertTextAfterToken(SL.getLocWithOffset(RangeSize), "\n}");
#endif
    }
    else
    {
      std::cout << "matcher -bubba-hotep- returned nullptr." << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
class SwitchDfFixer : public MatchFinder::MatchCallback
{
public:
  SwitchDfFixer (Rewriter &Rewrite) : Rewrite (Rewrite) {}

  virtual void run(const MatchFinder::MatchResult &MR)
  {
    if (MR.Nodes.getNodeAs<clang::DefaultStmt>("mumma-hotep") != nullptr)
    {
      const DefaultStmt *DS = MR.Nodes.getNodeAs<clang::DefaultStmt>("mumma-hotep");

      const Stmt *SB = DS->getSubStmt();

      SourceLocation CSL = SB->getLocStart();
      CSL = Devi::SourceLocationHasMacro(CSL, Rewrite, "start");

      SourceLocation SL = DS->getLocStart();
      SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
      SourceLocation SLE = DS->getLocEnd();
      SLE = Devi::SourceLocationHasMacro(SLE, Rewrite, "end");

      SourceRange SR;
      SR.setBegin(SL);
      SR.setEnd(SLE);
      Rewriter::RewriteOptions opts;
      int RangeSize = Rewrite.getRangeSize(SR, opts);

#if 0
      Rewrite.InsertText(CSL, "{\n", "true", "true");
      Rewrite.InsertTextAfterToken(SL.getLocWithOffset(RangeSize), "\n}");
#endif
    }
    else
    {
      std::cout << "matcher -mumma-hotep- returned nullptr." << std::endl;
    }
  }

private:
  Rewriter &Rewrite;
};
/**********************************************************************************************************************/
/**********************************************************************************************************************/
class MyASTConsumer : public ASTConsumer {

public:
  MyASTConsumer(Rewriter &R) : HandlerForFunction(R), HandlerForIfTrap(R), HandlerForStmtTrap(R), HandlerForStmtRet(R), HandlerForFixer(R), \
    HandlerForWhile(R), HandlerForIfElse(R), HandlerForIfFixer(R), HandlerForSwitchFixer(R), HandlerForSwitchDf(R)
  {
    Matcher.addMatcher(binaryOperator(hasOperatorName("==")).bind("binopeq"), &HandlerForFunction);

    Matcher.addMatcher(ifStmt(hasCondition(anything())).bind("iftrap"), &HandlerForIfTrap);

    Matcher.addMatcher(stmt().bind("stmttrap") , &HandlerForStmtTrap);

    Matcher.addMatcher(returnStmt().bind("stmtret"), &HandlerForStmtRet);

    Matcher.addMatcher(forStmt(unless(hasDescendant(compoundStmt()))).bind("mrfor"), &HandlerForFixer);

    Matcher.addMatcher(whileStmt(unless(hasDescendant(compoundStmt()))).bind("mrwhile"), &HandlerForWhile);

    Matcher.addMatcher(ifStmt(allOf(hasElse(unless(ifStmt())), hasElse(unless(compoundStmt())))).bind("mrifelse"), &HandlerForIfElse);

    Matcher.addMatcher(ifStmt(unless(hasDescendant(compoundStmt()))).bind("mrif"), &HandlerForIfFixer);

    Matcher.addMatcher(switchStmt(forEachDescendant(caseStmt(unless(hasDescendant(compoundStmt()))).bind("bubba-hotep"))), &HandlerForSwitchFixer);

    Matcher.addMatcher(switchStmt(hasDescendant(defaultStmt(unless(hasDescendant(compoundStmt()))).bind("mumma-hotep"))), &HandlerForSwitchDf);
  }

  void HandleTranslationUnit(ASTContext &Context) override {
    Matcher.matchAST(Context);
  }

private:
  FunctionHandler HandlerForFunction;
  StmtTrapIf HandlerForIfTrap;
  StmtTrap HandlerForStmtTrap;
  StmtRet HandlerForStmtRet;
  ForFixer HandlerForFixer;
  WhileFixer HandlerForWhile;
  IfElseFixer HandlerForIfElse;
  IfFixer HandlerForIfFixer;
  SwitchFixer HandlerForSwitchFixer;
  SwitchDfFixer HandlerForSwitchDf;
  MatchFinder Matcher;
};
/**********************************************************************************************************************/
class MyFrontendAction : public ASTFrontendAction {
public:
  MyFrontendAction() {}
  void EndSourceFileAction() override {
    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;
};
/**********************************************************************************************************************/
/*Main*/
int main(int argc, const char **argv) {
  CommonOptionsParser op(argc, argv, MatcherSampleCategory);
  ClangTool Tool(op.getCompilations(), op.getSourcePathList());

  return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}
/*last line intentionally left blank.*/