26 std::shared_ptr<IndexingContext> IndexCtx;
29 IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
30 : IndexCtx(std::move(IndexCtx)) {}
32 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
33 SourceRange Range,
const MacroArgs *Args)
override {
38 void MacroDefined(
const Token &MacroNameTok,
39 const MacroDirective *MD)
override {
45 void MacroUndefined(
const Token &MacroNameTok,
const MacroDefinition &MD,
46 const MacroDirective *Undef)
override {
54 void Defined(
const Token &MacroNameTok,
const MacroDefinition &MD,
55 SourceRange Range)
override {
63 void Ifdef(SourceLocation Loc,
const Token &MacroNameTok,
64 const MacroDefinition &MD)
override {
71 void Ifndef(SourceLocation Loc,
const Token &MacroNameTok,
72 const MacroDefinition &MD)
override {
82 void Elifdef(SourceLocation Loc,
const Token &MacroNameTok,
83 const MacroDefinition &MD)
override {
90 void Elifndef(SourceLocation Loc,
const Token &MacroNameTok,
91 const MacroDefinition &MD)
override {
101 std::shared_ptr<IndexDataConsumer> DataConsumer;
102 std::shared_ptr<IndexingContext> IndexCtx;
103 std::shared_ptr<Preprocessor> PP;
105 bool DeferIndexingToEndOfTranslationUnit;
108 IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
109 const IndexingOptions &Opts,
110 std::shared_ptr<Preprocessor> PP,
112 : DataConsumer(std::move(DataConsumer)),
113 IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
115 ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)),
116 DeferIndexingToEndOfTranslationUnit(
117 Opts.DeferIndexingToEndOfTranslationUnit) {
118 assert(this->DataConsumer !=
nullptr);
119 assert(this->PP !=
nullptr);
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));
130 bool HandleTopLevelDecl(DeclGroupRef DG)
override {
131 if (!DeferIndexingToEndOfTranslationUnit)
132 return IndexCtx->indexDeclGroupRef(DG);
136 void HandleInterestingDecl(DeclGroupRef DG)
override {
140 void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG)
override {
141 if (!DeferIndexingToEndOfTranslationUnit)
142 IndexCtx->indexDeclGroupRef(DG);
145 void HandleTranslationUnit(ASTContext &Ctx)
override {
146 if (DeferIndexingToEndOfTranslationUnit)
148 IndexCtx->indexTopLevelDecl(DG);
149 DataConsumer->finish();
152 bool shouldSkipFunctionBody(Decl *D)
override {
153 return ShouldSkipFunctionBody(D);
158 std::shared_ptr<IndexDataConsumer> DataConsumer;
159 IndexingOptions Opts;
162 IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
163 const IndexingOptions &Opts)
164 : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
165 assert(this->DataConsumer !=
nullptr);
169 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
170 StringRef InFile)
override {
171 return std::make_unique<IndexASTConsumer>(
173 [](
const Decl *) { return false; });
180 std::shared_ptr<IndexDataConsumer> DataConsumer,
183 return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
184 ShouldSkipFunctionBody);
188 std::shared_ptr<IndexDataConsumer> DataConsumer,
194 ShouldSkipFunctionBody =
196 return !ShouldTraverseDecl(D);
199 std::move(ShouldSkipFunctionBody));
202std::unique_ptr<FrontendAction>
205 assert(DataConsumer !=
nullptr);
206 return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
242 for (
const auto &M : PP.
macros()) {
243 for (
auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
245 MD->getLocation(), DataConsumer);
253 for (
const auto &M : PP.
macros()) {
254 if (M.second.getLatest() ==
nullptr) {
256 auto *OwningMod = MM->getOwningModule();
257 if (OwningMod && OwningMod->getASTFile() == Mod.
File) {
258 if (
auto *MI = MM->getMacroInfo()) {
260 MI->getDefinitionLoc(), DataConsumer);
293 for (
const Decl *D : Decls)
298std::unique_ptr<PPCallbacks>
300 return std::make_unique<IndexPPCallbacks>(
301 std::make_shared<IndexingContext>(Opts, Consumer));
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.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
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.
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
llvm::iterator_range< ModuleDeclIterator > getModuleFileLevelDecls(ModuleFile &Mod)
Preprocessor & getPreprocessor() const
Retrieve the preprocessor.
Utility class for loading a ASTContext from an AST file.
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
const Preprocessor & getPreprocessor() const
const ASTContext & getASTContext() const
std::shared_ptr< Preprocessor > getPreprocessorPtr()
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
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.
const MacroInfo * getMacroInfo() const
Encapsulates the data about a macro definition (e.g.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
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
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
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.
FileEntryRef File
The file entry for the module file.
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
std::function< bool(const Decl *)> ShouldTraverseDecl
bool IndexMacrosInPreprocessor