From aa9cdad54fd63b7110ae2d8473d2725dd0cd39f9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 13 Jul 2018 09:36:02 -0700 Subject: [PATCH] Use clangTooling --- cmake/FindClang.cmake | 1 + src/indexer.cc | 2 +- src/project.cc | 56 +++++++++++++------------------------------ 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/cmake/FindClang.cmake b/cmake/FindClang.cmake index 12cbd503..fe44ece1 100644 --- a/cmake/FindClang.cmake +++ b/cmake/FindClang.cmake @@ -69,6 +69,7 @@ set(_Clang_REQUIRED_VARS Clang_LIBRARY Clang_INCLUDE_DIR Clang_EXECUTABLE LLVM_INCLUDE_DIR LLVM_BUILD_INCLUDE_DIR) _Clang_find_library(Clang_LIBRARY clang) +_Clang_find_add_library(clangTooling) _Clang_find_add_library(clangIndex) _Clang_find_add_library(clangFrontend) _Clang_find_add_library(clangParse) diff --git a/src/indexer.cc b/src/indexer.cc index d7178e4f..01a67494 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -810,7 +810,7 @@ public: } [[fallthrough]]; case Decl::Record: { - auto *RD = cast(D); + auto *RD = cast(OrigD); // spec has no Union, use Class type->def.kind = RD->getTagKind() == TTK_Struct ? lsSymbolKind::Struct : lsSymbolKind::Class; diff --git a/src/project.cc b/src/project.cc index dc729ad0..c74ef410 100644 --- a/src/project.cc +++ b/src/project.cc @@ -16,6 +16,7 @@ using namespace ccls; #include #include #include +#include #include #include #include @@ -25,7 +26,6 @@ using namespace clang; using namespace llvm; using namespace llvm::opt; -#include #include #if defined(__unix__) || defined(__APPLE__) @@ -142,14 +142,14 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( return result; const driver::ArgStringList& CCArgs = Jobs.begin()->getArguments(); - auto Invocation = std::make_unique(); - CompilerInvocation::CreateFromArgs(*Invocation, CCArgs.data(), + auto CI = std::make_unique(); + CompilerInvocation::CreateFromArgs(*CI, CCArgs.data(), CCArgs.data() + CCArgs.size(), Diags); - Invocation->getFrontendOpts().DisableFree = false; - Invocation->getCodeGenOpts().DisableFree = false; + CI->getFrontendOpts().DisableFree = false; + CI->getCodeGenOpts().DisableFree = false; - HeaderSearchOptions& HeaderOpts = Invocation->getHeaderSearchOpts(); - for (auto& E : HeaderOpts.UserEntries) { + HeaderSearchOptions &HeaderOpts = CI->getHeaderSearchOpts(); + for (auto &E : HeaderOpts.UserEntries) { std::string path = entry.ResolveIfRelative(E.Path); switch (E.Group) { default: @@ -178,7 +178,7 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( // if (HeaderOpts.ResourceDir.empty() && HeaderOpts.UseBuiltinIncludes) args.push_back("-resource-dir=" + g_config->clang.resourceDir); - // if (Invocation->getFileSystemOpts().WorkingDir.empty()) + // if (CI->getFileSystemOpts().WorkingDir.empty()) args.push_back("-working-directory=" + entry.directory); // There could be a clang version mismatch between what the project uses and @@ -300,9 +300,9 @@ std::vector LoadCompilationEntriesFromDirectory( #endif } - CXCompilationDatabase_Error cx_db_load_error; - CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory( - comp_db_dir.c_str(), &cx_db_load_error); + std::string err_msg; + std::unique_ptr CDB = + tooling::CompilationDatabase::loadFromDirectory(comp_db_dir, err_msg); if (!g_config->compilationDatabaseCommand.empty()) { #ifdef _WIN32 // TODO @@ -311,44 +311,22 @@ std::vector LoadCompilationEntriesFromDirectory( rmdir(comp_db_dir.c_str()); #endif } - if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) { - LOG_S(WARNING) << "unable to load " << Path.c_str(); + if (!CDB) { + LOG_S(WARNING) << "failed to load " << Path.c_str() << " " << err_msg; return {}; } LOG_S(INFO) << "loaded " << Path.c_str(); - CXCompileCommands cx_commands = - clang_CompilationDatabase_getAllCompileCommands(cx_db); - unsigned int num_commands = clang_CompileCommands_getSize(cx_commands); - std::vector result; - for (unsigned int i = 0; i < num_commands; i++) { - CXCompileCommand cx_command = - clang_CompileCommands_getCommand(cx_commands, i); - - std::string directory = - ToString(clang_CompileCommand_getDirectory(cx_command)); - std::string relative_filename = - ToString(clang_CompileCommand_getFilename(cx_command)); - - unsigned num_args = clang_CompileCommand_getNumArgs(cx_command); + for (tooling::CompileCommand &Cmd : CDB->getAllCompileCommands()) { CompileCommandsEntry entry; - entry.args.reserve(num_args); - for (unsigned j = 0; j < num_args; ++j) { - entry.args.push_back( - ToString(clang_CompileCommand_getArg(cx_command, j))); - } - - entry.directory = directory; - entry.file = entry.ResolveIfRelative(relative_filename); - + entry.directory = std::move(Cmd.Directory); + entry.file = entry.ResolveIfRelative(Cmd.Filename); + entry.args = std::move(Cmd.CommandLine); result.push_back( GetCompilationEntryFromCompileCommandEntry(project, entry)); } - - clang_CompileCommands_dispose(cx_commands); - clang_CompilationDatabase_dispose(cx_db); return result; }