Support an optional compilation database dir at initialization

This commit is contained in:
Walter Erquinigo 2017-11-21 08:47:28 -08:00 committed by Jacob Dufault
parent 6f6cd4ea7f
commit 3cee3cb775
4 changed files with 26 additions and 20 deletions

View File

@ -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)");

View File

@ -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,

View File

@ -229,15 +229,19 @@ std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig* config) {
}
std::vector<Project::Entry> 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<std::string>& 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(),

View File

@ -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<std::string>& 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