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
|
/*first line intentionally left blank.*/
/*************************************************************************************************/
//-*-c++-*-
/*Copyright (C) 2018 Farzad Sadeghi
* Licensed under GPL-3.0
* */
/*************************************************************************************************/
/*included modules*/
/*project headers*/
#include "./cfe-extra/cfe_extra.h"
/*standard headers*/
#include <cassert>
#include <cstdlib>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
/*LLVM headers*/
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/LLVM.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
/*************************************************************************************************/
/*used namespaces*/
using namespace llvm;
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
/*************************************************************************************************/
namespace {
static llvm::cl::OptionCategory CGrepCat("cgrep options");
cl::opt<std::string> CO_DIRECTORY("dir", cl::desc("recursively goes through all the files and directories. assumes compilation databases are present for all source files."), cl::init(""), cl::cat(CGrepCat), cl::Optional);
cl::opt<std::string> CO_REGEX("regex", cl::desc("the regex to match against"), cl::init(""), cl::cat(CGrepCat), cl::Required); //done
cl::opt<bool> CO_FUNCTION("func", cl::desc("match functions only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_MEM_FUNCTION("memfunc", cl::desc("match member functions only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_VAR("var", cl::desc("map variables only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_MEMVAR("memvar", cl::desc("match member variables only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_CLASS("class", cl::desc("match class declrations only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_STRUCT("struct", cl::desc("match structures only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); // done
cl::opt<bool> CO_UNION("union", cl::desc("match unions only"), cl::init(false), cl::cat(CGrepCat), cl::Optional); // done
cl::opt<bool> CO_MACRO("macro", cl::desc("match macro definitions"), cl::init(false), cl::cat(CGrepCat), cl::Optional);
cl::opt<bool> CO_HEADER("header", cl::desc("match headers in header inclusions"), cl::init(false), cl::cat(CGrepCat), cl::Optional);
cl::opt<bool> CO_ALL("all", cl::desc("turns on all switches other than nameddecl"), cl::init(false), cl::cat(CGrepCat), cl::Optional); // done
cl::opt<bool> CO_NAMEDDECL("nameddecl", cl::desc("matches all named declrations"), cl::init(false), cl::cat(CGrepCat), cl::Optional); // done
cl::opt<bool> CO_AWK("awk", cl::desc("outputs location in a gawk freidnly format"), cl::init(false), cl::cat(CGrepCat), cl::Optional); // done
cl::opt<bool> CO_SYSHDR("syshdr", cl::desc("match identifiers in system header as well"), cl::init(false), cl::cat(CGrepCat), cl::Optional); //done
cl::opt<bool> CO_MAINFILE("mainfile", cl::desc("mathc identifiers in the main file only"), cl::init(true), cl::cat(CGrepCat), cl::Optional); //done
}
/*************************************************************************************************/
#if 1
#define REGEX_PP(RX_STR) RX_STR
#endif
#if 0
#define REGEX_PP(RX_STR) regex_preprocessor(RX_STR)
#endif
/**
* @brief does some preprocessing on the regex string we get as input
* @param rx_str
* @return the preprocessed string
*/
std::string regex_preprocessor(std::string rx_str) {
std::string ret_rx_str;
return ret_rx_str;
}
bool regex_handler(std::string rx_str, std::string identifier_name) {
std::regex rx(rx_str);
std::smatch result;
return std::regex_search(identifier_name, result, rx);
}
std::string output_handler(SourceLocation SL, SourceManager &SM) {
return SL.printToString(SM);
}
std::string output_handler(SourceRange SR, SourceManager &SM) {
return SR.getBegin().printToString(SM);
}
/*************************************************************************************************/
class FunctionHandler : public MatchFinder::MatchCallback {
public:
FunctionHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const FunctionDecl *FD = MR.Nodes.getNodeAs<clang::FunctionDecl>("funcdecl");
if (FD) {
SourceRange SR = FD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = FD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class FieldHandler : public MatchFinder::MatchCallback {
public:
FieldHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const FieldDecl *VD = MR.Nodes.getNodeAs<clang::FieldDecl>("fielddecl");
if (VD) {
SourceRange SR = VD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = VD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class CXXMethodHandler : public MatchFinder::MatchCallback {
public:
CXXMethodHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const CXXMethodDecl *MD = MR.Nodes.getNodeAs<clang::CXXMethodDecl>("cxxmethoddecl");
if (MD) {
SourceRange SR = MD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = MD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class VDecl : public MatchFinder::MatchCallback {
public:
VDecl(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const VarDecl *VD = MR.Nodes.getNodeAs<clang::VarDecl>("vardecl");
if (VD) {
SourceRange SR = VD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = VD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class ClassDecl : public MatchFinder::MatchCallback {
public:
ClassDecl(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const RecordDecl *RD = MR.Nodes.getNodeAs<clang::RecordDecl>("classdecl");
if (RD) {
SourceRange SR = RD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = RD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class StructHandler : public MatchFinder::MatchCallback {
public:
StructHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const RecordDecl *RD = MR.Nodes.getNodeAs<clang::RecordDecl>("structdecl");
if (RD) {
SourceRange SR = RD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = RD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class UnionHandler : public MatchFinder::MatchCallback {
public:
UnionHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const RecordDecl *RD = MR.Nodes.getNodeAs<clang::RecordDecl>("uniondecl");
if (RD) {
SourceRange SR = RD->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = RD->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class NamedDeclHandler : public MatchFinder::MatchCallback {
public:
NamedDeclHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &MR) {
const NamedDecl *ND = MR.Nodes.getNodeAs<clang::NamedDecl>("namedecl");
if (ND) {
SourceRange SR = ND->getSourceRange();
SourceLocation SL = SR.getBegin();
CheckSLValidity(SL);
SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start");
if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) return void();
if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) return void();
std::string name = ND->getNameAsString();
if (regex_handler(REGEX_PP(CO_REGEX), name)) {
std::cout << name << "\t";
std::cout << SR.getBegin().printToString(*MR.SourceManager) << "\t";
std::cout << SR.getEnd().printToString(*MR.SourceManager) << "\n";
}
}
}
private:
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
class PPInclusion : public PPCallbacks {
public:
explicit PPInclusion(SourceManager *SM, Rewriter *Rewrite)
: SM(*SM), Rewrite(*Rewrite) {}
virtual void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) {}
virtual void MacroExpands(const Token &MacroNameTok,
const MacroDefinition &MD, SourceRange Range,
const MacroArgs *Args) {}
#if __clang_major__ <= 6
virtual void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *File, StringRef SearchPath,
StringRef RelativePath,
const clang::Module *Imported) {
#elif __clang_major__ >= 8
virtual void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *File, StringRef SearchPath,
StringRef RelativePath,
const clang::Module *Imported,
SrcMgr::CharacteristicKind FileType) {
#endif
}
private:
const SourceManager &SM [[maybe_unused]];
Rewriter &Rewrite [[maybe_unused]];
};
/*************************************************************************************************/
// brief A Clang Diagnostic Consumer that does nothing.
class BlankDiagConsumer : public clang::DiagnosticConsumer {
public:
BlankDiagConsumer() = default;
virtual ~BlankDiagConsumer() {}
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override {}
};
/*************************************************************************************************/
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(Rewriter &R)
: HandlerForVar(R), HandlerForClass(R),
HandlerForCalledFunc(R), HandlerForCXXMethod(R), HandlerForField(R), HandlerForStruct(R), HandlerForUnion(R), HandlerForNamedDecl(R) {
#if 1
if (CO_FUNCTION || CO_ALL) {
Matcher.addMatcher(functionDecl().bind("funcdecl"), &HandlerForCalledFunc);}
if (CO_VAR || CO_ALL) {
Matcher.addMatcher(varDecl(anyOf(unless(hasDescendant(expr(anything()))), hasDescendant(expr(anything()).bind("expr")))).bind("vardecl"),&HandlerForVar);}
if (CO_CLASS || CO_ALL) {
Matcher.addMatcher(recordDecl(isClass()).bind("classdecl"),&HandlerForClass);}
if (CO_MEM_FUNCTION || CO_ALL) {
Matcher.addMatcher(cxxMethodDecl().bind("cxxmethoddecl"), &HandlerForCXXMethod);}
if (CO_MEMVAR || CO_ALL) {
Matcher.addMatcher(fieldDecl().bind("fielddecl"), &HandlerForField);}
if (CO_STRUCT || CO_ALL) {
Matcher.addMatcher(recordDecl(isStruct()).bind("structdecl"), &HandlerForStruct);}
if (CO_UNION || CO_ALL) {
Matcher.addMatcher(recordDecl(isUnion()).bind("uniondecl"), &HandlerForUnion);}
if (CO_NAMEDDECL) {
Matcher.addMatcher(namedDecl().bind("namedecl"), &HandlerForNamedDecl);}
#endif
}
void HandleTranslationUnit(ASTContext &Context) override {
Matcher.matchAST(Context);
}
private:
VDecl HandlerForVar;
ClassDecl HandlerForClass;
FunctionHandler HandlerForCalledFunc;
CXXMethodHandler HandlerForCXXMethod;
FieldHandler HandlerForField;
StructHandler HandlerForStruct;
UnionHandler HandlerForUnion;
NamedDeclHandler HandlerForNamedDecl;
MatchFinder Matcher;
};
/*************************************************************************************************/
class AppFrontendAction : public ASTFrontendAction {
public:
AppFrontendAction() {}
~AppFrontendAction() { delete BDCProto; }
void EndSourceFileAction() override {
std::error_code EC;
//TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID()).write(llvm::outs());
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
CI.getPreprocessor().addPPCallbacks(
llvm::make_unique<PPInclusion>(&CI.getSourceManager(), &TheRewriter));
DiagnosticsEngine &DE = CI.getPreprocessor().getDiagnostics();
DE.setClient(BDCProto, false);
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return llvm::make_unique<MyASTConsumer>(TheRewriter);
}
private:
BlankDiagConsumer *BDCProto = new BlankDiagConsumer;
Rewriter TheRewriter;
};
/*************************************************************************************************/
/*************************************************************************************************/
/*Main*/
int main(int argc, const char **argv) {
CommonOptionsParser op(argc, argv, CGrepCat);
const std::vector<std::string> &SourcePathList [[maybe_unused]] = op.getSourcePathList();
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
int ret = Tool.run(newFrontendActionFactory<AppFrontendAction>().get());
return ret;
}
/*************************************************************************************************/
|