IndexingAction.cpp
7.37 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
//===- IndexingAction.cpp - Frontend index action -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Index/IndexingAction.h"
#include "IndexingContext.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Index/IndexDataConsumer.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/STLExtras.h"
#include <memory>
using namespace clang;
using namespace clang::index;
namespace {
class IndexPPCallbacks final : public PPCallbacks {
std::shared_ptr<IndexingContext> IndexCtx;
public:
IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
: IndexCtx(std::move(IndexCtx)) {}
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange Range, const MacroArgs *Args) override {
IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
Range.getBegin(), *MD.getMacroInfo());
}
void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) override {
IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
MacroNameTok.getLocation(),
*MD->getMacroInfo());
}
void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
const MacroDirective *Undef) override {
if (!MD.getMacroInfo()) // Ignore noop #undef.
return;
IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(),
MacroNameTok.getLocation(),
*MD.getMacroInfo());
}
};
class IndexASTConsumer final : public ASTConsumer {
std::shared_ptr<IndexDataConsumer> DataConsumer;
std::shared_ptr<IndexingContext> IndexCtx;
std::shared_ptr<Preprocessor> PP;
std::function<bool(const Decl *)> ShouldSkipFunctionBody;
public:
IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
const IndexingOptions &Opts,
std::shared_ptr<Preprocessor> PP,
std::function<bool(const Decl *)> ShouldSkipFunctionBody)
: DataConsumer(std::move(DataConsumer)),
IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
PP(std::move(PP)),
ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)) {
assert(this->DataConsumer != nullptr);
assert(this->PP != nullptr);
}
protected:
void Initialize(ASTContext &Context) override {
IndexCtx->setASTContext(Context);
IndexCtx->getDataConsumer().initialize(Context);
IndexCtx->getDataConsumer().setPreprocessor(PP);
PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
}
bool HandleTopLevelDecl(DeclGroupRef DG) override {
return IndexCtx->indexDeclGroupRef(DG);
}
void HandleInterestingDecl(DeclGroupRef DG) override {
// Ignore deserialized decls.
}
void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
IndexCtx->indexDeclGroupRef(DG);
}
void HandleTranslationUnit(ASTContext &Ctx) override {
DataConsumer->finish();
}
bool shouldSkipFunctionBody(Decl *D) override {
return ShouldSkipFunctionBody(D);
}
};
class IndexAction final : public ASTFrontendAction {
std::shared_ptr<IndexDataConsumer> DataConsumer;
IndexingOptions Opts;
public:
IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
const IndexingOptions &Opts)
: DataConsumer(std::move(DataConsumer)), Opts(Opts) {
assert(this->DataConsumer != nullptr);
}
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
return std::make_unique<IndexASTConsumer>(
DataConsumer, Opts, CI.getPreprocessorPtr(),
/*ShouldSkipFunctionBody=*/[](const Decl *) { return false; });
}
};
} // anonymous namespace
std::unique_ptr<ASTConsumer> index::createIndexingASTConsumer(
std::shared_ptr<IndexDataConsumer> DataConsumer,
const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
std::function<bool(const Decl *)> ShouldSkipFunctionBody) {
return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
ShouldSkipFunctionBody);
}
std::unique_ptr<FrontendAction>
index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
const IndexingOptions &Opts) {
assert(DataConsumer != nullptr);
return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
}
static bool topLevelDeclVisitor(void *context, const Decl *D) {
IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
return IndexCtx.indexTopLevelDecl(D);
}
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor);
}
static void indexPreprocessorMacros(const Preprocessor &PP,
IndexDataConsumer &DataConsumer) {
for (const auto &M : PP.macros())
if (MacroDirective *MD = M.second.getLatest())
DataConsumer.handleMacroOccurrence(
M.first, MD->getMacroInfo(),
static_cast<unsigned>(index::SymbolRole::Definition),
MD->getLocation());
}
void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer,
IndexingOptions Opts) {
IndexingContext IndexCtx(Opts, DataConsumer);
IndexCtx.setASTContext(Unit.getASTContext());
DataConsumer.initialize(Unit.getASTContext());
DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
if (Opts.IndexMacrosInPreprocessor)
indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer);
indexTranslationUnit(Unit, IndexCtx);
DataConsumer.finish();
}
void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP,
ArrayRef<const Decl *> Decls,
IndexDataConsumer &DataConsumer,
IndexingOptions Opts) {
IndexingContext IndexCtx(Opts, DataConsumer);
IndexCtx.setASTContext(Ctx);
DataConsumer.initialize(Ctx);
if (Opts.IndexMacrosInPreprocessor)
indexPreprocessorMacros(PP, DataConsumer);
for (const Decl *D : Decls)
IndexCtx.indexTopLevelDecl(D);
DataConsumer.finish();
}
std::unique_ptr<PPCallbacks>
index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) {
return std::make_unique<IndexPPCallbacks>(
std::make_shared<IndexingContext>(Opts, Consumer));
}
void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader,
IndexDataConsumer &DataConsumer,
IndexingOptions Opts) {
ASTContext &Ctx = Reader.getContext();
IndexingContext IndexCtx(Opts, DataConsumer);
IndexCtx.setASTContext(Ctx);
DataConsumer.initialize(Ctx);
if (Opts.IndexMacrosInPreprocessor)
indexPreprocessorMacros(Reader.getPreprocessor(), DataConsumer);
for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
IndexCtx.indexTopLevelDecl(D);
}
DataConsumer.finish();
}