This commit is contained in:
Fangrui Song 2018-03-31 01:01:32 -07:00
parent f7872d143d
commit e1398ff319
19 changed files with 52 additions and 386 deletions

View File

@ -173,7 +173,6 @@ target_sources(ccls PRIVATE
src/clang_indexer.cc
src/clang_translation_unit.cc
src/clang_utils.cc
src/code_complete_cache.cc
src/command_line.cc
src/diagnostics_engine.cc
src/file_consumer.cc
@ -189,7 +188,6 @@ target_sources(ccls PRIVATE
src/lsp.cc
src/match.cc
src/message_handler.cc
src/options.cc
src/platform_posix.cc
src/platform_win.cc
src/platform.cc

View File

@ -816,3 +816,14 @@ void ClangCompleteManager::FlushAllSessions() {
preloaded_sessions_.Clear();
completion_sessions_.Clear();
}
void CodeCompleteCache::WithLock(std::function<void()> action) {
std::lock_guard<std::mutex> lock(mutex_);
action();
}
bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) {
std::lock_guard<std::mutex> lock(mutex_);
return cached_path_ == position.textDocument.uri.GetPath() &&
cached_completion_position_ == position.position;
}

View File

@ -145,3 +145,18 @@ struct ClangCompleteManager {
// reparsed.
ThreadedQueue<ParseRequest> parse_requests_;
};
// Cached completion information, so we can give fast completion results when
// the user erases a character. vscode will resend the completion request if
// that happens.
struct CodeCompleteCache {
// NOTE: Make sure to access these variables under |WithLock|.
std::optional<std::string> cached_path_;
std::optional<lsPosition> cached_completion_position_;
std::vector<lsCompletionItem> cached_results_;
std::mutex mutex_;
void WithLock(std::function<void()> action);
bool IsCacheValid(lsTextDocumentPositionParams position);
};

View File

@ -1,12 +0,0 @@
#include "code_complete_cache.h"
void CodeCompleteCache::WithLock(std::function<void()> action) {
std::lock_guard<std::mutex> lock(mutex_);
action();
}
bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) {
std::lock_guard<std::mutex> lock(mutex_);
return cached_path_ == position.textDocument.uri.GetPath() &&
cached_completion_position_ == position.position;
}

View File

@ -1,22 +0,0 @@
#pragma once
#include "lsp_completion.h"
#include <optional>
#include <mutex>
// Cached completion information, so we can give fast completion results when
// the user erases a character. vscode will resend the completion request if
// that happens.
struct CodeCompleteCache {
// NOTE: Make sure to access these variables under |WithLock|.
std::optional<std::string> cached_path_;
std::optional<lsPosition> cached_completion_position_;
std::vector<lsCompletionItem> cached_results_;
std::mutex mutex_;
void WithLock(std::function<void()> action);
bool IsCacheValid(lsTextDocumentPositionParams position);
};

View File

@ -1,7 +1,6 @@
// TODO: cleanup includes
#include "cache_manager.h"
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "diagnostics_engine.h"
#include "file_consumer.h"
#include "import_manager.h"
@ -13,7 +12,6 @@
#include "lsp_diagnostic.h"
#include "match.h"
#include "message_handler.h"
#include "options.h"
#include "platform.h"
#include "project.h"
#include "query.h"
@ -45,6 +43,31 @@ std::string g_init_options;
namespace {
std::unordered_map<std::string, std::string> ParseOptions(int argc,
char** argv) {
std::unordered_map<std::string, std::string> output;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') {
auto equal = arg.find('=');
if (equal != std::string::npos) {
output[arg.substr(0, equal)] = arg.substr(equal + 1);
} else if (i + 1 < argc && argv[i + 1][0] != '-')
output[arg] = argv[++i];
else
output[arg] = "";
}
}
return output;
}
bool HasOption(const std::unordered_map<std::string, std::string>& options,
const std::string& option) {
return options.find(option) != options.end();
}
// This function returns true if e2e timing should be displayed for the given
// MethodId.
bool ShouldDisplayMethodTiming(MethodType type) {

View File

@ -1,9 +1,8 @@
#pragma once
#include <string_view>
#include <limits.h>
#include <string>
#include <string_view>
class FuzzyMatcher {
public:

View File

@ -24,28 +24,6 @@ int GetOffsetForPosition(lsPosition position, std::string_view content) {
return int(i);
}
lsPosition CharPos(std::string_view search,
char character,
int character_offset) {
lsPosition result;
size_t index = 0;
while (index < search.size()) {
char c = search[index];
if (c == character)
break;
if (c == '\n') {
result.line += 1;
result.character = 0;
} else {
result.character += 1;
}
++index;
}
assert(index < search.size());
result.character += character_offset;
return result;
}
// TODO: eliminate |line_number| param.
std::optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line) {
// Find starting and ending quote.
@ -237,132 +215,3 @@ TEST_SUITE("Substring") {
std::make_pair(true, 7));
}
}
TEST_SUITE("LexFunctionDeclaration") {
TEST_CASE("simple") {
std::string buffer_content = " void Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("ctor") {
std::string buffer_content = " Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("dtor") {
std::string buffer_content = " ~Foo(); ";
lsPosition declaration = CharPos(buffer_content, '~');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::~Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("complex return type") {
std::string buffer_content = " std::vector<int> Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("extra complex return type") {
std::string buffer_content = " std::function < int() > \n Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("parameters") {
std::string buffer_content = "void Foo(int a,\n\n int b); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);
}
}
TEST_SUITE("LexWordAroundPos") {
TEST_CASE("edges") {
std::string content = "Foobar";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}
TEST_CASE("simple") {
std::string content = " Foobar ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}
TEST_CASE("underscores, numbers and ::") {
std::string content = " file:ns::_my_t5ype7 ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'f'), content) == "file");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 's'), content) == "ns");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'y'), content) ==
"ns::_my_t5ype7");
}
TEST_CASE("dot, dash, colon are skipped") {
std::string content = "1. 2- 3:";
REQUIRE(LexIdentifierAroundPos(CharPos(content, '1'), content) == "1");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '2'), content) == "2");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '3'), content) == "3");
}
}

View File

@ -9,10 +9,6 @@
// Utility method to map |position| to an offset inside of |content|.
int GetOffsetForPosition(lsPosition position, std::string_view content);
// Utility method to find a position for the given character.
lsPosition CharPos(std::string_view search,
char character,
int character_offset = 0);
// TODO: eliminate |line_number| param.
std::optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line);

View File

@ -1,5 +1,4 @@
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "fuzzy_match.h"
#include "include_complete.h"
#include "message_handler.h"

View File

@ -1,5 +1,4 @@
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "message_handler.h"
#include "queue_manager.h"
#include "timer.h"

View File

@ -1,30 +0,0 @@
#include "options.h"
#include <loguru.hpp>
#include <iostream>
std::unordered_map<std::string, std::string> ParseOptions(int argc,
char** argv) {
std::unordered_map<std::string, std::string> output;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') {
auto equal = arg.find('=');
if (equal != std::string::npos) {
output[arg.substr(0, equal)] = arg.substr(equal + 1);
} else if (i + 1 < argc && argv[i + 1][0] != '-')
output[arg] = argv[++i];
else
output[arg] = "";
}
}
return output;
}
bool HasOption(const std::unordered_map<std::string, std::string>& options,
const std::string& option) {
return options.find(option) != options.end();
}

View File

@ -1,9 +0,0 @@
#pragma once
#include <unordered_map>
std::unordered_map<std::string, std::string> ParseOptions(int argc,
char** argv);
bool HasOption(const std::unordered_map<std::string, std::string>& options,
const std::string& option);

View File

@ -41,9 +41,6 @@ void CopyFileTo(const std::string& destination, const std::string& source);
bool IsSymLink(const std::string& path);
// Returns any clang arguments that are specific to the current platform.
std::vector<const char*> GetPlatformClangArguments();
// Free any unused memory and return it to the system.
void FreeUnusedMemory();

View File

@ -259,10 +259,6 @@ bool IsSymLink(const std::string& path) {
return lstat(path.c_str(), &buf) == 0 && S_ISLNK(buf.st_mode);
}
std::vector<const char*> GetPlatformClangArguments() {
return {};
}
void FreeUnusedMemory() {
#if defined(__GLIBC__)
malloc_trim(0);

View File

@ -129,25 +129,6 @@ bool IsSymLink(const std::string& path) {
return false;
}
std::vector<const char*> GetPlatformClangArguments() {
//
// Found by executing
//
// $ clang++ -E -x c++ - -v
//
// https://clang.llvm.org/docs/MSVCCompatibility.html
//
//
// These options are only needed if clang is targeting the msvc triple,
// which depends on how clang was build for windows. clang downloaded from
// releases.llvm.org defaults to msvc, so ccls does as well.
//
// https://github.com/cquery-project/cquery/issues/509 has more context.
//
return {"-fms-extensions", "-fms-compatibility",
"-fdelayed-template-parsing"};
}
void FreeUnusedMemory() {}
bool RunObjectiveCIndexTests() {

View File

@ -188,12 +188,6 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
if (!AnyStartsWith(args, "-working-directory"))
result.args.emplace_back("-working-directory=" + entry.directory);
if (!gTestOutputMode) {
std::vector<const char*> platform = GetPlatformClangArguments();
for (auto arg : platform)
result.args.push_back(arg);
}
bool next_flag_is_path = false;
bool add_next_flag_to_quote_dirs = false;
bool add_next_flag_to_angle_dirs = false;

View File

@ -19,17 +19,6 @@
namespace {
template <typename T>
void VerifyUnique(const std::vector<T>& values0) {
// FIXME: Run on a big code-base for a while and verify no assertions are
// triggered.
#if false
auto values = values0;
std::sort(values.begin(), values.end());
assert(std::unique(values.begin(), values.end()) == values.end());
#endif
}
template <typename T>
void RemoveRange(std::vector<T>* dest, const std::vector<T>& to_remove) {
std::unordered_set<T> to_remove_set(to_remove.begin(), to_remove.end());
@ -767,9 +756,6 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
// There means that there is some memory growth that will never be reclaimed,
// but it should be pretty minimal and is solved by simply restarting the
// indexer and loading from cache, which is a fast operation.
//
// TODO: Add "ccls: Reload Index" command which unloads all querydb state
// and fully reloads from cache. This will address the memory leak above.
switch (usr_kind) {
case SymbolKind::Type: {
@ -827,7 +813,6 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) {
auto& def = storage_name[merge_update.id.id]; \
AddRange(&def.def_var_name, merge_update.to_add); \
RemoveRange(&def.def_var_name, merge_update.to_remove); \
VerifyUnique(def.def_var_name); \
}
for (const std::string& filename : update->files_removed)

View File

@ -546,106 +546,3 @@ WorkingFiles::Snapshot WorkingFiles::AsSnapshot(
}
return result;
}
lsPosition CharPos(const WorkingFile& file,
char character,
int character_offset = 0) {
return CharPos(file.buffer_content, character, character_offset);
}
TEST_SUITE("WorkingFile") {
TEST_CASE("simple call") {
WorkingFile f("foo.cc", "abcd(1, 2");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
}
TEST_CASE("nested call") {
WorkingFile f("foo.cc", "abcd(efg(), 2");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) ==
"efg");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) ==
"efg");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
}
TEST_CASE("auto-insert )") {
WorkingFile f("foo.cc", "abc()");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) ==
"abc");
REQUIRE(active_param == 0);
}
TEST_CASE("existing completion") {
WorkingFile f("foo.cc", "zzz.asdf");
bool is_global_completion;
std::string existing_completion;
f.FindStableCompletionSource(CharPos(f, '.'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "zzz");
f.FindStableCompletionSource(CharPos(f, 'a', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "a");
f.FindStableCompletionSource(CharPos(f, 's', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "as");
f.FindStableCompletionSource(CharPos(f, 'd', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "asd");
f.FindStableCompletionSource(CharPos(f, 'f', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "asdf");
}
TEST_CASE("existing completion underscore") {
WorkingFile f("foo.cc", "ABC_DEF");
bool is_global_completion;
std::string existing_completion;
f.FindStableCompletionSource(CharPos(f, 'C'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "AB");
f.FindStableCompletionSource(CharPos(f, '_'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "ABC");
f.FindStableCompletionSource(CharPos(f, 'D'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "ABC_");
}
}