mirror of
https://github.com/MaskRay/ccls.git
synced 2025-06-14 04:02:16 +00:00
Clean up
This commit is contained in:
parent
1ae97c64ed
commit
222d096d00
@ -1,10 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
OUTPUT:
|
OUTPUT:
|
||||||
{
|
{}
|
||||||
"includes": [],
|
|
||||||
"skipped_ranges": [],
|
|
||||||
"usr2func": [],
|
|
||||||
"usr2type": [],
|
|
||||||
"usr2var": []
|
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
|
@ -484,30 +484,10 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr<CompilerInvocation>
|
std::unique_ptr<CompilerInstance> BuildCompilerInstance(
|
||||||
buildCompilerInvocation(const std::vector<std::string> &args,
|
CompletionSession &session, std::unique_ptr<CompilerInvocation> CI,
|
||||||
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
|
DiagnosticConsumer &DC, const WorkingFiles::Snapshot &snapshot,
|
||||||
std::vector<const char *> cargs;
|
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Bufs) {
|
||||||
for (auto &arg : args)
|
|
||||||
cargs.push_back(arg.c_str());
|
|
||||||
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
||||||
CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
|
||||||
std::unique_ptr<CompilerInvocation> CI =
|
|
||||||
createInvocationFromCommandLine(cargs, Diags, VFS);
|
|
||||||
if (CI) {
|
|
||||||
CI->getFrontendOpts().DisableFree = false;
|
|
||||||
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
|
|
||||||
CI->getLangOpts()->SpellChecking = false;
|
|
||||||
}
|
|
||||||
return CI;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<CompilerInstance>
|
|
||||||
BuildCompilerInstance(CompletionSession &session,
|
|
||||||
std::unique_ptr<CompilerInvocation> CI,
|
|
||||||
DiagnosticConsumer &DC,
|
|
||||||
const WorkingFiles::Snapshot &snapshot,
|
|
||||||
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Bufs) {
|
|
||||||
for (auto &file : snapshot.files) {
|
for (auto &file : snapshot.files) {
|
||||||
Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(file.content));
|
Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(file.content));
|
||||||
if (file.filename == session.file.filename) {
|
if (file.filename == session.file.filename) {
|
||||||
@ -570,7 +550,7 @@ void CompletionPreloadMain(ClangCompleteManager *completion_manager) {
|
|||||||
|
|
||||||
LOG_S(INFO) << "create completion session for " << session->file.filename;
|
LOG_S(INFO) << "create completion session for " << session->file.filename;
|
||||||
if (std::unique_ptr<CompilerInvocation> CI =
|
if (std::unique_ptr<CompilerInvocation> CI =
|
||||||
buildCompilerInvocation(args, session->FS))
|
BuildCompilerInvocation(args, session->FS))
|
||||||
session->BuildPreamble(*CI);
|
session->BuildPreamble(*CI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -595,7 +575,7 @@ void CompletionQueryMain(ClangCompleteManager *completion_manager) {
|
|||||||
true /*create_if_needed*/);
|
true /*create_if_needed*/);
|
||||||
|
|
||||||
std::unique_ptr<CompilerInvocation> CI =
|
std::unique_ptr<CompilerInvocation> CI =
|
||||||
buildCompilerInvocation(session->file.args, session->FS);
|
BuildCompilerInvocation(session->file.args, session->FS);
|
||||||
if (!CI)
|
if (!CI)
|
||||||
continue;
|
continue;
|
||||||
clang::CodeCompleteOptions CCOpts;
|
clang::CodeCompleteOptions CCOpts;
|
||||||
@ -608,6 +588,7 @@ void CompletionQueryMain(ClangCompleteManager *completion_manager) {
|
|||||||
FOpts.CodeCompletionAt.FileName = session->file.filename;
|
FOpts.CodeCompletionAt.FileName = session->file.filename;
|
||||||
FOpts.CodeCompletionAt.Line = request->position.line + 1;
|
FOpts.CodeCompletionAt.Line = request->position.line + 1;
|
||||||
FOpts.CodeCompletionAt.Column = request->position.character + 1;
|
FOpts.CodeCompletionAt.Column = request->position.character + 1;
|
||||||
|
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
|
||||||
|
|
||||||
StoreDiags DC;
|
StoreDiags DC;
|
||||||
WorkingFiles::Snapshot snapshot =
|
WorkingFiles::Snapshot snapshot =
|
||||||
@ -641,7 +622,7 @@ void DiagnosticQueryMain(ClangCompleteManager *manager) {
|
|||||||
path, true /*mark_as_completion*/, true /*create_if_needed*/);
|
path, true /*mark_as_completion*/, true /*create_if_needed*/);
|
||||||
|
|
||||||
std::unique_ptr<CompilerInvocation> CI =
|
std::unique_ptr<CompilerInvocation> CI =
|
||||||
buildCompilerInvocation(session->file.args, session->FS);
|
BuildCompilerInvocation(session->file.args, session->FS);
|
||||||
if (!CI)
|
if (!CI)
|
||||||
continue;
|
continue;
|
||||||
StoreDiags DC;
|
StoreDiags DC;
|
||||||
@ -701,6 +682,7 @@ void CompletionSession::BuildPreamble(CompilerInvocation &CI) {
|
|||||||
if (OldP && OldP->Preamble.CanReuse(CI, Buf.get(), Bounds, FS.get()))
|
if (OldP && OldP->Preamble.CanReuse(CI, Buf.get(), Bounds, FS.get()))
|
||||||
return;
|
return;
|
||||||
CI.getFrontendOpts().SkipFunctionBodies = true;
|
CI.getFrontendOpts().SkipFunctionBodies = true;
|
||||||
|
CI.getLangOpts()->CommentOpts.ParseAllComments = true;
|
||||||
#if LLVM_VERSION_MAJOR >= 7
|
#if LLVM_VERSION_MAJOR >= 7
|
||||||
CI.getPreprocessorOpts().WriteCommentListToPCH = false;
|
CI.getPreprocessorOpts().WriteCommentListToPCH = false;
|
||||||
#endif
|
#endif
|
||||||
|
@ -63,3 +63,20 @@ Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts,
|
|||||||
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getTokenRange(R),
|
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getTokenRange(R),
|
||||||
UniqueID);
|
UniqueID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<CompilerInvocation>
|
||||||
|
BuildCompilerInvocation(const std::vector<std::string> &args,
|
||||||
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
|
||||||
|
std::vector<const char *> cargs;
|
||||||
|
for (auto &arg : args)
|
||||||
|
cargs.push_back(arg.c_str());
|
||||||
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
||||||
|
CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
||||||
|
std::unique_ptr<CompilerInvocation> CI =
|
||||||
|
createInvocationFromCommandLine(cargs, Diags, VFS);
|
||||||
|
if (CI) {
|
||||||
|
CI->getFrontendOpts().DisableFree = false;
|
||||||
|
CI->getLangOpts()->SpellChecking = false;
|
||||||
|
}
|
||||||
|
return CI;
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ limitations under the License.
|
|||||||
|
|
||||||
#include <clang/Basic/LangOptions.h>
|
#include <clang/Basic/LangOptions.h>
|
||||||
#include <clang/Basic/SourceManager.h>
|
#include <clang/Basic/SourceManager.h>
|
||||||
|
#include <clang/Frontend/CompilerInstance.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -33,3 +34,7 @@ Range FromCharRange(const clang::SourceManager &SM,
|
|||||||
Range FromTokenRange(const clang::SourceManager &SM,
|
Range FromTokenRange(const clang::SourceManager &SM,
|
||||||
const clang::LangOptions &LangOpts, clang::SourceRange R,
|
const clang::LangOptions &LangOpts, clang::SourceRange R,
|
||||||
llvm::sys::fs::UniqueID *UniqueID = nullptr);
|
llvm::sys::fs::UniqueID *UniqueID = nullptr);
|
||||||
|
|
||||||
|
std::unique_ptr<clang::CompilerInvocation>
|
||||||
|
BuildCompilerInvocation(const std::vector<std::string> &args,
|
||||||
|
llvm::IntrusiveRefCntPtr<clang::vfs::FileSystem> VFS);
|
||||||
|
@ -1173,21 +1173,17 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
|
|||||||
if (!g_config->index.enabled)
|
if (!g_config->index.enabled)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
std::vector<const char *> Args;
|
|
||||||
for (auto &arg : args)
|
|
||||||
Args.push_back(arg.c_str());
|
|
||||||
auto PCHCO = std::make_shared<PCHContainerOperations>();
|
auto PCHCO = std::make_shared<PCHContainerOperations>();
|
||||||
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
||||||
CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
||||||
std::shared_ptr<CompilerInvocation> CI =
|
llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
|
||||||
createInvocationFromCommandLine(Args, Diags);
|
std::shared_ptr<CompilerInvocation> CI = BuildCompilerInvocation(args, FS);
|
||||||
if (!CI)
|
if (!CI)
|
||||||
return {};
|
return {};
|
||||||
// -fparse-all-comments enables documentation in the indexer and in
|
// -fparse-all-comments enables documentation in the indexer and in
|
||||||
// code completion.
|
// code completion.
|
||||||
if (g_config->index.comments > 1)
|
if (g_config->index.comments > 1)
|
||||||
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
|
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
|
||||||
CI->getLangOpts()->SpellChecking = false;
|
|
||||||
{
|
{
|
||||||
// FileSystemOptions& FSOpts = CI->getFileSystemOpts();
|
// FileSystemOptions& FSOpts = CI->getFileSystemOpts();
|
||||||
// if (FSOpts.WorkingDir.empty())
|
// if (FSOpts.WorkingDir.empty())
|
||||||
@ -1232,9 +1228,9 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
|
|||||||
auto compile = [&]() {
|
auto compile = [&]() {
|
||||||
ASTUnit::LoadFromCompilerInvocationAction(
|
ASTUnit::LoadFromCompilerInvocationAction(
|
||||||
std::move(CI), PCHCO, Diags, IndexAction.get(), Unit.get(),
|
std::move(CI), PCHCO, Diags, IndexAction.get(), Unit.get(),
|
||||||
/*Persistent=*/true, /*ResourceDir=*/"",
|
/*Persistent=*/false, /*ResourceDir=*/"",
|
||||||
/*OnlyLocalDecls=*/true,
|
/*OnlyLocalDecls=*/true,
|
||||||
/*CaptureDiagnostics=*/true, 0, false, false,
|
/*CaptureDiagnostics=*/false, 0, false, false,
|
||||||
/*UserFilesAreVolatile=*/true);
|
/*UserFilesAreVolatile=*/true);
|
||||||
};
|
};
|
||||||
if (!CRC.RunSafely(compile)) {
|
if (!CRC.RunSafely(compile)) {
|
||||||
@ -1246,14 +1242,6 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const SourceManager &SM = Unit->getSourceManager();
|
|
||||||
const FileEntry *FE = SM.getFileEntryForID(SM.getMainFileID());
|
|
||||||
IndexFile *main_file = param.ConsumeFile(*FE);
|
|
||||||
std::unordered_map<std::string, int> inc_to_line;
|
|
||||||
if (main_file)
|
|
||||||
for (auto &inc : main_file->includes)
|
|
||||||
inc_to_line[inc.resolved_path] = inc.line;
|
|
||||||
|
|
||||||
auto result = param.file_consumer->TakeLocalState();
|
auto result = param.file_consumer->TakeLocalState();
|
||||||
for (std::unique_ptr<IndexFile> &entry : result) {
|
for (std::unique_ptr<IndexFile> &entry : result) {
|
||||||
entry->import_file = file;
|
entry->import_file = file;
|
||||||
@ -1276,22 +1264,6 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
|
|||||||
for (auto &it : entry->usr2var)
|
for (auto &it : entry->usr2var)
|
||||||
Uniquify(it.second.uses);
|
Uniquify(it.second.uses);
|
||||||
|
|
||||||
if (main_file) {
|
|
||||||
// If there are errors, show at least one at the include position.
|
|
||||||
auto it = inc_to_line.find(entry->path);
|
|
||||||
if (it != inc_to_line.end()) {
|
|
||||||
int line = it->second;
|
|
||||||
for (auto ls_diagnostic : entry->diagnostics_) {
|
|
||||||
if (ls_diagnostic.severity != lsDiagnosticSeverity::Error)
|
|
||||||
continue;
|
|
||||||
ls_diagnostic.range =
|
|
||||||
lsRange{lsPosition{line, 10}, lsPosition{line, 10}};
|
|
||||||
main_file->diagnostics_.push_back(ls_diagnostic);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update file contents and modification time.
|
// Update file contents and modification time.
|
||||||
entry->last_write_time = param.file2write_time[entry->path];
|
entry->last_write_time = param.file2write_time[entry->path];
|
||||||
|
|
||||||
|
@ -263,8 +263,6 @@ struct IndexFile {
|
|||||||
std::unordered_map<Usr, IndexType> usr2type;
|
std::unordered_map<Usr, IndexType> usr2type;
|
||||||
std::unordered_map<Usr, IndexVar> usr2var;
|
std::unordered_map<Usr, IndexVar> usr2var;
|
||||||
|
|
||||||
// Diagnostics found when indexing this file. Not serialized.
|
|
||||||
std::vector<lsDiagnostic> diagnostics_;
|
|
||||||
// File contents at the time of index. Not serialized.
|
// File contents at the time of index. Not serialized.
|
||||||
std::string file_contents;
|
std::string file_contents;
|
||||||
|
|
||||||
|
@ -250,12 +250,6 @@ bool Indexer_Parse(DiagnosticsPublisher *diag_pub, WorkingFiles *working_files,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (std::unique_ptr<IndexFile> &curr : indexes) {
|
for (std::unique_ptr<IndexFile> &curr : indexes) {
|
||||||
// Only emit diagnostics for non-interactive sessions, which makes it easier
|
|
||||||
// to identify indexing problems. For interactive sessions, diagnostics are
|
|
||||||
// handled by code completion.
|
|
||||||
if (!request.is_interactive)
|
|
||||||
diag_pub->Publish(working_files, curr->path, curr->diagnostics_);
|
|
||||||
|
|
||||||
std::string path = curr->path;
|
std::string path = curr->path;
|
||||||
if (!(vfs->Stamp(path, curr->last_write_time) || path == path_to_index))
|
if (!(vfs->Stamp(path, curr->last_write_time) || path == path_to_index))
|
||||||
continue;
|
continue;
|
||||||
|
34
src/test.cc
34
src/test.cc
@ -307,42 +307,8 @@ bool RunIndexTests(const std::string &filter_path, bool enable_update) {
|
|||||||
const std::string &expected_path = entry.first;
|
const std::string &expected_path = entry.first;
|
||||||
std::string expected_output = text_replacer.Apply(entry.second);
|
std::string expected_output = text_replacer.Apply(entry.second);
|
||||||
|
|
||||||
// FIXME: promote to utils, find and remove duplicates (ie,
|
|
||||||
// ccls_call_tree.cc, maybe something in project.cc).
|
|
||||||
auto basename = [](const std::string &path) -> std::string {
|
|
||||||
size_t last_index = path.find_last_of('/');
|
|
||||||
if (last_index == std::string::npos)
|
|
||||||
return path;
|
|
||||||
return path.substr(last_index + 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get output from index operation.
|
// Get output from index operation.
|
||||||
IndexFile *db = FindDbForPathEnding(expected_path, dbs);
|
IndexFile *db = FindDbForPathEnding(expected_path, dbs);
|
||||||
if (db && !db->diagnostics_.empty()) {
|
|
||||||
printf("For %s\n", path.c_str());
|
|
||||||
for (const lsDiagnostic &diagnostic : db->diagnostics_) {
|
|
||||||
printf(" ");
|
|
||||||
if (diagnostic.severity)
|
|
||||||
switch (*diagnostic.severity) {
|
|
||||||
case lsDiagnosticSeverity::Error:
|
|
||||||
printf("error ");
|
|
||||||
break;
|
|
||||||
case lsDiagnosticSeverity::Warning:
|
|
||||||
printf("warning ");
|
|
||||||
break;
|
|
||||||
case lsDiagnosticSeverity::Information:
|
|
||||||
printf("information ");
|
|
||||||
break;
|
|
||||||
case lsDiagnosticSeverity::Hint:
|
|
||||||
printf("hint ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("%s:%s-%s:%s\n", basename(db->path).c_str(),
|
|
||||||
diagnostic.range.start.ToString().c_str(),
|
|
||||||
diagnostic.range.end.ToString().c_str(),
|
|
||||||
diagnostic.message.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::string actual_output = "{}";
|
std::string actual_output = "{}";
|
||||||
if (db) {
|
if (db) {
|
||||||
VerifySerializeToFrom(db);
|
VerifySerializeToFrom(db);
|
||||||
|
Loading…
Reference in New Issue
Block a user