clang 22.0.0git
IndexingAction.cpp
Go to the documentation of this file.
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
10#include "IndexingContext.h"
11#include "clang/AST/DeclGroup.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(*MacroNameTok.getIdentifierInfo(),
35 Range.getBegin(), *MD.getMacroInfo());
36 }
37
38 void MacroDefined(const Token &MacroNameTok,
39 const MacroDirective *MD) override {
40 IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
41 MacroNameTok.getLocation(),
42 *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(*MacroNameTok.getIdentifierInfo(),
50 MacroNameTok.getLocation(),
51 *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(*MacroNameTok.getIdentifierInfo(),
60 MacroNameTok.getLocation(),
61 *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(*MacroNameTok.getIdentifierInfo(),
68 MacroNameTok.getLocation(),
69 *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(*MacroNameTok.getIdentifierInfo(),
76 MacroNameTok.getLocation(),
77 *MD.getMacroInfo());
78 }
79
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(*MacroNameTok.getIdentifierInfo(),
87 MacroNameTok.getLocation(),
88 *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(*MacroNameTok.getIdentifierInfo(),
95 MacroNameTok.getLocation(),
96 *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(Context);
126 IndexCtx->getDataConsumer().setPreprocessor(PP);
127 PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(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(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 DataConsumer, Opts, CI.getPreprocessorPtr(),
173 /*ShouldSkipFunctionBody=*/[](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>(DataConsumer, Opts, PP,
184 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(std::move(DataConsumer), Opts, std::move(PP),
199 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>(std::move(DataConsumer), 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) {
216}
217
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
237 DataConsumer.handleMacroOccurrence(II, MI, static_cast<unsigned>(Role), Loc);
238}
239
241 IndexDataConsumer &DataConsumer) {
242 for (const auto &M : PP.macros()) {
243 for (auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
244 indexPreprocessorMacro(M.first, MD->getMacroInfo(), MD->getKind(),
245 MD->getLocation(), DataConsumer);
246 }
247 }
248}
249
252 IndexDataConsumer &DataConsumer) {
253 for (const auto &M : PP.macros()) {
254 if (M.second.getLatest() == nullptr) {
255 for (auto *MM : PP.getLeafModuleMacros(M.first)) {
256 auto *OwningMod = MM->getOwningModule();
257 if (OwningMod && OwningMod->getASTFile() == Mod.File) {
258 if (auto *MI = MM->getMacroInfo()) {
260 MI->getDefinitionLoc(), DataConsumer);
261 }
262 }
263 }
264 }
265 }
266}
267
269 IndexingOptions Opts) {
270 IndexingContext IndexCtx(Opts, DataConsumer);
271 IndexCtx.setASTContext(Unit.getASTContext());
272 DataConsumer.initialize(Unit.getASTContext());
273 DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
274
276 indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer);
277 indexTranslationUnit(Unit, IndexCtx);
278 DataConsumer.finish();
279}
280
283 IndexDataConsumer &DataConsumer,
284 IndexingOptions Opts) {
285 IndexingContext IndexCtx(Opts, DataConsumer);
286 IndexCtx.setASTContext(Ctx);
287
288 DataConsumer.initialize(Ctx);
289
291 indexPreprocessorMacros(PP, DataConsumer);
292
293 for (const Decl *D : Decls)
294 IndexCtx.indexTopLevelDecl(D);
295 DataConsumer.finish();
296}
297
298std::unique_ptr<PPCallbacks>
300 return std::make_unique<IndexPPCallbacks>(
301 std::make_shared<IndexingContext>(Opts, Consumer));
302}
303
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(Reader.getPreprocessor(), Mod, DataConsumer);
314 }
315
316 for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
317 IndexCtx.indexTopLevelDecl(D);
318 }
319 DataConsumer.finish();
320}
std::shared_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token's formatting.
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
static void indexPreprocessorModuleMacros(Preprocessor &PP, serialization::ModuleFile &Mod, IndexDataConsumer &DataConsumer)
static void indexPreprocessorMacros(Preprocessor &PP, IndexDataConsumer &DataConsumer)
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx)
static void indexPreprocessorMacro(const IdentifierInfo *II, const MacroInfo *MI, MacroDirective::Kind DirectiveKind, SourceLocation Loc, IndexDataConsumer &DataConsumer)
static bool topLevelDeclVisitor(void *context, const Decl *D)
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition ASTConsumer.h:35
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
TranslationUnitDecl * getTranslationUnitDecl() const
Abstract base class to use for AST consumer-based frontend actions.
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:430
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
Definition ASTReader.h:2611
llvm::iterator_range< ModuleDeclIterator > getModuleFileLevelDecls(ModuleFile &Mod)
Preprocessor & getPreprocessor() const
Retrieve the preprocessor.
Definition ASTReader.h:1996
Utility class for loading a ASTContext from an AST file.
Definition ASTUnit.h:93
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
Definition ASTUnit.cpp:2424
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
Definition ASTUnit.h:447
const Preprocessor & getPreprocessor() const
Definition ASTUnit.h:445
const ASTContext & getASTContext() const
Definition ASTUnit.h:449
std::shared_ptr< Preprocessor > getPreprocessorPtr()
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2373
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
One of these records is kept for each identifier that is lexed.
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
Definition MacroInfo.h:606
const MacroInfo * getMacroInfo() const
Definition MacroInfo.h:416
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
virtual void Elifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifndef branch is taken.
virtual void Elifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifdef branch is taken.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
Encodes a location in the source.
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:195
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition Token.h:140
virtual void setPreprocessor(std::shared_ptr< Preprocessor > PP)
virtual bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, SymbolRoleSet Roles, SourceLocation Loc)
virtual void initialize(ASTContext &Ctx)
void setASTContext(ASTContext &ctx)
bool indexTopLevelDecl(const Decl *D)
Information about a module that has been loaded by the ASTReader.
Definition ModuleFile.h:130
FileEntryRef File
The file entry for the module file.
Definition ModuleFile.h:185
#define bool
Definition gpuintrin.h:32
std::unique_ptr< PPCallbacks > indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts)
Creates a PPCallbacks that indexes macros and feeds macros to Consumer.
void indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP, ArrayRef< const Decl * > Decls, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes Decls.
void indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all top-level decls in the module.
std::unique_ptr< ASTConsumer > createIndexingASTConsumer(std::shared_ptr< IndexDataConsumer > DataConsumer, const IndexingOptions &Opts, std::shared_ptr< Preprocessor > PP)
Creates an ASTConsumer that indexes all symbols (macros and AST decls).
std::unique_ptr< FrontendAction > createIndexingAction(std::shared_ptr< IndexDataConsumer > DataConsumer, const IndexingOptions &Opts)
Creates a frontend action that indexes all symbols (macros and AST decls).
void indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all decls in the AST.
The JSON file list parser is used to communicate input to InstallAPI.
int const char * function
Definition c++config.h:31
std::function< bool(const Decl *)> ShouldTraverseDecl