25 std::shared_ptr<IndexingContext> IndexCtx;
28 IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
29 : IndexCtx(std::move(IndexCtx)) {}
31 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
32 SourceRange Range,
const MacroArgs *Args)
override {
37 void MacroDefined(
const Token &MacroNameTok,
38 const MacroDirective *MD)
override {
44 void MacroUndefined(
const Token &MacroNameTok,
const MacroDefinition &MD,
45 const MacroDirective *Undef)
override {
53 void Defined(
const Token &MacroNameTok,
const MacroDefinition &MD,
54 SourceRange Range)
override {
62 void Ifdef(SourceLocation Loc,
const Token &MacroNameTok,
63 const MacroDefinition &MD)
override {
70 void Ifndef(SourceLocation Loc,
const Token &MacroNameTok,
71 const MacroDefinition &MD)
override {
81 void Elifdef(SourceLocation Loc,
const Token &MacroNameTok,
82 const MacroDefinition &MD)
override {
89 void Elifndef(SourceLocation Loc,
const Token &MacroNameTok,
90 const MacroDefinition &MD)
override {
100 std::shared_ptr<IndexDataConsumer> DataConsumer;
101 std::shared_ptr<IndexingContext> IndexCtx;
102 std::shared_ptr<Preprocessor> PP;
106 IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
107 const IndexingOptions &Opts,
108 std::shared_ptr<Preprocessor> PP,
110 : DataConsumer(std::move(DataConsumer)),
111 IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
113 ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)) {
114 assert(this->DataConsumer !=
nullptr);
115 assert(this->PP !=
nullptr);
119 void Initialize(ASTContext &Context)
override {
120 IndexCtx->setASTContext(Context);
121 IndexCtx->getDataConsumer().initialize(Context);
122 IndexCtx->getDataConsumer().setPreprocessor(PP);
123 PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
126 bool HandleTopLevelDecl(DeclGroupRef DG)
override {
127 return IndexCtx->indexDeclGroupRef(DG);
130 void HandleInterestingDecl(DeclGroupRef DG)
override {
134 void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG)
override {
135 IndexCtx->indexDeclGroupRef(DG);
138 void HandleTranslationUnit(ASTContext &Ctx)
override {
139 DataConsumer->finish();
142 bool shouldSkipFunctionBody(Decl *D)
override {
143 return ShouldSkipFunctionBody(D);
148 std::shared_ptr<IndexDataConsumer> DataConsumer;
149 IndexingOptions Opts;
152 IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
153 const IndexingOptions &Opts)
154 : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
155 assert(this->DataConsumer !=
nullptr);
159 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
160 StringRef InFile)
override {
161 return std::make_unique<IndexASTConsumer>(
163 [](
const Decl *) { return false; });
170 std::shared_ptr<IndexDataConsumer> DataConsumer,
173 return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
174 ShouldSkipFunctionBody);
178 std::shared_ptr<IndexDataConsumer> DataConsumer,
184 ShouldSkipFunctionBody =
186 return !ShouldTraverseDecl(D);
189 std::move(ShouldSkipFunctionBody));
192std::unique_ptr<FrontendAction>
195 assert(DataConsumer !=
nullptr);
196 return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
232 for (
const auto &M : PP.
macros()) {
233 for (
auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
235 MD->getLocation(), DataConsumer);
243 for (
const auto &M : PP.
macros()) {
244 if (M.second.getLatest() ==
nullptr) {
246 auto *OwningMod = MM->getOwningModule();
247 if (OwningMod && OwningMod->getASTFile() == Mod.
File) {
248 if (
auto *MI = MM->getMacroInfo()) {
250 MI->getDefinitionLoc(), DataConsumer);
283 for (
const Decl *D : Decls)
288std::unique_ptr<PPCallbacks>
290 return std::make_unique<IndexPPCallbacks>(
291 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 ...
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 - 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