2017-03-26 21:40:34 +00:00
|
|
|
#include "project.h"
|
|
|
|
|
2018-03-11 22:12:26 +00:00
|
|
|
#include "cache_manager.h"
|
2017-11-04 22:29:03 +00:00
|
|
|
#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"
|
2017-09-22 01:14:57 +00:00
|
|
|
#include "match.h"
|
2017-03-31 04:21:52 +00:00
|
|
|
#include "platform.h"
|
2018-03-11 22:12:26 +00:00
|
|
|
#include "queue_manager.h"
|
2018-02-22 21:50:46 +00:00
|
|
|
#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"
|
2018-03-11 22:12:26 +00:00
|
|
|
#include "working_files.h"
|
2017-03-31 04:21:52 +00:00
|
|
|
|
|
|
|
#include <clang-c/CXCompilationDatabase.h>
|
2017-05-07 05:36:29 +00:00
|
|
|
#include <doctest/doctest.h>
|
2018-02-22 21:50:46 +00:00
|
|
|
#include <rapidjson/writer.h>
|
2018-03-11 17:08:41 +00:00
|
|
|
#include <loguru.hpp>
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2018-02-20 00:19:57 +00:00
|
|
|
#if defined(__unix__) || defined(__APPLE__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fstream>
|
2017-03-31 04:21:52 +00:00
|
|
|
#include <iostream>
|
2017-06-20 15:17:23 +00:00
|
|
|
#include <limits>
|
2017-05-21 19:51:15 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <unordered_set>
|
2017-04-17 07:06:01 +00:00
|
|
|
#include <vector>
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
struct CompileCommandsEntry {
|
2018-03-31 20:59:27 +00:00
|
|
|
fs::path 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
|
|
|
|
|
|
|
fs::path ResolveIfRelative(fs::path path) const {
|
|
|
|
if (path.is_absolute())
|
|
|
|
return path;
|
|
|
|
return directory / path;
|
|
|
|
}
|
2017-04-17 07:06:01 +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
|
|
|
|
2017-04-17 07:06:01 +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 };
|
2018-02-20 02:15:43 +00:00
|
|
|
|
2017-09-22 03:09:11 +00:00
|
|
|
struct ProjectConfig {
|
2017-09-22 03:02:48 +00:00
|
|
|
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;
|
2018-02-20 02:15:43 +00:00
|
|
|
ProjectMode mode = ProjectMode::CompileCommandsJson;
|
2017-09-22 03:02:48 +00:00
|
|
|
};
|
|
|
|
|
2017-10-18 06:23:07 +00:00
|
|
|
// TODO: See
|
|
|
|
// https://github.com/Valloric/ycmd/blob/master/ycmd/completers/cpp/flags.py.
|
2018-03-26 13:32:04 +00:00
|
|
|
// Flags '-include' and '-include-pch' are blacklisted here cause libclang returns error in case when
|
|
|
|
// precompiled header was generated by a different compiler (even two different builds of same version
|
|
|
|
// of clang for the same platform are incompatible). Note that libclang always generate it's own pch
|
|
|
|
// internally. For details, see https://github.com/Valloric/ycmd/issues/892 .
|
2018-01-05 21:41:38 +00:00
|
|
|
std::vector<std::string> kBlacklistMulti = {
|
2018-03-26 13:21:23 +00:00
|
|
|
"-MF", "-MT", "-MQ", "-o", "--serialize-diagnostics", "-Xclang", "-include", "-include-pch"};
|
2017-05-09 01:21:21 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// Blacklisted flags which are always removed from the command line.
|
2018-01-05 21:41:38 +00:00
|
|
|
std::vector<std::string> kBlacklist = {
|
2018-03-22 09:00:59 +00:00
|
|
|
"-c", "-MP", "-MD", "-MMD", "--fcolor-diagnostics", "-showIncludes"
|
2017-04-26 04:03:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Arguments which are followed by a potentially relative path. We need to make
|
|
|
|
// all relative paths absolute, otherwise libclang will not resolve them.
|
2018-01-05 21:41:38 +00:00
|
|
|
std::vector<std::string> kPathArgs = {
|
2017-10-18 08:24:52 +00:00
|
|
|
"-I", "-iquote", "-isystem", "--sysroot=",
|
|
|
|
"-isysroot", "-gcc-toolchain", "-include-pch", "-iframework",
|
2018-03-18 16:15:32 +00:00
|
|
|
"-F", "-imacros", "-include", "/I",
|
2018-03-20 02:51:42 +00:00
|
|
|
"-idirafter"};
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2018-01-05 21:41:38 +00:00
|
|
|
// Arguments which always require an absolute path, ie, clang -working-directory
|
|
|
|
// does not work as expected. Argument processing assumes that this is a subset
|
|
|
|
// of kPathArgs.
|
2018-01-07 04:26:22 +00:00
|
|
|
std::vector<std::string> kNormalizePathArgs = {"--sysroot="};
|
2018-01-05 21:41:38 +00:00
|
|
|
|
2017-10-18 06:23:07 +00:00
|
|
|
// Arguments whose path arguments should be injected into include dir lookup
|
|
|
|
// for #include completion.
|
2018-01-05 21:41:38 +00:00
|
|
|
std::vector<std::string> kQuoteIncludeArgs = {"-iquote"};
|
2018-03-11 17:08:41 +00:00
|
|
|
std::vector<std::string> kAngleIncludeArgs = {"-I", "/I", "-isystem"};
|
2017-05-21 07:37:53 +00:00
|
|
|
|
|
|
|
bool ShouldAddToQuoteIncludes(const std::string& arg) {
|
2017-10-18 08:24:52 +00:00
|
|
|
return StartsWithAny(arg, kQuoteIncludeArgs);
|
2017-05-21 07:37:53 +00:00
|
|
|
}
|
|
|
|
bool ShouldAddToAngleIncludes(const std::string& arg) {
|
2017-10-18 08:24:52 +00:00
|
|
|
return StartsWithAny(arg, kAngleIncludeArgs);
|
2017-05-21 07:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 02:15:43 +00:00
|
|
|
LanguageId SourceFileLanguage(const std::string& path) {
|
2017-12-15 05:18:43 +00:00
|
|
|
if (EndsWith(path, ".c"))
|
2018-02-20 01:19:50 +00:00
|
|
|
return LanguageId::C;
|
2017-12-15 05:18:43 +00:00
|
|
|
else if (EndsWith(path, ".cpp") || EndsWith(path, ".cc"))
|
2018-02-20 01:19:50 +00:00
|
|
|
return LanguageId::Cpp;
|
2017-12-15 05:18:43 +00:00
|
|
|
else if (EndsWith(path, ".mm"))
|
2018-02-20 01:19:50 +00:00
|
|
|
return LanguageId::ObjCpp;
|
2017-12-15 05:18:43 +00:00
|
|
|
else if (EndsWith(path, ".m"))
|
2018-02-20 01:19:50 +00:00
|
|
|
return LanguageId::ObjC;
|
2018-02-20 02:15:43 +00:00
|
|
|
return LanguageId::Unknown;
|
2017-05-22 06:06:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 07:37:53 +00:00
|
|
|
Project::Entry GetCompilationEntryFromCompileCommandEntry(
|
2018-01-11 02:33:20 +00:00
|
|
|
Config* init_opts,
|
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;
|
2018-02-24 22:51:51 +00:00
|
|
|
const std::string base_name = GetBaseName(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;
|
2018-02-24 22:51:51 +00:00
|
|
|
|
2018-03-18 22:52:01 +00:00
|
|
|
std::string first_arg = args[0];
|
|
|
|
// Windows' filesystem is not case sensitive, so we compare only
|
|
|
|
// the lower case variant.
|
|
|
|
std::transform(first_arg.begin(), first_arg.end(), first_arg.begin(),
|
|
|
|
tolower);
|
|
|
|
bool clang_cl = strstr(first_arg.c_str(), "clang-cl") ||
|
|
|
|
strstr(first_arg.c_str(), "cl.exe");
|
|
|
|
// Clang only cares about the last --driver-mode flag, so the loop
|
|
|
|
// iterates in reverse to find the last one as soon as possible
|
|
|
|
// in case of multiple --driver-mode flags.
|
|
|
|
for (int i = args.size() - 1; i >= 0; --i) {
|
|
|
|
if (strstr(args[i].c_str(), "--dirver-mode=")) {
|
|
|
|
clang_cl = clang_cl || strstr(args[i].c_str(), "--driver-mode=cl");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 01:19:50 +00:00
|
|
|
// Compiler driver.
|
2018-03-31 03:16:33 +00:00
|
|
|
result.args.push_back(args[0]);
|
2017-10-18 06:23:07 +00:00
|
|
|
|
2018-01-11 02:33:20 +00:00
|
|
|
// Add -working-directory if not provided.
|
2018-02-24 22:51:51 +00:00
|
|
|
if (!AnyStartsWith(args, "-working-directory"))
|
2018-03-31 20:59:27 +00:00
|
|
|
result.args.emplace_back("-working-directory=" + entry.directory.string());
|
2017-12-31 21:56:15 +00:00
|
|
|
|
2017-10-18 08:24:52 +00:00
|
|
|
bool next_flag_is_path = false;
|
|
|
|
bool add_next_flag_to_quote_dirs = false;
|
|
|
|
bool add_next_flag_to_angle_dirs = false;
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-01-05 21:41:38 +00:00
|
|
|
// Note that when processing paths, some arguments support multiple forms, ie,
|
|
|
|
// {"-Ifoo"} or {"-I", "foo"}. Support both styles.
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
size_t i = 1;
|
2018-02-24 22:51:51 +00:00
|
|
|
result.args.reserve(args.size() + config->extra_flags.size());
|
|
|
|
for (; i < args.size(); ++i) {
|
|
|
|
std::string arg = args[i];
|
2017-05-07 05:36:29 +00:00
|
|
|
|
2017-11-19 23:46:02 +00:00
|
|
|
// If blacklist skip.
|
|
|
|
if (!next_flag_is_path) {
|
|
|
|
if (StartsWithAny(arg, kBlacklistMulti)) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (StartsWithAny(arg, kBlacklist))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:41:38 +00:00
|
|
|
// Finish processing path for the previous argument, which was a switch.
|
|
|
|
// {"-I", "foo"} style.
|
2017-10-18 08:24:52 +00:00
|
|
|
if (next_flag_is_path) {
|
2018-03-31 20:59:27 +00:00
|
|
|
std::string normalized_arg = entry.ResolveIfRelative(arg);
|
2017-10-18 08:24:52 +00:00
|
|
|
if (add_next_flag_to_quote_dirs)
|
2017-11-27 06:17:51 +00:00
|
|
|
config->quote_dirs.insert(normalized_arg);
|
2017-10-18 08:24:52 +00:00
|
|
|
if (add_next_flag_to_angle_dirs)
|
2017-11-27 06:17:51 +00:00
|
|
|
config->angle_dirs.insert(normalized_arg);
|
2018-03-11 17:08:41 +00:00
|
|
|
if (clang_cl)
|
|
|
|
arg = normalized_arg;
|
2017-09-22 06:50:27 +00:00
|
|
|
|
2017-10-18 08:24:52 +00:00
|
|
|
next_flag_is_path = false;
|
|
|
|
add_next_flag_to_quote_dirs = false;
|
|
|
|
add_next_flag_to_angle_dirs = false;
|
2018-02-20 01:19:50 +00:00
|
|
|
} else {
|
2018-01-05 21:41:38 +00:00
|
|
|
// Check to see if arg is a path and needs to be updated.
|
|
|
|
for (const std::string& flag_type : kPathArgs) {
|
|
|
|
// {"-I", "foo"} style.
|
|
|
|
if (arg == flag_type) {
|
|
|
|
next_flag_is_path = true;
|
|
|
|
add_next_flag_to_quote_dirs = ShouldAddToQuoteIncludes(arg);
|
|
|
|
add_next_flag_to_angle_dirs = ShouldAddToAngleIncludes(arg);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-01-05 21:41:38 +00:00
|
|
|
// {"-Ifoo"} style.
|
|
|
|
if (StartsWith(arg, flag_type)) {
|
|
|
|
std::string path = arg.substr(flag_type.size());
|
|
|
|
assert(!path.empty());
|
2018-03-31 20:59:27 +00:00
|
|
|
path = entry.ResolveIfRelative(path);
|
2018-03-11 17:08:41 +00:00
|
|
|
if (clang_cl || StartsWithAny(arg, kNormalizePathArgs))
|
2018-01-05 21:41:38 +00:00
|
|
|
arg = flag_type + path;
|
|
|
|
if (ShouldAddToQuoteIncludes(flag_type))
|
|
|
|
config->quote_dirs.insert(path);
|
|
|
|
if (ShouldAddToAngleIncludes(flag_type))
|
|
|
|
config->angle_dirs.insert(path);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-26 04:03:22 +00:00
|
|
|
}
|
2018-01-11 02:33:20 +00:00
|
|
|
|
2018-02-28 07:08:23 +00:00
|
|
|
// 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.
|
2018-01-11 02:33:20 +00:00
|
|
|
if (EndsWith(arg, base_name))
|
2018-03-31 20:59:27 +00:00
|
|
|
arg = entry.ResolveIfRelative(arg);
|
2018-02-07 18:27:30 +00:00
|
|
|
// TODO Exclude .a .o to make link command in compile_commands.json work.
|
|
|
|
// Also, clang_parseTranslationUnit2FullArgv does not seem to accept
|
|
|
|
// multiple source filenames.
|
|
|
|
else if (EndsWith(arg, ".a") || EndsWith(arg, ".o"))
|
|
|
|
continue;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.args.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// We don't do any special processing on user-given extra flags.
|
2017-09-22 03:09:11 +00:00
|
|
|
for (const auto& flag : config->extra_flags)
|
2017-04-26 04:03:22 +00:00
|
|
|
result.args.push_back(flag);
|
|
|
|
|
2017-10-25 01:02:15 +00:00
|
|
|
// Add -resource-dir so clang can correctly resolve system includes like
|
|
|
|
// <cstddef>
|
|
|
|
if (!AnyStartsWith(result.args, "-resource-dir"))
|
2018-03-31 16:25:58 +00:00
|
|
|
result.args.push_back("-resource-dir=" + init_opts->resourceDirectory);
|
2017-10-18 08:24:52 +00:00
|
|
|
|
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.
|
2017-12-01 18:11:09 +00:00
|
|
|
if (!AnyStartsWith(result.args, "-Wno-unknown-warning-option"))
|
|
|
|
result.args.push_back("-Wno-unknown-warning-option");
|
|
|
|
|
2018-02-14 05:23:35 +00:00
|
|
|
// Using -fparse-all-comments enables documentation in the indexer and in
|
2017-12-27 15:56:17 +00:00
|
|
|
// code completion.
|
2018-01-21 06:55:29 +00:00
|
|
|
if (init_opts->index.comments > 1 &&
|
2018-01-11 02:43:01 +00:00
|
|
|
!AnyStartsWith(result.args, "-fparse-all-comments")) {
|
2017-12-27 15:56:17 +00:00
|
|
|
result.args.push_back("-fparse-all-comments");
|
2018-01-11 02:33:20 +00:00
|
|
|
}
|
2017-12-27 15:56:17 +00:00
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-03 21:16:38 +00:00
|
|
|
std::vector<std::string> ReadCompilerArgumentsFromFile(
|
|
|
|
const std::string& path) {
|
2017-04-17 07:06:01 +00:00
|
|
|
std::vector<std::string> args;
|
2018-03-31 17:37:03 +00:00
|
|
|
for (std::string line : ReadFileLines(path)) {
|
2017-12-22 16:48:12 +00:00
|
|
|
TrimInPlace(line);
|
2017-04-17 07:06:01 +00:00
|
|
|
if (line.empty() || StartsWith(line, "#"))
|
|
|
|
continue;
|
|
|
|
args.push_back(line);
|
|
|
|
}
|
2018-02-03 21:16:38 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Project::Entry> LoadFromDirectoryListing(Config* init_opts,
|
|
|
|
ProjectConfig* config) {
|
|
|
|
std::vector<Project::Entry> result;
|
2018-03-31 03:16:33 +00:00
|
|
|
config->mode = ProjectMode::DotCcls;
|
|
|
|
LOG_IF_S(WARNING, !FileExists(config->project_dir + "/.ccls") &&
|
2018-01-07 04:26:22 +00:00
|
|
|
config->extra_flags.empty())
|
2018-03-31 03:16:33 +00:00
|
|
|
<< "ccls has no clang arguments. Considering adding either a "
|
|
|
|
"compile_commands.json or .ccls file. See the ccls README for "
|
2018-01-07 04:26:22 +00:00
|
|
|
"more information.";
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-02-03 21:16:38 +00:00
|
|
|
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);
|
2018-03-31 03:16:33 +00:00
|
|
|
} else if (GetBaseName(path) == ".ccls") {
|
|
|
|
LOG_S(INFO) << "Using .ccls arguments from " << path;
|
2018-02-22 07:34:32 +00:00
|
|
|
folder_args.emplace(GetDirName(path),
|
|
|
|
ReadCompilerArgumentsFromFile(path));
|
|
|
|
}
|
|
|
|
});
|
2018-02-03 21:16:38 +00:00
|
|
|
|
|
|
|
const auto& project_dir_args = folder_args[config->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);
|
2018-02-03 21:16:38 +00:00
|
|
|
|
|
|
|
auto GetCompilerArgumentForFile = [&config,
|
|
|
|
&folder_args](const std::string& path) {
|
2018-03-11 03:48:40 +00:00
|
|
|
for (std::string cur = GetDirName(path);; cur = GetDirName(cur)) {
|
|
|
|
auto it = folder_args.find(cur);
|
|
|
|
if (it != folder_args.end())
|
|
|
|
return it->second;
|
2018-03-03 22:43:51 +00:00
|
|
|
std::string normalized = NormalizePath(cur);
|
2018-03-11 03:48:40 +00:00
|
|
|
// Break if outside of the project root.
|
2018-03-03 22:43:51 +00:00
|
|
|
if (normalized.size() <= config->project_dir.size() ||
|
|
|
|
normalized.compare(0, config->project_dir.size(),
|
|
|
|
config->project_dir) != 0)
|
|
|
|
break;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2018-02-03 21:16:38 +00:00
|
|
|
return folder_args[config->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())
|
2018-03-11 17:08:41 +00:00
|
|
|
e.args.push_back("%clang"); // Add a Dummy.
|
2018-02-03 21:16:38 +00:00
|
|
|
e.args.push_back(e.file);
|
|
|
|
result.push_back(
|
|
|
|
GetCompilationEntryFromCompileCommandEntry(init_opts, config, e));
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:37:53 +00:00
|
|
|
std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
|
2018-03-11 22:12:26 +00:00
|
|
|
Config* config,
|
|
|
|
ProjectConfig* project,
|
2017-11-22 07:24:39 +00:00
|
|
|
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.
|
|
|
|
if (FileExists(project->project_dir + ".ccls"))
|
2018-03-11 22:12:26 +00:00
|
|
|
return LoadFromDirectoryListing(config, project);
|
2018-01-07 04:26:22 +00:00
|
|
|
|
2018-02-20 00:19:57 +00:00
|
|
|
// If |compilationDatabaseCommand| is specified, execute it to get the compdb.
|
|
|
|
std::string comp_db_dir;
|
2018-03-11 22:12:26 +00:00
|
|
|
if (config->compilationDatabaseCommand.empty()) {
|
|
|
|
project->mode = ProjectMode::CompileCommandsJson;
|
2018-02-20 00:19:57 +00:00
|
|
|
// Try to load compile_commands.json, but fallback to a project listing.
|
2018-03-11 22:12:26 +00:00
|
|
|
comp_db_dir = opt_compilation_db_dir.empty() ? project->project_dir
|
2018-02-20 00:19:57 +00:00
|
|
|
: opt_compilation_db_dir;
|
|
|
|
} else {
|
2018-03-11 22:12:26 +00:00
|
|
|
project->mode = ProjectMode::ExternalCommand;
|
2018-02-20 00:19:57 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// TODO
|
|
|
|
#else
|
2018-03-31 03:16:33 +00:00
|
|
|
char tmpdir[] = "/tmp/ccls-compdb-XXXXXX";
|
2018-02-20 00:19:57 +00:00
|
|
|
if (!mkdtemp(tmpdir))
|
|
|
|
return {};
|
|
|
|
comp_db_dir = tmpdir;
|
|
|
|
rapidjson::StringBuffer input;
|
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(input);
|
|
|
|
JsonWriter json_writer(&writer);
|
2018-03-11 22:12:26 +00:00
|
|
|
Reflect(json_writer, *config);
|
2018-02-20 00:19:57 +00:00
|
|
|
std::string contents = GetExternalCommandOutput(
|
2018-03-11 22:12:26 +00:00
|
|
|
std::vector<std::string>{config->compilationDatabaseCommand,
|
|
|
|
project->project_dir},
|
2018-02-20 00:19:57 +00:00
|
|
|
input.GetString());
|
|
|
|
std::ofstream(comp_db_dir + "/compile_commands.json") << contents;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-22 03:09:11 +00:00
|
|
|
LOG_S(INFO) << "Trying to load compile_commands.json";
|
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(
|
2018-02-20 00:19:57 +00:00
|
|
|
comp_db_dir.c_str(), &cx_db_load_error);
|
2018-03-11 22:12:26 +00:00
|
|
|
if (!config->compilationDatabaseCommand.empty()) {
|
2018-02-20 00:19:57 +00:00
|
|
|
#ifdef _WIN32
|
2018-03-20 02:51:42 +00:00
|
|
|
// TODO
|
2018-02-20 00:19:57 +00:00
|
|
|
#else
|
|
|
|
unlink((comp_db_dir + "/compile_commands.json").c_str());
|
|
|
|
rmdir(comp_db_dir.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:21:52 +00:00
|
|
|
if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) {
|
2017-09-22 03:09:11 +00:00
|
|
|
LOG_S(INFO) << "Unable to load compile_commands.json located at \""
|
2018-02-20 00:19:57 +00:00
|
|
|
<< comp_db_dir << "\"; using directory listing instead.";
|
2018-03-11 22:12:26 +00:00
|
|
|
return LoadFromDirectoryListing(config, project);
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
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 =
|
2017-11-04 22:29:03 +00:00
|
|
|
ToString(clang_CompileCommand_getDirectory(cx_command));
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string relative_filename =
|
2017-11-04 22:29:03 +00:00
|
|
|
ToString(clang_CompileCommand_getFilename(cx_command));
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2017-04-20 04:57:44 +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(
|
2017-11-04 22:29:03 +00:00
|
|
|
ToString(clang_CompileCommand_getArg(cx_command, j)));
|
2017-11-19 22:46:05 +00:00
|
|
|
}
|
2017-11-11 19:41:09 +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();
|
2017-11-27 06:17:51 +00:00
|
|
|
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-03-11 22:12:26 +00:00
|
|
|
GetCompilationEntryFromCompileCommandEntry(config, 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;
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
|
|
|
|
// 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, '/'));
|
2017-05-07 05:36:29 +00:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:21:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-03-11 22:12:26 +00:00
|
|
|
void Project::Load(Config* config, const std::string& root_directory) {
|
2017-09-22 03:09:11 +00:00
|
|
|
// Load data.
|
2018-03-11 22:12:26 +00:00
|
|
|
ProjectConfig project;
|
|
|
|
project.extra_flags = config->extraClangArguments;
|
|
|
|
project.project_dir = root_directory;
|
|
|
|
entries = LoadCompilationEntriesFromDirectory(
|
|
|
|
config, &project, config->compilationDatabaseDirectory);
|
2017-09-22 03:09:11 +00:00
|
|
|
|
|
|
|
// Cleanup / postprocess include directories.
|
2018-03-11 22:12:26 +00:00
|
|
|
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-04-20 05:46:10 +00:00
|
|
|
|
2017-09-22 03:09:11 +00:00
|
|
|
// Setup project entries.
|
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)
|
2017-04-20 05:46:10 +00:00
|
|
|
absolute_path_to_entry_index_[entries[i].filename] = i;
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:49:28 +00:00
|
|
|
void Project::SetFlagsForFile(
|
|
|
|
const std::vector<std::string>& flags,
|
|
|
|
const std::string& path) {
|
|
|
|
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) {
|
2017-04-20 05:46:10 +00:00
|
|
|
auto it = absolute_path_to_entry_index_.find(filename);
|
|
|
|
if (it != absolute_path_to_entry_index_.end())
|
|
|
|
return entries[it->second];
|
|
|
|
|
2017-05-07 05:36:29 +00:00
|
|
|
// 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;
|
2017-06-20 15:17:23 +00:00
|
|
|
int best_score = std::numeric_limits<int>::min();
|
2017-05-07 05:36:29 +00:00
|
|
|
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) {
|
2018-02-24 22:51:51 +00:00
|
|
|
result.args.push_back("%clang");
|
2018-01-16 01:30:26 +00:00
|
|
|
result.args.push_back(filename);
|
2018-01-16 01:38:28 +00:00
|
|
|
} else {
|
2017-05-07 05:36:29 +00:00
|
|
|
result.args = best_entry->args;
|
2018-01-11 05:27:58 +00:00
|
|
|
|
2018-01-16 01:30:26 +00:00
|
|
|
// |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 = GetBaseName(best_entry->filename);
|
|
|
|
for (std::string& arg : result.args) {
|
|
|
|
if (arg == best_entry->filename ||
|
|
|
|
GetBaseName(arg) == best_entry_base_name) {
|
|
|
|
arg = filename;
|
|
|
|
}
|
2018-01-11 05:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 05:36:29 +00:00
|
|
|
return result;
|
2017-04-21 04:50:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void Project::ForAllFilteredFiles(
|
|
|
|
Config* config,
|
|
|
|
std::function<void(int i, const Entry& entry)> action) {
|
2018-02-22 05:50:36 +00:00
|
|
|
GroupMatch matcher(config->index.whitelist, config->index.blacklist);
|
2017-04-21 04:50:31 +00:00
|
|
|
for (int i = 0; i < entries.size(); ++i) {
|
|
|
|
const Project::Entry& entry = entries[i];
|
2017-05-21 19:51:15 +00:00
|
|
|
std::string failure_reason;
|
|
|
|
if (matcher.IsMatch(entry.filename, &failure_reason))
|
|
|
|
action(i, entries[i]);
|
|
|
|
else {
|
2018-02-22 05:50:36 +00:00
|
|
|
if (config->index.logSkippedPaths) {
|
2017-09-22 01:14:57 +00:00
|
|
|
LOG_S(INFO) << "[" << i + 1 << "/" << entries.size() << "]: Failed "
|
|
|
|
<< failure_reason << "; skipping " << entry.filename;
|
2017-07-30 04:46:21 +00:00
|
|
|
}
|
2017-04-21 04:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
|
2018-03-11 22:12:26 +00:00
|
|
|
void Project::Index(Config* config,
|
|
|
|
QueueManager* queue,
|
|
|
|
WorkingFiles* wfiles,
|
|
|
|
lsRequestId id) {
|
|
|
|
ForAllFilteredFiles(config, [&](int i, const Project::Entry& entry) {
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<std::string> content = ReadContent(entry.filename);
|
2018-03-11 22:12:26 +00:00
|
|
|
if (!content) {
|
|
|
|
LOG_S(ERROR) << "When loading project, canont read file "
|
|
|
|
<< entry.filename;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_interactive = wfiles->GetFileByFilename(entry.filename) != nullptr;
|
|
|
|
queue->index_request.PushBack(
|
|
|
|
Index_Request(entry.filename, entry.args, is_interactive, *content,
|
|
|
|
ICacheManager::Make(config), id));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-03-11 22:12:26 +00:00
|
|
|
Config config;
|
|
|
|
ProjectConfig project;
|
|
|
|
project.project_dir = "/w/c/s/";
|
2018-03-31 16:25:58 +00:00
|
|
|
config.resourceDirectory = "/w/resource_dir/";
|
2017-10-18 08:24:52 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
CompileCommandsEntry entry;
|
|
|
|
entry.directory = directory;
|
|
|
|
entry.args = raw;
|
|
|
|
entry.file = file;
|
|
|
|
Project::Entry result =
|
2018-03-11 22:12:26 +00:00
|
|
|
GetCompilationEntryFromCompileCommandEntry(&config, &project, entry);
|
2017-11-19 18:05:06 +00:00
|
|
|
|
2017-11-19 22:46:05 +00:00
|
|
|
if (result.args != expected) {
|
|
|
|
std::cout << "Raw: " << StringJoin(raw) << std::endl;
|
|
|
|
std::cout << "Expected: " << StringJoin(expected) << std::endl;
|
|
|
|
std::cout << "Actual: " << StringJoin(result.args) << std::endl;
|
|
|
|
}
|
2017-11-19 18:05:06 +00:00
|
|
|
for (int i = 0; i < std::min(result.args.size(), expected.size()); ++i) {
|
|
|
|
if (result.args[i] != expected[i]) {
|
2018-01-05 21:41:38 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "mismatch at " << i << std::endl;
|
|
|
|
std::cout << " expected: " << expected[i] << std::endl;
|
|
|
|
std::cout << " actual: " << result.args[i] << std::endl;
|
2017-10-18 08:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
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-10-18 08:24:52 +00:00
|
|
|
|
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 */
|
2018-03-31 20:59:27 +00:00
|
|
|
{"clang", "-working-directory=/dir/", "-lstdc++", "/dir/myfile.cc",
|
2018-02-22 07:34:32 +00:00
|
|
|
"-resource-dir=/w/resource_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 */
|
2018-02-22 16:50:49 +00:00
|
|
|
{"clang.exe", "-working-directory=/dir/",
|
2018-01-30 00:27:43 +00:00
|
|
|
"-resource-dir=/w/resource_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",
|
2018-03-11 17:08:41 +00:00
|
|
|
"-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",
|
2018-03-11 17:08:41 +00:00
|
|
|
"-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",
|
2018-03-11 17:08:41 +00:00
|
|
|
"-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") {
|
2018-02-22 07:34:32 +00:00
|
|
|
CheckFlags("/home/user", "/home/user/foo/bar.c",
|
|
|
|
/* raw */ {"cc", "-O0", "foo/bar.c"},
|
|
|
|
/* expected */
|
2018-02-22 16:50:49 +00:00
|
|
|
{"cc", "-working-directory=/home/user", "-O0",
|
2018-03-31 20:59:27 +00:00
|
|
|
"/home/user/foo/bar.c", "-resource-dir=/w/resource_dir/",
|
2018-02-22 07:34:32 +00:00
|
|
|
"-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-01-11 02:33:20 +00:00
|
|
|
Config init_opts;
|
2017-11-19 18:05:06 +00:00
|
|
|
ProjectConfig config;
|
|
|
|
config.project_dir = "/w/c/s/";
|
2017-09-22 03:02:48 +00:00
|
|
|
|
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",
|
|
|
|
"-I",
|
|
|
|
"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-01-11 02:33:20 +00:00
|
|
|
GetCompilationEntryFromCompileCommandEntry(&init_opts, &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-03-31 20:59:27 +00:00
|
|
|
"/q_absolute1", "/q_absolute2", "/base/q_relative1",
|
|
|
|
"/base/q_relative2"};
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(config.angle_dirs == angle_expected);
|
|
|
|
REQUIRE(config.quote_dirs == quote_expected);
|
2017-05-07 05:36:29 +00:00
|
|
|
}
|
|
|
|
|
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-05-07 05:36:29 +00:00
|
|
|
|
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-05-07 05:36:29 +00:00
|
|
|
|
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-05-07 05:36:29 +00:00
|
|
|
|
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"});
|
|
|
|
}
|
2017-06-20 02:09:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 05:27:58 +00:00
|
|
|
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");
|
2018-01-11 05:27:58 +00:00
|
|
|
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-06-20 02:09:15 +00:00
|
|
|
|
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"});
|
|
|
|
}
|
2017-06-20 02:09:15 +00:00
|
|
|
}
|
|
|
|
}
|