diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2021-08-22 19:00:43 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2021-11-13 20:39:49 +0000 |
commit | fe6060f10f634930ff71b7c50291ddc610da2475 (patch) | |
tree | 1483580c790bd4d27b6500a7542b5ee00534d3cc /contrib/llvm-project/clang/lib/Index/IndexingAction.cpp | |
parent | b61bce17f346d79cecfd8f195a64b10f77be43b1 (diff) | |
parent | 344a3780b2e33f6ca763666c380202b18aab72a3 (diff) |
Diffstat (limited to 'contrib/llvm-project/clang/lib/Index/IndexingAction.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Index/IndexingAction.cpp | 111 |
1 files changed, 94 insertions, 17 deletions
diff --git a/contrib/llvm-project/clang/lib/Index/IndexingAction.cpp b/contrib/llvm-project/clang/lib/Index/IndexingAction.cpp index 4986303cac47..c9fcaad31128 100644 --- a/contrib/llvm-project/clang/lib/Index/IndexingAction.cpp +++ b/contrib/llvm-project/clang/lib/Index/IndexingAction.cpp @@ -51,6 +51,51 @@ public: MacroNameTok.getLocation(), *MD.getMacroInfo()); } + + void Defined(const Token &MacroNameTok, const MacroDefinition &MD, + SourceRange Range) override { + if (!MD.getMacroInfo()) // Ignore nonexistent macro. + return; + // Note: this is defined(M), not #define M + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } + void Ifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDefinition &MD) override { + if (!MD.getMacroInfo()) // Ignore non-existent macro. + return; + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } + void Ifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDefinition &MD) override { + if (!MD.getMacroInfo()) // Ignore nonexistent macro. + return; + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } + + using PPCallbacks::Elifdef; + using PPCallbacks::Elifndef; + void Elifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDefinition &MD) override { + if (!MD.getMacroInfo()) // Ignore non-existent macro. + return; + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } + void Elifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDefinition &MD) override { + if (!MD.getMacroInfo()) // Ignore non-existent macro. + return; + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } }; class IndexASTConsumer final : public ASTConsumer { @@ -162,23 +207,54 @@ static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) { Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor); } -static void indexPreprocessorMacros(const Preprocessor &PP, +static void indexPreprocessorMacro(const IdentifierInfo *II, + const MacroInfo *MI, + MacroDirective::Kind DirectiveKind, + SourceLocation Loc, + IndexDataConsumer &DataConsumer) { + // When using modules, it may happen that we find #undef of a macro that + // was defined in another module. In such case, MI may be nullptr, since + // we only look for macro definitions in the current TU. In that case, + // there is nothing to index. + if (!MI) + return; + + // Skip implicit visibility change. + if (DirectiveKind == MacroDirective::MD_Visibility) + return; + + auto Role = DirectiveKind == MacroDirective::MD_Define + ? SymbolRole::Definition + : SymbolRole::Undefinition; + DataConsumer.handleMacroOccurrence(II, MI, static_cast<unsigned>(Role), Loc); +} + +static void indexPreprocessorMacros(Preprocessor &PP, IndexDataConsumer &DataConsumer) { - for (const auto &M : PP.macros()) - if (MacroDirective *MD = M.second.getLatest()) { - auto *MI = MD->getMacroInfo(); - // When using modules, it may happen that we find #undef of a macro that - // was defined in another module. In such case, MI may be nullptr, since - // we only look for macro definitions in the current TU. In that case, - // there is nothing to index. - if (!MI) - continue; - - DataConsumer.handleMacroOccurrence( - M.first, MD->getMacroInfo(), - static_cast<unsigned>(index::SymbolRole::Definition), - MD->getLocation()); + for (const auto &M : PP.macros()) { + for (auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) { + indexPreprocessorMacro(M.first, MD->getMacroInfo(), MD->getKind(), + MD->getLocation(), DataConsumer); } + } +} + +static void indexPreprocessorModuleMacros(Preprocessor &PP, + serialization::ModuleFile &Mod, + IndexDataConsumer &DataConsumer) { + for (const auto &M : PP.macros()) { + if (M.second.getLatest() == nullptr) { + for (auto *MM : PP.getLeafModuleMacros(M.first)) { + auto *OwningMod = MM->getOwningModule(); + if (OwningMod && OwningMod->getASTFile() == Mod.File) { + if (auto *MI = MM->getMacroInfo()) { + indexPreprocessorMacro(M.first, MI, MacroDirective::MD_Define, + MI->getDefinitionLoc(), DataConsumer); + } + } + } + } + } } void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, @@ -225,8 +301,9 @@ void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexCtx.setASTContext(Ctx); DataConsumer.initialize(Ctx); - if (Opts.IndexMacrosInPreprocessor) - indexPreprocessorMacros(Reader.getPreprocessor(), DataConsumer); + if (Opts.IndexMacrosInPreprocessor) { + indexPreprocessorModuleMacros(Reader.getPreprocessor(), Mod, DataConsumer); + } for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) { IndexCtx.indexTopLevelDecl(D); |