From 9ed024f5cc4dbf668169fbe0e46f5f3a1ef07894 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 8 Apr 2018 10:32:08 -0700 Subject: [PATCH] Simplify lsp.h and fix qual_name_offset when SetVarDetail is called on an existing variable --- src/clang_indexer.cc | 14 +++-------- src/indexer.h | 23 ++++++------------- src/lsp.cc | 2 -- src/lsp.h | 8 +++---- src/message_handler.cc | 2 ++ src/messages/ccls_random.cc | 2 ++ src/messages/text_document_code_lens.cc | 9 +++----- src/messages/text_document_document_symbol.cc | 2 +- src/messages/workspace_symbol.cc | 5 ++-- src/query_utils.cc | 8 +++---- src/query_utils.h | 2 +- src/serializer.h | 22 +++++++----------- src/working_files.cc | 6 ++--- 13 files changed, 38 insertions(+), 67 deletions(-) diff --git a/src/clang_indexer.cc b/src/clang_indexer.cc index e335a750..7440acb9 100644 --- a/src/clang_indexer.cc +++ b/src/clang_indexer.cc @@ -632,6 +632,7 @@ void SetVarDetail(IndexVar* var, else hover += std::to_string(clang_getEnumConstantDeclValue(cursor.cx_cursor)); def.detailed_name = std::move(qualified_name); + def.qual_name_offset = 0; def.hover = hover; } else { #if 0 && CINDEX_HAVE_PRETTY @@ -639,9 +640,9 @@ void SetVarDetail(IndexVar* var, #else int offset = type_name.size(); offset += ConcatTypeAndName(type_name, qualified_name); + def.detailed_name = type_name; def.qual_name_offset = offset; def.short_name_offset += offset; - def.detailed_name = type_name; // Append the textual initializer, bit field, constructor to |hover|. // Omit |hover| for these types: // int (*a)(); int (&a)(); int (&&a)(); int a[1]; auto x = ... @@ -664,12 +665,6 @@ void SetVarDetail(IndexVar* var, } #endif } - // FIXME QualifiedName should return index - auto idx = def.detailed_name.rfind(short_name.begin(), std::string::npos, - short_name.size()); - assert(idx != std::string::npos); - def.short_name_offset = idx; - def.short_name_size = short_name.size(); if (is_first_seen) { std::optional var_type = @@ -1711,9 +1706,6 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // indexing the definition, then there will not be any (ie) outline // information. if (!is_template_specialization) { - // Build detailed name. The type desc looks like void (void *). We - // insert the qualified name before the first '('. - // FIXME GetFunctionSignature should set index #if CINDEX_HAVE_PRETTY std::tie(func->def.detailed_name, func->def.qual_name_offset, func->def.short_name_offset, func->def.short_name_size) = @@ -2384,7 +2376,7 @@ void Reflect(Writer& visitor, Reference& value) { char buf[99]; snprintf(buf, sizeof buf, "%s|%" PRId32 "|%d|%d", value.range.ToString().c_str(), - static_cast::type>(value.id.id), + static_cast>(value.id.id), int(value.kind), int(value.role)); std::string s(buf); Reflect(visitor, s); diff --git a/src/indexer.h b/src/indexer.h index 05c8e610..9d6a828d 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -15,7 +15,6 @@ #include "utils.h" #include -#include #include #include #include @@ -40,14 +39,13 @@ struct Id { explicit Id(RawId id) : id(id) {} // Id -> Id or Id -> Id is allowed implicitly. template ::value || - std::is_same::value, - bool>::type = false> + typename std::enable_if_t || std::is_same_v, + bool> = false> Id(Id o) : id(o.id) {} - template ::value || - std::is_same::value), - bool>::type = false> + template < + typename U, + typename std::enable_if_t || std::is_same_v), + bool> = false> explicit Id(Id o) : id(o.id) {} // Needed for google::dense_hash_map. @@ -84,15 +82,11 @@ struct SymbolIdx { bool operator==(const SymbolIdx& o) const { return id == o.id && kind == o.kind; } - bool operator!=(const SymbolIdx& o) const { return !(*this == o); } bool operator<(const SymbolIdx& o) const { - if (id != o.id) - return id < o.id; - return kind < o.kind; + return !(id == o.id) ? id < o.id : kind < o.kind; } }; MAKE_REFLECT_STRUCT(SymbolIdx, kind, id); -MAKE_HASHABLE(SymbolIdx, t.kind, t.id); struct Reference { Range range; @@ -240,7 +234,6 @@ struct IndexType { bool operator<(const IndexType& other) const { return id < other.id; } }; -MAKE_HASHABLE(IndexType, t.id); template struct FuncDef : NameMixin> { @@ -332,7 +325,6 @@ struct IndexFunc : NameMixin { bool operator<(const IndexFunc& other) const { return id < other.id; } }; -MAKE_HASHABLE(IndexFunc, t.id); MAKE_REFLECT_STRUCT(IndexFunc::Declaration, spell, param_spellings); template @@ -401,7 +393,6 @@ struct IndexVar { bool operator<(const IndexVar& other) const { return id < other.id; } }; -MAKE_HASHABLE(IndexVar, t.id); struct IdCache { std::string primary_file; diff --git a/src/lsp.cc b/src/lsp.cc index 1c1c6673..e40f2d71 100644 --- a/src/lsp.cc +++ b/src/lsp.cc @@ -200,8 +200,6 @@ lsDocumentUri lsDocumentUri::FromPath(const std::string& path) { return result; } -lsDocumentUri::lsDocumentUri() {} - bool lsDocumentUri::operator==(const lsDocumentUri& other) const { return raw_uri == other.raw_uri; } diff --git a/src/lsp.h b/src/lsp.h index 2341e87e..31ebd07b 100644 --- a/src/lsp.h +++ b/src/lsp.h @@ -89,7 +89,6 @@ struct lsResponseError { struct lsDocumentUri { static lsDocumentUri FromPath(const std::string& path); - lsDocumentUri(); bool operator==(const lsDocumentUri& other) const; void SetPath(const std::string& path); @@ -97,7 +96,6 @@ struct lsDocumentUri { std::string raw_uri; }; -MAKE_HASHABLE(lsDocumentUri, t.raw_uri); template void Reflect(TVisitor& visitor, lsDocumentUri& value) { @@ -115,7 +113,6 @@ struct lsPosition { } std::string ToString() const; }; -MAKE_HASHABLE(lsPosition, t.line, t.character); MAKE_REFLECT_STRUCT(lsPosition, line, character); struct lsRange { @@ -128,7 +125,6 @@ struct lsRange { return !(start == o.start) ? start < o.start : end < o.end; } }; -MAKE_HASHABLE(lsRange, t.start, t.end); MAKE_REFLECT_STRUCT(lsRange, start, end); struct lsLocation { @@ -142,7 +138,6 @@ struct lsLocation { : range < o.range; } }; -MAKE_HASHABLE(lsLocation, t.uri, t.range); MAKE_REFLECT_STRUCT(lsLocation, uri, range); enum class lsSymbolKind : uint8_t { @@ -358,11 +353,13 @@ MAKE_REFLECT_STRUCT(lsTextDocumentDidChangeParams, // Show a message to the user. enum class lsMessageType : int { Error = 1, Warning = 2, Info = 3, Log = 4 }; MAKE_REFLECT_TYPE_PROXY(lsMessageType) + struct Out_ShowLogMessageParams { lsMessageType type = lsMessageType::Error; std::string message; }; MAKE_REFLECT_STRUCT(Out_ShowLogMessageParams, type, message); + struct Out_ShowLogMessage : public lsOutMessage { enum class DisplayType { Show, Log }; DisplayType display_type = DisplayType::Show; @@ -370,6 +367,7 @@ struct Out_ShowLogMessage : public lsOutMessage { std::string method(); Out_ShowLogMessageParams params; }; + template void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) { REFLECT_MEMBER_START(); diff --git a/src/message_handler.cc b/src/message_handler.cc index 7c0fd96e..b43f8764 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -9,6 +9,8 @@ #include +MAKE_HASHABLE(SymbolIdx, t.kind, t.id); + namespace { struct Out_CclsSetInactiveRegion diff --git a/src/messages/ccls_random.cc b/src/messages/ccls_random.cc index ce0bb437..80f1e85a 100644 --- a/src/messages/ccls_random.cc +++ b/src/messages/ccls_random.cc @@ -6,6 +6,8 @@ #include #include +MAKE_HASHABLE(SymbolIdx, t.kind, t.id); + namespace { MethodType kMethodType = "$ccls/random"; diff --git a/src/messages/text_document_code_lens.cc b/src/messages/text_document_code_lens.cc index cbe065cb..3251e3f2 100644 --- a/src/messages/text_document_code_lens.cc +++ b/src/messages/text_document_code_lens.cc @@ -59,13 +59,10 @@ void AddCodeLens(const char* singular, code_lens.command->arguments.position = code_lens.range.start; // Add unique uses. - std::unordered_set unique_uses; + std::vector unique_uses; for (Use use1 : uses) { - std::optional location = - GetLsLocation(common->db, common->working_files, use1); - if (!location) - continue; - unique_uses.insert(*location); + if (auto ls_loc = GetLsLocation(common->db, common->working_files, use1)) + unique_uses.push_back(*ls_loc); } code_lens.command->arguments.locations.assign(unique_uses.begin(), unique_uses.end()); diff --git a/src/messages/text_document_document_symbol.cc b/src/messages/text_document_document_symbol.cc index 09f6723b..b3461029 100644 --- a/src/messages/text_document_document_symbol.cc +++ b/src/messages/text_document_document_symbol.cc @@ -41,7 +41,7 @@ struct Handler_TextDocumentDocumentSymbol for (SymbolRef sym : file->def->outline) { std::optional info = - GetSymbolInfo(db, working_files, sym, true /*use_short_name*/); + GetSymbolInfo(db, working_files, sym, false); if (!info) continue; if (sym.kind == SymbolKind::Var) { diff --git a/src/messages/workspace_symbol.cc b/src/messages/workspace_symbol.cc index e556ba93..173a53d9 100644 --- a/src/messages/workspace_symbol.cc +++ b/src/messages/workspace_symbol.cc @@ -20,13 +20,12 @@ bool InsertSymbolIntoResult(QueryDatabase* db, SymbolIdx symbol, std::vector* result) { std::optional info = - GetSymbolInfo(db, working_files, symbol, false /*use_short_name*/); + GetSymbolInfo(db, working_files, symbol, true); if (!info) return false; - Maybe location = GetDefinitionExtent(db, symbol); Use loc; - if (location) + if (Maybe location = GetDefinitionExtent(db, symbol)) loc = *location; else { auto decls = GetNonDefDeclarations(db, symbol); diff --git a/src/query_utils.cc b/src/query_utils.cc index c77b180d..4c213568 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -284,7 +284,7 @@ lsSymbolKind GetSymbolKind(QueryDatabase* db, SymbolIdx sym) { std::optional GetSymbolInfo(QueryDatabase* db, WorkingFiles* working_files, SymbolIdx sym, - bool use_short_name) { + bool detailed_name) { switch (sym.kind) { case SymbolKind::Invalid: break; @@ -301,10 +301,10 @@ std::optional GetSymbolInfo(QueryDatabase* db, default: { lsSymbolInformation info; EachEntityDef(db, sym, [&](const auto& def) { - if (use_short_name) - info.name = def.Name(true); - else + if (detailed_name) info.name = def.detailed_name; + else + info.name = def.Name(true); info.kind = def.kind; info.containerName = def.detailed_name; return false; diff --git a/src/query_utils.h b/src/query_utils.h index 94ef7009..5b00a7db 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -46,7 +46,7 @@ std::vector GetLsLocationExs(QueryDatabase* db, std::optional GetSymbolInfo(QueryDatabase* db, WorkingFiles* working_files, SymbolIdx sym, - bool use_short_name); + bool detailed_name); std::vector FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, diff --git a/src/serializer.h b/src/serializer.h index 4ea92cc7..d7a6ed9e 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -76,7 +76,7 @@ struct IndexFile; #define REFLECT_MEMBER2(name, value) ReflectMember(visitor, name, value) #define MAKE_REFLECT_TYPE_PROXY(type_name) \ - MAKE_REFLECT_TYPE_PROXY2(type_name, std::underlying_type::type) + MAKE_REFLECT_TYPE_PROXY2(type_name, std::underlying_type_t) #define MAKE_REFLECT_TYPE_PROXY2(type, as_type) \ ATTRIBUTE_UNUSED inline void Reflect(Reader& visitor, type& value) { \ as_type value0; \ @@ -243,19 +243,13 @@ struct ReflectVariant { // If T appears in Ts..., we should set the value of std::variant to // what we get from Reader. template - typename std::enable_if...>::value, - void>::type - ReflectTag(Reader& visitor, std::variant& value) { - T a; - Reflect(visitor, a); - value = std::move(a); + void ReflectTag(Reader& visitor, std::variant& value) { + if constexpr (std::disjunction_v...>) { + T a; + Reflect(visitor, a); + value = std::move(a); + } } - // This SFINAE overload is used to prevent compile error. value = a; is not - // allowed if T does not appear in Ts... - template - typename std::enable_if...>::value, - void>::type - ReflectTag(Reader&, std::variant&) {} void operator()(Reader& visitor, std::variant& value) { // Based on tag dispatch, call different ReflectTag helper. @@ -263,7 +257,7 @@ struct ReflectVariant { ReflectTag(visitor, value); // It is possible that IsInt64() && IsInt(). We don't call ReflectTag // if int is not in Ts... - else if (std::disjunction...>::value && visitor.IsInt()) + else if (std::disjunction_v...> && visitor.IsInt()) ReflectTag(visitor, value); else if (visitor.IsInt64()) ReflectTag(visitor, value); diff --git a/src/working_files.cc b/src/working_files.cc index cdc5e5ef..1bd00aa1 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -256,8 +256,7 @@ void WorkingFile::ComputeLineMapping() { // For index line i, set index_to_buffer[i] to -1 if line i is duplicated. int i = 0; for (auto& line : index_lines) { - std::string trimmed = Trim(line); - uint64_t h = HashUsr(trimmed); + uint64_t h = HashUsr(line); auto it = hash_to_unique.find(h); if (it == hash_to_unique.end()) { hash_to_unique[h] = i; @@ -274,8 +273,7 @@ void WorkingFile::ComputeLineMapping() { i = 0; hash_to_unique.clear(); for (auto& line : buffer_lines) { - std::string trimmed = Trim(line); - uint64_t h = HashUsr(trimmed); + uint64_t h = HashUsr(line); auto it = hash_to_unique.find(h); if (it == hash_to_unique.end()) { hash_to_unique[h] = i;