1//===- IndexingAction.cpp - Frontend index action -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Index/IndexingAction.h"
10#include "IndexingContext.h"
11#include "clang/AST/DeclGroup.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/FrontendAction.h"
14#include "clang/Index/IndexDataConsumer.h"
15#include "clang/Lex/PPCallbacks.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Serialization/ASTReader.h"
18#include <memory>
19
20using namespace clang;
21using namespace clang::index;
22
23namespace {
24
25class IndexPPCallbacks final : public PPCallbacks {
26 std::shared_ptr<IndexingContext> IndexCtx;
27
28public:
29 IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
30 : IndexCtx(std::move(IndexCtx)) {}
31
32 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
33 SourceRange Range, const MacroArgs *Args) override {
34 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
35 Loc: Range.getBegin(), MD: *MD.getMacroInfo());
36 }
37
38 void MacroDefined(const Token &MacroNameTok,
39 const MacroDirective *MD) override {
40 IndexCtx->handleMacroDefined(Name: *MacroNameTok.getIdentifierInfo(),
41 Loc: MacroNameTok.getLocation(),
42 MI: *MD->getMacroInfo());
43 }
44
45 void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
46 const MacroDirective *Undef) override {
47 if (!MD.getMacroInfo()) // Ignore noop #undef.
48 return;
49 IndexCtx->handleMacroUndefined(Name: *MacroNameTok.getIdentifierInfo(),
50 Loc: MacroNameTok.getLocation(),
51 MI: *MD.getMacroInfo());
52 }
53
54 void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
55 SourceRange Range) override {
56 if (!MD.getMacroInfo()) // Ignore nonexistent macro.
57 return;
58 // Note: this is defined(M), not #define M
59 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
60 Loc: MacroNameTok.getLocation(),
61 MD: *MD.getMacroInfo());
62 }
63 void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
64 const MacroDefinition &MD) override {
65 if (!MD.getMacroInfo()) // Ignore non-existent macro.
66 return;
67 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
68 Loc: MacroNameTok.getLocation(),
69 MD: *MD.getMacroInfo());
70 }
71 void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
72 const MacroDefinition &MD) override {
73 if (!MD.getMacroInfo()) // Ignore nonexistent macro.
74 return;
75 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
76 Loc: MacroNameTok.getLocation(),
77 MD: *MD.getMacroInfo());
78 }
79
80 using PPCallbacks::Elifdef;
81 using PPCallbacks::Elifndef;
82 void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
83 const MacroDefinition &MD) override {
84 if (!MD.getMacroInfo()) // Ignore non-existent macro.
85 return;
86 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
87 Loc: MacroNameTok.getLocation(),
88 MD: *MD.getMacroInfo());
89 }
90 void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
91 const MacroDefinition &MD) override {
92 if (!MD.getMacroInfo()) // Ignore non-existent macro.
93 return;
94 IndexCtx->handleMacroReference(Name: *MacroNameTok.getIdentifierInfo(),
95 Loc: MacroNameTok.getLocation(),
96 MD: *MD.getMacroInfo());
97 }
98};
99
100class IndexASTConsumer final : public ASTConsumer {
101 std::shared_ptr<IndexDataConsumer> DataConsumer;
102 std::shared_ptr<IndexingContext> IndexCtx;
103 std::shared_ptr<Preprocessor> PP;
104 std::function<bool(const Decl *)> ShouldSkipFunctionBody;
105 bool DeferIndexingToEndOfTranslationUnit;
106
107public:
108 IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
109 const IndexingOptions &Opts,
110 std::shared_ptr<Preprocessor> PP,
111 std::function<bool(const Decl *)> ShouldSkipFunctionBody)
112 : DataConsumer(std::move(DataConsumer)),
113 IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
114 PP(std::move(PP)),
115 ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)),
116 DeferIndexingToEndOfTranslationUnit(
117 Opts.DeferIndexingToEndOfTranslationUnit) {
118 assert(this->DataConsumer != nullptr);
119 assert(this->PP != nullptr);
120 }
121
122protected:
123 void Initialize(ASTContext &Context) override {
124 IndexCtx->setASTContext(Context);
125 IndexCtx->getDataConsumer().initialize(Ctx&: Context);
126 IndexCtx->getDataConsumer().setPreprocessor(PP);
127 PP->addPPCallbacks(C: std::make_unique<IndexPPCallbacks>(args&: IndexCtx));
128 }
129
130 bool HandleTopLevelDecl(DeclGroupRef DG) override {
131 if (!DeferIndexingToEndOfTranslationUnit)
132 return IndexCtx->indexDeclGroupRef(DG);
133 return true;
134 }
135
136 void HandleInterestingDecl(DeclGroupRef DG) override {
137 // Ignore deserialized decls.
138 }
139
140 void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
141 if (!DeferIndexingToEndOfTranslationUnit)
142 IndexCtx->indexDeclGroupRef(DG);
143 }
144
145 void HandleTranslationUnit(ASTContext &Ctx) override {
146 if (DeferIndexingToEndOfTranslationUnit)
147 for (auto *DG : Ctx.getTranslationUnitDecl()->decls())
148 IndexCtx->indexTopLevelDecl(D: DG);
149 DataConsumer->finish();
150 }
151
152 bool shouldSkipFunctionBody(Decl *D) override {
153 return ShouldSkipFunctionBody(D);
154 }
155};
156
157class IndexAction final : public ASTFrontendAction {
158 std::shared_ptr<IndexDataConsumer> DataConsumer;
159 IndexingOptions Opts;
160
161public:
162 IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
163 const IndexingOptions &Opts)
164 : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
165 assert(this->DataConsumer != nullptr);
166 }
167
168protected:
169 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
170 StringRef InFile) override {
171 return std::make_unique<IndexASTConsumer>(
172 args&: DataConsumer, args&: Opts, args: CI.getPreprocessorPtr(),
173 /*ShouldSkipFunctionBody=*/args: [](const Decl *) { return false; });
174 }
175};
176
177} // anonymous namespace
178
179std::unique_ptr<ASTConsumer> index::createIndexingASTConsumer(
180 std::shared_ptr<IndexDataConsumer> DataConsumer,
181 const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
182 std::function<bool(const Decl *)> ShouldSkipFunctionBody) {
183 return std::make_unique<IndexASTConsumer>(args&: DataConsumer, args: Opts, args&: PP,
184 args&: ShouldSkipFunctionBody);
185}
186
187std::unique_ptr<ASTConsumer> clang::index::createIndexingASTConsumer(
188 std::shared_ptr<IndexDataConsumer> DataConsumer,
189 const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP) {
190 std::function<bool(const Decl *)> ShouldSkipFunctionBody = [](const Decl *) {
191 return false;
192 };
193 if (Opts.ShouldTraverseDecl)
194 ShouldSkipFunctionBody =
195 [ShouldTraverseDecl(Opts.ShouldTraverseDecl)](const Decl *D) {
196 return !ShouldTraverseDecl(D);
197 };
198 return createIndexingASTConsumer(DataConsumer: std::move(DataConsumer), Opts, PP: std::move(PP),
199 ShouldSkipFunctionBody: std::move(ShouldSkipFunctionBody));
200}
201
202std::unique_ptr<FrontendAction>
203index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
204 const IndexingOptions &Opts) {
205 assert(DataConsumer != nullptr);
206 return std::make_unique<IndexAction>(args: std::move(DataConsumer), args: Opts);
207}
208
209static bool topLevelDeclVisitor(void *context, const Decl *D) {
210 IndexingContext &IndexCtx = *static_cast<IndexingContext *>(context);
211 return IndexCtx.indexTopLevelDecl(D);
212}
213
214static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
215 Unit.visitLocalTopLevelDecls(context: &IndexCtx, Fn: topLevelDeclVisitor);
216}
217
218static void indexPreprocessorMacro(const IdentifierInfo *II,
219 const MacroInfo *MI,
220 MacroDirective::Kind DirectiveKind,
221 SourceLocation Loc,
222 IndexDataConsumer &DataConsumer) {
223 // When using modules, it may happen that we find #undef of a macro that
224 // was defined in another module. In such case, MI may be nullptr, since
225 // we only look for macro definitions in the current TU. In that case,
226 // there is nothing to index.
227 if (!MI)
228 return;
229
230 // Skip implicit visibility change.
231 if (DirectiveKind == MacroDirective::MD_Visibility)
232 return;
233
234 auto Role = DirectiveKind == MacroDirective::MD_Define
235 ? SymbolRole::Definition
236 : SymbolRole::Undefinition;
237 DataConsumer.handleMacroOccurrence(Name: II, MI, Roles: static_cast<unsigned>(Role), Loc);
238}
239
240static void indexPreprocessorMacros(Preprocessor &PP,
241 IndexDataConsumer &DataConsumer) {
242 for (const auto &M : PP.macros()) {
243 for (auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
244 indexPreprocessorMacro(II: M.first, MI: MD->getMacroInfo(), DirectiveKind: MD->getKind(),
245 Loc: MD->getLocation(), DataConsumer);
246 }
247 }
248}
249
250static void indexPreprocessorModuleMacros(Preprocessor &PP,
251 serialization::ModuleFile &Mod,
252 IndexDataConsumer &DataConsumer) {
253 for (const auto &M : PP.macros()) {
254 if (M.second.getLatest() == nullptr) {
255 for (auto *MM : PP.getLeafModuleMacros(II: M.first)) {
256 auto *OwningMod = MM->getOwningModule();
257 if (OwningMod && OwningMod->getASTFile() == Mod.File) {
258 if (auto *MI = MM->getMacroInfo()) {
259 indexPreprocessorMacro(II: M.first, MI, DirectiveKind: MacroDirective::MD_Define,
260 Loc: MI->getDefinitionLoc(), DataConsumer);
261 }
262 }
263 }
264 }
265 }
266}
267
268void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer,
269 IndexingOptions Opts) {
270 IndexingContext IndexCtx(Opts, DataConsumer);
271 IndexCtx.setASTContext(Unit.getASTContext());
272 DataConsumer.initialize(Ctx&: Unit.getASTContext());
273 DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
274
275 if (Opts.IndexMacrosInPreprocessor)
276 indexPreprocessorMacros(PP&: Unit.getPreprocessor(), DataConsumer);
277 indexTranslationUnit(Unit, IndexCtx);
278 DataConsumer.finish();
279}
280
281void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP,
282 ArrayRef<const Decl *> Decls,
283 IndexDataConsumer &DataConsumer,
284 IndexingOptions Opts) {
285 IndexingContext IndexCtx(Opts, DataConsumer);
286 IndexCtx.setASTContext(Ctx);
287
288 DataConsumer.initialize(Ctx);
289
290 if (Opts.IndexMacrosInPreprocessor)
291 indexPreprocessorMacros(PP, DataConsumer);
292
293 for (const Decl *D : Decls)
294 IndexCtx.indexTopLevelDecl(D);
295 DataConsumer.finish();
296}
297
298std::unique_ptr<PPCallbacks>
299index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) {
300 return std::make_unique<IndexPPCallbacks>(
301 args: std::make_shared<IndexingContext>(args&: Opts, args&: Consumer));
302}
303
304void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader,
305 IndexDataConsumer &DataConsumer,
306 IndexingOptions Opts) {
307 ASTContext &Ctx = Reader.getContext();
308 IndexingContext IndexCtx(Opts, DataConsumer);
309 IndexCtx.setASTContext(Ctx);
310 DataConsumer.initialize(Ctx);
311
312 if (Opts.IndexMacrosInPreprocessor) {
313 indexPreprocessorModuleMacros(PP&: Reader.getPreprocessor(), Mod, DataConsumer);
314 }
315
316 for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
317 IndexCtx.indexTopLevelDecl(D);
318 }
319 DataConsumer.finish();
320}
321