Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Load global .cquery config at end of LoadCompilationEntriesfromdirectory
Browse files Browse the repository at this point in the history
Previously the global .cquery config was being loaded before the
project specific compile_commands.json file. The global .cquery config
is intended as a fallback when there are no other configs, so we don't
want this.

To make it explicit that the global .cquery is being loaded, add a new
parameter to LoadFromDirectorylisting (use_global_config) which, if
true, will load the global .cquery. This parameter defaults to false,
so no changes are required to existing calls.
  • Loading branch information
Nick Spain committed Jun 11, 2018
1 parent 541a313 commit 9c53e2b
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,31 +453,36 @@ std::vector<std::string> ReadCompilerArgumentsFromFile(
return args;
}

std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig* config) {
std::vector<Project::Entry> LoadFromDirectoryListing(
ProjectConfig* config,
bool use_global_config = false) {
std::vector<Project::Entry> result;
config->mode = ProjectMode::DotCquery;

std::unordered_map<std::string, std::vector<std::string>> folder_args;
std::vector<std::string> files;

GetFilesInFolder(config->project_dir, true /*recursive*/,
true /*add_folder_to_path*/,
[&folder_args, &files](const std::string& path) {
if (SourceFileLanguage(path) != LanguageId::Unknown) {
files.push_back(path);
} else if (GetBaseName(path) == ".cquery") {
LOG_S(INFO) << "Using .cquery arguments from " << path;
folder_args.emplace(GetDirName(path),
ReadCompilerArgumentsFromFile(path));
}
});

optional<std::string> maybe_cfg = GetGlobalConfigDirectory();
if (folder_args.empty() && maybe_cfg) {
std::string cfg = *maybe_cfg + ".cquery";
if (cfg.size() && FileExists(cfg)) {
LOG_S(INFO) << "Using .cquery arguments from " << cfg;
folder_args.emplace(config->project_dir, ReadCompilerArgumentsFromFile(cfg));
GetFilesInFolder(
config->project_dir, true /*recursive*/, true /*add_folder_to_path*/,
[&folder_args, &files, &use_global_config](const std::string& path) {
if (SourceFileLanguage(path) != LanguageId::Unknown) {
files.push_back(path);
} else if (GetBaseName(path) == ".cquery" && !use_global_config) {
LOG_S(INFO) << "Using .cquery arguments from " << path;
folder_args.emplace(GetDirName(path),
ReadCompilerArgumentsFromFile(path));
}
});

if (use_global_config) {
optional<std::string> maybe_cfg = GetGlobalConfigDirectory();
if (folder_args.empty() && maybe_cfg) {
std::string cfg = *maybe_cfg + ".cquery";
if (cfg.size() && FileExists(cfg)) {
LOG_S(INFO) << "Using .cquery arguments from " << cfg;
folder_args.emplace(config->project_dir,
ReadCompilerArgumentsFromFile(cfg));
}
}
}

Expand Down Expand Up @@ -530,9 +535,6 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
// dir takes precedence.
if (FileExists(project->project_dir + ".cquery")) {
return LoadFromDirectoryListing(project);
} else if (GetGlobalConfigDirectory() &&
FileExists(*GetGlobalConfigDirectory() + ".cquery")) {
return LoadFromDirectoryListing(project);
}

// If |compilationDatabaseCommand| is specified, execute it to get the compdb.
Expand Down Expand Up @@ -587,6 +589,7 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
project->project_dir.c_str(), &cx_db_load_error);
}
}

if (!g_config->compilationDatabaseCommand.empty()) {
#if defined(_WIN32)
// TODO
Expand All @@ -599,7 +602,7 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
if (cx_db_load_error != CXCompilationDatabase_NoError) {
LOG_S(INFO) << "Unable to load compile_commands.json located at \""
<< comp_db_dir << "\"; using directory listing instead.";
return LoadFromDirectoryListing(project);
return LoadFromDirectoryListing(project, true);
}

Timer clang_time;
Expand Down

0 comments on commit 9c53e2b

Please sign in to comment.