diff options
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Index/IndexSymbol.cpp | 12 | ||||
-rw-r--r-- | clang/lib/Index/IndexingAction.cpp | 116 | ||||
-rw-r--r-- | clang/lib/Index/IndexingContext.cpp | 23 | ||||
-rw-r--r-- | clang/lib/Index/IndexingContext.h | 11 |
4 files changed, 128 insertions, 34 deletions
diff --git a/clang/lib/Index/IndexSymbol.cpp b/clang/lib/Index/IndexSymbol.cpp index 733d4dbc2f9..03b55ffe8a4 100644 --- a/clang/lib/Index/IndexSymbol.cpp +++ b/clang/lib/Index/IndexSymbol.cpp @@ -12,6 +12,7 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/PrettyPrinter.h" +#include "clang/Lex/MacroInfo.h" using namespace clang; using namespace clang::index; @@ -348,6 +349,15 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { return Info; } +SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) { + SymbolInfo Info; + Info.Kind = SymbolKind::Macro; + Info.SubKind = SymbolSubKind::None; + Info.Properties = SymbolPropertySet(); + Info.Lang = SymbolLanguage::C; + return Info; +} + bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, llvm::function_ref<bool(SymbolRole)> Fn) { #define APPLY_FOR_ROLE(Role) \ @@ -364,6 +374,7 @@ bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, APPLY_FOR_ROLE(Dynamic); APPLY_FOR_ROLE(AddressOf); APPLY_FOR_ROLE(Implicit); + APPLY_FOR_ROLE(Undefinition); APPLY_FOR_ROLE(RelationChildOf); APPLY_FOR_ROLE(RelationBaseOf); APPLY_FOR_ROLE(RelationOverrideOf); @@ -405,6 +416,7 @@ void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) { case SymbolRole::Dynamic: OS << "Dyn"; break; case SymbolRole::AddressOf: OS << "Addr"; break; case SymbolRole::Implicit: OS << "Impl"; break; + case SymbolRole::Undefinition: OS << "Undef"; break; case SymbolRole::RelationChildOf: OS << "RelChild"; break; case SymbolRole::RelationBaseOf: OS << "RelBase"; break; case SymbolRole::RelationOverrideOf: OS << "RelOver"; break; diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp index e378a092bea..b8faf7075e7 100644 --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -13,8 +13,11 @@ #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/MultiplexConsumer.h" #include "clang/Index/IndexDataConsumer.h" +#include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" #include "clang/Serialization/ASTReader.h" +#include "llvm/ADT/STLExtras.h" +#include <memory> using namespace clang; using namespace clang::index; @@ -43,21 +46,22 @@ namespace { class IndexASTConsumer : public ASTConsumer { std::shared_ptr<Preprocessor> PP; - IndexingContext &IndexCtx; + std::shared_ptr<IndexingContext> IndexCtx; public: - IndexASTConsumer(std::shared_ptr<Preprocessor> PP, IndexingContext &IndexCtx) - : PP(std::move(PP)), IndexCtx(IndexCtx) {} + IndexASTConsumer(std::shared_ptr<Preprocessor> PP, + std::shared_ptr<IndexingContext> IndexCtx) + : PP(std::move(PP)), IndexCtx(std::move(IndexCtx)) {} protected: void Initialize(ASTContext &Context) override { - IndexCtx.setASTContext(Context); - IndexCtx.getDataConsumer().initialize(Context); - IndexCtx.getDataConsumer().setPreprocessor(PP); + IndexCtx->setASTContext(Context); + IndexCtx->getDataConsumer().initialize(Context); + IndexCtx->getDataConsumer().setPreprocessor(PP); } bool HandleTopLevelDecl(DeclGroupRef DG) override { - return IndexCtx.indexDeclGroupRef(DG); + return IndexCtx->indexDeclGroupRef(DG); } void HandleInterestingDecl(DeclGroupRef DG) override { @@ -65,22 +69,50 @@ protected: } void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { - IndexCtx.indexDeclGroupRef(DG); + IndexCtx->indexDeclGroupRef(DG); } void HandleTranslationUnit(ASTContext &Ctx) override { } }; +class IndexPPCallbacks : public PPCallbacks { + std::shared_ptr<IndexingContext> IndexCtx; + +public: + IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx) + : IndexCtx(std::move(IndexCtx)) {} + + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, + SourceRange Range, const MacroArgs *Args) override { + IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), + Range.getBegin(), *MD.getMacroInfo()); + } + + void MacroDefined(const Token &MacroNameTok, + const MacroDirective *MD) override { + IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD->getMacroInfo()); + } + + void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + const MacroDirective *Undef) override { + IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(), + MacroNameTok.getLocation(), + *MD.getMacroInfo()); + } +}; + class IndexActionBase { protected: std::shared_ptr<IndexDataConsumer> DataConsumer; - IndexingContext IndexCtx; + std::shared_ptr<IndexingContext> IndexCtx; IndexActionBase(std::shared_ptr<IndexDataConsumer> dataConsumer, IndexingOptions Opts) - : DataConsumer(std::move(dataConsumer)), - IndexCtx(Opts, *DataConsumer) {} + : DataConsumer(std::move(dataConsumer)), + IndexCtx(new IndexingContext(Opts, *DataConsumer)) {} std::unique_ptr<IndexASTConsumer> createIndexASTConsumer(CompilerInstance &CI) { @@ -88,6 +120,10 @@ protected: IndexCtx); } + std::unique_ptr<PPCallbacks> createIndexPPCallbacks() { + return llvm::make_unique<IndexPPCallbacks>(IndexCtx); + } + void finish() { DataConsumer->finish(); } @@ -105,6 +141,11 @@ protected: return createIndexASTConsumer(CI); } + bool BeginSourceFileAction(clang::CompilerInstance &CI) override { + CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); + return true; + } + void EndSourceFileAction() override { FrontendAction::EndSourceFileAction(); finish(); @@ -123,32 +164,34 @@ public: protected: std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, - StringRef InFile) override; - void EndSourceFileAction() override; -}; - -} // anonymous namespace + StringRef InFile) override { + auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); + if (!OtherConsumer) { + IndexActionFailed = true; + return nullptr; + } + + std::vector<std::unique_ptr<ASTConsumer>> Consumers; + Consumers.push_back(std::move(OtherConsumer)); + Consumers.push_back(createIndexASTConsumer(CI)); + return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); + } -void WrappingIndexAction::EndSourceFileAction() { - // Invoke wrapped action's method. - WrapperFrontendAction::EndSourceFileAction(); - if (!IndexActionFailed) - finish(); -} + bool BeginSourceFileAction(clang::CompilerInstance &CI) override { + WrapperFrontendAction::BeginSourceFileAction(CI); + CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks()); + return true; + } -std::unique_ptr<ASTConsumer> -WrappingIndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { - auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); - if (!OtherConsumer) { - IndexActionFailed = true; - return nullptr; + void EndSourceFileAction() override { + // Invoke wrapped action's method. + WrapperFrontendAction::EndSourceFileAction(); + if (!IndexActionFailed) + finish(); } +}; - std::vector<std::unique_ptr<ASTConsumer>> Consumers; - Consumers.push_back(std::move(OtherConsumer)); - Consumers.push_back(createIndexASTConsumer(CI)); - return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); -} +} // anonymous namespace std::unique_ptr<FrontendAction> index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer, @@ -161,7 +204,6 @@ index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer, return llvm::make_unique<IndexAction>(std::move(DataConsumer), Opts); } - static bool topLevelDeclVisitor(void *context, const Decl *D) { IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context); return IndexCtx.indexTopLevelDecl(D); @@ -193,6 +235,12 @@ void index::indexTopLevelDecls(ASTContext &Ctx, ArrayRef<const Decl *> Decls, DataConsumer.finish(); } +std::unique_ptr<PPCallbacks> +index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) { + return llvm::make_unique<IndexPPCallbacks>( + std::make_shared<IndexingContext>(Opts, Consumer)); +} + void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexDataConsumer &DataConsumer, IndexingOptions Opts) { diff --git a/clang/lib/Index/IndexingContext.cpp b/clang/lib/Index/IndexingContext.cpp index 97b3e10450d..71b92bc6bf2 100644 --- a/clang/lib/Index/IndexingContext.cpp +++ b/clang/lib/Index/IndexingContext.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "IndexingContext.h" +#include "clang/Basic/SourceLocation.h" #include "clang/Index/IndexDataConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" @@ -290,6 +291,7 @@ static bool shouldReportOccurrenceForSystemDeclOnlyMode( case SymbolRole::Dynamic: case SymbolRole::AddressOf: case SymbolRole::Implicit: + case SymbolRole::Undefinition: case SymbolRole::RelationReceivedBy: case SymbolRole::RelationCalledBy: case SymbolRole::RelationContainedBy: @@ -406,3 +408,24 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc, IndexDataConsumer::ASTNodeInfo Node{OrigE, OrigD, Parent, ContainerDC}; return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, Loc, Node); } + +void IndexingContext::handleMacroDefined(const IdentifierInfo &Name, + SourceLocation Loc, + const MacroInfo &MI) { + SymbolRoleSet Roles = (unsigned)SymbolRole::Definition; + DataConsumer.handleMacroOccurence(&Name, &MI, Roles, Loc); +} + +void IndexingContext::handleMacroUndefined(const IdentifierInfo &Name, + SourceLocation Loc, + const MacroInfo &MI) { + SymbolRoleSet Roles = (unsigned)SymbolRole::Undefinition; + DataConsumer.handleMacroOccurence(&Name, &MI, Roles, Loc); +} + +void IndexingContext::handleMacroReference(const IdentifierInfo &Name, + SourceLocation Loc, + const MacroInfo &MI) { + SymbolRoleSet Roles = (unsigned)SymbolRole::Reference; + DataConsumer.handleMacroOccurence(&Name, &MI, Roles, Loc); +} diff --git a/clang/lib/Index/IndexingContext.h b/clang/lib/Index/IndexingContext.h index 566651c83a7..f05eaae6965 100644 --- a/clang/lib/Index/IndexingContext.h +++ b/clang/lib/Index/IndexingContext.h @@ -10,9 +10,11 @@ #ifndef LLVM_CLANG_LIB_INDEX_INDEXINGCONTEXT_H #define LLVM_CLANG_LIB_INDEX_INDEXINGCONTEXT_H +#include "clang/Basic/IdentifierTable.h" #include "clang/Basic/LLVM.h" #include "clang/Index/IndexSymbol.h" #include "clang/Index/IndexingAction.h" +#include "clang/Lex/MacroInfo.h" #include "llvm/ADT/ArrayRef.h" namespace clang { @@ -80,6 +82,15 @@ public: const Expr *RefE = nullptr, const Decl *RefD = nullptr); + void handleMacroDefined(const IdentifierInfo &Name, SourceLocation Loc, + const MacroInfo &MI); + + void handleMacroUndefined(const IdentifierInfo &Name, SourceLocation Loc, + const MacroInfo &MI); + + void handleMacroReference(const IdentifierInfo &Name, SourceLocation Loc, + const MacroInfo &MD); + bool importedModule(const ImportDecl *ImportD); bool indexDecl(const Decl *D); |