From 3cee3cb77545bcdf14d512d9017f04f7f684cbf7 Mon Sep 17 00:00:00 2001 From: Walter Erquinigo Date: Tue, 21 Nov 2017 08:47:28 -0800 Subject: [PATCH] Support an optional compilation database dir at initialization --- src/command_line.cc | 9 +++------ src/config.h | 6 +++--- src/project.cc | 18 ++++++++++++------ src/project.h | 13 ++++++++----- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index f006e1cd..02f2db29 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -1462,10 +1462,6 @@ bool QueryDbMainLoop(Config* config, LOG_S(FATAL) << "Exiting; no cache directory"; exit(1); } - // Make sure compile commands directory is valid. - if (config->compileCommandsDirectory.empty()) { - config->compileCommandsDirectory = project_path; - } config->cacheDirectory = NormalizePath(config->cacheDirectory); EnsureEndsInSlash(config->cacheDirectory); @@ -1571,8 +1567,9 @@ bool QueryDbMainLoop(Config* config, Timer time; // Open up / load the project. - project->Load(config->extraClangArguments, config->compileCommandsDirectory, - config->resourceDirectory); + project->Load(config->extraClangArguments, + config->compilationDatabaseDirectory, + project_path, config->resourceDirectory); time.ResetAndPrint("[perf] Loaded compilation entries (" + std::to_string(project->entries.size()) + " files)"); diff --git a/src/config.h b/src/config.h index edaafbd2..f72c7153 100644 --- a/src/config.h +++ b/src/config.h @@ -8,7 +8,7 @@ struct Config { // Root directory of the project. **Not serialized** std::string projectRoot; // Location of compile_commands.json. - std::string compileCommandsDirectory; + std::string compilationDatabaseDirectory; // Cache directory for indexed files. std::string cacheDirectory; // Value to use for clang -resource-dir if not present in @@ -72,7 +72,7 @@ struct Config { bool enableSnippetInsertion = true; }; MAKE_REFLECT_STRUCT(Config, - compileCommandsDirectory, + compilationDatabaseDirectory, cacheDirectory, resourceDirectory, @@ -101,4 +101,4 @@ MAKE_REFLECT_STRUCT(Config, codeLensOnLocalVariables, clientVersion, - enableSnippetInsertion); \ No newline at end of file + enableSnippetInsertion); diff --git a/src/project.cc b/src/project.cc index fc15be8c..0b7a23ae 100644 --- a/src/project.cc +++ b/src/project.cc @@ -229,15 +229,19 @@ std::vector LoadFromDirectoryListing(ProjectConfig* config) { } std::vector LoadCompilationEntriesFromDirectory( - ProjectConfig* config) { + ProjectConfig* config, + const std::string& compilationDatabaseDirectory) { // Try to load compile_commands.json, but fallback to a project listing. + const auto& compilationDbDir = compilationDatabaseDirectory.empty() + ? config->project_dir + : compilationDatabaseDirectory; LOG_S(INFO) << "Trying to load compile_commands.json"; CXCompilationDatabase_Error cx_db_load_error; CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory( - config->project_dir.c_str(), &cx_db_load_error); + compilationDbDir.c_str(), &cx_db_load_error); if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) { LOG_S(INFO) << "Unable to load compile_commands.json located at \"" - << config->project_dir + << compilationDbDir << "\"; using directory listing instead."; return LoadFromDirectoryListing(config); } @@ -339,14 +343,16 @@ int ComputeGuessScore(const std::string& a, const std::string& b) { } // namespace void Project::Load(const std::vector& extra_flags, - const std::string& directory, + const std::string& compilationDatabaseDirectory, + const std::string& rootDirectory, const std::string& resource_directory) { // Load data. ProjectConfig config; config.extra_flags = extra_flags; - config.project_dir = directory; + config.project_dir = rootDirectory; config.resource_dir = resource_directory; - entries = LoadCompilationEntriesFromDirectory(&config); + entries = LoadCompilationEntriesFromDirectory(&config, + compilationDatabaseDirectory); // Cleanup / postprocess include directories. quote_include_directories.assign(config.quote_dirs.begin(), diff --git a/src/project.h b/src/project.h index b74df809..c243502e 100644 --- a/src/project.h +++ b/src/project.h @@ -31,12 +31,15 @@ struct Project { // Loads a project for the given |directory|. // - // If |directory| contains a compile_commands.json file, that will be used to - // discover all files and args. Otherwise, a recursive directory listing of - // all *.cpp, *.cc, *.h, and *.hpp files will be used. clang arguments can be - // specified in a clang_args file located inside of |directory|. + // If |compilationDatabaseDirectory| is not empty, the compile_commands.json + // file in it will be used to discover all files and args. If it's empty and + // |directory| contains a compile_commands.json file, that one will be used + // instead. Otherwise, a recursive directory listing of all *.cpp, *.cc, *.h, + // and *.hpp files will be used. clang arguments can be specified in a + // clang_args file located inside of |directory|. void Load(const std::vector& extra_flags, - const std::string& directory, + const std::string& compilationDatabaseDirectory, + const std::string& rootDirectory, const std::string& resource_directory); // Lookup the CompilationEntry for |filename|. If no entry was found this