2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "project.hh"
|
2017-03-26 21:40:34 +00:00
|
|
|
|
2018-03-31 20:59:27 +00:00
|
|
|
#include "filesystem.hh"
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
2017-09-22 01:14:57 +00:00
|
|
|
#include "match.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-08-09 17:08:14 +00:00
|
|
|
#include "platform.h"
|
2018-02-22 21:50:46 +00:00
|
|
|
#include "serializers/json.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
|
|
|
|
2018-06-03 07:29:33 +00:00
|
|
|
#include <clang/Driver/Compilation.h>
|
|
|
|
#include <clang/Driver/Driver.h>
|
2018-10-14 06:22:29 +00:00
|
|
|
#include <clang/Driver/Types.h>
|
2018-06-03 07:29:33 +00:00
|
|
|
#include <clang/Frontend/CompilerInstance.h>
|
2018-07-13 16:36:02 +00:00
|
|
|
#include <clang/Tooling/CompilationDatabase.h>
|
2018-09-04 20:02:48 +00:00
|
|
|
#include <llvm/ADT/STLExtras.h>
|
2018-07-27 07:21:57 +00:00
|
|
|
#include <llvm/ADT/StringSet.h>
|
2018-05-15 05:13:18 +00:00
|
|
|
#include <llvm/Support/LineIterator.h>
|
2018-05-12 20:31:56 +00:00
|
|
|
|
2018-02-22 21:50:46 +00:00
|
|
|
#include <rapidjson/writer.h>
|
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
|
|
|
|
|
2018-09-23 19:10:40 +00:00
|
|
|
#include <limits.h>
|
2017-05-21 19:51:15 +00:00
|
|
|
#include <unordered_set>
|
2017-04-17 07:06:01 +00:00
|
|
|
#include <vector>
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2018-09-23 19:10:40 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-10-14 06:22:29 +00:00
|
|
|
std::pair<LanguageId, bool> lookupExtension(std::string_view filename) {
|
|
|
|
using namespace clang::driver;
|
|
|
|
auto I = types::lookupTypeForExtension(
|
|
|
|
sys::path::extension({filename.data(), filename.size()}).substr(1));
|
|
|
|
bool header = I == types::TY_CHeader || I == types::TY_CXXHeader ||
|
|
|
|
I == types::TY_ObjCXXHeader;
|
|
|
|
bool objc = types::isObjC(I);
|
|
|
|
LanguageId ret;
|
|
|
|
if (types::isCXX(I))
|
|
|
|
ret = objc ? LanguageId::ObjCpp : LanguageId::Cpp;
|
|
|
|
else if (objc)
|
|
|
|
ret = LanguageId::ObjC;
|
|
|
|
else if (I == types::TY_C || I == types::TY_CHeader)
|
|
|
|
ret = LanguageId::C;
|
|
|
|
else
|
|
|
|
ret = LanguageId::Unknown;
|
|
|
|
return {ret, header};
|
|
|
|
}
|
|
|
|
|
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;
|
2018-10-08 05:02:28 +00:00
|
|
|
std::string root;
|
2018-02-20 02:15:43 +00:00
|
|
|
ProjectMode mode = ProjectMode::CompileCommandsJson;
|
2017-09-22 03:02:48 +00:00
|
|
|
};
|
|
|
|
|
2018-05-12 20:31:56 +00:00
|
|
|
enum OptionClass {
|
|
|
|
EqOrJoinOrSep,
|
|
|
|
EqOrSep,
|
|
|
|
JoinOrSep,
|
|
|
|
Separate,
|
2017-04-26 04:03:22 +00:00
|
|
|
};
|
|
|
|
|
2018-08-18 05:20:51 +00:00
|
|
|
struct ProjectProcessor {
|
|
|
|
ProjectConfig *config;
|
|
|
|
std::unordered_set<size_t> command_set;
|
|
|
|
ProjectProcessor(ProjectConfig *config) : config(config) {}
|
|
|
|
|
|
|
|
void Process(Project::Entry &entry) {
|
|
|
|
const std::string base_name = sys::path::filename(entry.filename);
|
|
|
|
|
|
|
|
// Expand %c %cpp %clang
|
2018-09-19 16:31:45 +00:00
|
|
|
std::vector<const char *> args;
|
|
|
|
args.reserve(entry.args.size() + g_config->clang.extraArgs.size() + 1);
|
2018-10-14 06:22:29 +00:00
|
|
|
const LanguageId lang = lookupExtension(entry.filename).first;
|
2018-09-19 16:31:45 +00:00
|
|
|
for (const char *arg : entry.args) {
|
|
|
|
if (strncmp(arg, "%c ", 3) == 0) {
|
2018-08-18 05:20:51 +00:00
|
|
|
if (lang == LanguageId::C)
|
2018-09-19 16:31:45 +00:00
|
|
|
args.push_back(arg + 3);
|
|
|
|
} else if (strncmp(arg, "%cpp ", 5) == 0) {
|
2018-08-18 05:20:51 +00:00
|
|
|
if (lang == LanguageId::Cpp)
|
2018-09-19 16:31:45 +00:00
|
|
|
args.push_back(arg + 5);
|
|
|
|
} else if (strcmp(arg, "%clang") == 0) {
|
2018-08-18 05:20:51 +00:00
|
|
|
args.push_back(lang == LanguageId::Cpp ? "clang++" : "clang");
|
2018-09-04 20:02:48 +00:00
|
|
|
} else if (!llvm::is_contained(g_config->clang.excludeArgs, arg)) {
|
2018-08-18 05:20:51 +00:00
|
|
|
args.push_back(arg);
|
|
|
|
}
|
2018-02-24 22:51:51 +00:00
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
if (args.empty())
|
|
|
|
return;
|
2018-09-19 16:31:45 +00:00
|
|
|
for (const std::string &arg : g_config->clang.extraArgs)
|
|
|
|
args.push_back(Intern(arg));
|
2018-08-18 05:20:51 +00:00
|
|
|
|
|
|
|
size_t hash = std::hash<std::string>{}(entry.directory);
|
|
|
|
for (auto &arg : args) {
|
|
|
|
if (arg[0] != '-' && EndsWith(arg, base_name)) {
|
2018-10-14 06:22:29 +00:00
|
|
|
LanguageId lang = lookupExtension(arg).first;
|
2018-08-18 05:20:51 +00:00
|
|
|
if (lang != LanguageId::Unknown) {
|
|
|
|
hash_combine(hash, size_t(lang));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash_combine(hash, std::hash<std::string>{}(arg));
|
|
|
|
}
|
2018-09-19 16:31:45 +00:00
|
|
|
args.push_back(Intern("-working-directory=" + entry.directory));
|
2018-08-18 05:20:51 +00:00
|
|
|
|
|
|
|
if (!command_set.insert(hash).second) {
|
|
|
|
entry.args = std::move(args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// a weird C++ deduction guide heap-use-after-free causes libclang to crash.
|
|
|
|
IgnoringDiagConsumer DiagC;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
|
|
|
|
DiagnosticsEngine Diags(
|
2018-06-03 07:29:33 +00:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
|
|
|
|
&DiagC, false);
|
|
|
|
|
2018-08-18 05:20:51 +00:00
|
|
|
driver::Driver Driver(args[0], llvm::sys::getDefaultTargetTriple(), Diags);
|
|
|
|
auto TargetAndMode =
|
2018-06-03 07:29:33 +00:00
|
|
|
driver::ToolChain::getTargetAndModeFromProgramName(args[0]);
|
2018-08-18 05:20:51 +00:00
|
|
|
if (!TargetAndMode.TargetPrefix.empty()) {
|
|
|
|
const char *arr[] = {"-target", TargetAndMode.TargetPrefix.c_str()};
|
|
|
|
args.insert(args.begin() + 1, std::begin(arr), std::end(arr));
|
|
|
|
Driver.setTargetAndMode(TargetAndMode);
|
2018-06-03 07:29:33 +00:00
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
Driver.setCheckInputsExist(false);
|
|
|
|
|
2018-09-19 16:31:45 +00:00
|
|
|
args.push_back("-fsyntax-only");
|
2018-08-18 05:20:51 +00:00
|
|
|
|
2018-09-19 16:31:45 +00:00
|
|
|
std::unique_ptr<driver::Compilation> C(Driver.BuildCompilation(args));
|
2018-08-18 05:20:51 +00:00
|
|
|
const driver::JobList &Jobs = C->getJobs();
|
|
|
|
if (Jobs.size() != 1)
|
|
|
|
return;
|
2018-10-12 23:36:07 +00:00
|
|
|
const auto &CCArgs = Jobs.begin()->getArguments();
|
2018-08-18 05:20:51 +00:00
|
|
|
|
|
|
|
auto CI = std::make_unique<CompilerInvocation>();
|
|
|
|
CompilerInvocation::CreateFromArgs(*CI, CCArgs.data(),
|
|
|
|
CCArgs.data() + CCArgs.size(), Diags);
|
|
|
|
CI->getFrontendOpts().DisableFree = false;
|
|
|
|
CI->getCodeGenOpts().DisableFree = false;
|
|
|
|
|
|
|
|
HeaderSearchOptions &HeaderOpts = CI->getHeaderSearchOpts();
|
|
|
|
for (auto &E : HeaderOpts.UserEntries) {
|
2018-09-11 19:13:54 +00:00
|
|
|
std::string path =
|
|
|
|
NormalizePath(ResolveIfRelative(entry.directory, E.Path));
|
2018-08-18 05:20:51 +00:00
|
|
|
switch (E.Group) {
|
|
|
|
default:
|
|
|
|
config->angle_dirs.insert(path);
|
|
|
|
break;
|
|
|
|
case frontend::Quoted:
|
|
|
|
config->quote_dirs.insert(path);
|
|
|
|
break;
|
|
|
|
case frontend::Angled:
|
|
|
|
config->angle_dirs.insert(path);
|
|
|
|
config->quote_dirs.insert(path);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
entry.args = std::move(args);
|
|
|
|
}
|
|
|
|
};
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-09-19 16:31:45 +00:00
|
|
|
std::vector<const char *>
|
2018-08-09 17:08:14 +00:00
|
|
|
ReadCompilerArgumentsFromFile(const std::string &path) {
|
2018-05-15 05:13:18 +00:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(path);
|
2018-08-09 17:08:14 +00:00
|
|
|
if (!MBOrErr)
|
|
|
|
return {};
|
2018-09-19 16:31:45 +00:00
|
|
|
std::vector<const char *> args;
|
2018-09-16 18:07:15 +00:00
|
|
|
for (line_iterator I(*MBOrErr.get(), true, '#'), E; I != E; ++I) {
|
2018-09-19 16:31:45 +00:00
|
|
|
std::string line = *I;
|
|
|
|
DoPathMapping(line);
|
|
|
|
args.push_back(Intern(line));
|
2018-09-16 18:07:15 +00:00
|
|
|
}
|
2018-02-03 21:16:38 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig *config) {
|
2018-02-03 21:16:38 +00:00
|
|
|
std::vector<Project::Entry> result;
|
2018-03-31 03:16:33 +00:00
|
|
|
config->mode = ProjectMode::DotCcls;
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-09-19 16:31:45 +00:00
|
|
|
std::unordered_map<std::string, std::vector<const char *>> folder_args;
|
2018-02-03 21:16:38 +00:00
|
|
|
std::vector<std::string> files;
|
2018-10-08 05:02:28 +00:00
|
|
|
const std::string &root = config->root;
|
2018-02-03 21:16:38 +00:00
|
|
|
|
2018-10-08 05:02:28 +00:00
|
|
|
GetFilesInFolder(root, true /*recursive*/,
|
2018-02-22 07:34:32 +00:00
|
|
|
true /*add_folder_to_path*/,
|
2018-08-09 17:08:14 +00:00
|
|
|
[&folder_args, &files](const std::string &path) {
|
2018-10-14 06:22:29 +00:00
|
|
|
std::pair<LanguageId, bool> lang = lookupExtension(path);
|
|
|
|
if (lang.first != LanguageId::Unknown && !lang.second) {
|
2018-02-22 07:34:32 +00:00
|
|
|
files.push_back(path);
|
2018-05-13 20:30:24 +00:00
|
|
|
} else if (sys::path::filename(path) == ".ccls") {
|
2018-10-01 07:39:44 +00:00
|
|
|
std::vector<const char *> args = ReadCompilerArgumentsFromFile(path);
|
|
|
|
folder_args.emplace(sys::path::parent_path(path), args);
|
|
|
|
std::string l;
|
|
|
|
for (size_t i = 0; i < args.size(); i++) {
|
|
|
|
if (i)
|
|
|
|
l += ' ';
|
|
|
|
l += args[i];
|
|
|
|
}
|
|
|
|
LOG_S(INFO) << "use " << path << ": " << l;
|
2018-02-22 07:34:32 +00:00
|
|
|
}
|
|
|
|
});
|
2018-02-03 21:16:38 +00:00
|
|
|
|
2018-10-08 05:02:28 +00:00
|
|
|
auto GetCompilerArgumentForFile = [&root,
|
2018-08-09 17:08:14 +00:00
|
|
|
&folder_args](std::string cur) {
|
2018-05-13 20:30:24 +00:00
|
|
|
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;
|
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-10-08 05:02:28 +00:00
|
|
|
if (normalized.size() <= root.size() ||
|
|
|
|
normalized.compare(0, root.size(), root) != 0)
|
2018-03-03 22:43:51 +00:00
|
|
|
break;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2018-10-08 05:02:28 +00:00
|
|
|
return folder_args[root];
|
2018-02-03 21:16:38 +00:00
|
|
|
};
|
|
|
|
|
2018-08-18 05:20:51 +00:00
|
|
|
ProjectProcessor proc(config);
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const std::string &file : files) {
|
2018-08-18 05:20:51 +00:00
|
|
|
Project::Entry e;
|
2018-10-08 05:02:28 +00:00
|
|
|
e.root = config->root;
|
|
|
|
e.directory = config->root;
|
2018-08-18 05:20:51 +00:00
|
|
|
e.filename = file;
|
2018-02-03 21:16:38 +00:00
|
|
|
e.args = GetCompilerArgumentForFile(file);
|
2018-02-23 10:43:15 +00:00
|
|
|
if (e.args.empty())
|
2018-08-09 17:08:14 +00:00
|
|
|
e.args.push_back("%clang"); // Add a Dummy.
|
2018-09-19 16:31:45 +00:00
|
|
|
e.args.push_back(Intern(e.filename));
|
2018-08-18 05:20:51 +00:00
|
|
|
proc.Process(e);
|
|
|
|
result.push_back(e);
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::vector<Project::Entry>
|
2018-08-18 05:20:51 +00:00
|
|
|
LoadEntriesFromDirectory(ProjectConfig *project,
|
|
|
|
const std::string &opt_compdb_dir) {
|
2018-03-31 03:16:33 +00:00
|
|
|
// If there is a .ccls file always load using directory listing.
|
2018-10-14 06:22:29 +00:00
|
|
|
SmallString<256> Path, CclsPath;
|
|
|
|
sys::path::append(CclsPath, project->root, ".ccls");
|
|
|
|
if (sys::fs::exists(CclsPath))
|
2018-04-04 06:05:41 +00:00
|
|
|
return LoadFromDirectoryListing(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.
|
2018-05-13 20:30:24 +00:00
|
|
|
std::string comp_db_dir;
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->compilationDatabaseCommand.empty()) {
|
2018-03-11 22:12:26 +00:00
|
|
|
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-10-08 05:02:28 +00:00
|
|
|
comp_db_dir = opt_compdb_dir.empty() ? project->root : opt_compdb_dir;
|
2018-05-13 20:30:24 +00:00
|
|
|
sys::path::append(Path, comp_db_dir, "compile_commands.json");
|
2018-02-20 00:19:57 +00:00
|
|
|
} 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;
|
2018-05-13 20:30:24 +00:00
|
|
|
sys::path::append(Path, comp_db_dir, "compile_commands.json");
|
2018-02-20 00:19:57 +00:00
|
|
|
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);
|
2018-02-20 00:19:57 +00:00
|
|
|
std::string contents = GetExternalCommandOutput(
|
2018-04-04 06:05:41 +00:00
|
|
|
std::vector<std::string>{g_config->compilationDatabaseCommand,
|
2018-10-08 05:02:28 +00:00
|
|
|
project->root},
|
2018-02-20 00:19:57 +00:00
|
|
|
input.GetString());
|
2018-08-09 17:08:14 +00:00
|
|
|
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);
|
2018-02-20 00:19:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-13 16:36:02 +00:00
|
|
|
std::string err_msg;
|
|
|
|
std::unique_ptr<tooling::CompilationDatabase> CDB =
|
|
|
|
tooling::CompilationDatabase::loadFromDirectory(comp_db_dir, err_msg);
|
2018-04-04 06:05:41 +00:00
|
|
|
if (!g_config->compilationDatabaseCommand.empty()) {
|
2018-02-20 00:19:57 +00:00
|
|
|
#ifdef _WIN32
|
2018-08-09 17:08:14 +00:00
|
|
|
// TODO
|
2018-02-20 00:19:57 +00:00
|
|
|
#else
|
2018-05-13 20:30:24 +00:00
|
|
|
unlink(Path.c_str());
|
2018-02-20 00:19:57 +00:00
|
|
|
rmdir(comp_db_dir.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-13 16:36:02 +00:00
|
|
|
if (!CDB) {
|
2018-10-14 06:22:29 +00:00
|
|
|
LOG_S(WARNING) << "no .ccls or compile_commands.json . Consider adding one";
|
|
|
|
return LoadFromDirectoryListing(project);
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 20:30:24 +00:00
|
|
|
LOG_S(INFO) << "loaded " << Path.c_str();
|
|
|
|
|
2018-07-27 07:21:57 +00:00
|
|
|
StringSet<> Seen;
|
2017-04-20 05:01:36 +00:00
|
|
|
std::vector<Project::Entry> result;
|
2018-08-18 05:20:51 +00:00
|
|
|
ProjectProcessor proc(project);
|
2018-07-13 16:36:02 +00:00
|
|
|
for (tooling::CompileCommand &Cmd : CDB->getAllCompileCommands()) {
|
2018-08-18 05:20:51 +00:00
|
|
|
Project::Entry entry;
|
2018-10-08 05:02:28 +00:00
|
|
|
entry.root = project->root;
|
|
|
|
DoPathMapping(entry.root);
|
2018-09-11 19:13:54 +00:00
|
|
|
entry.directory = NormalizePath(Cmd.Directory);
|
2018-09-16 18:07:15 +00:00
|
|
|
DoPathMapping(entry.directory);
|
2018-09-11 19:13:54 +00:00
|
|
|
entry.filename =
|
|
|
|
NormalizePath(ResolveIfRelative(entry.directory, Cmd.Filename));
|
2018-09-16 18:07:15 +00:00
|
|
|
DoPathMapping(entry.filename);
|
2018-09-19 16:31:45 +00:00
|
|
|
std::vector<std::string> args = std::move(Cmd.CommandLine);
|
|
|
|
entry.args.reserve(args.size());
|
|
|
|
for (std::string &arg : args) {
|
2018-09-16 18:07:15 +00:00
|
|
|
DoPathMapping(arg);
|
2018-09-19 16:31:45 +00:00
|
|
|
entry.args.push_back(Intern(arg));
|
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
proc.Process(entry);
|
|
|
|
if (Seen.insert(entry.filename).second)
|
|
|
|
result.push_back(entry);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|
2017-03-31 04:21:52 +00:00
|
|
|
|
2018-10-08 05:02:28 +00:00
|
|
|
void Project::Load(const std::string &root) {
|
|
|
|
assert(root.back() == '/');
|
2018-03-11 22:12:26 +00:00
|
|
|
ProjectConfig project;
|
2018-10-08 05:02:28 +00:00
|
|
|
project.root = root;
|
|
|
|
Folder &folder = root2folder[root];
|
|
|
|
|
|
|
|
folder.entries = LoadEntriesFromDirectory(
|
|
|
|
&project, g_config->compilationDatabaseDirectory);
|
|
|
|
folder.quote_search_list.assign(project.quote_dirs.begin(),
|
|
|
|
project.quote_dirs.end());
|
|
|
|
folder.angle_search_list.assign(project.angle_dirs.begin(),
|
|
|
|
project.angle_dirs.end());
|
|
|
|
for (std::string &path : folder.angle_search_list) {
|
2017-05-21 07:37:53 +00:00
|
|
|
EnsureEndsInSlash(path);
|
2018-10-08 05:02:28 +00:00
|
|
|
LOG_S(INFO) << "angle search: " << path;
|
2017-05-21 07:37:53 +00:00
|
|
|
}
|
2018-10-08 05:02:28 +00:00
|
|
|
for (std::string &path : folder.quote_search_list) {
|
2017-05-21 07:37:53 +00:00
|
|
|
EnsureEndsInSlash(path);
|
2018-10-08 05:02:28 +00:00
|
|
|
LOG_S(INFO) << "quote search: " << path;
|
2017-05-21 07:37:53 +00:00
|
|
|
}
|
2018-09-10 06:46:13 +00:00
|
|
|
|
|
|
|
// Setup project entries.
|
|
|
|
std::lock_guard lock(mutex_);
|
2018-10-08 05:02:28 +00:00
|
|
|
folder.path2entry_index.reserve(folder.entries.size());
|
|
|
|
for (size_t i = 0; i < folder.entries.size(); ++i) {
|
|
|
|
folder.entries[i].id = i;
|
|
|
|
folder.path2entry_index[folder.entries[i].filename] = i;
|
2018-09-10 06:46:13 +00:00
|
|
|
}
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 16:31:45 +00:00
|
|
|
void Project::SetArgsForFile(const std::vector<const char *> &args,
|
|
|
|
const std::string &path) {
|
2018-05-05 22:29:17 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2018-10-08 05:02:28 +00:00
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
auto it = folder.path2entry_index.find(path);
|
|
|
|
if (it != folder.path2entry_index.end()) {
|
|
|
|
// The entry already exists in the project, just set the flags.
|
|
|
|
folder.entries[it->second].args = args;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-21 19:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 05:54:48 +00:00
|
|
|
Project::Entry Project::FindEntry(const std::string &path,
|
|
|
|
bool can_be_inferred) {
|
2018-10-08 05:02:28 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
auto it = folder.path2entry_index.find(path);
|
|
|
|
if (it != folder.path2entry_index.end()) {
|
|
|
|
Project::Entry &entry = folder.entries[it->second];
|
2018-10-01 05:54:48 +00:00
|
|
|
if (can_be_inferred || entry.filename == path)
|
|
|
|
return entry;
|
|
|
|
}
|
2018-05-05 22:29:17 +00:00
|
|
|
}
|
2017-04-20 05:46:10 +00:00
|
|
|
|
2018-10-08 05:02:28 +00:00
|
|
|
Project::Entry result;
|
|
|
|
const Entry *best_entry = nullptr;
|
2018-09-23 19:10:40 +00:00
|
|
|
int best_score = INT_MIN;
|
2018-10-08 05:02:28 +00:00
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
for (const Entry &entry : folder.entries) {
|
|
|
|
int score = ComputeGuessScore(path, entry.filename);
|
|
|
|
if (score > best_score) {
|
|
|
|
best_score = score;
|
|
|
|
best_entry = &entry;
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
}
|
2018-10-08 05:02:28 +00:00
|
|
|
if (StartsWith(path, root))
|
|
|
|
result.root = root;
|
2017-05-07 05:36:29 +00:00
|
|
|
}
|
2018-10-08 05:02:28 +00:00
|
|
|
if (result.root.empty())
|
|
|
|
result.root = g_config->fallbackFolder;
|
2017-05-07 05:36:29 +00:00
|
|
|
|
|
|
|
result.is_inferred = true;
|
2018-10-01 05:54:48 +00:00
|
|
|
result.filename = path;
|
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-10-01 05:54:48 +00:00
|
|
|
result.args.push_back(Intern(path));
|
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.
|
2018-08-09 17:08:14 +00:00
|
|
|
std::string best_entry_base_name =
|
|
|
|
sys::path::filename(best_entry->filename);
|
2018-09-19 16:31:45 +00:00
|
|
|
for (const char *&arg : result.args) {
|
2018-04-08 00:10:54 +00:00
|
|
|
try {
|
|
|
|
if (arg == best_entry->filename ||
|
2018-05-13 20:30:24 +00:00
|
|
|
sys::path::filename(arg) == best_entry_base_name)
|
2018-10-01 05:54:48 +00:00
|
|
|
arg = Intern(path);
|
2018-04-08 00:10:54 +00:00
|
|
|
} catch (...) {
|
2018-01-16 01:30:26 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-09-24 01:45:41 +00:00
|
|
|
void Project::Index(WorkingFiles *wfiles, lsRequestId id) {
|
|
|
|
auto &gi = g_config->index;
|
|
|
|
GroupMatch match(gi.whitelist, gi.blacklist),
|
|
|
|
match_i(gi.initialWhitelist, gi.initialBlacklist);
|
2018-10-08 05:02:28 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex_);
|
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
int i = 0;
|
|
|
|
for (const Project::Entry &entry : folder.entries) {
|
|
|
|
std::string reason;
|
|
|
|
if (match.IsMatch(entry.filename, &reason) &&
|
|
|
|
match_i.IsMatch(entry.filename, &reason)) {
|
|
|
|
bool interactive =
|
|
|
|
wfiles->GetFileByFilename(entry.filename) != nullptr;
|
|
|
|
pipeline::Index(
|
|
|
|
entry.filename, entry.args,
|
|
|
|
interactive ? IndexMode::Normal : IndexMode::NonInteractive, id);
|
|
|
|
} else {
|
|
|
|
LOG_V(1) << "[" << i << "/" << folder.entries.size() << "]: " << reason
|
|
|
|
<< "; skip " << entry.filename;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2017-04-21 04:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
|
2018-09-18 01:03:59 +00:00
|
|
|
pipeline::loaded_ts = pipeline::tick;
|
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-09-08 06:40:22 +00:00
|
|
|
pipeline::Index("", {}, IndexMode::NonInteractive);
|
2018-03-11 22:12:26 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace ccls
|