ccls/src/project.cc

313 lines
8.2 KiB
C++
Raw Normal View History

#include "project.h"
#include "fuzzy.h"
2017-03-31 04:21:52 +00:00
#include "libclangmm/Utility.h"
#include "platform.h"
#include "serializer.h"
2017-03-31 04:21:52 +00:00
#include "utils.h"
#include <clang-c/CXCompilationDatabase.h>
#include <iostream>
#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;
};
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
// https://github.com/Andersbakken/rtags/blob/6b16b81ea93aeff4a58930b44b2a0a207b456192/src/Source.cpp
static const char *kValueArgs[] = {
"--param",
"-G",
"-MF",
"-MQ",
"-MT",
"-T",
"-V",
"-Xanalyzer",
"-Xassembler",
"-Xclang",
"-Xlinker",
"-Xpreprocessor",
"-arch",
"-b",
"-gcc-toolchain",
2017-04-17 20:40:50 +00:00
//"-imacros",
2017-03-31 04:21:52 +00:00
"-imultilib",
2017-04-17 20:40:50 +00:00
//"-include",
//"-iprefix",
//"-isysroot",
2017-03-31 04:21:52 +00:00
"-ivfsoverlay",
"-iwithprefix",
"-iwithprefixbefore",
"-o",
"-target",
"-x"
};
static const char *kBlacklist[] = {
"--param",
"-M",
"-MD",
"-MF",
"-MG",
"-MM",
"-MMD",
"-MP",
"-MQ",
"-MT",
"-Og",
"-Wa,--32",
"-Wa,--64",
"-Wl,--incremental-full",
"-Wl,--incremental-patch,1",
"-Wl,--no-incremental",
"-fbuild-session-file=",
"-fbuild-session-timestamp=",
"-fembed-bitcode",
"-fembed-bitcode-marker",
"-fmodules-validate-once-per-build-session",
"-fno-delete-null-pointer-checks",
"-fno-use-linker-plugin"
"-fno-var-tracking",
"-fno-var-tracking-assignments",
"-fno-enforce-eh-specs",
"-fvar-tracking",
"-fvar-tracking-assignments",
"-fvar-tracking-assignments-toggle",
"-gcc-toolchain",
"-march=",
"-masm=",
"-mcpu=",
"-mfpmath=",
"-mtune=",
"-s",
//"-B",
//"-f",
//"-pipe",
//"-W",
2017-04-17 20:40:50 +00:00
// TODO
"-Wno-unused-lambda-capture",
2017-03-31 04:21:52 +00:00
"/",
"..",
};
2017-04-20 05:01:36 +00:00
Project::Entry GetCompilationEntryFromCompileCommandEntry(const CompileCommandsEntry& entry) {
Project::Entry result;
2017-04-17 20:40:50 +00:00
result.filename = NormalizePath(entry.file);
size_t num_args = entry.args.size();
result.args.reserve(num_args);
for (size_t j = 0; j < num_args; ++j) {
std::string arg = entry.args[j];
bool bad = false;
for (auto& entry : kValueArgs) {
if (StartsWith(arg, entry)) {
bad = true;
continue;
}
}
if (bad) {
++j;
continue;
}
for (auto& entry : kBlacklist) {
if (StartsWith(arg, entry)) {
bad = true;
continue;
}
}
if (bad) {
continue;
}
if (StartsWith(arg, "-I")) {
std::string path = entry.directory + "/" + arg.substr(2);
path = NormalizePath(path);
arg = "-I" + path;
}
result.args.push_back(arg);
//if (StartsWith(arg, "-I") || StartsWith(arg, "-D") || StartsWith(arg, "-std"))
}
// TODO/fixme
result.args.push_back("-xc++");
result.args.push_back("-std=c++11");
return result;
}
2017-04-20 05:01:36 +00:00
std::vector<Project::Entry> LoadFromCompileCommandsJson(const std::string& project_directory) {
optional<std::string> compile_commands_content = ReadContent(project_directory + "/compile_commands.json");
if (!compile_commands_content)
return {};
rapidjson::Document reader;
reader.Parse(compile_commands_content->c_str());
if (reader.HasParseError())
return {};
std::vector<CompileCommandsEntry> entries;
Reflect(reader, entries);
2017-04-20 05:01:36 +00:00
std::vector<Project::Entry> result;
result.reserve(entries.size());
for (const auto& entry : entries)
result.push_back(GetCompilationEntryFromCompileCommandEntry(entry));
return result;
}
2017-04-20 05:01:36 +00:00
std::vector<Project::Entry> LoadFromDirectoryListing(const std::string& project_directory) {
std::vector<Project::Entry> result;
std::vector<std::string> args;
std::cerr << "Using arguments: ";
for (const std::string& line : ReadLines(project_directory + "/clang_args")) {
if (line.empty() || StartsWith(line, "#"))
continue;
if (!args.empty())
std::cerr << ", ";
std::cerr << line;
args.push_back(line);
}
std::cerr << std::endl;
std::vector<std::string> files = GetFilesInFolder(project_directory, true /*recursive*/, true /*add_folder_to_path*/);
for (const std::string& file : files) {
if (EndsWith(file, ".cc") || EndsWith(file, ".cpp") || EndsWith(file, ".c")) {
2017-04-20 05:01:36 +00:00
Project::Entry entry;
entry.filename = NormalizePath(file);
entry.args = args;
result.push_back(entry);
}
}
return result;
}
2017-04-20 05:01:36 +00:00
std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(const std::string& project_directory) {
// TODO: Figure out if this function or the clang one is faster.
2017-04-17 20:40:50 +00:00
//return LoadFromCompileCommandsJson(project_directory);
2017-03-31 04:21:52 +00:00
CXCompilationDatabase_Error cx_db_load_error;
CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory(project_directory.c_str(), &cx_db_load_error);
if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) {
std::cerr << "Unable to load compile_commands.json located at \"" << project_directory << "\"; using directory listing instead." << std::endl;
return LoadFromDirectoryListing(project_directory);
}
CXCompileCommands cx_commands = clang_CompilationDatabase_getAllCompileCommands(cx_db);
unsigned int num_commands = clang_CompileCommands_getSize(cx_commands);
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++) {
CXCompileCommand cx_command = clang_CompileCommands_getCommand(cx_commands, i);
std::string directory = clang::ToString(clang_CompileCommand_getDirectory(cx_command));
std::string relative_filename = clang::ToString(clang_CompileCommand_getFilename(cx_command));
std::string absolute_filename = directory + "/" + relative_filename;
2017-04-17 20:40:50 +00:00
CompileCommandsEntry entry;
2017-04-17 20:40:50 +00:00
entry.file = NormalizePath(absolute_filename);
entry.directory = directory;
2017-03-31 04:21:52 +00:00
unsigned num_args = clang_CompileCommand_getNumArgs(cx_command);
2017-03-31 04:21:52 +00:00
entry.args.reserve(num_args);
for (unsigned i = 0; i < num_args; ++i)
entry.args.push_back(clang::ToString(clang_CompileCommand_getArg(cx_command, i)));
2017-04-17 20:40:50 +00:00
result.push_back(GetCompilationEntryFromCompileCommandEntry(entry));
2017-03-31 04:21:52 +00:00
}
clang_CompileCommands_dispose(cx_commands);
clang_CompilationDatabase_dispose(cx_db);
return result;
}
} // namespace
void Project::Load(const std::string& directory) {
entries = LoadCompilationEntriesFromDirectory(directory);
absolute_path_to_entry_index_.resize(entries.size());
for (int i = 0; i < entries.size(); ++i)
absolute_path_to_entry_index_[entries[i].filename] = i;
2017-03-31 04:21:52 +00:00
}
2017-04-20 05:01:36 +00:00
optional<Project::Entry> Project::FindCompilationEntryForFile(const std::string& filename) {
auto it = absolute_path_to_entry_index_.find(filename);
if (it != absolute_path_to_entry_index_.end())
return entries[it->second];
return nullopt;
}
optional<std::vector<std::string>> Project::FindArgsForFile(const std::string& filename) {
auto entry = FindCompilationEntryForFile(filename);
if (!entry)
return nullopt;
return entry->args;
}
void Project::ForAllFilteredFiles(IndexerConfig* config, std::function<void(int i, const Entry& entry)> action) {
std::vector<Matcher> whitelist;
std::cerr << "Using whitelist" << std::endl;
for (const std::string& entry : config->whitelist) {
std::cerr << " - " << entry << std::endl;
whitelist.push_back(Matcher(entry));
}
std::vector<Matcher> blacklist;
std::cerr << "Using blacklist" << std::endl;
for (const std::string& entry : config->blacklist) {
std::cerr << " - " << entry << std::endl;
blacklist.push_back(Matcher(entry));
}
for (int i = 0; i < entries.size(); ++i) {
const Project::Entry& entry = entries[i];
std::string filepath = entry.filename;
const Matcher* is_bad = nullptr;
for (const Matcher& m : whitelist) {
if (!m.IsMatch(filepath)) {
is_bad = &m;
break;
}
}
if (is_bad) {
std::cerr << "[" << i << "/" << (entries.size() - 1) << "] Failed whitelist check \"" << is_bad->regex_string << "\"; skipping " << filepath << std::endl;
continue;
}
for (const Matcher& m : blacklist) {
if (m.IsMatch(filepath)) {
is_bad = &m;
break;
}
}
if (is_bad) {
std::cerr << "[" << i << "/" << (entries.size() - 1) << "] Failed blacklist check \"" << is_bad->regex_string << "\"; skipping " << filepath << std::endl;
continue;
}
action(i, entries[i]);
}
}