diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2016-02-14 06:39:11 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2016-02-14 06:39:11 +0000 |
commit | 4d8332ae58eff1ea752517c3f47d7801b8d9c919 (patch) | |
tree | 18d8cb29eeb746f79dce4a30b657e39146c6b169 /tools/c-index-test | |
parent | 43eeb3bb6b36465b7e9aa0942242494103cde580 (diff) |
[index] Enhance c-index-test tool and have it link and test the clangIndex library directly.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260842 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/c-index-test')
-rw-r--r-- | tools/c-index-test/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 20 | ||||
-rw-r--r-- | tools/c-index-test/core_main.cpp | 197 |
3 files changed, 220 insertions, 4 deletions
diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt index c78a42ffe8..1228a65486 100644 --- a/tools/c-index-test/CMakeLists.txt +++ b/tools/c-index-test/CMakeLists.txt @@ -1,5 +1,10 @@ +set(LLVM_LINK_COMPONENTS + support +) + add_clang_executable(c-index-test c-index-test.c + core_main.cpp ) if(NOT MSVC) @@ -12,10 +17,12 @@ endif() if (LLVM_BUILD_STATIC) target_link_libraries(c-index-test libclang_static + clangIndex ) else() target_link_libraries(c-index-test libclang + clangIndex ) endif() diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index b2f9120baf..a67afb7cfa 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -23,6 +23,8 @@ # include <unistd.h> #endif +extern int indextest_core_main(int argc, const char **argv); + /******************************************************************************/ /* Utility functions. */ /******************************************************************************/ @@ -4410,13 +4412,15 @@ int cindextest_main(int argc, const char **argv) { * size). */ typedef struct thread_info { + int (*main_func)(int argc, const char **argv); int argc; const char **argv; int result; } thread_info; void thread_runner(void *client_data_v) { thread_info *client_data = client_data_v; - client_data->result = cindextest_main(client_data->argc, client_data->argv); + client_data->result = client_data->main_func(client_data->argc, + client_data->argv); } static void flush_atexit(void) { @@ -4435,11 +4439,19 @@ int main(int argc, const char **argv) { LIBXML_TEST_VERSION #endif - if (getenv("CINDEXTEST_NOTHREADS")) - return cindextest_main(argc, argv); - + client_data.main_func = cindextest_main; client_data.argc = argc; client_data.argv = argv; + + if (argc > 1 && strcmp(argv[1], "core") == 0) { + client_data.main_func = indextest_core_main; + --client_data.argc; + ++client_data.argv; + } + + if (getenv("CINDEXTEST_NOTHREADS")) + return client_data.main_func(client_data.argc, client_data.argv); + clang_executeOnThread(thread_runner, &client_data, 0); return client_data.result; } diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp new file mode 100644 index 0000000000..d2faf2d069 --- /dev/null +++ b/tools/c-index-test/core_main.cpp @@ -0,0 +1,197 @@ +//===-- core_main.cpp - Core Index Tool testbed ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "clang/Frontend/FrontendAction.h" +#include "clang/Index/IndexingAction.h" +#include "clang/Index/IndexDataConsumer.h" +#include "clang/Index/USRGeneration.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/PrettyStackTrace.h" + +using namespace clang; +using namespace clang::index; +using namespace llvm; + +extern "C" int indextest_core_main(int argc, const char **argv); + +namespace { + +enum class ActionType { + None, + PrintSourceSymbols, +}; + +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"), + clEnumValEnd), + cl::cat(IndexTestCoreCategory)); + +static cl::extrahelp MoreHelp( + "\nAdd \"-- <compiler arguments>\" at the end to setup the compiler " + "invocation\n" +); + +} +} // anonymous namespace + +static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS); +static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx, + raw_ostream &OS); + +namespace { + +class PrintIndexDataConsumer : public IndexDataConsumer { + raw_ostream &OS; + +public: + PrintIndexDataConsumer(raw_ostream &OS) : OS(OS) { + } + + bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, + ArrayRef<SymbolRelation> Relations, + FileID FID, unsigned Offset, + 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 << " | "; + + 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'; + } + + return true; + } +}; + +} // anonymous namespace + +//===----------------------------------------------------------------------===// +// Print Source Symbols +//===----------------------------------------------------------------------===// + +static bool printSourceSymbols(ArrayRef<const char *> Args) { + SmallVector<const char *, 4> ArgsWithProgName; + ArgsWithProgName.push_back("clang"); + ArgsWithProgName.append(Args.begin(), Args.end()); + IntrusiveRefCntPtr<DiagnosticsEngine> + Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); + IntrusiveRefCntPtr<CompilerInvocation> + CInvok(createInvocationFromCommandLine(ArgsWithProgName, Diags)); + if (!CInvok) + return true; + + auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs()); + IndexingOptions IndexOpts; + std::unique_ptr<FrontendAction> IndexAction; + IndexAction = createIndexingAction(DataConsumer, IndexOpts); + + auto PCHContainerOps = std::make_shared<PCHContainerOperations>(); + ASTUnit *Unit = + ASTUnit::LoadFromCompilerInvocationAction(CInvok.get(), PCHContainerOps, + Diags, IndexAction.get()); + + if (!Unit) + return true; + + return false; +} + +//===----------------------------------------------------------------------===// +// Helper Utils +//===----------------------------------------------------------------------===// + +static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) { + OS << getSymbolKindString(SymInfo.Kind); + if (SymInfo.TemplateKind != SymbolCXXTemplateKind::NonTemplate) { + OS << '-' << getTemplateKindStr(SymInfo.TemplateKind); + } + OS << '/' << getSymbolLanguageString(SymInfo.Lang); +} + +static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx, + raw_ostream &OS) { + if (auto *ND = dyn_cast<NamedDecl>(D)) { + PrintingPolicy PrintPolicy(Ctx.getLangOpts()); + ND->getDeclName().print(OS, PrintPolicy); + } else { + OS << "<no-name>"; + } + OS << " | "; + + SmallString<256> USRBuf; + if (generateUSRForDecl(D, USRBuf)) { + OS << "<no-usr>"; + } else { + OS << USRBuf; + } +} + +//===----------------------------------------------------------------------===// +// Command line processing. +//===----------------------------------------------------------------------===// + +int indextest_core_main(int argc, const char **argv) { + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + std::vector<const char *> CompArgs; + const char *const *DoubleDash = std::find(argv, argv + argc, StringRef("--")); + if (DoubleDash != argv + argc) { + CompArgs = std::vector<const char *>(DoubleDash + 1, argv + argc); + argc = DoubleDash - argv; + } + + cl::HideUnrelatedOptions(options::IndexTestCoreCategory); + cl::ParseCommandLineOptions(argc, argv, "index-test-core"); + + if (options::Action == ActionType::None) { + errs() << "error: action required; pass '-help' for options\n"; + return 1; + } + + if (options::Action == ActionType::PrintSourceSymbols) { + if (CompArgs.empty()) { + errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n"; + return 1; + } + return printSourceSymbols(CompArgs); + } + + return 0; +} |