ccls/src/project.cc

713 lines
24 KiB
C++
Raw Normal View History

#include "project.h"
#include "clang_utils.h"
2018-03-31 20:59:27 +00:00
#include "filesystem.hh"
2018-02-20 03:06:48 +00:00
#include "language.h"
2018-05-27 19:24:56 +00:00
#include "log.hh"
2017-09-22 01:14:57 +00:00
#include "match.h"
2017-03-31 04:21:52 +00:00
#include "platform.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
#include "serializers/json.h"
2017-10-25 01:39:38 +00:00
#include "timer.h"
2017-10-31 19:49:19 +00:00
#include "utils.h"
#include "working_files.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
2017-03-31 04:21:52 +00:00
#include <clang/Driver/Options.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/LineIterator.h>
using namespace clang;
using namespace llvm;
using namespace llvm::opt;
2017-03-31 04:21:52 +00:00
#include <clang-c/CXCompilationDatabase.h>
#include <doctest/doctest.h>
#include <rapidjson/writer.h>
2017-03-31 04:21:52 +00:00
#if defined(__unix__) || defined(__APPLE__)
#include <unistd.h>
#endif
#include <limits>
#include <unordered_set>
#include <vector>
2017-03-31 04:21:52 +00:00
struct CompileCommandsEntry {
std::string directory;
2017-04-17 20:40:50 +00:00
std::string file;
std::string command;
2017-03-31 04:21:52 +00:00
std::vector<std::string> args;
2018-03-31 20:59:27 +00:00
std::string ResolveIfRelative(std::string path) const {
if (sys::path::is_absolute(path))
2018-03-31 20:59:27 +00:00
return path;
SmallString<256> Ret;
sys::path::append(Ret, directory, path);
return Ret.str();
2018-03-31 20:59:27 +00:00
}
};
2017-04-17 20:40:50 +00:00
MAKE_REFLECT_STRUCT(CompileCommandsEntry, directory, file, command, args);
2017-03-31 04:21:52 +00:00
namespace {
2017-03-31 04:21:52 +00:00
2018-03-31 03:16:33 +00:00
enum class ProjectMode { CompileCommandsJson, DotCcls, ExternalCommand };
2017-09-22 03:09:11 +00:00
struct ProjectConfig {
std::unordered_set<std::string> quote_dirs;
std::unordered_set<std::string> angle_dirs;
2017-09-22 03:09:11 +00:00
std::vector<std::string> extra_flags;
std::string project_dir;
ProjectMode mode = ProjectMode::CompileCommandsJson;
};
enum OptionClass {
EqOrJoinOrSep,
EqOrSep,
JoinOrSep,
Separate,
};
2017-05-21 07:37:53 +00:00
Project::Entry GetCompilationEntryFromCompileCommandEntry(
2017-09-22 03:09:11 +00:00
ProjectConfig* config,
2017-09-22 01:14:57 +00:00
const CompileCommandsEntry& entry) {
2017-04-20 05:01:36 +00:00
Project::Entry result;
2018-03-31 20:59:27 +00:00
result.filename = entry.file;
const std::string base_name = sys::path::filename(entry.file);
// Expand %c %cpp %clang
std::vector<std::string> args;
const LanguageId lang = SourceFileLanguage(entry.file);
for (const std::string& arg : entry.args) {
if (arg.compare(0, 3, "%c ") == 0) {
if (lang == LanguageId::C)
args.push_back(arg.substr(3));
} else if (arg.compare(0, 5, "%cpp ") == 0) {
if (lang == LanguageId::Cpp)
args.push_back(arg.substr(5));
} else if (arg == "%clang") {
args.push_back(lang == LanguageId::Cpp ? "clang++" : "clang");
} else {
args.push_back(arg);
}
}
if (args.empty())
2018-02-21 07:52:49 +00:00
return result;
args.insert(args.end(), config->extra_flags.begin(),
config->extra_flags.end());
std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
unsigned MissingArgIndex, MissingArgCount;
std::vector<const char*> cargs;
for (auto& arg : args)
cargs.push_back(arg.c_str());
InputArgList Args =
Opts->ParseArgs(makeArrayRef(cargs), MissingArgIndex, MissingArgCount,
driver::options::CC1Option);
using namespace clang::driver::options;
for (const auto* A :
Args.filtered(OPT_I, OPT_c_isystem, OPT_cxx_isystem, OPT_isystem))
config->angle_dirs.insert(entry.ResolveIfRelative(A->getValue()));
for (const auto* A : Args.filtered(OPT_I, OPT_iquote))
config->quote_dirs.insert(entry.ResolveIfRelative(A->getValue()));
for (const auto* A : Args.filtered(OPT_idirafter)) {
std::string dir = entry.ResolveIfRelative(A->getValue());
config->angle_dirs.insert(dir);
config->quote_dirs.insert(dir);
}
for (size_t i = 1; i < args.size(); i++)
// This is most likely the file path we will be passing to clang. The
// path needs to be absolute, otherwise clang_codeCompleteAt is extremely
// slow. See
// https://github.com/cquery-project/cquery/commit/af63df09d57d765ce12d40007bf56302a0446678.
if (args[i][0] != '-' && EndsWith(args[i], base_name)) {
args[i] = entry.ResolveIfRelative(args[i]);
continue;
}
if (!Args.hasArg(OPT_resource_dir))
args.push_back("-resource-dir=" + g_config->clang.resourceDir);
if (!Args.hasArg(OPT_working_directory))
args.push_back("-working-directory=" + entry.directory);
2017-12-01 18:11:09 +00:00
// There could be a clang version mismatch between what the project uses and
2018-03-31 03:16:33 +00:00
// what ccls uses. Make sure we do not emit warnings for mismatched options.
args.push_back("-Wno-unknown-warning-option");
2017-12-01 18:11:09 +00:00
// Using -fparse-all-comments enables documentation in the indexer and in
2017-12-27 15:56:17 +00:00
// code completion.
if (g_config->index.comments > 1)
args.push_back("-fparse-all-comments");
2017-12-27 15:56:17 +00:00
result.args = std::move(args);
return result;
}
std::vector<std::string> ReadCompilerArgumentsFromFile(
const std::string& path) {
auto MBOrErr = MemoryBuffer::getFile(path);
if (!MBOrErr) return {};
std::vector<std::string> args;
for (line_iterator I(*MBOrErr.get(), true, '#'), E; I != E; ++I)
args.push_back(*I);
return args;
}
2018-04-04 06:05:41 +00:00
std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig* config) {
std::vector<Project::Entry> result;
2018-03-31 03:16:33 +00:00
config->mode = ProjectMode::DotCcls;
SmallString<256> Path;
sys::path::append(Path, config->project_dir, ".ccls");
LOG_IF_S(WARNING, !sys::fs::exists(Path) && config->extra_flags.empty())
<< "ccls has no clang arguments. Use either "
"compile_commands.json or .ccls, See ccls README for "
"more information.";
std::unordered_map<std::string, std::vector<std::string>> folder_args;
std::vector<std::string> files;
2018-02-22 07:34:32 +00:00
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 (sys::path::filename(path) == ".ccls") {
2018-03-31 03:16:33 +00:00
LOG_S(INFO) << "Using .ccls arguments from " << path;
folder_args.emplace(sys::path::parent_path(path),
ReadCompilerArgumentsFromFile(path));
2018-02-22 07:34:32 +00:00
}
});
const std::string& project_dir = config->project_dir;
2018-04-08 00:10:54 +00:00
const auto& project_dir_args = folder_args[project_dir];
LOG_IF_S(INFO, !project_dir_args.empty())
2018-03-31 03:16:33 +00:00
<< "Using .ccls arguments " << StringJoin(project_dir_args);
auto GetCompilerArgumentForFile = [&project_dir, &folder_args](std::string cur) {
while (!(cur = sys::path::parent_path(cur)).empty()) {
2018-03-11 03:48:40 +00:00
auto it = folder_args.find(cur);
if (it != folder_args.end())
return it->second;
std::string normalized = NormalizePath(cur);
2018-03-11 03:48:40 +00:00
// Break if outside of the project root.
2018-04-08 00:10:54 +00:00
if (normalized.size() <= project_dir.size() ||
normalized.compare(0, project_dir.size(), project_dir) != 0)
break;
}
2018-04-08 00:10:54 +00:00
return folder_args[project_dir];
};
for (const std::string& file : files) {
CompileCommandsEntry e;
e.directory = config->project_dir;
e.file = file;
e.args = GetCompilerArgumentForFile(file);
2018-02-23 10:43:15 +00:00
if (e.args.empty())
e.args.push_back("%clang"); // Add a Dummy.
e.args.push_back(e.file);
2018-04-04 06:05:41 +00:00
result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e));
}
return result;
}
2017-05-21 07:37:53 +00:00
std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
ProjectConfig* project,
const std::string& opt_compilation_db_dir) {
2018-03-31 03:16:33 +00:00
// If there is a .ccls file always load using directory listing.
SmallString<256> Path;
sys::path::append(Path, project->project_dir, ".ccls");
if (sys::fs::exists(Path))
2018-04-04 06:05:41 +00:00
return LoadFromDirectoryListing(project);
// If |compilationDatabaseCommand| is specified, execute it to get the compdb.
std::string comp_db_dir;
Path.clear();
2018-04-04 06:05:41 +00:00
if (g_config->compilationDatabaseCommand.empty()) {
project->mode = ProjectMode::CompileCommandsJson;
// Try to load compile_commands.json, but fallback to a project listing.
comp_db_dir = opt_compilation_db_dir.empty() ? project->project_dir
: opt_compilation_db_dir;
sys::path::append(Path, comp_db_dir, "compile_commands.json");
} else {
project->mode = ProjectMode::ExternalCommand;
#ifdef _WIN32
// TODO
#else
2018-03-31 03:16:33 +00:00
char tmpdir[] = "/tmp/ccls-compdb-XXXXXX";
if (!mkdtemp(tmpdir))
return {};
comp_db_dir = tmpdir;
sys::path::append(Path, comp_db_dir, "compile_commands.json");
rapidjson::StringBuffer input;
rapidjson::Writer<rapidjson::StringBuffer> writer(input);
JsonWriter json_writer(&writer);
2018-04-04 06:05:41 +00:00
Reflect(json_writer, *g_config);
std::string contents = GetExternalCommandOutput(
2018-04-04 06:05:41 +00:00
std::vector<std::string>{g_config->compilationDatabaseCommand,
project->project_dir},
input.GetString());
FILE* fout = fopen(Path.c_str(), "wb");
2018-04-04 06:05:41 +00:00
fwrite(contents.c_str(), contents.size(), 1, fout);
fclose(fout);
#endif
}
2017-03-31 04:21:52 +00:00
CXCompilationDatabase_Error cx_db_load_error;
2017-09-22 01:14:57 +00:00
CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory(
comp_db_dir.c_str(), &cx_db_load_error);
2018-04-04 06:05:41 +00:00
if (!g_config->compilationDatabaseCommand.empty()) {
#ifdef _WIN32
2018-03-20 02:51:42 +00:00
// TODO
#else
unlink(Path.c_str());
rmdir(comp_db_dir.c_str());
#endif
}
2017-03-31 04:21:52 +00:00
if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) {
LOG_S(INFO) << "unable to load " << Path.c_str()
2018-04-04 06:05:41 +00:00
<< "; using directory listing instead.";
return LoadFromDirectoryListing(project);
2017-03-31 04:21:52 +00:00
}
LOG_S(INFO) << "loaded " << Path.c_str();
2017-10-25 01:39:38 +00:00
Timer clang_time;
Timer our_time;
clang_time.Pause();
our_time.Pause();
clang_time.Resume();
2017-09-22 01:14:57 +00:00
CXCompileCommands cx_commands =
clang_CompilationDatabase_getAllCompileCommands(cx_db);
2017-03-31 04:21:52 +00:00
unsigned int num_commands = clang_CompileCommands_getSize(cx_commands);
2017-10-25 01:39:38 +00:00
clang_time.Pause();
2017-04-20 05:01:36 +00:00
std::vector<Project::Entry> result;
2017-03-31 04:21:52 +00:00
for (unsigned int i = 0; i < num_commands; i++) {
2017-10-25 01:39:38 +00:00
clang_time.Resume();
2017-09-22 01:14:57 +00:00
CXCompileCommand cx_command =
clang_CompileCommands_getCommand(cx_commands, i);
2017-03-31 04:21:52 +00:00
2017-09-22 01:14:57 +00:00
std::string directory =
ToString(clang_CompileCommand_getDirectory(cx_command));
2017-09-22 01:14:57 +00:00
std::string relative_filename =
ToString(clang_CompileCommand_getFilename(cx_command));
2017-03-31 04:21:52 +00:00
unsigned num_args = clang_CompileCommand_getNumArgs(cx_command);
2017-10-25 01:39:38 +00:00
CompileCommandsEntry entry;
2017-03-31 04:21:52 +00:00
entry.args.reserve(num_args);
2017-11-19 22:46:05 +00:00
for (unsigned j = 0; j < num_args; ++j) {
2017-09-22 01:14:57 +00:00
entry.args.push_back(
ToString(clang_CompileCommand_getArg(cx_command, j)));
2017-11-19 22:46:05 +00:00
}
clang_time.Pause(); // TODO: don't call ToString in this block.
2017-11-19 22:46:05 +00:00
// LOG_S(INFO) << "Got args " << StringJoin(entry.args);
2017-10-25 01:39:38 +00:00
our_time.Resume();
entry.directory = directory;
2018-03-31 20:59:27 +00:00
entry.file = entry.ResolveIfRelative(relative_filename);
2017-04-17 20:40:50 +00:00
2018-01-11 02:43:01 +00:00
result.push_back(
2018-04-04 06:05:41 +00:00
GetCompilationEntryFromCompileCommandEntry(project, entry));
2017-10-25 01:39:38 +00:00
our_time.Pause();
2017-03-31 04:21:52 +00:00
}
2017-10-25 01:39:38 +00:00
clang_time.Resume();
2017-03-31 04:21:52 +00:00
clang_CompileCommands_dispose(cx_commands);
clang_CompilationDatabase_dispose(cx_db);
2017-10-25 01:39:38 +00:00
clang_time.Pause();
clang_time.ResetAndPrint("compile_commands.json clang time");
our_time.ResetAndPrint("compile_commands.json our time");
2017-03-31 04:21:52 +00:00
return result;
}
// Computes a score based on how well |a| and |b| match. This is used for
// argument guessing.
2018-03-31 16:25:58 +00:00
int ComputeGuessScore(std::string_view a, std::string_view b) {
// Increase score based on common prefix and suffix. Prefixes are prioritized.
if (a.size() < b.size())
std::swap(a, b);
size_t i = std::mismatch(a.begin(), a.end(), b.begin()).first - a.begin();
size_t j = std::mismatch(a.rbegin(), a.rend(), b.rbegin()).first - a.rbegin();
int score = 10 * i + j;
if (i + j < b.size())
score -= 100 * (std::count(a.begin() + i, a.end() - j, '/') +
std::count(b.begin() + i, b.end() - j, '/'));
return score;
}
2017-03-31 04:21:52 +00:00
} // namespace
2018-04-04 06:05:41 +00:00
void Project::Load(const std::string& root_directory) {
2017-09-22 03:09:11 +00:00
// Load data.
ProjectConfig project;
project.extra_flags = g_config->clang.extraArgs;
project.project_dir = root_directory;
entries = LoadCompilationEntriesFromDirectory(
2018-04-04 06:05:41 +00:00
&project, g_config->compilationDatabaseDirectory);
2017-09-22 03:09:11 +00:00
// Cleanup / postprocess include directories.
quote_include_directories.assign(project.quote_dirs.begin(),
project.quote_dirs.end());
angle_include_directories.assign(project.angle_dirs.begin(),
project.angle_dirs.end());
2017-05-21 07:37:53 +00:00
for (std::string& path : quote_include_directories) {
EnsureEndsInSlash(path);
2017-07-28 02:14:33 +00:00
LOG_S(INFO) << "quote_include_dir: " << path;
2017-05-21 07:37:53 +00:00
}
for (std::string& path : angle_include_directories) {
EnsureEndsInSlash(path);
2017-07-28 02:14:33 +00:00
LOG_S(INFO) << "angle_include_dir: " << path;
2017-05-21 07:37:53 +00:00
}
2017-09-22 03:09:11 +00:00
// Setup project entries.
std::lock_guard<std::mutex> lock(mutex_);
2018-03-31 20:59:27 +00:00
absolute_path_to_entry_index_.reserve(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
entries[i].id = i;
absolute_path_to_entry_index_[entries[i].filename] = i;
}
2017-03-31 04:21:52 +00:00
}
void Project::SetFlagsForFile(
const std::vector<std::string>& flags,
const std::string& path) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = absolute_path_to_entry_index_.find(path);
if (it != absolute_path_to_entry_index_.end()) {
// The entry already exists in the project, just set the flags.
this->entries[it->second].args = flags;
} else {
// Entry wasn't found, so we create a new one.
Entry entry;
entry.is_inferred = false;
entry.filename = path;
entry.args = flags;
this->entries.emplace_back(entry);
}
}
2017-09-22 01:14:57 +00:00
Project::Entry Project::FindCompilationEntryForFile(
const std::string& filename) {
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = absolute_path_to_entry_index_.find(filename);
if (it != absolute_path_to_entry_index_.end())
return entries[it->second];
}
// We couldn't find the file. Try to infer it.
// TODO: Cache inferred file in a separate array (using a lock or similar)
Entry* best_entry = nullptr;
int best_score = std::numeric_limits<int>::min();
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;
2018-01-16 01:38:28 +00:00
if (!best_entry) {
result.args.push_back("%clang");
result.args.push_back(filename);
2018-01-16 01:38:28 +00:00
} else {
result.args = best_entry->args;
// |best_entry| probably has its own path in the arguments. We need to remap
// that path to the new filename.
std::string best_entry_base_name = sys::path::filename(best_entry->filename);
for (std::string& arg : result.args) {
2018-04-08 00:10:54 +00:00
try {
if (arg == best_entry->filename ||
sys::path::filename(arg) == best_entry_base_name)
2018-04-08 00:10:54 +00:00
arg = filename;
} catch (...) {
}
}
}
return result;
}
2017-09-22 01:14:57 +00:00
void Project::ForAllFilteredFiles(
std::function<void(int i, const Entry& entry)> action) {
2018-04-04 06:05:41 +00:00
GroupMatch matcher(g_config->index.whitelist, g_config->index.blacklist);
for (int i = 0; i < entries.size(); ++i) {
const Project::Entry& entry = entries[i];
std::string failure_reason;
if (matcher.IsMatch(entry.filename, &failure_reason))
action(i, entries[i]);
2018-04-04 06:05:41 +00:00
else if (g_config->index.logSkippedPaths) {
LOG_S(INFO) << "[" << i + 1 << "/" << entries.size() << "]: Failed "
<< failure_reason << "; skipping " << entry.filename;
}
}
}
2018-05-28 00:50:02 +00:00
void Project::Index(WorkingFiles* wfiles,
lsRequestId id) {
2018-04-04 06:05:41 +00:00
ForAllFilteredFiles([&](int i, const Project::Entry& entry) {
bool is_interactive = wfiles->GetFileByFilename(entry.filename) != nullptr;
2018-05-28 00:50:02 +00:00
pipeline::Index(entry.filename, entry.args, is_interactive, id);
});
2018-05-08 15:56:20 +00:00
// Dummy request to indicate that project is loaded and
// trigger refreshing semantic highlight for all working files.
2018-05-28 00:50:02 +00:00
pipeline::Index("", {}, false);
}
2017-11-19 18:05:06 +00:00
TEST_SUITE("Project") {
2017-11-19 22:11:54 +00:00
void CheckFlags(const std::string& directory, const std::string& file,
2017-11-19 18:05:06 +00:00
std::vector<std::string> raw,
std::vector<std::string> expected) {
2018-04-04 06:05:41 +00:00
g_config = std::make_unique<Config>();
g_config->clang.resourceDir = "/w/resource_dir/";
ProjectConfig project;
project.project_dir = "/w/c/s/";
2017-11-19 18:05:06 +00:00
CompileCommandsEntry entry;
entry.directory = directory;
entry.args = raw;
entry.file = file;
Project::Entry result =
2018-04-04 06:05:41 +00:00
GetCompilationEntryFromCompileCommandEntry(&project, entry);
2017-11-19 18:05:06 +00:00
2017-11-19 22:46:05 +00:00
if (result.args != expected) {
2018-04-04 06:05:41 +00:00
fprintf(stderr, "Raw: %s\n", StringJoin(raw).c_str());
fprintf(stderr, "Expected: %s\n", StringJoin(expected).c_str());
fprintf(stderr, "Actual: %s\n", StringJoin(result.args).c_str());
}
2017-11-19 18:05:06 +00:00
REQUIRE(result.args == expected);
2017-10-18 06:23:07 +00:00
}
2017-11-19 18:05:06 +00:00
void CheckFlags(std::vector<std::string> raw,
std::vector<std::string> expected) {
CheckFlags("/dir/", "file.cc", raw, expected);
}
2017-11-19 18:05:06 +00:00
TEST_CASE("strip meta-compiler invocations") {
CheckFlags(
/* raw */ {"clang", "-lstdc++", "myfile.cc"},
2017-12-23 16:01:43 +00:00
/* expected */
{"clang", "-lstdc++", "/dir/myfile.cc",
"-resource-dir=/w/resource_dir/", "-working-directory=/dir/",
"-Wno-unknown-warning-option", "-fparse-all-comments"});
2017-12-23 16:01:43 +00:00
2018-01-20 03:05:26 +00:00
CheckFlags(
/* raw */ {"clang.exe"},
/* expected */
{"clang.exe", "-resource-dir=/w/resource_dir/",
"-working-directory=/dir/", "-Wno-unknown-warning-option",
2018-02-22 07:34:32 +00:00
"-fparse-all-comments"});
2017-11-19 18:05:06 +00:00
}
2017-10-18 06:23:07 +00:00
2018-03-31 20:59:27 +00:00
#ifdef _WIN32
2018-01-11 04:07:50 +00:00
TEST_CASE("Windows path normalization") {
2018-02-22 07:34:32 +00:00
CheckFlags("E:/workdir", "E:/workdir/bar.cc", /* raw */ {"clang", "bar.cc"},
/* expected */
2018-03-31 20:59:27 +00:00
{"clang", "-working-directory=E:/workdir", "E:/workdir/bar.cc",
"-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option",
"-fparse-all-comments"});
2018-02-22 07:34:32 +00:00
CheckFlags("E:/workdir", "E:/workdir/bar.cc",
/* raw */ {"clang", "E:/workdir/bar.cc"},
/* expected */
2018-03-31 20:59:27 +00:00
{"clang", "-working-directory=E:/workdir", "E:/workdir/bar.cc",
"-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option",
"-fparse-all-comments"});
CheckFlags("E:/workdir", "E:/workdir/bar.cc",
/* raw */ {"clang-cl.exe", "/I./test", "E:/workdir/bar.cc"},
/* expected */
{"clang-cl.exe", "-working-directory=E:/workdir",
2018-03-31 20:59:27 +00:00
"/I&E:/workdir/./test", "E:/workdir/bar.cc",
"-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option",
"-fparse-all-comments"});
CheckFlags("E:/workdir", "E:/workdir/bar.cc",
/* raw */
{"cl.exe", "/I../third_party/test/include", "E:/workdir/bar.cc"},
/* expected */
{"cl.exe", "-working-directory=E:/workdir",
"/I&E:/workdir/../third_party/test/include",
2018-03-31 20:59:27 +00:00
"E:/workdir/bar.cc", "-resource-dir=/w/resource_dir/",
2018-02-22 07:34:32 +00:00
"-Wno-unknown-warning-option", "-fparse-all-comments"});
2018-01-11 04:07:50 +00:00
}
2018-03-31 20:59:27 +00:00
#endif
2018-01-11 04:07:50 +00:00
2017-11-19 22:46:05 +00:00
TEST_CASE("Path in args") {
CheckFlags(
"/home/user", "/home/user/foo/bar.c",
/* raw */ {"cc", "-O0", "foo/bar.c"},
/* expected */
{"cc", "-O0", "/home/user/foo/bar.c", "-resource-dir=/w/resource_dir/",
"-working-directory=/home/user", "-Wno-unknown-warning-option",
"-fparse-all-comments"});
2017-11-19 22:46:05 +00:00
}
2017-11-19 18:05:06 +00:00
TEST_CASE("Directory extraction") {
2018-04-04 06:05:41 +00:00
g_config = std::make_unique<Config>();
2017-11-19 18:05:06 +00:00
ProjectConfig config;
config.project_dir = "/w/c/s/";
2017-11-19 18:05:06 +00:00
CompileCommandsEntry entry;
entry.directory = "/base";
entry.args = {"clang",
"-I/a_absolute1",
"--foobar",
"-I",
"/a_absolute2",
"--foobar",
"-Ia_relative1",
"--foobar",
2018-04-08 00:10:54 +00:00
"-isystem",
2017-11-19 18:05:06 +00:00
"a_relative2",
"--foobar",
"-iquote/q_absolute1",
"--foobar",
"-iquote",
"/q_absolute2",
"--foobar",
"-iquoteq_relative1",
"--foobar",
"-iquote",
"q_relative2",
"--foobar",
"foo.cc"};
entry.file = "foo.cc";
Project::Entry result =
2018-04-04 06:05:41 +00:00
GetCompilationEntryFromCompileCommandEntry(&config, entry);
2017-11-19 18:05:06 +00:00
std::unordered_set<std::string> angle_expected{
2018-03-31 20:59:27 +00:00
"/a_absolute1", "/a_absolute2", "/base/a_relative1",
"/base/a_relative2"};
2017-11-19 18:05:06 +00:00
std::unordered_set<std::string> quote_expected{
2018-04-08 00:10:54 +00:00
"/a_absolute1", "/a_absolute2", "/base/a_relative1",
"/q_absolute1", "/q_absolute2", "/base/q_relative1",
2018-03-31 20:59:27 +00:00
"/base/q_relative2"};
2017-11-19 18:05:06 +00:00
REQUIRE(config.angle_dirs == angle_expected);
REQUIRE(config.quote_dirs == quote_expected);
}
2017-11-19 18:05:06 +00:00
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);
}
2017-11-19 18:05:06 +00:00
// Guess at same directory level, when there are parent directories.
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("/a/b/c/d/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
2017-11-19 18:05:06 +00:00
// Guess at same directory level, when there are child directories.
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("/a/b/c/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
2017-11-19 18:05:06 +00:00
// Guess at new directory (use the closest parent directory).
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("/a/b/c/new/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
}
TEST_CASE("Entry inference remaps file names") {
Project p;
{
Project::Entry e;
e.args = {"a", "b", "aaaa.cc", "d"};
e.filename = "absolute/aaaa.cc";
p.entries.push_back(e);
}
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry = p.FindCompilationEntryForFile("ee.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"a", "b", "ee.cc", "d"});
}
}
2017-11-19 18:05:06 +00:00
TEST_CASE("Entry inference prefers same file endings") {
Project p;
{
Project::Entry e;
e.args = {"arg1"};
e.filename = "common/simple_browsertest.cc";
p.entries.push_back(e);
}
{
Project::Entry e;
e.args = {"arg2"};
e.filename = "common/simple_unittest.cc";
p.entries.push_back(e);
}
{
Project::Entry e;
e.args = {"arg3"};
e.filename = "common/a/simple_unittest.cc";
p.entries.push_back(e);
}
// Prefer files with the same ending.
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("my_browsertest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("my_unittest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("common/my_browsertest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("common/my_unittest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
2017-11-19 18:05:06 +00:00
// Prefer the same directory over matching file-ending.
{
2018-03-31 03:16:33 +00:00
std::optional<Project::Entry> entry =
2017-11-19 18:05:06 +00:00
p.FindCompilationEntryForFile("common/a/foo.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg3"});
}
}
}