[c-index-test] Provide capability for 'c-index-test core' to dump symbol information from a PCH/module file.

llvm-svn: 293416
This commit is contained in:
Argyrios Kyrtzidis 2017-01-29 04:50:35 +00:00
parent 688b69adf8
commit 5fab854082
3 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,13 @@
// RUN: c-index-test core -print-source-symbols -- %s | FileCheck %s
// RUN: %clang_cc1 -emit-pch %s -o %t.pch
// RUN: c-index-test core -print-source-symbols -module-file %t.pch | FileCheck %s
// CHECK: [[@LINE+1]]:6 | function/C | test1 | [[TEST1_USR:.*]] | [[TEST1_CG:.*]] | Decl | rel: 0
void test1();
// CHECK: [[@LINE+1]]:20 | function/C | test2 | [[TEST2_USR:.*]] | {{.*}} | Def | rel: 0
static inline void test2() {
// CHECK: [[@LINE+2]]:3 | function/C | test1 | [[TEST1_USR]] | [[TEST1_CG]] | Ref,Call,RelCall,RelCont | rel: 1
// CHECK-NEXT: RelCall,RelCont | test2 | [[TEST2_USR]]
test1();
}

View File

@ -24,6 +24,7 @@ else()
libclang
clangAST
clangBasic
clangCodeGen
clangFrontend
clangIndex
)

View File

@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
@ -49,6 +50,13 @@ static cl::extrahelp MoreHelp(
"invocation\n"
);
static cl::opt<std::string>
ModuleFilePath("module-file",
cl::desc("Path to module file to print symbols from"));
static cl::opt<std::string>
ModuleFormat("fmodule-format", cl::init("raw"),
cl::desc("Container format for clang modules and PCH, 'raw' or 'obj'"));
}
} // anonymous namespace
@ -160,6 +168,39 @@ static bool printSourceSymbols(ArrayRef<const char *> Args) {
return false;
}
static bool printSourceSymbolsFromModule(StringRef modulePath,
StringRef format) {
FileSystemOptions FileSystemOpts;
auto pchContOps = std::make_shared<PCHContainerOperations>();
// Register the support for object-file-wrapped Clang modules.
pchContOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
auto pchRdr = pchContOps->getReaderOrNull(format);
if (!pchRdr) {
errs() << "unknown module format: " << format << '\n';
return true;
}
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions());
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
modulePath, *pchRdr, Diags,
FileSystemOpts, /*UseDebugInfo=*/false,
/*OnlyLocalDecls=*/true, None,
/*CaptureDiagnostics=*/false,
/*AllowPCHWithCompilerErrors=*/true,
/*UserFilesAreVolatile=*/false);
if (!AU) {
errs() << "failed to create TU for: " << modulePath << '\n';
return true;
}
auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs());
IndexingOptions IndexOpts;
indexASTUnit(*AU, DataConsumer, IndexOpts);
return false;
}
//===----------------------------------------------------------------------===//
// Helper Utils
//===----------------------------------------------------------------------===//
@ -219,6 +260,10 @@ int indextest_core_main(int argc, const char **argv) {
}
if (options::Action == ActionType::PrintSourceSymbols) {
if (!options::ModuleFilePath.empty()) {
return printSourceSymbolsFromModule(options::ModuleFilePath,
options::ModuleFormat);
}
if (CompArgs.empty()) {
errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n";
return 1;