-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.cpp
75 lines (63 loc) · 2.28 KB
/
where.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
#include <string>
#include <clang/AST/ASTContext.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/Decl.h>
#include <clang/AST/DeclGroup.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Parse/ParseAST.h>
#include <clang/Lex/Preprocessor.h>
struct consumer : clang::ASTConsumer {
explicit consumer(std::string const& target)
: target_(target),
ctx_(nullptr) {}
void HandleTopLevelDecl(clang::DeclGroupRef decls) {
for (auto& decl : decls) {
if (auto const* fd = llvm::dyn_cast<clang::FunctionDecl>(decl)) {
handle_functiondecl(fd);
}
}
}
void Initialize(clang::ASTContext& ctx) {
ctx_ = &ctx;
}
private:
void handle_functiondecl(clang::FunctionDecl const* fd) const {
llvm::errs() << fd->getDeclName().getAsString() << '\n';
if (fd->getDeclName().getAsString() == target_) {
auto const& range = fd->getSourceRange();
range.getBegin().print(llvm::outs(), ctx_->getSourceManager());
llvm::outs() << '\n';
}
}
std::string target_;
clang::ASTContext* ctx_;
};
int main(int argc, char** argv) {
clang::CompilerInstance compiler;
compiler.createDiagnostics(argc, argv);
auto& diag = compiler.getDiagnostics();
auto& invocation = compiler.getInvocation();
clang::CompilerInvocation::CreateFromArgs(invocation, argv + 1, argv + argc - 1, diag);
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(diag, compiler.getTargetOpts()));
compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor();
compiler.createASTContext();
compiler.setASTConsumer(new consumer(argv[argc - 1]));
compiler.createSema(false, nullptr);
auto& pp = compiler.getPreprocessor();
pp.getBuiltinInfo().InitializeBuiltins(
pp.getIdentifierTable(), pp.getLangOptions());
auto& inputs = compiler.getFrontendOpts().Inputs;
if (inputs.size() > 0) {
compiler.InitializeSourceManager(inputs[0].second);
clang::ParseAST(
pp,
&compiler.getASTConsumer(),
compiler.getASTContext()
);
}
return 0;
}