From e1398ff319da2474a32d4513d2897c143c8febf3 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 31 Mar 2018 01:01:32 -0700 Subject: [PATCH] . --- CMakeLists.txt | 2 - src/clang_complete.cc | 11 ++ src/clang_complete.h | 15 ++ src/code_complete_cache.cc | 12 -- src/code_complete_cache.h | 22 --- src/command_line.cc | 27 +++- src/fuzzy_match.h | 3 +- src/lex_utils.cc | 151 ------------------- src/lex_utils.h | 4 - src/messages/text_document_completion.cc | 1 - src/messages/text_document_signature_help.cc | 1 - src/options.cc | 30 ---- src/options.h | 9 -- src/platform.h | 3 - src/platform_posix.cc | 4 - src/platform_win.cc | 19 --- src/project.cc | 6 - src/query.cc | 15 -- src/working_files.cc | 103 ------------- 19 files changed, 52 insertions(+), 386 deletions(-) delete mode 100644 src/code_complete_cache.cc delete mode 100644 src/code_complete_cache.h delete mode 100644 src/options.cc delete mode 100644 src/options.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 63db0c0f..e3364e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/clang_complete.cc b/src/clang_complete.cc index 1ac65e46..d76dbf81 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -816,3 +816,14 @@ void ClangCompleteManager::FlushAllSessions() { preloaded_sessions_.Clear(); completion_sessions_.Clear(); } + +void CodeCompleteCache::WithLock(std::function action) { + std::lock_guard lock(mutex_); + action(); +} + +bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) { + std::lock_guard lock(mutex_); + return cached_path_ == position.textDocument.uri.GetPath() && + cached_completion_position_ == position.position; +} diff --git a/src/clang_complete.h b/src/clang_complete.h index 142ab86a..8080c0e5 100644 --- a/src/clang_complete.h +++ b/src/clang_complete.h @@ -145,3 +145,18 @@ struct ClangCompleteManager { // reparsed. ThreadedQueue 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 cached_path_; + std::optional cached_completion_position_; + std::vector cached_results_; + + std::mutex mutex_; + + void WithLock(std::function action); + bool IsCacheValid(lsTextDocumentPositionParams position); +}; diff --git a/src/code_complete_cache.cc b/src/code_complete_cache.cc deleted file mode 100644 index df924461..00000000 --- a/src/code_complete_cache.cc +++ /dev/null @@ -1,12 +0,0 @@ -#include "code_complete_cache.h" - -void CodeCompleteCache::WithLock(std::function action) { - std::lock_guard lock(mutex_); - action(); -} - -bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) { - std::lock_guard lock(mutex_); - return cached_path_ == position.textDocument.uri.GetPath() && - cached_completion_position_ == position.position; -} \ No newline at end of file diff --git a/src/code_complete_cache.h b/src/code_complete_cache.h deleted file mode 100644 index 39696f1b..00000000 --- a/src/code_complete_cache.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "lsp_completion.h" - -#include - -#include - -// 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 cached_path_; - std::optional cached_completion_position_; - std::vector cached_results_; - - std::mutex mutex_; - - void WithLock(std::function action); - bool IsCacheValid(lsTextDocumentPositionParams position); -}; diff --git a/src/command_line.cc b/src/command_line.cc index c5be6682..eda149ee 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -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 ParseOptions(int argc, + char** argv) { + std::unordered_map 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& 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) { diff --git a/src/fuzzy_match.h b/src/fuzzy_match.h index 52a89986..958f35cd 100644 --- a/src/fuzzy_match.h +++ b/src/fuzzy_match.h @@ -1,9 +1,8 @@ #pragma once -#include - #include #include +#include class FuzzyMatcher { public: diff --git a/src/lex_utils.cc b/src/lex_utils.cc index ff040688..5d66adf0 100644 --- a/src/lex_utils.cc +++ b/src/lex_utils.cc @@ -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 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 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 Foo() {\n}"); - REQUIRE(newlines_after_name == 0); - - LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "std::vector 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"); - } -} diff --git a/src/lex_utils.h b/src/lex_utils.h index 6fbc6864..36328ad3 100644 --- a/src/lex_utils.h +++ b/src/lex_utils.h @@ -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 ExtractQuotedRange(int line_number, const std::string& line); diff --git a/src/messages/text_document_completion.cc b/src/messages/text_document_completion.cc index a186e178..14e82af4 100644 --- a/src/messages/text_document_completion.cc +++ b/src/messages/text_document_completion.cc @@ -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" diff --git a/src/messages/text_document_signature_help.cc b/src/messages/text_document_signature_help.cc index 9ceb7410..c06050ea 100644 --- a/src/messages/text_document_signature_help.cc +++ b/src/messages/text_document_signature_help.cc @@ -1,5 +1,4 @@ #include "clang_complete.h" -#include "code_complete_cache.h" #include "message_handler.h" #include "queue_manager.h" #include "timer.h" diff --git a/src/options.cc b/src/options.cc deleted file mode 100644 index b0962f5f..00000000 --- a/src/options.cc +++ /dev/null @@ -1,30 +0,0 @@ -#include "options.h" - -#include - -#include - -std::unordered_map ParseOptions(int argc, - char** argv) { - std::unordered_map 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& options, - const std::string& option) { - return options.find(option) != options.end(); -} diff --git a/src/options.h b/src/options.h deleted file mode 100644 index eec6f17e..00000000 --- a/src/options.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -std::unordered_map ParseOptions(int argc, - char** argv); - -bool HasOption(const std::unordered_map& options, - const std::string& option); \ No newline at end of file diff --git a/src/platform.h b/src/platform.h index 106b8902..77001214 100644 --- a/src/platform.h +++ b/src/platform.h @@ -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 GetPlatformClangArguments(); - // Free any unused memory and return it to the system. void FreeUnusedMemory(); diff --git a/src/platform_posix.cc b/src/platform_posix.cc index e3e34b29..ad861320 100644 --- a/src/platform_posix.cc +++ b/src/platform_posix.cc @@ -259,10 +259,6 @@ bool IsSymLink(const std::string& path) { return lstat(path.c_str(), &buf) == 0 && S_ISLNK(buf.st_mode); } -std::vector GetPlatformClangArguments() { - return {}; -} - void FreeUnusedMemory() { #if defined(__GLIBC__) malloc_trim(0); diff --git a/src/platform_win.cc b/src/platform_win.cc index 2b7f8126..2ddb96a8 100644 --- a/src/platform_win.cc +++ b/src/platform_win.cc @@ -129,25 +129,6 @@ bool IsSymLink(const std::string& path) { return false; } -std::vector 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() { diff --git a/src/project.cc b/src/project.cc index 387fe3a0..ff8c75ff 100644 --- a/src/project.cc +++ b/src/project.cc @@ -188,12 +188,6 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( if (!AnyStartsWith(args, "-working-directory")) result.args.emplace_back("-working-directory=" + entry.directory); - if (!gTestOutputMode) { - std::vector 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; diff --git a/src/query.cc b/src/query.cc index b1336b9e..9fcb4c41 100644 --- a/src/query.cc +++ b/src/query.cc @@ -19,17 +19,6 @@ namespace { -template -void VerifyUnique(const std::vector& 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 void RemoveRange(std::vector* dest, const std::vector& to_remove) { std::unordered_set 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) diff --git a/src/working_files.cc b/src/working_files.cc index 1e173eec..52fcd516 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -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_"); - } -}