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-11-08 19:32:27 +00:00
|
|
|
#include "clang_tu.hh" // llvm::vfs
|
2018-03-31 20:59:27 +00:00
|
|
|
#include "filesystem.hh"
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "platform.hh"
|
|
|
|
#include "utils.hh"
|
|
|
|
#include "working_files.hh"
|
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-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 {
|
2018-12-21 09:05:23 +00:00
|
|
|
Project::Folder &folder;
|
2018-08-18 05:20:51 +00:00
|
|
|
std::unordered_set<size_t> command_set;
|
2018-11-02 22:17:46 +00:00
|
|
|
StringSet<> excludeArgs;
|
2018-12-21 09:05:23 +00:00
|
|
|
ProjectProcessor(Project::Folder &folder) : folder(folder) {
|
2018-11-02 22:17:46 +00:00
|
|
|
for (auto &arg : g_config->clang.excludeArgs)
|
|
|
|
excludeArgs.insert(arg);
|
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
// Expand %c %cpp ... in .ccls
|
2018-08-18 05:20:51 +00:00
|
|
|
void Process(Project::Entry &entry) {
|
2018-12-21 09:05:23 +00:00
|
|
|
std::vector<const char *> args(entry.args.begin(),
|
|
|
|
entry.args.begin() + entry.compdb_size);
|
|
|
|
auto [lang, header] = lookupExtension(entry.filename);
|
|
|
|
for (int i = entry.compdb_size; i < entry.args.size(); i++) {
|
|
|
|
const char *arg = entry.args[i];
|
2018-11-20 07:14:49 +00:00
|
|
|
StringRef A(arg);
|
|
|
|
if (A[0] == '%') {
|
|
|
|
bool ok = false;
|
|
|
|
for (;;) {
|
|
|
|
if (A.consume_front("%c "))
|
|
|
|
ok |= lang == LanguageId::C;
|
2018-12-21 09:05:23 +00:00
|
|
|
else if (A.consume_front("%h "))
|
|
|
|
ok |= lang == LanguageId::C && header;
|
2018-11-20 07:14:49 +00:00
|
|
|
else if (A.consume_front("%cpp "))
|
|
|
|
ok |= lang == LanguageId::Cpp;
|
2018-12-21 09:05:23 +00:00
|
|
|
else if (A.consume_front("%hpp "))
|
|
|
|
ok |= lang == LanguageId::Cpp && header;
|
2018-11-20 07:14:49 +00:00
|
|
|
else if (A.consume_front("%objective-c "))
|
|
|
|
ok |= lang == LanguageId::ObjC;
|
|
|
|
else if (A.consume_front("%objective-cpp "))
|
|
|
|
ok |= lang == LanguageId::ObjCpp;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ok)
|
|
|
|
args.push_back(A.data());
|
|
|
|
} else if (!excludeArgs.count(A)) {
|
2018-08-18 05:20:51 +00:00
|
|
|
args.push_back(arg);
|
|
|
|
}
|
2018-02-24 22:51:51 +00:00
|
|
|
}
|
2018-12-21 09:05:23 +00:00
|
|
|
entry.args = args;
|
|
|
|
GetSearchDirs(entry);
|
|
|
|
}
|
2018-08-18 05:20:51 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
void GetSearchDirs(Project::Entry &entry) {
|
|
|
|
#if LLVM_VERSION_MAJOR < 8
|
|
|
|
const std::string base_name = sys::path::filename(entry.filename);
|
2018-08-18 05:20:51 +00:00
|
|
|
size_t hash = std::hash<std::string>{}(entry.directory);
|
2018-11-02 22:17:46 +00:00
|
|
|
bool OPT_o = false;
|
2018-12-21 09:05:23 +00:00
|
|
|
for (auto &arg : entry.args) {
|
2018-11-02 22:17:46 +00:00
|
|
|
bool last_o = OPT_o;
|
|
|
|
OPT_o = false;
|
|
|
|
if (arg[0] == '-') {
|
|
|
|
OPT_o = arg[1] == 'o' && arg[2] == '\0';
|
|
|
|
if (OPT_o || arg[1] == 'D' || arg[1] == 'W')
|
|
|
|
continue;
|
|
|
|
} else if (last_o) {
|
|
|
|
continue;
|
|
|
|
} else if (sys::path::filename(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) {
|
2018-11-02 22:17:46 +00:00
|
|
|
hash_combine(hash, (size_t)lang);
|
2018-08-18 05:20:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 22:17:46 +00:00
|
|
|
hash_combine(hash, std::hash<std::string_view>{}(arg));
|
2018-08-18 05:20:51 +00:00
|
|
|
}
|
2018-11-02 22:17:46 +00:00
|
|
|
if (!command_set.insert(hash).second)
|
2018-08-18 05:20:51 +00:00
|
|
|
return;
|
2018-12-21 09:05:23 +00:00
|
|
|
auto args = entry.args;
|
|
|
|
args.push_back("-fsyntax-only");
|
|
|
|
for (const std::string &arg : g_config->clang.extraArgs)
|
|
|
|
args.push_back(Intern(arg));
|
|
|
|
args.push_back(Intern("-working-directory=" + entry.directory));
|
|
|
|
args.push_back(Intern("-resource-dir=" + g_config->clang.resourceDir));
|
2018-08-18 05:20:51 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
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-12-21 09:05:23 +00:00
|
|
|
EnsureEndsInSlash(path);
|
2018-08-18 05:20:51 +00:00
|
|
|
switch (E.Group) {
|
|
|
|
default:
|
2018-12-21 09:05:23 +00:00
|
|
|
folder.search_dir2kind[path] |= 2;
|
2018-08-18 05:20:51 +00:00
|
|
|
break;
|
|
|
|
case frontend::Quoted:
|
2018-12-21 09:05:23 +00:00
|
|
|
folder.search_dir2kind[path] |= 1;
|
2018-08-18 05:20:51 +00:00
|
|
|
break;
|
|
|
|
case frontend::Angled:
|
2018-12-21 09:05:23 +00:00
|
|
|
folder.search_dir2kind[path] |= 3;
|
2018-08-18 05:20:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2018-11-09 05:18:41 +00:00
|
|
|
#endif
|
2018-08-18 05:20:51 +00:00
|
|
|
}
|
|
|
|
};
|
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-12-21 09:05:23 +00:00
|
|
|
bool AppendToCDB(const std::vector<const char *> &args) {
|
|
|
|
return args.size() && StringRef("%compile_commands.json") == args[0];
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
std::vector<const char *> GetFallback(const std::string path) {
|
|
|
|
std::vector<const char *> argv{"clang"};
|
|
|
|
if (sys::path::extension(path) == ".h")
|
|
|
|
argv.push_back("-xobjective-c++-header");
|
|
|
|
argv.push_back(Intern(path));
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadDirectoryListing(ProjectProcessor &proc, const std::string &root,
|
|
|
|
const StringSet<> &Seen) {
|
|
|
|
Project::Folder &folder = proc.folder;
|
2018-02-03 21:16:38 +00:00
|
|
|
std::vector<std::string> files;
|
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
auto GetDotCcls = [&root, &folder](std::string cur) {
|
|
|
|
while (!(cur = sys::path::parent_path(cur)).empty()) {
|
|
|
|
auto it = folder.dot_ccls.find(cur);
|
|
|
|
if (it != folder.dot_ccls.end())
|
|
|
|
return it->second;
|
|
|
|
std::string normalized = NormalizePath(cur);
|
|
|
|
// Break if outside of the project root.
|
|
|
|
if (normalized.size() <= root.size() ||
|
|
|
|
normalized.compare(0, root.size(), root) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return folder.dot_ccls[root];
|
|
|
|
};
|
|
|
|
|
|
|
|
GetFilesInFolder(root, true /*recursive*/, true /*add_folder_to_path*/,
|
|
|
|
[&folder, &files, &Seen](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-12-21 09:05:23 +00:00
|
|
|
if (!Seen.count(path))
|
|
|
|
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);
|
2018-12-21 09:05:23 +00:00
|
|
|
folder.dot_ccls.emplace(sys::path::parent_path(path),
|
|
|
|
args);
|
2018-10-01 07:39:44 +00:00
|
|
|
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-12-21 09:05:23 +00:00
|
|
|
// If the first line of .ccls is %compile_commands.json, append extra flags.
|
|
|
|
for (auto &e : folder.entries)
|
|
|
|
if (const auto &args = GetDotCcls(e.filename); AppendToCDB(args)) {
|
|
|
|
if (args.size())
|
|
|
|
e.args.insert(e.args.end(), args.begin() + 1, args.end());
|
|
|
|
proc.Process(e);
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2018-12-21 09:05:23 +00:00
|
|
|
// Set flags for files not in compile_commands.json
|
|
|
|
for (const std::string &file : files)
|
|
|
|
if (const auto &args = GetDotCcls(file); !AppendToCDB(args)) {
|
|
|
|
Project::Entry e;
|
|
|
|
e.root = e.directory = root;
|
|
|
|
e.filename = file;
|
|
|
|
if (args.empty()) {
|
|
|
|
e.args = GetFallback(e.filename);
|
|
|
|
} else {
|
|
|
|
e.args = args;
|
|
|
|
e.args.push_back(Intern(e.filename));
|
|
|
|
}
|
|
|
|
proc.Process(e);
|
|
|
|
folder.entries.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
// Computes a score based on how well |a| and |b| match. This is used for
|
|
|
|
// argument guessing.
|
|
|
|
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 < a.size())
|
|
|
|
score -= 100 * (std::count(a.begin() + i, a.end() - j, '/') +
|
|
|
|
std::count(b.begin() + i, b.end() - j, '/'));
|
|
|
|
return score;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Project::LoadDirectory(const std::string &root, Project::Folder &folder) {
|
|
|
|
SmallString<256> Path, CDBDir;
|
|
|
|
folder.entries.clear();
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->compilationDatabaseCommand.empty()) {
|
2018-12-21 09:05:23 +00:00
|
|
|
CDBDir = root;
|
|
|
|
if (g_config->compilationDatabaseDirectory.size())
|
|
|
|
sys::path::append(CDBDir, g_config->compilationDatabaseDirectory);
|
|
|
|
sys::path::append(Path, CDBDir, "compile_commands.json");
|
2018-02-20 00:19:57 +00:00
|
|
|
} else {
|
2018-12-21 09:05:23 +00:00
|
|
|
// If `compilationDatabaseCommand` is specified, execute it to get the
|
|
|
|
// compdb.
|
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))
|
2018-12-21 09:05:23 +00:00
|
|
|
return;
|
|
|
|
CDBDir = tmpdir;
|
|
|
|
sys::path::append(Path, CDBDir, "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-12-21 09:05:23 +00:00
|
|
|
std::vector<std::string>{g_config->compilationDatabaseCommand, 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 =
|
2018-12-21 09:05:23 +00:00
|
|
|
tooling::CompilationDatabase::loadFromDirectory(CDBDir, 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-12-21 09:05:23 +00:00
|
|
|
rmdir(CDBDir.c_str());
|
2018-02-20 00:19:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
2018-05-13 20:30:24 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
ProjectProcessor proc(folder);
|
2018-07-27 07:21:57 +00:00
|
|
|
StringSet<> Seen;
|
2017-04-20 05:01:36 +00:00
|
|
|
std::vector<Project::Entry> result;
|
2018-12-21 09:05:23 +00:00
|
|
|
if (CDB) {
|
|
|
|
LOG_S(INFO) << "loaded " << Path.c_str();
|
|
|
|
for (tooling::CompileCommand &Cmd : CDB->getAllCompileCommands()) {
|
|
|
|
static bool once;
|
|
|
|
Project::Entry entry;
|
|
|
|
entry.root = root;
|
|
|
|
DoPathMapping(entry.root);
|
|
|
|
entry.directory = NormalizePath(Cmd.Directory);
|
|
|
|
DoPathMapping(entry.directory);
|
|
|
|
entry.filename =
|
|
|
|
NormalizePath(ResolveIfRelative(entry.directory, Cmd.Filename));
|
|
|
|
DoPathMapping(entry.filename);
|
|
|
|
std::vector<std::string> args = std::move(Cmd.CommandLine);
|
|
|
|
entry.args.reserve(args.size());
|
|
|
|
for (std::string &arg : args) {
|
|
|
|
DoPathMapping(arg);
|
|
|
|
if (!proc.excludeArgs.count(arg))
|
|
|
|
entry.args.push_back(Intern(arg));
|
|
|
|
}
|
|
|
|
entry.compdb_size = entry.args.size();
|
|
|
|
|
|
|
|
// Work around relative --sysroot= as it isn't affected by
|
|
|
|
// -working-directory=. chdir is thread hostile but this function runs
|
|
|
|
// before indexers do actual work and it works when there is only one
|
|
|
|
// workspace folder.
|
|
|
|
if (!once) {
|
|
|
|
once = true;
|
|
|
|
llvm::vfs::getRealFileSystem()->setCurrentWorkingDirectory(
|
|
|
|
entry.directory);
|
|
|
|
}
|
|
|
|
proc.GetSearchDirs(entry);
|
2018-11-08 19:32:27 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
if (Seen.insert(entry.filename).second)
|
|
|
|
folder.entries.push_back(entry);
|
2018-11-08 19:32:27 +00:00
|
|
|
}
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
// Use directory listing if .ccls exists or compile_commands.json does not
|
|
|
|
// exist.
|
|
|
|
Path.clear();
|
|
|
|
sys::path::append(Path, root, ".ccls");
|
|
|
|
if (sys::fs::exists(Path))
|
|
|
|
LoadDirectoryListing(proc, root, Seen);
|
|
|
|
std::vector<const char *> extra_args;
|
|
|
|
for (const std::string &arg : g_config->clang.extraArgs)
|
|
|
|
extra_args.push_back(Intern(arg));
|
|
|
|
for (auto &e : folder.entries) {
|
|
|
|
e.args.insert(e.args.end(), extra_args.begin(), extra_args.end());
|
|
|
|
e.args.push_back(Intern("-working-directory=" + e.directory));
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 05:02:28 +00:00
|
|
|
void Project::Load(const std::string &root) {
|
|
|
|
assert(root.back() == '/');
|
2018-12-21 09:05:23 +00:00
|
|
|
std::lock_guard lock(mtx);
|
2018-10-08 05:02:28 +00:00
|
|
|
Folder &folder = root2folder[root];
|
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
LoadDirectory(root, folder);
|
|
|
|
for (auto &[path, kind] : folder.search_dir2kind)
|
|
|
|
LOG_S(INFO) << "search directory: " << path << ' ' << " \"< "[kind];
|
2018-09-10 06:46:13 +00:00
|
|
|
|
|
|
|
// Setup project entries.
|
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-10-01 05:54:48 +00:00
|
|
|
Project::Entry Project::FindEntry(const std::string &path,
|
|
|
|
bool can_be_inferred) {
|
2018-12-21 09:05:23 +00:00
|
|
|
Project::Folder *best_folder = nullptr;
|
|
|
|
const Entry *best = nullptr;
|
|
|
|
std::lock_guard lock(mtx);
|
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()) {
|
|
|
|
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-12-21 09:05:23 +00:00
|
|
|
std::string dir;
|
|
|
|
const std::vector<const char *> *extra = nullptr;
|
|
|
|
Project::Entry ret;
|
|
|
|
for (auto &[root, folder] : root2folder)
|
2018-11-17 18:23:12 +00:00
|
|
|
if (StringRef(path).startswith(root))
|
2018-12-21 09:05:23 +00:00
|
|
|
for (auto &[dir1, args] : folder.dot_ccls)
|
|
|
|
if (StringRef(path).startswith(dir1)) {
|
|
|
|
if (AppendToCDB(args)) {
|
|
|
|
dir = dir1;
|
|
|
|
extra = &args;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret.root = ret.directory = root;
|
|
|
|
ret.filename = path;
|
|
|
|
if (args.empty()) {
|
|
|
|
ret.args = GetFallback(path);
|
|
|
|
} else {
|
|
|
|
ret.args = args;
|
|
|
|
ret.args.push_back(Intern(path));
|
|
|
|
}
|
|
|
|
ProjectProcessor(folder).Process(ret);
|
|
|
|
for (const std::string &arg : g_config->clang.extraArgs)
|
|
|
|
ret.args.push_back(Intern(arg));
|
|
|
|
ret.args.push_back(Intern("-working-directory=" + ret.directory));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
|
|
|
|
if (!best) {
|
|
|
|
int best_score = INT_MIN;
|
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
if (dir.size() && !StringRef(path).startswith(dir))
|
|
|
|
continue;
|
|
|
|
for (const Entry &e : folder.entries)
|
|
|
|
if (e.compdb_size) {
|
|
|
|
int score = ComputeGuessScore(path, e.filename);
|
|
|
|
if (score > best_score) {
|
|
|
|
best_score = score;
|
|
|
|
best = &e;
|
|
|
|
best_folder = &folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
}
|
2018-01-11 05:27:58 +00:00
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
ret.is_inferred = true;
|
|
|
|
ret.filename = path;
|
|
|
|
if (!best) {
|
|
|
|
if (ret.root.empty())
|
|
|
|
ret.root = g_config->fallbackFolder;
|
|
|
|
ret.directory = ret.root;
|
|
|
|
ret.args = GetFallback(path);
|
|
|
|
} else {
|
|
|
|
ret.root = best->root;
|
|
|
|
ret.directory = best->directory;
|
|
|
|
ret.args = best->args;
|
|
|
|
std::string base_name = sys::path::filename(best->filename);
|
|
|
|
for (const char *&arg : ret.args) {
|
2018-04-08 00:10:54 +00:00
|
|
|
try {
|
2018-12-21 09:05:23 +00:00
|
|
|
if (arg == best->filename || sys::path::filename(arg) == 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
|
|
|
}
|
2018-12-21 09:05:23 +00:00
|
|
|
ret.args.resize(best->compdb_size);
|
|
|
|
if (extra && extra->size())
|
|
|
|
ret.args.insert(ret.args.end(), extra->begin() + 1, extra->end());
|
|
|
|
ProjectProcessor(*best_folder).Process(ret);
|
|
|
|
for (const std::string &arg : g_config->clang.extraArgs)
|
|
|
|
ret.args.push_back(Intern(arg));
|
|
|
|
ret.args.push_back(Intern("-working-directory=" + ret.directory));
|
2018-01-11 05:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 09:05:23 +00:00
|
|
|
return ret;
|
2017-04-21 04:50:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 20:52:43 +00:00
|
|
|
void Project::Index(WorkingFiles *wfiles, RequestId id) {
|
2018-09-24 01:45:41 +00:00
|
|
|
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
|
|
|
{
|
2018-12-21 09:05:23 +00:00
|
|
|
std::lock_guard lock(mtx);
|
2018-10-08 05:02:28 +00:00
|
|
|
for (auto &[root, folder] : root2folder) {
|
|
|
|
int i = 0;
|
|
|
|
for (const Project::Entry &entry : folder.entries) {
|
|
|
|
std::string reason;
|
2018-11-27 06:22:27 +00:00
|
|
|
if (match.Matches(entry.filename, &reason) &&
|
|
|
|
match_i.Matches(entry.filename, &reason)) {
|
2018-12-01 06:44:52 +00:00
|
|
|
bool interactive = wfiles->GetFile(entry.filename) != nullptr;
|
2018-10-08 05:02:28 +00:00
|
|
|
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
|