diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-08-01 10:45:08 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-08-01 12:39:03 +0000 |
commit | f8db5041bcff568499b3740c5272960e7f20e023 (patch) | |
tree | 6de24ad043542abe6abb40e6df997f5096153ff0 /tools | |
parent | c959fde3859389bf20235ce515f4cff548034f8a (diff) |
Index whild build. Part 1
https://reviews.llvm.org/D39050 adapted for 6.0
Change-Id: I2cb9c7d94c95fde7c3eb3042a6de0e55ab186974
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/c-index-test/core_main.cpp | 251 | ||||
-rw-r--r-- | tools/diagtool/DiagnosticNames.cpp | 1 | ||||
-rw-r--r-- | tools/libclang/CXIndexDataConsumer.cpp | 4 | ||||
-rw-r--r-- | tools/libclang/CXIndexDataConsumer.h | 4 |
4 files changed, 218 insertions, 42 deletions
diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp index c255f54ba6..805f21cc77 100644 --- a/tools/c-index-test/core_main.cpp +++ b/tools/c-index-test/core_main.cpp @@ -12,15 +12,17 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/FrontendAction.h" -#include "clang/Index/IndexingAction.h" +#include "clang/Index/CodegenNameGenerator.h" #include "clang/Index/IndexDataConsumer.h" #include "clang/Index/USRGeneration.h" -#include "clang/Index/CodegenNameGenerator.h" +#include "clang/Index/UnitIndexDataConsumer.h" +#include "clang/Index/UnitIndexingAction.h" #include "clang/Serialization/ASTReader.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/PrettyStackTrace.h" using namespace clang; using namespace clang::index; @@ -33,18 +35,20 @@ namespace { enum class ActionType { None, PrintSourceSymbols, + PrintSourceUnit, }; namespace options { static cl::OptionCategory IndexTestCoreCategory("index-test-core options"); -static cl::opt<ActionType> -Action(cl::desc("Action:"), cl::init(ActionType::None), - cl::values( - clEnumValN(ActionType::PrintSourceSymbols, - "print-source-symbols", "Print symbols from source")), - cl::cat(IndexTestCoreCategory)); +static cl::opt<ActionType> Action( + cl::desc("Action:"), cl::init(ActionType::None), + cl::values(clEnumValN(ActionType::PrintSourceSymbols, + "print-source-symbols", "Print symbols from source"), + clEnumValN(ActionType::PrintSourceUnit, "print-source-unit", + "Print unit info from source")), + cl::cat(IndexTestCoreCategory)); static cl::extrahelp MoreHelp( "\nAdd \"-- <compiler arguments>\" at the end to setup the compiler " @@ -72,6 +76,42 @@ static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS); static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx, raw_ostream &OS); +static void printDeclOccurrence(const Decl *D, SymbolRoleSet Roles, + ArrayRef<SymbolRelation> Relations, FileID FID, + unsigned Offset, bool IsInSystemFile, + CodegenNameGenerator &CGNameGen, + raw_ostream &OS) { + ASTContext &Ctx = D->getASTContext(); + SourceManager &SM = Ctx.getSourceManager(); + + unsigned Line = SM.getLineNumber(FID, Offset); + unsigned Col = SM.getColumnNumber(FID, Offset); + OS << Line << ':' << Col << " | "; + + printSymbolInfo(getSymbolInfo(D), OS); + OS << " | "; + + printSymbolNameAndUSR(D, Ctx, OS); + OS << " | "; + + if (CGNameGen.writeName(D, OS)) + OS << "<no-cgname>"; + OS << " | "; + + printSymbolRoles(Roles, OS); + OS << " | "; + + OS << "rel: " << Relations.size() << '\n'; + + for (auto &SymRel : Relations) { + OS << '\t'; + printSymbolRoles(SymRel.Roles, OS); + OS << " | "; + printSymbolNameAndUSR(SymRel.RelatedSymbol, Ctx, OS); + OS << '\n'; + } +} + namespace { class PrintIndexDataConsumer : public IndexDataConsumer { @@ -89,42 +129,17 @@ public: bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations, FileID FID, unsigned Offset, - ASTNodeInfo ASTNode) override { + bool IsInSystemFile, ASTNodeInfo ASTNode) override { ASTContext &Ctx = D->getASTContext(); SourceManager &SM = Ctx.getSourceManager(); - unsigned Line = SM.getLineNumber(FID, Offset); - unsigned Col = SM.getColumnNumber(FID, Offset); - OS << Line << ':' << Col << " | "; - - printSymbolInfo(getSymbolInfo(D), OS); - OS << " | "; - - printSymbolNameAndUSR(D, Ctx, OS); - OS << " | "; - - if (CGNameGen->writeName(D, OS)) - OS << "<no-cgname>"; - OS << " | "; - - printSymbolRoles(Roles, OS); - OS << " | "; - - OS << "rel: " << Relations.size() << '\n'; - - for (auto &SymRel : Relations) { - OS << '\t'; - printSymbolRoles(SymRel.Roles, OS); - OS << " | "; - printSymbolNameAndUSR(SymRel.RelatedSymbol, Ctx, OS); - OS << '\n'; - } - + printDeclOccurrence(D, Roles, Relations, FID, Offset, IsInSystemFile, + *CGNameGen, OS); return true; } bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles, - FileID FID, unsigned Offset) override { + FileID FID, unsigned Offset, bool IsInSystemFile) override { ASTContext &Ctx = ImportD->getASTContext(); SourceManager &SM = Ctx.getSourceManager(); @@ -235,6 +250,156 @@ static bool printSourceSymbolsFromModule(StringRef modulePath, return false; } +class PrintUnitDataConsumer : public UnitIndexDataConsumer { + struct Dependency { + enum Kind { File, Module }; + + Kind Kind; + std::string Name; + bool IsSystem; + + void print(raw_ostream &OS) const { + switch (Kind) { + case File: + OS << "File"; + break; + case Module: + OS << "Module"; + break; + } + OS << " | " << (IsSystem ? "system" : "user") << " | " << Name << '\n'; + } + }; + + struct Include { + std::string Source; + std::string Target; + unsigned Line; + + void print(raw_ostream &OS) const { + OS << Source << ':' << Line << " -> " << Target << '\n'; + } + }; + + struct FileOccurrences { + SourceManager &SM; + FileID FID; + bool IsSystem; + std::vector<DeclOccurrence> Occurrences; + + void print(raw_ostream &OS, CodegenNameGenerator &CGNameGen) const { + const FileEntry *FE = SM.getFileEntryForID(FID); + OS << '\n' << FE->getName() << "\n----------\n"; + for (const DeclOccurrence &Occur : Occurrences) { + printDeclOccurrence(Occur.Dcl, Occur.Roles, Occur.Relations, FID, + Occur.Offset, IsSystem, CGNameGen, OS); + } + } + }; + + raw_ostream &OS; + UnitDetails UnitInfo; + const CompilerInstance &CI; + CodegenNameGenerator CGNameGen; + bool IndexModDependencies = true; + std::vector<Dependency> Dependencies; + std::vector<Include> Includes; + std::vector<FileOccurrences> FileOccurInfos; + +public: + PrintUnitDataConsumer(raw_ostream &OS, UnitDetails UnitInfo, + bool IndexModDeps) + : OS(OS), UnitInfo(UnitInfo), CI(UnitInfo.CI), + CGNameGen(CI.getASTContext()), IndexModDependencies(IndexModDeps) {} + + void handleFileDependency(const FileEntry *FE, bool IsSystem) override { + Dependencies.push_back({Dependency::File, FE->getName(), IsSystem}); + } + + void handleModuleImport(const serialization::ModuleFile &Mod, + bool IsSystem) override { + Dependencies.push_back({Dependency::Module, Mod.FileName, IsSystem}); + } + + void handleInclude(const FileEntry *Source, unsigned Line, + const FileEntry *Target) override { + Includes.push_back({Source->getName(), Target->getName(), Line}); + } + + bool + shouldIndexModuleDependency(const serialization::ModuleFile &Mod) override { + return IndexModDependencies; + } + + bool handleFileOccurrences(FileID FID, + ArrayRef<DeclOccurrence> OccurrencesSortedByOffset, + bool IsSystem) override { + SourceManager &SM = CI.getASTContext().getSourceManager(); + FileOccurInfos.push_back({SM, FID, IsSystem, OccurrencesSortedByOffset}); + return false; + } + + void finish() override { + OS << sys::path::filename(UnitInfo.OutputFile) << '\n' + << "----------\n" + << "is-system: " << UnitInfo.IsSystemUnit << '\n' + << "is-module: " << UnitInfo.IsModuleUnit << '\n' + << "module-name: " << UnitInfo.ModuleName << '\n' + << "has-main: " << !!UnitInfo.RootFile << '\n' + << "main-path: " + << (UnitInfo.RootFile ? UnitInfo.RootFile->getName() : "") << '\n' + << "out-file: " << UnitInfo.OutputFile << '\n' + << "target: " << UnitInfo.CI.getTargetOpts().Triple << '\n' + << "is-debug: " << UnitInfo.IsDebugCompilation << '\n'; + + OS << "\nDEPEND START\n"; + for (const Dependency &Dep : Dependencies) { + Dep.print(OS); + } + OS << "DEPEND END (" << Dependencies.size() << ")\n"; + OS << "\nINCLUDE START\n"; + for (const Include &Inc : Includes) { + Inc.print(OS); + } + OS << "INCLUDE END (" << Includes.size() << ")\n"; + for (const FileOccurrences &FileInfo : FileOccurInfos) { + FileInfo.print(OS, CGNameGen); + } + OS << '\n'; + } +}; + +static bool printSourceUnit(ArrayRef<const char *> Args, bool IndexLocals, + bool IndexModDeps) { + SmallVector<const char *, 4> ArgsWithProgName; + ArgsWithProgName.push_back("clang"); + ArgsWithProgName.append(Args.begin(), Args.end()); + IntrusiveRefCntPtr<DiagnosticsEngine> Diags( + CompilerInstance::createDiagnostics(new DiagnosticOptions)); + auto CInvok = createInvocationFromCommandLine(ArgsWithProgName, Diags); + if (!CInvok) + return true; + + raw_ostream &OS = outs(); + UnitIndexingOptions IndexOpts; + IndexOpts.IndexFunctionLocals = IndexLocals; + + auto ConsumerFactory = [&OS, IndexModDeps](UnitDetails UnitInfo) { + return llvm::make_unique<PrintUnitDataConsumer>(OS, std::move(UnitInfo), + IndexModDeps); + }; + + std::unique_ptr<FrontendAction> IndexAction; + IndexAction = createUnitIndexingAction(ConsumerFactory, IndexOpts, + /*WrappedAction=*/nullptr); + + auto PCHContainerOps = std::make_shared<PCHContainerOperations>(); + std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction( + std::move(CInvok), PCHContainerOps, Diags, IndexAction.get())); + + return !Unit; +} + //===----------------------------------------------------------------------===// // Helper Utils //===----------------------------------------------------------------------===// @@ -305,5 +470,15 @@ int indextest_core_main(int argc, const char **argv) { return printSourceSymbols(CompArgs, options::DumpModuleImports, options::IncludeLocals); } + if (options::Action == ActionType::PrintSourceUnit) { + if (CompArgs.empty()) { + errs() + << "error: missing compiler args; pass '-- <compiler arguments>'\n"; + return 1; + } + return printSourceUnit(CompArgs, options::IncludeLocals, + /*IndexModDepedencies=*/true); + } + return 0; } diff --git a/tools/diagtool/DiagnosticNames.cpp b/tools/diagtool/DiagnosticNames.cpp index b0ca7f9806..0a3630d217 100644 --- a/tools/diagtool/DiagnosticNames.cpp +++ b/tools/diagtool/DiagnosticNames.cpp @@ -43,6 +43,7 @@ static const DiagnosticRecord BuiltinDiagnosticsByID[] = { #include "clang/Basic/DiagnosticSemaKinds.inc" #include "clang/Basic/DiagnosticAnalysisKinds.inc" #include "clang/Basic/DiagnosticRefactoringKinds.inc" +#include "clang/Basic/DiagnosticIndexKinds.inc" #undef DIAG }; diff --git a/tools/libclang/CXIndexDataConsumer.cpp b/tools/libclang/CXIndexDataConsumer.cpp index 89ac23be73..8df616573d 100644 --- a/tools/libclang/CXIndexDataConsumer.cpp +++ b/tools/libclang/CXIndexDataConsumer.cpp @@ -154,7 +154,7 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations, FileID FID, unsigned Offset, - ASTNodeInfo ASTNode) { + bool IsInSystemFile, ASTNodeInfo ASTNode) { SourceLocation Loc = getASTContext().getSourceManager() .getLocForStartOfFile(FID).getLocWithOffset(Offset); @@ -221,7 +221,7 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D, bool CXIndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles, FileID FID, - unsigned Offset) { + unsigned Offset, bool IsInSystemFile) { IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD); return !shouldAbort(); } diff --git a/tools/libclang/CXIndexDataConsumer.h b/tools/libclang/CXIndexDataConsumer.h index a54baadd07..b1cf0b1ea3 100644 --- a/tools/libclang/CXIndexDataConsumer.h +++ b/tools/libclang/CXIndexDataConsumer.h @@ -464,11 +464,11 @@ private: bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset, - ASTNodeInfo ASTNode) override; + bool IsInSystemFile, ASTNodeInfo ASTNode) override; bool handleModuleOccurence(const ImportDecl *ImportD, index::SymbolRoleSet Roles, - FileID FID, unsigned Offset) override; + FileID FID, unsigned Offset, bool IsInSystemFile) override; void finish() override; |