intern args

This commit is contained in:
Fangrui Song 2018-09-19 09:31:45 -07:00
parent 525b6da1ac
commit 08645d64c1
13 changed files with 76 additions and 71 deletions

View File

@ -26,9 +26,9 @@ OUTPUT:
"uses": [] "uses": []
}, { }, {
"usr": 1428566502523368801, "usr": 1428566502523368801,
"detailed_name": "struct {}", "detailed_name": "anon struct",
"qual_name_offset": 0, "qual_name_offset": 0,
"short_name": "", "short_name": "anon struct",
"kind": 23, "kind": 23,
"declarations": [], "declarations": [],
"spell": "2:3-2:9|17937907487590875128|2|1026|-1", "spell": "2:3-2:9|17937907487590875128|2|1026|-1",

View File

@ -66,17 +66,14 @@ Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts,
} }
std::unique_ptr<CompilerInvocation> std::unique_ptr<CompilerInvocation>
BuildCompilerInvocation(const std::vector<std::string> &args, BuildCompilerInvocation(std::vector<const char *> args,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) { IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
std::string save = "-resource-dir=" + g_config->clang.resourceDir; std::string save = "-resource-dir=" + g_config->clang.resourceDir;
std::vector<const char *> cargs; args.push_back(save.c_str());
for (auto &arg : args)
cargs.push_back(arg.c_str());
cargs.push_back(save.c_str());
IntrusiveRefCntPtr<DiagnosticsEngine> Diags( IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
CompilerInstance::createDiagnostics(new DiagnosticOptions)); CompilerInstance::createDiagnostics(new DiagnosticOptions));
std::unique_ptr<CompilerInvocation> CI = std::unique_ptr<CompilerInvocation> CI =
createInvocationFromCommandLine(cargs, Diags, VFS); createInvocationFromCommandLine(args, Diags, VFS);
if (CI) { if (CI) {
CI->getDiagnosticOpts().IgnoreWarnings = true; CI->getDiagnosticOpts().IgnoreWarnings = true;
CI->getFrontendOpts().DisableFree = false; CI->getFrontendOpts().DisableFree = false;

View File

@ -36,5 +36,5 @@ Range FromTokenRange(const clang::SourceManager &SM,
llvm::sys::fs::UniqueID *UniqueID = nullptr); llvm::sys::fs::UniqueID *UniqueID = nullptr);
std::unique_ptr<clang::CompilerInvocation> std::unique_ptr<clang::CompilerInvocation>
BuildCompilerInvocation(const std::vector<std::string> &args, BuildCompilerInvocation(std::vector<const char *> args,
llvm::IntrusiveRefCntPtr<clang::vfs::FileSystem> VFS); llvm::IntrusiveRefCntPtr<clang::vfs::FileSystem> VFS);

View File

@ -1236,7 +1236,7 @@ void Init() {
std::vector<std::unique_ptr<IndexFile>> std::vector<std::unique_ptr<IndexFile>>
Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs, Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
const std::string &opt_wdir, const std::string &file, const std::string &opt_wdir, const std::string &file,
const std::vector<std::string> &args, const std::vector<const char *> &args,
const std::vector<std::pair<std::string, std::string>> &remapped) { const std::vector<std::pair<std::string, std::string>> &remapped) {
if (!g_config->index.enabled) if (!g_config->index.enabled)
return {}; return {};

View File

@ -247,7 +247,7 @@ struct IndexFile {
llvm::sys::fs::UniqueID UniqueID; llvm::sys::fs::UniqueID UniqueID;
std::string path; std::string path;
std::vector<std::string> args; std::vector<const char *> args;
// This is unfortunately time_t as used by clang::FileEntry // This is unfortunately time_t as used by clang::FileEntry
int64_t mtime = 0; int64_t mtime = 0;
LanguageId language = LanguageId::C; LanguageId language = LanguageId::C;
@ -293,6 +293,6 @@ void Init();
std::vector<std::unique_ptr<IndexFile>> std::vector<std::unique_ptr<IndexFile>>
Index(CompletionManager *complete, WorkingFiles *wfiles, VFS *vfs, Index(CompletionManager *complete, WorkingFiles *wfiles, VFS *vfs,
const std::string &opt_wdir, const std::string &file, const std::string &opt_wdir, const std::string &file,
const std::vector<std::string> &args, const std::vector<const char *> &args,
const std::vector<std::pair<std::string, std::string>> &remapped); const std::vector<std::pair<std::string, std::string>> &remapped);
} }

View File

@ -64,14 +64,15 @@ struct Handler_TextDocumentDidOpen
} }
include_complete->AddFile(working_file->filename); include_complete->AddFile(working_file->filename);
if (params.args.size()) std::vector<const char *> args;
project->SetFlagsForFile(params.args, path); for (const std::string &arg : params.args)
args.push_back(Intern(arg));
if (args.size())
project->SetArgsForFile(args, path);
// Submit new index request if it is not a header file. // Submit new index request if it is not a header file.
if (SourceFileLanguage(path) != LanguageId::Unknown) { if (SourceFileLanguage(path) != LanguageId::Unknown) {
pipeline::Index( pipeline::Index(path, args, IndexMode::Normal);
path, params.args.size() ? params.args : std::vector<std::string>{},
IndexMode::Normal);
clang_complete->FlushSession(path); clang_complete->FlushSession(path);
} }

View File

@ -81,7 +81,7 @@ namespace {
struct Index_Request { struct Index_Request {
std::string path; std::string path;
std::vector<std::string> args; std::vector<const char *> args;
IndexMode mode; IndexMode mode;
lsRequestId id; lsRequestId id;
int64_t ts = tick++; int64_t ts = tick++;
@ -108,7 +108,7 @@ std::shared_mutex g_index_mutex;
std::unordered_map<std::string, InMemoryIndexFile> g_index; std::unordered_map<std::string, InMemoryIndexFile> g_index;
bool CacheInvalid(VFS *vfs, IndexFile *prev, const std::string &path, bool CacheInvalid(VFS *vfs, IndexFile *prev, const std::string &path,
const std::vector<std::string> &args, const std::vector<const char *> &args,
const std::optional<std::string> &from) { const std::optional<std::string> &from) {
{ {
std::lock_guard<std::mutex> lock(vfs->mutex); std::lock_guard<std::mutex> lock(vfs->mutex);
@ -119,13 +119,14 @@ bool CacheInvalid(VFS *vfs, IndexFile *prev, const std::string &path,
} }
} }
if (prev->args != args) { bool changed = prev->args.size() != args.size();
for (size_t i = 0; !changed && i < args.size(); i++)
if (strcmp(prev->args[i], args[i]))
changed = true;
if (changed)
LOG_S(INFO) << "args changed for " << path LOG_S(INFO) << "args changed for " << path
<< (from ? " (via " + *from + ")" : std::string()); << (from ? " (via " + *from + ")" : std::string());
return true; return changed;
}
return false;
}; };
std::string AppendSerializationFormat(const std::string &base) { std::string AppendSerializationFormat(const std::string &base) {
@ -537,7 +538,7 @@ void MainLoop() {
} }
} }
void Index(const std::string &path, const std::vector<std::string> &args, void Index(const std::string &path, const std::vector<const char *> &args,
IndexMode mode, lsRequestId id) { IndexMode mode, lsRequestId id) {
index_request->PushBack({path, args, mode, id}, mode != IndexMode::NonInteractive); index_request->PushBack({path, args, mode, id}, mode != IndexMode::NonInteractive);
} }

View File

@ -43,7 +43,7 @@ void Indexer_Main(CompletionManager *completion, VFS *vfs, Project *project,
WorkingFiles *wfiles); WorkingFiles *wfiles);
void MainLoop(); void MainLoop();
void Index(const std::string &path, const std::vector<std::string> &args, void Index(const std::string &path, const std::vector<const char *> &args,
IndexMode mode, lsRequestId id = {}); IndexMode mode, lsRequestId id = {});
std::optional<int64_t> LastWriteTime(const std::string &path); std::optional<int64_t> LastWriteTime(const std::string &path);

View File

@ -55,7 +55,6 @@ enum class ProjectMode { CompileCommandsJson, DotCcls, ExternalCommand };
struct ProjectConfig { struct ProjectConfig {
std::unordered_set<std::string> quote_dirs; std::unordered_set<std::string> quote_dirs;
std::unordered_set<std::string> angle_dirs; std::unordered_set<std::string> angle_dirs;
std::vector<std::string> extra_flags;
std::string project_dir; std::string project_dir;
ProjectMode mode = ProjectMode::CompileCommandsJson; ProjectMode mode = ProjectMode::CompileCommandsJson;
}; };
@ -85,17 +84,17 @@ struct ProjectProcessor {
const std::string base_name = sys::path::filename(entry.filename); const std::string base_name = sys::path::filename(entry.filename);
// Expand %c %cpp %clang // Expand %c %cpp %clang
std::vector<std::string> args; std::vector<const char *> args;
args.reserve(entry.args.size() + config->extra_flags.size() + 3); args.reserve(entry.args.size() + g_config->clang.extraArgs.size() + 1);
const LanguageId lang = SourceFileLanguage(entry.filename); const LanguageId lang = SourceFileLanguage(entry.filename);
for (const std::string &arg : entry.args) { for (const char *arg : entry.args) {
if (arg.compare(0, 3, "%c ") == 0) { if (strncmp(arg, "%c ", 3) == 0) {
if (lang == LanguageId::C) if (lang == LanguageId::C)
args.push_back(arg.substr(3)); args.push_back(arg + 3);
} else if (arg.compare(0, 5, "%cpp ") == 0) { } else if (strncmp(arg, "%cpp ", 5) == 0) {
if (lang == LanguageId::Cpp) if (lang == LanguageId::Cpp)
args.push_back(arg.substr(5)); args.push_back(arg + 5);
} else if (arg == "%clang") { } else if (strcmp(arg, "%clang") == 0) {
args.push_back(lang == LanguageId::Cpp ? "clang++" : "clang"); args.push_back(lang == LanguageId::Cpp ? "clang++" : "clang");
} else if (!llvm::is_contained(g_config->clang.excludeArgs, arg)) { } else if (!llvm::is_contained(g_config->clang.excludeArgs, arg)) {
args.push_back(arg); args.push_back(arg);
@ -103,8 +102,8 @@ struct ProjectProcessor {
} }
if (args.empty()) if (args.empty())
return; return;
args.insert(args.end(), config->extra_flags.begin(), for (const std::string &arg : g_config->clang.extraArgs)
config->extra_flags.end()); args.push_back(Intern(arg));
size_t hash = std::hash<std::string>{}(entry.directory); size_t hash = std::hash<std::string>{}(entry.directory);
for (auto &arg : args) { for (auto &arg : args) {
@ -117,7 +116,7 @@ struct ProjectProcessor {
} }
hash_combine(hash, std::hash<std::string>{}(arg)); hash_combine(hash, std::hash<std::string>{}(arg));
} }
args.push_back("-working-directory=" + entry.directory); args.push_back(Intern("-working-directory=" + entry.directory));
if (!command_set.insert(hash).second) { if (!command_set.insert(hash).second) {
entry.args = std::move(args); entry.args = std::move(args);
@ -141,13 +140,9 @@ struct ProjectProcessor {
} }
Driver.setCheckInputsExist(false); Driver.setCheckInputsExist(false);
std::vector<const char *> cargs; args.push_back("-fsyntax-only");
cargs.reserve(args.size() + 1);
for (auto &arg : args)
cargs.push_back(arg.c_str());
cargs.push_back("-fsyntax-only");
std::unique_ptr<driver::Compilation> C(Driver.BuildCompilation(cargs)); std::unique_ptr<driver::Compilation> C(Driver.BuildCompilation(args));
const driver::JobList &Jobs = C->getJobs(); const driver::JobList &Jobs = C->getJobs();
if (Jobs.size() != 1) if (Jobs.size() != 1)
return; return;
@ -180,15 +175,16 @@ struct ProjectProcessor {
} }
}; };
std::vector<std::string> std::vector<const char *>
ReadCompilerArgumentsFromFile(const std::string &path) { ReadCompilerArgumentsFromFile(const std::string &path) {
auto MBOrErr = MemoryBuffer::getFile(path); auto MBOrErr = MemoryBuffer::getFile(path);
if (!MBOrErr) if (!MBOrErr)
return {}; return {};
std::vector<std::string> args; std::vector<const char *> args;
for (line_iterator I(*MBOrErr.get(), true, '#'), E; I != E; ++I) { for (line_iterator I(*MBOrErr.get(), true, '#'), E; I != E; ++I) {
args.push_back(*I); std::string line = *I;
DoPathMapping(args.back()); DoPathMapping(line);
args.push_back(Intern(line));
} }
return args; return args;
} }
@ -198,12 +194,12 @@ std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig *config) {
config->mode = ProjectMode::DotCcls; config->mode = ProjectMode::DotCcls;
SmallString<256> Path; SmallString<256> Path;
sys::path::append(Path, config->project_dir, ".ccls"); sys::path::append(Path, config->project_dir, ".ccls");
LOG_IF_S(WARNING, !sys::fs::exists(Path) && config->extra_flags.empty()) LOG_IF_S(WARNING, !sys::fs::exists(Path) && g_config->clang.extraArgs.empty())
<< "ccls has no clang arguments. Use either " << "ccls has no clang arguments. Use either "
"compile_commands.json or .ccls, See ccls README for " "compile_commands.json or .ccls, See ccls README for "
"more information."; "more information.";
std::unordered_map<std::string, std::vector<std::string>> folder_args; std::unordered_map<std::string, std::vector<const char *>> folder_args;
std::vector<std::string> files; std::vector<std::string> files;
GetFilesInFolder(config->project_dir, true /*recursive*/, GetFilesInFolder(config->project_dir, true /*recursive*/,
@ -246,7 +242,7 @@ std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig *config) {
e.args = GetCompilerArgumentForFile(file); e.args = GetCompilerArgumentForFile(file);
if (e.args.empty()) if (e.args.empty())
e.args.push_back("%clang"); // Add a Dummy. e.args.push_back("%clang"); // Add a Dummy.
e.args.push_back(e.filename); e.args.push_back(Intern(e.filename));
proc.Process(e); proc.Process(e);
result.push_back(e); result.push_back(e);
} }
@ -324,9 +320,12 @@ LoadEntriesFromDirectory(ProjectConfig *project,
entry.filename = entry.filename =
NormalizePath(ResolveIfRelative(entry.directory, Cmd.Filename)); NormalizePath(ResolveIfRelative(entry.directory, Cmd.Filename));
DoPathMapping(entry.filename); DoPathMapping(entry.filename);
entry.args = std::move(Cmd.CommandLine); std::vector<std::string> args = std::move(Cmd.CommandLine);
for (std::string &arg : entry.args) entry.args.reserve(args.size());
for (std::string &arg : args) {
DoPathMapping(arg); DoPathMapping(arg);
entry.args.push_back(Intern(arg));
}
proc.Process(entry); proc.Process(entry);
if (Seen.insert(entry.filename).second) if (Seen.insert(entry.filename).second)
result.push_back(entry); result.push_back(entry);
@ -353,7 +352,6 @@ int ComputeGuessScore(std::string_view a, std::string_view b) {
void Project::Load(const std::string &root_directory) { void Project::Load(const std::string &root_directory) {
ProjectConfig project; ProjectConfig project;
project.extra_flags = g_config->clang.extraArgs;
project.project_dir = root_directory; project.project_dir = root_directory;
entries = LoadEntriesFromDirectory(&project, entries = LoadEntriesFromDirectory(&project,
g_config->compilationDatabaseDirectory); g_config->compilationDatabaseDirectory);
@ -381,19 +379,19 @@ void Project::Load(const std::string &root_directory) {
} }
} }
void Project::SetFlagsForFile(const std::vector<std::string> &flags, void Project::SetArgsForFile(const std::vector<const char *> &args,
const std::string &path) { const std::string &path) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto it = path_to_entry_index.find(path); auto it = path_to_entry_index.find(path);
if (it != path_to_entry_index.end()) { if (it != path_to_entry_index.end()) {
// The entry already exists in the project, just set the flags. // The entry already exists in the project, just set the flags.
this->entries[it->second].args = flags; this->entries[it->second].args = args;
} else { } else {
// Entry wasn't found, so we create a new one. // Entry wasn't found, so we create a new one.
Entry entry; Entry entry;
entry.is_inferred = false; entry.is_inferred = false;
entry.filename = path; entry.filename = path;
entry.args = flags; entry.args = args;
this->entries.emplace_back(entry); this->entries.emplace_back(entry);
} }
} }
@ -424,7 +422,7 @@ Project::FindCompilationEntryForFile(const std::string &filename) {
result.filename = filename; result.filename = filename;
if (!best_entry) { if (!best_entry) {
result.args.push_back("%clang"); result.args.push_back("%clang");
result.args.push_back(filename); result.args.push_back(Intern(filename));
} else { } else {
result.args = best_entry->args; result.args = best_entry->args;
@ -432,11 +430,11 @@ Project::FindCompilationEntryForFile(const std::string &filename) {
// that path to the new filename. // that path to the new filename.
std::string best_entry_base_name = std::string best_entry_base_name =
sys::path::filename(best_entry->filename); sys::path::filename(best_entry->filename);
for (std::string &arg : result.args) { for (const char *&arg : result.args) {
try { try {
if (arg == best_entry->filename || if (arg == best_entry->filename ||
sys::path::filename(arg) == best_entry_base_name) sys::path::filename(arg) == best_entry_base_name)
arg = filename; arg = Intern(filename);
} catch (...) { } catch (...) {
} }
} }

View File

@ -30,7 +30,7 @@ struct Project {
struct Entry { struct Entry {
std::string directory; std::string directory;
std::string filename; std::string filename;
std::vector<std::string> args; std::vector<const char *> args;
// If true, this entry is inferred and was not read from disk. // If true, this entry is inferred and was not read from disk.
bool is_inferred = false; bool is_inferred = false;
int id = -1; int id = -1;
@ -64,8 +64,8 @@ struct Project {
// If the client has overridden the flags, or specified them for a file // If the client has overridden the flags, or specified them for a file
// that is not in the compilation_database.json make sure those changes // that is not in the compilation_database.json make sure those changes
// are permanent. // are permanent.
void SetFlagsForFile(const std::vector<std::string> &flags, void SetArgsForFile(const std::vector<const char *> &args,
const std::string &path); const std::string &path);
// Run |action| on every file in the project. // Run |action| on every file in the project.
void void

View File

@ -39,7 +39,7 @@ template <> struct DenseMapInfo<SymbolRef> {
struct QueryFile { struct QueryFile {
struct Def { struct Def {
std::string path; std::string path;
std::vector<std::string> args; std::vector<const char *> args;
LanguageId language; LanguageId language;
// Includes in the file. // Includes in the file.
std::vector<IndexInclude> includes; std::vector<IndexInclude> includes;

View File

@ -464,8 +464,13 @@ Deserialize(SerializeFormat format, const std::string &path,
file->path = path; file->path = path;
if (g_config->clang.pathMappings.size()) { if (g_config->clang.pathMappings.size()) {
DoPathMapping(file->import_file); DoPathMapping(file->import_file);
for (std::string &arg : file->args) std::vector<const char *> args;
DoPathMapping(arg); for (const char *arg : file->args) {
std::string s(arg);
DoPathMapping(s);
args.push_back(Intern(s));
}
file->args = std::move(args);
for (auto &[_, path] : file->lid2path) for (auto &[_, path] : file->lid2path)
DoPathMapping(path); DoPathMapping(path);
for (auto &include : file->includes) { for (auto &include : file->includes) {

View File

@ -259,6 +259,9 @@ bool RunIndexTests(const std::string &filter_path, bool enable_update) {
bool update_all = false; bool update_all = false;
// FIXME: show diagnostics in STL/headers when running tests. At the moment // FIXME: show diagnostics in STL/headers when running tests. At the moment
// this can be done by constructing ClangIndex index(1, 1); // this can be done by constructing ClangIndex index(1, 1);
CompletionManager completion(
nullptr, nullptr, [&](std::string, std::vector<lsDiagnostic>) {},
[](lsRequestId id) {});
GetFilesInFolder( GetFilesInFolder(
"index_tests", true /*recursive*/, true /*add_folder_to_path*/, "index_tests", true /*recursive*/, true /*add_folder_to_path*/,
[&](const std::string &path) { [&](const std::string &path) {
@ -302,11 +305,11 @@ bool RunIndexTests(const std::string &filter_path, bool enable_update) {
// Run test. // Run test.
g_config = new Config; g_config = new Config;
VFS vfs; VFS vfs;
CompletionManager completion(
nullptr, nullptr, [&](std::string, std::vector<lsDiagnostic>) {},
[](lsRequestId id) {});
WorkingFiles wfiles; WorkingFiles wfiles;
auto dbs = ccls::idx::Index(&completion, &wfiles, &vfs, "", path, flags, {}); std::vector<const char *> cargs;
for (auto &arg : flags)
cargs.push_back(arg.c_str());
auto dbs = ccls::idx::Index(&completion, &wfiles, &vfs, "", path, cargs, {});
for (const auto &entry : all_expected_output) { for (const auto &entry : all_expected_output) {
const std::string &expected_path = entry.first; const std::string &expected_path = entry.first;