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": []
}, {
"usr": 1428566502523368801,
"detailed_name": "struct {}",
"detailed_name": "anon struct",
"qual_name_offset": 0,
"short_name": "",
"short_name": "anon struct",
"kind": 23,
"declarations": [],
"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>
BuildCompilerInvocation(const std::vector<std::string> &args,
BuildCompilerInvocation(std::vector<const char *> args,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
std::string save = "-resource-dir=" + g_config->clang.resourceDir;
std::vector<const char *> cargs;
for (auto &arg : args)
cargs.push_back(arg.c_str());
cargs.push_back(save.c_str());
args.push_back(save.c_str());
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
CompilerInstance::createDiagnostics(new DiagnosticOptions));
std::unique_ptr<CompilerInvocation> CI =
createInvocationFromCommandLine(cargs, Diags, VFS);
createInvocationFromCommandLine(args, Diags, VFS);
if (CI) {
CI->getDiagnosticOpts().IgnoreWarnings = true;
CI->getFrontendOpts().DisableFree = false;

View File

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

View File

@ -1236,7 +1236,7 @@ void Init() {
std::vector<std::unique_ptr<IndexFile>>
Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
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) {
if (!g_config->index.enabled)
return {};

View File

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

View File

@ -64,14 +64,15 @@ struct Handler_TextDocumentDidOpen
}
include_complete->AddFile(working_file->filename);
if (params.args.size())
project->SetFlagsForFile(params.args, path);
std::vector<const char *> args;
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.
if (SourceFileLanguage(path) != LanguageId::Unknown) {
pipeline::Index(
path, params.args.size() ? params.args : std::vector<std::string>{},
IndexMode::Normal);
pipeline::Index(path, args, IndexMode::Normal);
clang_complete->FlushSession(path);
}

View File

@ -81,7 +81,7 @@ namespace {
struct Index_Request {
std::string path;
std::vector<std::string> args;
std::vector<const char *> args;
IndexMode mode;
lsRequestId id;
int64_t ts = tick++;
@ -108,7 +108,7 @@ std::shared_mutex g_index_mutex;
std::unordered_map<std::string, InMemoryIndexFile> g_index;
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) {
{
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
<< (from ? " (via " + *from + ")" : std::string());
return true;
}
return false;
return changed;
};
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) {
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);
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 = {});
std::optional<int64_t> LastWriteTime(const std::string &path);

View File

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

View File

@ -30,7 +30,7 @@ struct Project {
struct Entry {
std::string directory;
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.
bool is_inferred = false;
int id = -1;
@ -64,7 +64,7 @@ struct Project {
// 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
// are permanent.
void SetFlagsForFile(const std::vector<std::string> &flags,
void SetArgsForFile(const std::vector<const char *> &args,
const std::string &path);
// Run |action| on every file in the project.

View File

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

View File

@ -464,8 +464,13 @@ Deserialize(SerializeFormat format, const std::string &path,
file->path = path;
if (g_config->clang.pathMappings.size()) {
DoPathMapping(file->import_file);
for (std::string &arg : file->args)
DoPathMapping(arg);
std::vector<const char *> args;
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)
DoPathMapping(path);
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;
// FIXME: show diagnostics in STL/headers when running tests. At the moment
// this can be done by constructing ClangIndex index(1, 1);
CompletionManager completion(
nullptr, nullptr, [&](std::string, std::vector<lsDiagnostic>) {},
[](lsRequestId id) {});
GetFilesInFolder(
"index_tests", true /*recursive*/, true /*add_folder_to_path*/,
[&](const std::string &path) {
@ -302,11 +305,11 @@ bool RunIndexTests(const std::string &filter_path, bool enable_update) {
// Run test.
g_config = new Config;
VFS vfs;
CompletionManager completion(
nullptr, nullptr, [&](std::string, std::vector<lsDiagnostic>) {},
[](lsRequestId id) {});
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) {
const std::string &expected_path = entry.first;