From: Argyrios Kyrtzidis Date: Mon, 30 Jan 2017 06:05:58 +0000 (+0000) Subject: [c-index-test] Provide capability to index module file imports and dump their input... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5625df6660329b7b3e1eb8cd4ce96622b02efdf1;p=clang [c-index-test] Provide capability to index module file imports and dump their input files. This ensures the capability to index a module file using an existing ASTReader from a compiler instance or ASTUnit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@293461 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index b1cdb46d50..03961f1a3c 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -519,6 +519,8 @@ public: const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; } + IntrusiveRefCntPtr getASTReader() const; + StringRef getOriginalSourceFileName() { return OriginalSourceFile; } diff --git a/include/clang/Index/IndexingAction.h b/include/clang/Index/IndexingAction.h index e2e63dc635..8eed33c612 100644 --- a/include/clang/Index/IndexingAction.h +++ b/include/clang/Index/IndexingAction.h @@ -14,9 +14,14 @@ #include namespace clang { + class ASTReader; class ASTUnit; class FrontendAction; +namespace serialization { + class ModuleFile; +} + namespace index { class IndexDataConsumer; @@ -42,6 +47,11 @@ void indexASTUnit(ASTUnit &Unit, std::shared_ptr DataConsumer, IndexingOptions Opts); +void indexModuleFile(serialization::ModuleFile &Mod, + ASTReader &Reader, + std::shared_ptr DataConsumer, + IndexingOptions Opts); + } // namespace index } // namespace clang diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index 5a57911c1b..d638ecb2e5 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -1270,6 +1270,7 @@ private: llvm::iterator_range getModulePreprocessedEntities(ModuleFile &Mod) const; +public: class ModuleDeclIterator : public llvm::iterator_adaptor_base< ModuleDeclIterator, const serialization::LocalDeclID *, @@ -1300,6 +1301,7 @@ private: llvm::iterator_range getModuleFileLevelDecls(ModuleFile &Mod); +private: void PassInterestingDeclsToConsumer(); void PassInterestingDeclToConsumer(Decl *D); @@ -2192,6 +2194,12 @@ public: /// \brief Loads comments ranges. void ReadComments() override; + /// Visit all the input files of the given module file. + void visitInputFiles(serialization::ModuleFile &MF, + bool IncludeSystem, bool Complain, + llvm::function_ref Visitor); + bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; } }; diff --git a/include/clang/Serialization/Module.h b/include/clang/Serialization/Module.h index 3b46446dd6..15cd8c22b3 100644 --- a/include/clang/Serialization/Module.h +++ b/include/clang/Serialization/Module.h @@ -211,6 +211,10 @@ public: /// \brief The input files that have been loaded from this AST file. std::vector InputFilesLoaded; + // All user input files reside at the index range [0, NumUserInputFiles), and + // system input files reside at [NumUserInputFiles, InputFilesLoaded.size()). + unsigned NumUserInputFiles = 0; + /// \brief If non-zero, specifies the time when we last validated input /// files. Zero means we never validated them. /// diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 8064da084b..f3d033c3e7 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -619,6 +619,10 @@ void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, StoredDiags.emplace_back(Level, Info); } +IntrusiveRefCntPtr ASTUnit::getASTReader() const { + return Reader; +} + ASTMutationListener *ASTUnit::getASTMutationListener() { if (WriterData) return &WriterData->Writer; diff --git a/lib/Index/IndexingAction.cpp b/lib/Index/IndexingAction.cpp index d744293152..cac24d4b9c 100644 --- a/lib/Index/IndexingAction.cpp +++ b/lib/Index/IndexingAction.cpp @@ -13,6 +13,7 @@ #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/MultiplexConsumer.h" #include "clang/Lex/Preprocessor.h" +#include "clang/Serialization/ASTReader.h" using namespace clang; using namespace clang::index; @@ -173,4 +174,20 @@ void index::indexASTUnit(ASTUnit &Unit, IndexCtx.setASTContext(Unit.getASTContext()); DataConsumer->initialize(Unit.getASTContext()); indexTranslationUnit(Unit, IndexCtx); + DataConsumer->finish(); +} + +void index::indexModuleFile(serialization::ModuleFile &Mod, + ASTReader &Reader, + std::shared_ptr DataConsumer, + IndexingOptions Opts) { + ASTContext &Ctx = Reader.getContext(); + IndexingContext IndexCtx(Opts, *DataConsumer); + IndexCtx.setASTContext(Ctx); + DataConsumer->initialize(Ctx); + + for (const Decl *D :Reader.getModuleFileLevelDecls(Mod)) { + IndexCtx.indexTopLevelDecl(D); + } + DataConsumer->finish(); } diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index e3122039c7..da8e5ff6b9 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -2519,6 +2519,7 @@ ASTReader::ReadControlBlock(ModuleFile &F, F.InputFileOffsets = (const llvm::support::unaligned_uint64_t *)Blob.data(); F.InputFilesLoaded.resize(NumInputs); + F.NumUserInputFiles = NumUserInputs; break; } } @@ -8569,6 +8570,21 @@ void ASTReader::ReadComments() { } } +void ASTReader::visitInputFiles(serialization::ModuleFile &MF, + bool IncludeSystem, bool Complain, + llvm::function_ref Visitor) { + unsigned NumUserInputs = MF.NumUserInputFiles; + unsigned NumInputs = MF.InputFilesLoaded.size(); + assert(NumUserInputs <= NumInputs); + unsigned N = IncludeSystem ? NumInputs : NumUserInputs; + for (unsigned I = 0; I < N; ++I) { + bool IsSystem = I >= NumUserInputs; + InputFile IF = getInputFile(MF, I+1, Complain); + Visitor(IF, IsSystem); + } +} + std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { // If we know the owning module, use it. if (Module *M = D->getImportedOwningModule()) diff --git a/test/Index/Core/index-with-module.m b/test/Index/Core/index-with-module.m index e50b247e8d..c83de63701 100644 --- a/test/Index/Core/index-with-module.m +++ b/test/Index/Core/index-with-module.m @@ -1,5 +1,5 @@ // RUN: rm -rf %t.mcp -// RUN: c-index-test core -print-source-symbols -- %s -I %S/Inputs/module -fmodules -fmodules-cache-path=%t.mcp | FileCheck %s +// RUN: c-index-test core -print-source-symbols -dump-imported-module-files -- %s -I %S/Inputs/module -fmodules -fmodules-cache-path=%t.mcp | FileCheck %s // CHECK: [[@LINE+1]]:9 | module/C | ModA | Decl | @import ModA; @@ -10,3 +10,9 @@ void foo() { // CHECK: [[@LINE+1]]:3 | function/C | ModA_func | c:@F@ModA_func | {{.*}} | Ref,Call,RelCall,RelCont | rel: 1 ModA_func(); } + +// CHECK: ==== Module ModA ==== +// CHECK: 2:6 | function/C | ModA_func | c:@F@ModA_func | {{.*}} | Decl | rel: 0 +// CHECK: ---- Module Inputs ---- +// CHECK: user | {{.*}}ModA.h +// CHECK: user | {{.*}}module.modulemap diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp index 33653e0b6d..f371870117 100644 --- a/tools/c-index-test/core_main.cpp +++ b/tools/c-index-test/core_main.cpp @@ -16,6 +16,7 @@ #include "clang/Index/IndexDataConsumer.h" #include "clang/Index/USRGeneration.h" #include "clang/Index/CodegenNameGenerator.h" +#include "clang/Serialization/ASTReader.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" @@ -50,6 +51,10 @@ static cl::extrahelp MoreHelp( "invocation\n" ); +static cl::opt +DumpModuleImports("dump-imported-module-files", + cl::desc("Print symbols and input files from imported modules")); + static cl::opt ModuleFilePath("module-file", cl::desc("Path to module file to print symbols from")); @@ -142,7 +147,19 @@ public: // Print Source Symbols //===----------------------------------------------------------------------===// -static bool printSourceSymbols(ArrayRef Args) { +static void dumpModuleFileInputs(serialization::ModuleFile &Mod, + ASTReader &Reader, + raw_ostream &OS) { + OS << "---- Module Inputs ----\n"; + Reader.visitInputFiles(Mod, /*IncludeSystem=*/true, /*Complain=*/false, + [&](const serialization::InputFile &IF, bool isSystem) { + OS << (isSystem ? "system" : "user") << " | "; + OS << IF.getFile()->getName() << '\n'; + }); +} + +static bool printSourceSymbols(ArrayRef Args, + bool dumpModuleImports) { SmallVector ArgsWithProgName; ArgsWithProgName.push_back("clang"); ArgsWithProgName.append(Args.begin(), Args.end()); @@ -152,7 +169,8 @@ static bool printSourceSymbols(ArrayRef Args) { if (!CInvok) return true; - auto DataConsumer = std::make_shared(outs()); + raw_ostream &OS = outs(); + auto DataConsumer = std::make_shared(OS); IndexingOptions IndexOpts; std::unique_ptr IndexAction; IndexAction = createIndexingAction(DataConsumer, IndexOpts, @@ -165,6 +183,17 @@ static bool printSourceSymbols(ArrayRef Args) { if (!Unit) return true; + if (dumpModuleImports) { + if (auto Reader = Unit->getASTReader()) { + Reader->getModuleManager().visit([&](serialization::ModuleFile &Mod) -> bool { + OS << "==== Module " << Mod.ModuleName << " ====\n"; + indexModuleFile(Mod, *Reader, DataConsumer, IndexOpts); + dumpModuleFileInputs(Mod, *Reader, OS); + return true; // skip module dependencies. + }); + } + } + return false; } @@ -268,7 +297,7 @@ int indextest_core_main(int argc, const char **argv) { errs() << "error: missing compiler args; pass '-- '\n"; return 1; } - return printSourceSymbols(CompArgs); + return printSourceSymbols(CompArgs, options::DumpModuleImports); } return 0;