2017-03-26 21:40:34 +00:00
|
|
|
#include "project.h"
|
|
|
|
|
2017-04-21 04:50:31 +00:00
|
|
|
#include "fuzzy.h"
|
2017-03-31 04:21:52 +00:00
|
|
|
#include "libclangmm/Utility.h"
|
|
|
|
#include "platform.h"
|
2017-04-17 07:06:01 +00:00
|
|
|
#include "serializer.h"
|
2017-03-31 04:21:52 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include <clang-c/CXCompilationDatabase.h>
|
2017-05-07 05:36:29 +00:00
|
|
|
#include <doctest/doctest.h>
|
2017-03-31 04:21:52 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
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 {
|
|
|
|
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 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
|
|
|
|
2017-05-09 01:21:21 +00:00
|
|
|
|
|
|
|
static const char* kBlacklistMulti[] = {
|
|
|
|
"-MF",
|
|
|
|
"-Xclang"
|
|
|
|
};
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// Blacklisted flags which are always removed from the command line.
|
2017-03-31 04:21:52 +00:00
|
|
|
static const char *kBlacklist[] = {
|
2017-05-09 01:21:21 +00:00
|
|
|
"--param",
|
|
|
|
"-M",
|
|
|
|
"-MD",
|
|
|
|
"-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",
|
|
|
|
// TODO: make sure we consume includes before stripping all path-like args.
|
|
|
|
"/work/goma/gomacc",
|
|
|
|
"../../third_party/llvm-build/Release+Asserts/bin/clang++",
|
|
|
|
"-Wno-unused-lambda-capture",
|
|
|
|
"/",
|
|
|
|
"..",
|
|
|
|
//"-stdlib=libc++"
|
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.
|
|
|
|
const char* kPathArgs[] = {
|
|
|
|
"-isystem",
|
|
|
|
"-I",
|
|
|
|
"-iquote",
|
|
|
|
"--sysroot="
|
2017-03-31 04:21:52 +00:00
|
|
|
};
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
Project::Entry GetCompilationEntryFromCompileCommandEntry(const std::vector<std::string>& extra_flags, const CompileCommandsEntry& entry) {
|
2017-04-20 05:01:36 +00:00
|
|
|
Project::Entry result;
|
2017-04-17 20:40:50 +00:00
|
|
|
result.filename = NormalizePath(entry.file);
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
bool make_next_flag_absolute = false;
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
result.args.reserve(entry.args.size() + extra_flags.size());
|
|
|
|
for (size_t i = 0; i < entry.args.size(); ++i) {
|
|
|
|
std::string arg = entry.args[i];
|
2017-05-07 05:36:29 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// If blacklist skip.
|
2017-05-09 01:21:21 +00:00
|
|
|
if (std::any_of(std::begin(kBlacklistMulti), std::end(kBlacklistMulti), [&arg](const char* value) {
|
|
|
|
return StartsWith(arg, value);
|
|
|
|
})) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-26 04:03:22 +00:00
|
|
|
if (std::any_of(std::begin(kBlacklist), std::end(kBlacklist), [&arg](const char* value) {
|
|
|
|
return StartsWith(arg, value);
|
|
|
|
})) {
|
2017-04-17 07:06:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// Cleanup path for previous argument.
|
|
|
|
if (make_next_flag_absolute) {
|
|
|
|
if (arg.size() > 0 && arg[0] != '/')
|
|
|
|
arg = NormalizePath(entry.directory + arg);
|
|
|
|
make_next_flag_absolute = false;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
// Update arg if it is a path.
|
|
|
|
for (const char* flag_type : kPathArgs) {
|
|
|
|
if (arg == flag_type) {
|
|
|
|
make_next_flag_absolute = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
if (StartsWith(arg, flag_type)) {
|
|
|
|
std::string path = arg.substr(strlen(flag_type));
|
|
|
|
if (path.size() > 0 && path[0] != '/') {
|
|
|
|
path = NormalizePath(entry.directory + "/" + path);
|
|
|
|
arg = flag_type + path;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
2017-04-26 04:03:22 +00:00
|
|
|
if (make_next_flag_absolute)
|
|
|
|
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.
|
|
|
|
for (const auto& flag : extra_flags)
|
|
|
|
result.args.push_back(flag);
|
|
|
|
|
|
|
|
// Clang does not have good hueristics for determining source language. We
|
|
|
|
// default to C++11 if the user has not specified.
|
2017-05-09 01:21:21 +00:00
|
|
|
if (!StartsWithAny(result.args, "-x"))
|
2017-04-26 04:03:22 +00:00
|
|
|
result.args.push_back("-xc++");
|
2017-05-09 01:21:21 +00:00
|
|
|
if (!StartsWithAny(result.args, "-std="))
|
2017-04-26 04:03:22 +00:00
|
|
|
result.args.push_back("-std=c++11");
|
2017-04-17 07:06:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
std::vector<Project::Entry> LoadFromCompileCommandsJson(const std::vector<std::string>& extra_flags, const std::string& project_directory) {
|
|
|
|
// TODO: Fix this function, it may be way faster than libclang's implementation.
|
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
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;
|
2017-04-17 07:06:01 +00:00
|
|
|
result.reserve(entries.size());
|
|
|
|
for (const auto& entry : entries)
|
2017-04-26 04:03:22 +00:00
|
|
|
result.push_back(GetCompilationEntryFromCompileCommandEntry(extra_flags, entry));
|
2017-04-17 07:06:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
std::vector<Project::Entry> LoadFromDirectoryListing(const std::vector<std::string>& extra_flags, const std::string& project_directory) {
|
2017-04-20 05:01:36 +00:00
|
|
|
std::vector<Project::Entry> result;
|
2017-04-17 07:06:01 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-04-26 04:03:22 +00:00
|
|
|
for (const std::string& flag : extra_flags) {
|
|
|
|
std::cerr << flag << std::endl;
|
|
|
|
args.push_back(flag);
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
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;
|
2017-04-17 07:06:01 +00:00
|
|
|
entry.filename = NormalizePath(file);
|
|
|
|
entry.args = args;
|
|
|
|
result.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(const std::vector<std::string>& extra_flags, const std::string& project_directory) {
|
2017-04-17 07:06:01 +00:00
|
|
|
// 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-04-17 07:06:01 +00:00
|
|
|
|
2017-04-26 02:12:06 +00:00
|
|
|
std::cerr << "Trying to load compile_commands.json" << std::endl;
|
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;
|
2017-04-26 04:03:22 +00:00
|
|
|
return LoadFromDirectoryListing(extra_flags, project_directory);
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
CompileCommandsEntry entry;
|
2017-04-17 20:40:50 +00:00
|
|
|
entry.file = NormalizePath(absolute_filename);
|
2017-04-17 07:06:01 +00:00
|
|
|
entry.directory = directory;
|
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-03-31 04:21:52 +00:00
|
|
|
entry.args.reserve(num_args);
|
2017-04-20 04:57:44 +00:00
|
|
|
for (unsigned i = 0; i < num_args; ++i)
|
2017-04-17 07:06:01 +00:00
|
|
|
entry.args.push_back(clang::ToString(clang_CompileCommand_getArg(cx_command, i)));
|
2017-04-17 20:40:50 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
result.push_back(GetCompilationEntryFromCompileCommandEntry(extra_flags, entry));
|
2017-03-31 04:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clang_CompileCommands_dispose(cx_commands);
|
|
|
|
clang_CompilationDatabase_dispose(cx_db);
|
|
|
|
|
|
|
|
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.
|
|
|
|
int ComputeGuessScore(const std::string& a, const std::string& b) {
|
|
|
|
int score = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < a.length() && i < b.length(); ++i) {
|
|
|
|
if (a[i] != b[i])
|
|
|
|
break;
|
|
|
|
++score;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = i; j < a.length(); ++j) {
|
|
|
|
if (a[j] == '/')
|
|
|
|
--score;
|
|
|
|
}
|
|
|
|
for (int j = i; j < b.length(); ++j) {
|
|
|
|
if (b[j] == '/')
|
|
|
|
--score;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:21:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
void Project::Load(const std::vector<std::string>& extra_flags, const std::string& directory) {
|
|
|
|
entries = LoadCompilationEntriesFromDirectory(extra_flags, directory);
|
2017-04-20 05:46:10 +00:00
|
|
|
|
|
|
|
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-05-07 05:36:29 +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;
|
|
|
|
int best_score = 0;
|
|
|
|
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;
|
|
|
|
if (best_entry)
|
|
|
|
result.args = best_entry->args;
|
|
|
|
return result;
|
2017-04-21 04:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 05:36:29 +00:00
|
|
|
|
|
|
|
TEST_SUITE("Project");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess at same directory level, when there are parent directories.
|
|
|
|
{
|
|
|
|
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/d/new.cc");
|
|
|
|
REQUIRE(entry.has_value());
|
|
|
|
REQUIRE(entry->args == std::vector<std::string>{ "arg1" });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess at same directory level, when there are child directories.
|
|
|
|
{
|
|
|
|
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new.cc");
|
|
|
|
REQUIRE(entry.has_value());
|
|
|
|
REQUIRE(entry->args == std::vector<std::string>{ "arg2" });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess at new directory (use the closest parent directory).
|
|
|
|
{
|
|
|
|
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new/new.cc");
|
|
|
|
REQUIRE(entry.has_value());
|
|
|
|
REQUIRE(entry->args == std::vector<std::string>{ "arg2" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:21:21 +00:00
|
|
|
TEST_SUITE_END();
|