Infer arguments for files not in the project.

This commit is contained in:
Jacob Dufault 2017-05-06 22:36:29 -07:00
parent e1e45b6dc5
commit fc55589ed3
3 changed files with 108 additions and 32 deletions

View File

@ -749,12 +749,11 @@ struct Index_DoIndex {
Freshen, Freshen,
}; };
Index_DoIndex(Type type, const std::string& path, const optional<std::vector<std::string>>& args) Index_DoIndex(Type type, const Project::Entry& entry)
: type(type), path(path), args(args) {} : type(type), entry(entry) {}
Type type; Type type;
std::string path; Project::Entry entry;
optional<std::vector<std::string>> args;
}; };
struct Index_DoIdMap { struct Index_DoIdMap {
@ -955,16 +954,14 @@ bool ImportCachedIndex(IndexerConfig* config,
void ParseFile(IndexerConfig* config, void ParseFile(IndexerConfig* config,
FileConsumer::SharedState* file_consumer_shared, FileConsumer::SharedState* file_consumer_shared,
Index_DoIdMapQueue* queue_do_id_map, Index_DoIdMapQueue* queue_do_id_map,
const std::string& tu_or_dep_path, const Project::Entry& entry) {
const optional<std::vector<std::string>>& args) {
Timer time; Timer time;
std::unique_ptr<IndexedFile> cache_for_args = LoadCachedIndex(config, tu_or_dep_path); std::unique_ptr<IndexedFile> cache_for_args = LoadCachedIndex(config, entry.filename);
std::string tu_path = cache_for_args ? cache_for_args->import_file : entry.filename;
const std::vector<std::string>& tu_args = entry.args;
std::string tu_path = cache_for_args ? cache_for_args->import_file : tu_or_dep_path;
// TODO: Replace checking cache for arguments by guessing arguments on via directory structure. That will also work better for new files.
const std::vector<std::string>& tu_args = args ? *args : cache_for_args ? cache_for_args->args : kEmptyArgs;
std::vector<std::unique_ptr<IndexedFile>> indexes = Parse( std::vector<std::unique_ptr<IndexedFile>> indexes = Parse(
config, file_consumer_shared, config, file_consumer_shared,
tu_path, tu_args); tu_path, tu_args);
@ -1056,7 +1053,7 @@ bool IndexMain_DoIndex(IndexerConfig* config,
// This assumes index_request->path is a cc or translation unit file (ie, // This assumes index_request->path is a cc or translation unit file (ie,
// it is in compile_commands.json). // it is in compile_commands.json).
bool needs_reparse = ImportCachedIndex(config, file_consumer_shared, queue_do_id_map, index_request->path); bool needs_reparse = ImportCachedIndex(config, file_consumer_shared, queue_do_id_map, index_request->entry.filename);
// If the file has been updated, we need to reparse it. // If the file has been updated, we need to reparse it.
if (needs_reparse) { if (needs_reparse) {
@ -1072,8 +1069,8 @@ bool IndexMain_DoIndex(IndexerConfig* config,
case Index_DoIndex::Type::Parse: { case Index_DoIndex::Type::Parse: {
// index_request->path can be a cc/tu or a dependency path. // index_request->path can be a cc/tu or a dependency path.
file_consumer_shared->Reset(index_request->path); file_consumer_shared->Reset(index_request->entry.filename);
ParseFile(config, file_consumer_shared, queue_do_id_map, index_request->path, index_request->args); ParseFile(config, file_consumer_shared, queue_do_id_map, index_request->entry);
break; break;
} }
@ -1081,9 +1078,9 @@ bool IndexMain_DoIndex(IndexerConfig* config,
// This assumes index_request->path is a cc or translation unit file (ie, // This assumes index_request->path is a cc or translation unit file (ie,
// it is in compile_commands.json). // it is in compile_commands.json).
bool needs_reparse = ResetStaleFiles(config, file_consumer_shared, index_request->path); bool needs_reparse = ResetStaleFiles(config, file_consumer_shared, index_request->entry.filename);
if (needs_reparse) if (needs_reparse)
ParseFile(config, file_consumer_shared, queue_do_id_map, index_request->path, index_request->args); ParseFile(config, file_consumer_shared, queue_do_id_map, index_request->entry);
break; break;
} }
} }
@ -1291,7 +1288,7 @@ bool QueryDbMainLoop(
<< "] Dispatching index request for file " << entry.filename << "] Dispatching index request for file " << entry.filename
<< std::endl; << std::endl;
queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::ImportThenParse, entry.filename, entry.args)); queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::ImportThenParse, entry));
}); });
} }
@ -1342,7 +1339,7 @@ bool QueryDbMainLoop(
std::cerr << "[" << i << "/" << (project->entries.size() - 1) std::cerr << "[" << i << "/" << (project->entries.size() - 1)
<< "] Dispatching index request for file " << entry.filename << "] Dispatching index request for file " << entry.filename
<< std::endl; << std::endl;
queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::Freshen, entry.filename, entry.args)); queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::Freshen, entry));
}); });
break; break;
} }
@ -1381,7 +1378,8 @@ bool QueryDbMainLoop(
std::string path = msg->params.textDocument.uri.GetPath(); std::string path = msg->params.textDocument.uri.GetPath();
// Send an index update request. // Send an index update request.
queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::Parse, path, project->FindArgsForFile(path))); // TODO: Make sure we don't mess up header files when guessing.
queue_do_index->Enqueue(Index_DoIndex(Index_DoIndex::Type::Parse, project->FindCompilationEntryForFile(path)));
// Copy current buffer content so it can be applied when index request is done. // Copy current buffer content so it can be applied when index request is done.
WorkingFile* working_file = working_files->GetFileByFilename(path); WorkingFile* working_file = working_files->GetFileByFilename(path);

View File

@ -7,6 +7,7 @@
#include "utils.h" #include "utils.h"
#include <clang-c/CXCompilationDatabase.h> #include <clang-c/CXCompilationDatabase.h>
#include <doctest/doctest.h>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -44,7 +45,7 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(const std::vector<std:
result.args.reserve(entry.args.size() + extra_flags.size()); result.args.reserve(entry.args.size() + extra_flags.size());
for (size_t i = 0; i < entry.args.size(); ++i) { for (size_t i = 0; i < entry.args.size(); ++i) {
std::string arg = entry.args[i]; std::string arg = entry.args[i];
// If blacklist skip. // If blacklist skip.
if (std::any_of(std::begin(kBlacklist), std::end(kBlacklist), [&arg](const char* value) { if (std::any_of(std::begin(kBlacklist), std::end(kBlacklist), [&arg](const char* value) {
return StartsWith(arg, value); return StartsWith(arg, value);
@ -190,6 +191,31 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(const std::vecto
return result; return result;
} }
// Computes a score based on how well |a| and |b| match. This is used for
// argument guessing.
int ComputeGuessScore(const std::string& a, const std::string& b) {
int score = 0;
int i = 0;
for (i = 0; i < a.length() && i < b.length(); ++i) {
if (a[i] != b[i])
break;
++score;
}
for (int j = i; j < a.length(); ++j) {
if (a[j] == '/')
--score;
}
for (int j = i; j < b.length(); ++j) {
if (b[j] == '/')
--score;
}
return score;
}
} // namespace } // namespace
void Project::Load(const std::vector<std::string>& extra_flags, const std::string& directory) { void Project::Load(const std::vector<std::string>& extra_flags, const std::string& directory) {
@ -200,18 +226,29 @@ void Project::Load(const std::vector<std::string>& extra_flags, const std::strin
absolute_path_to_entry_index_[entries[i].filename] = i; absolute_path_to_entry_index_[entries[i].filename] = i;
} }
optional<Project::Entry> Project::FindCompilationEntryForFile(const std::string& filename) { Project::Entry Project::FindCompilationEntryForFile(const std::string& filename) {
auto it = absolute_path_to_entry_index_.find(filename); auto it = absolute_path_to_entry_index_.find(filename);
if (it != absolute_path_to_entry_index_.end()) if (it != absolute_path_to_entry_index_.end())
return entries[it->second]; return entries[it->second];
return nullopt;
}
optional<std::vector<std::string>> Project::FindArgsForFile(const std::string& filename) { // We couldn't find the file. Try to infer it.
auto entry = FindCompilationEntryForFile(filename); // TODO: Cache inferred file in a separate array (using a lock or similar)
if (!entry) Entry* best_entry = nullptr;
return nullopt; int best_score = 0;
return entry->args; for (Entry& entry : entries) {
int score = ComputeGuessScore(filename, entry.filename);
if (score > best_score) {
best_score = score;
best_entry = &entry;
}
}
Project::Entry result;
result.is_inferred = true;
result.filename = filename;
if (best_entry)
result.args = best_entry->args;
return result;
} }
void Project::ForAllFilteredFiles(IndexerConfig* config, std::function<void(int i, const Entry& entry)> action) { void Project::ForAllFilteredFiles(IndexerConfig* config, std::function<void(int i, const Entry& entry)> action) {
@ -260,3 +297,44 @@ void Project::ForAllFilteredFiles(IndexerConfig* config, std::function<void(int
action(i, entries[i]); action(i, entries[i]);
} }
} }
TEST_SUITE("Project");
TEST_CASE("Entry inference") {
Project p;
{
Project::Entry e;
e.args = { "arg1" };
e.filename = "/a/b/c/d/bar.cc";
p.entries.push_back(e);
}
{
Project::Entry e;
e.args = { "arg2" };
e.filename = "/a/b/c/baz.cc";
p.entries.push_back(e);
}
// Guess at same directory level, when there are parent directories.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/d/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{ "arg1" });
}
// Guess at same directory level, when there are child directories.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{ "arg2" });
}
// Guess at new directory (use the closest parent directory).
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{ "arg2" });
}
}
TEST_SUITE_END();

View File

@ -17,6 +17,8 @@ struct Project {
struct Entry { struct Entry {
std::string filename; std::string filename;
std::vector<std::string> args; std::vector<std::string> args;
// If true, this entry is inferred and was not read from disk.
bool is_inferred = false;
}; };
std::vector<Entry> entries; std::vector<Entry> entries;
@ -30,11 +32,9 @@ struct Project {
// specified in a clang_args file located inside of |directory|. // specified in a clang_args file located inside of |directory|.
void Load(const std::vector<std::string>& extra_flags, const std::string& directory); void Load(const std::vector<std::string>& extra_flags, const std::string& directory);
// Lookup the CompilationEntry for |filename|. // Lookup the CompilationEntry for |filename|. If no entry was found this
optional<Entry> FindCompilationEntryForFile(const std::string& filename); // will infer one based on existing project structure.
Entry FindCompilationEntryForFile(const std::string& filename);
// Helper that uses FindCompilationEntryForFile.
optional<std::vector<std::string>> FindArgsForFile(const std::string& filename);
void ForAllFilteredFiles(IndexerConfig* config, std::function<void(int i, const Entry& entry)> action); void ForAllFilteredFiles(IndexerConfig* config, std::function<void(int i, const Entry& entry)> action);
}; };