-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.cpp
More file actions
492 lines (401 loc) · 19.7 KB
/
Copy pathextractor.cpp
File metadata and controls
492 lines (401 loc) · 19.7 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
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
// Created on: 21 de jan de 2025
// Author: roger.moschiel
#ifdef _WIN32
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#include "clang/AST/AST.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/CommandLine.h"
// Mais includes para extrais macros
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PPCallbacks.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream> // Para salvar arquivo
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;
// Função para salvar os dados extraídos nos arquivos
void saveToFile(const std::string &definition, unsigned startLine, unsigned endLine) {
std::ofstream defFile("definition.txt", std::ios::trunc);
std::ofstream lineFile("lines.txt", std::ios::trunc);
if (defFile.is_open() && lineFile.is_open()) {
defFile << definition;
lineFile << startLine << ";" << endLine;
defFile.close();
lineFile.close();
}
std::exit(0);
}
// Opções para CLI
static cl::OptionCategory ToolCategory("extractor options");
static cl::opt<std::string> FunctionName(
"function", cl::desc("Name of the function to extract"),
cl::value_desc("function name"), cl::init(""));
static cl::opt<std::string> PrototypeName(
"prototype", cl::desc("Name of the function prototype to extract"),
cl::value_desc("prototype name"), cl::init(""));
static cl::opt<std::string> TypedefName(
"typedef", cl::desc("Name of the typedef to extract"),
cl::value_desc("typedef name"), cl::init(""));
static cl::opt<std::string> StructName(
"struct", cl::desc("Name of the struct to extract"),
cl::value_desc("struct name"), cl::init(""));
static cl::opt<std::string> EnumName(
"enum", cl::desc("Name of the enum to extract"),
cl::value_desc("enum name"), cl::init(""));
static cl::opt<std::string> UnionName(
"union", cl::desc("Name of the union to extract"),
cl::value_desc("union name"), cl::init(""));
static cl::opt<std::string> GlobalVarName(
"global", cl::desc("Name of the global variable to extract"),
cl::value_desc("global variable name"), cl::init(""));
static cl::opt<std::string> ExternFunctionName(
"extern-function", cl::desc("Name of the extern function to extract"),
cl::value_desc("extern function name"), cl::init(""));
static cl::opt<std::string> ExternVariableName(
"extern-variable", cl::desc("Name of the extern variable to extract"),
cl::value_desc("extern variable name"), cl::init(""));
static cl::opt<std::string> MacroName(
"macro", cl::desc("Name of the macro to extract"),
cl::value_desc("macro name"), cl::init(""));
// Callback para funções
class FunctionExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const FunctionDecl *Func = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl")) {
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = Func->getBeginLoc();
SourceLocation EndLoc = Func->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = Lexer::getSourceText(
CharSourceRange::getTokenRange(Func->getSourceRange()), SM, LangOptions()).str() + "\n";
llvm::outs() << "Function: "
<< Func->getNameInfo().getName().getAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para protótipos de funções
class PrototypeExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const FunctionDecl *Func = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl")) {
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = Func->getBeginLoc();
SourceLocation EndLoc = Func->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = Lexer::getSourceText(
CharSourceRange::getTokenRange(Func->getSourceRange()), SM, LangOptions()).str() + ";\n";
llvm::outs() << "Prototype: "
<< Func->getNameInfo().getName().getAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para typedefs
class TypedefExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const TypedefDecl *TD = Result.Nodes.getNodeAs<TypedefDecl>("typedefDecl")) {
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = TD->getBeginLoc();
SourceLocation EndLoc = TD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(TD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Typedef: " << TD->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para structs
class StructExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const RecordDecl *RD = Result.Nodes.getNodeAs<RecordDecl>("structDecl")) {
if (!RD->isStruct()) return; // Filtra apenas structs
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = RD->getBeginLoc();
SourceLocation EndLoc = RD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(RD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Struct: " << RD->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para enums
class EnumExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const EnumDecl *ED = Result.Nodes.getNodeAs<EnumDecl>("enumDecl")) {
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = ED->getBeginLoc();
SourceLocation EndLoc = ED->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(ED->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Enum: " << ED->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para unions
class UnionExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const RecordDecl *RD = Result.Nodes.getNodeAs<RecordDecl>("unionDecl")) {
if (!RD->isUnion()) return; // Filtra apenas unions
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = RD->getBeginLoc();
SourceLocation EndLoc = RD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(RD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Union: " << RD->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para variaveis globais
class GlobalVariableExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const VarDecl *VD = Result.Nodes.getNodeAs<VarDecl>("globalVarDecl")) {
// Verifica se a variável tem armazenamento global
if (!VD->hasGlobalStorage())
return;
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = VD->getBeginLoc();
SourceLocation EndLoc = VD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(VD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Global Variable: " << VD->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para funções extern
class ExternFunctionExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const FunctionDecl *FD = Result.Nodes.getNodeAs<FunctionDecl>("externFuncDecl")) {
// Funções possuem ligação externa por padrão, mas podemos checar se realmente tem external linkage
//if (FD->getLinkageInternal() != ExternalLinkage)
if (FD->getLinkageInternal() != Linkage::External)
return;
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = FD->getBeginLoc();
SourceLocation EndLoc = FD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(FD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Extern Function: "
<< FD->getNameInfo().getName().getAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para variáveis extern
class ExternVariableExtractorCallback : public MatchFinder::MatchCallback {
public:
void run(const MatchFinder::MatchResult &Result) override {
if (const VarDecl *VD = Result.Nodes.getNodeAs<VarDecl>("externVarDecl")) {
// Verifica se a variável tem storage class extern
if (VD->getStorageClass() != SC_Extern)
return;
SourceManager &SM = *Result.SourceManager;
SourceLocation StartLoc = VD->getBeginLoc();
SourceLocation EndLoc = VD->getEndLoc();
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = std::string(Lexer::getSourceText(CharSourceRange::getTokenRange(VD->getSourceRange()), SM, LangOptions())) + ";\n";
llvm::outs() << "Extern Variable: " << VD->getNameAsString() << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
}
};
// Callback para capturar definições de macro
class MacroExtractorCallback : public PPCallbacks {
public:
MacroExtractorCallback(SourceManager &SM, const std::string &TargetMacro)
: SM(SM), TargetMacro(TargetMacro) {}
void MacroDefined(const Token &MacroNameToken, const MacroDirective *MD) override {
IdentifierInfo *II = MacroNameToken.getIdentifierInfo();
if (!II)
return;
std::string DefinedMacro = II->getName().str();
if (DefinedMacro != TargetMacro)
return;
const MacroInfo *MI = MD->getMacroInfo();
if (!MI)
return;
// Obter o início da definição da macro
SourceLocation StartLoc = MI->getDefinitionLoc();
SourceLocation EndLoc;
ArrayRef<Token> Tokens = MI->tokens();
if (!Tokens.empty()) {
// Para macros, pegue o final do último token
EndLoc = Tokens.back().getEndLoc();
} else {
EndLoc = MI->getDefinitionLoc();
}
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
std::string Definition = "";
CharSourceRange Range = CharSourceRange::getCharRange(StartLoc, EndLoc);
StringRef MacroText = Lexer::getSourceText(Range, SM, LangOptions());
if(MacroText.empty())
Definition = "#define " + DefinedMacro + "\n";
else
Definition = std::string("#define") + MacroText.str() + "\n";
llvm::outs() << "Macro: " << DefinedMacro << "\n";
llvm::outs() << "Start Line: " << StartLine << "\n";
llvm::outs() << "End Line: " << EndLine << "\n";
llvm::outs() << "Definition:\n"
<< Definition;
saveToFile(Definition, StartLine, EndLine);
}
private:
SourceManager &SM;
std::string TargetMacro;
};
// FrontendAction customizada para extrair macros
class MacroExtractorAction : public ASTFrontendAction {
public:
MacroExtractorAction(const std::string &MacroName) : TargetMacro(MacroName) {}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
// Registra o callback para capturar macros no pré-processador
CI.getPreprocessor().addPPCallbacks(std::make_unique<MacroExtractorCallback>(CI.getSourceManager(), TargetMacro));
// Retorna um ASTConsumer vazio, pois não precisamos processar o AST
return std::make_unique<ASTConsumer>();
}
private:
std::string TargetMacro;
};
// Factory customizada para MacroExtractorAction
class MacroFrontendActionFactory : public FrontendActionFactory {
public:
MacroFrontendActionFactory(const std::string &MacroName) : TargetMacro(MacroName) {}
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<MacroExtractorAction>(TargetMacro);
}
private:
std::string TargetMacro;
};
int main(int argc, const char **argv) {
// Cria o OptionsParser; ele processa os argumentos e extra-args (como -extra-arg) automaticamente.
auto ExpectedParser = CommonOptionsParser::create(argc, argv, ToolCategory);
if (!ExpectedParser) {
llvm::errs() << "Erro ao criar OptionsParser.\n";
return 1;
}
CommonOptionsParser &OptionsParser = ExpectedParser.get();
// Cria o ClangTool usando o CompilationDatabase e a lista de arquivos obtidos pelo OptionsParser.
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
// Insere o argumento "-fsyntax-only" ao final dos argumentos.
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster({"-fsyntax-only"}, ArgumentInsertPosition::END));
// Se a opção -macro foi fornecida, utiliza a ação customizada para macro
if (!MacroName.empty()) {
MacroFrontendActionFactory MacroFactory(MacroName);
return Tool.run(&MacroFactory);
}
else
{
// Se não estiver extraindo macro, pode executar outra ação, como a SyntaxOnlyAction
// Configura os callbacks e o matcher.
FunctionExtractorCallback FuncCallback;
PrototypeExtractorCallback PrototypeCallback;
TypedefExtractorCallback TypedefCallback;
StructExtractorCallback StructCallback;
EnumExtractorCallback EnumCallback;
UnionExtractorCallback UnionCallback;
GlobalVariableExtractorCallback GlobalVarCallback;
ExternFunctionExtractorCallback ExternFuncCallback;
ExternVariableExtractorCallback ExternVarCallback;
MatchFinder Finder;
if (!FunctionName.empty()) {
Finder.addMatcher(functionDecl(hasName(FunctionName), isDefinition(), isExpansionInMainFile()).bind("funcDecl"), &FuncCallback);
}
if (!PrototypeName.empty()) {
Finder.addMatcher(functionDecl(hasName(PrototypeName), unless(isDefinition()), isExpansionInMainFile()).bind("funcDecl"), &PrototypeCallback);
}
if (!TypedefName.empty()) {
Finder.addMatcher(typedefDecl(hasName(TypedefName), isExpansionInMainFile()).bind("typedefDecl"), &TypedefCallback);
}
if (!StructName.empty()) {
Finder.addMatcher(recordDecl(hasName(StructName), isStruct(), isExpansionInMainFile()).bind("structDecl"), &StructCallback);
}
if (!EnumName.empty()) {
Finder.addMatcher(enumDecl(hasName(EnumName), isExpansionInMainFile()).bind("enumDecl"), &EnumCallback);
}
if (!UnionName.empty()) {
Finder.addMatcher(recordDecl(hasName(UnionName), isUnion(), isExpansionInMainFile()).bind("unionDecl"), &UnionCallback);
}
if (!GlobalVarName.empty()) {
Finder.addMatcher(
varDecl(hasGlobalStorage(), hasName(GlobalVarName), isExpansionInMainFile()).bind("globalVarDecl"),
&GlobalVarCallback);
}
if (!ExternFunctionName.empty()) {
Finder.addMatcher(
functionDecl(hasName(ExternFunctionName), isExpansionInMainFile()).bind("externFuncDecl"),
&ExternFuncCallback);
}
if (!ExternVariableName.empty()) {
Finder.addMatcher(
varDecl(hasName(ExternVariableName), isExpansionInMainFile()).bind("externVarDecl"),
&ExternVarCallback);
}
return Tool.run(newFrontendActionFactory(&Finder).get());
}
}