From 77dca1936be33e229b83434215ba5da3c5cefcf0 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 30 Jan 2018 23:01:45 -0800 Subject: [PATCH] Change TypeDefDefinition::short_name from string to string_view --- src/indexer.cc | 33 ++++++++++++++--------- src/indexer.h | 20 +++++++++----- src/messages/cquery_member_hierarchy.cc | 2 +- src/messages/text_document_code_action.cc | 4 +-- src/query.cc | 5 ++-- src/query_utils.cc | 4 +-- src/serializer.cc | 5 ++-- 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 9d4d10b7..1d0e72c9 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -411,11 +411,16 @@ void SetTypeName(IndexType* type, CXIdxContainerInfo parent; // |name| can be null in an anonymous struct (see // tests/types/anonymous_struct.cc). - type->def.short_name = name ? name : "(anon)"; + if (!name) + name = "(anon)"; if (!container) parent.cursor = cursor.get_semantic_parent().cx_cursor; type->def.detailed_name = - ns->QualifiedName(container ? container : &parent, type->def.short_name); + ns->QualifiedName(container ? container : &parent, name); + auto idx = type->def.detailed_name.find(name); + assert(idx != std::string::npos); + type->def.short_name_offset = idx; + type->def.short_name_size = strlen(name); } // Finds the cursor associated with the declaration type of |cursor|. This @@ -452,7 +457,7 @@ optional ResolveToDeclarationType(IndexFile* db, clang_disposeString(cx_usr); IndexTypeId type_id = db->ToTypeId(usr); IndexType* typ = db->Resolve(type_id); - if (typ->def.short_name.empty()) { + if (typ->def.detailed_name.empty()) { std::string name = declaration.get_spelling(); SetTypeName(typ, declaration, nullptr, name.c_str(), ns); } @@ -846,7 +851,7 @@ void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor, if (param->toplevel_type) { IndexType* ref_type = db->Resolve(*param->toplevel_type); std::string name = cursor.get_referenced().get_spelling(); - if (name == ref_type->def.short_name) { + if (name == ref_type->def.ShortName()) { UniqueAdd(ref_type->uses, cursor.get_spelling_range()); param->toplevel_type = nullopt; return; @@ -1240,11 +1245,12 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, // CXCursor_TemplateTemplateParameter can be visited by visiting // CXCursor_TranslationUnit, but not (confirm this) by visiting // {Class,Function}Template. Thus we need to initialize it here. - if (ref_index->def.short_name.empty()) { + if (ref_index->def.detailed_name.empty()) { ref_index->def.definition_spelling = ref_cursor.get_spelling_range(); ref_index->def.definition_extent = ref_cursor.get_extent(); - ref_index->def.short_name = ref_cursor.get_spelling(); - ref_index->def.detailed_name = ref_index->def.short_name; + ref_index->def.detailed_name = ref_cursor.get_spelling(); + ref_index->def.short_name_offset = 0; + ref_index->def.short_name_size = ref_index->def.detailed_name.size(); } UniqueAdd(ref_index->uses, cursor.get_spelling_range()); } @@ -1260,11 +1266,12 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, // CXCursor_TemplateTypeParameter can be visited by visiting // CXCursor_TranslationUnit, but not (confirm this) by visiting // {Class,Function}Template. Thus we need to initialize it here. - if (ref_index->def.short_name.empty()) { + if (ref_index->def.detailed_name.empty()) { ref_index->def.definition_spelling = ref_cursor.get_spelling_range(); ref_index->def.definition_extent = ref_cursor.get_extent(); - ref_index->def.short_name = ref_cursor.get_spelling(); - ref_index->def.detailed_name = ref_index->def.short_name; + ref_index->def.detailed_name = ref_cursor.get_spelling(); + ref_index->def.short_name_offset = 0; + ref_index->def.short_name_size = ref_index->def.detailed_name.size(); } UniqueAdd(ref_index->uses, cursor.get_spelling_range()); } @@ -1378,7 +1385,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { IndexTypeId ns_id = db->ToTypeId(HashUsr(decl->entityInfo->USR)); IndexType* ns = db->Resolve(ns_id); ns->def.kind = GetSymbolKind(decl->entityInfo->kind); - if (ns->def.short_name.empty()) { + if (ns->def.detailed_name.empty()) { SetTypeName(ns, decl_cursor, decl->semanticContainer, decl->entityInfo->name, ¶m->ns); ns->def.definition_spelling = decl_spell; @@ -1717,9 +1724,9 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // template class function; // not visited by // OnIndexDeclaration template<> class function {}; // current // cursor - if (origin->def.short_name.empty()) { + if (origin->def.detailed_name.empty()) { SetTypeName(origin, origin_cursor, nullptr, - type->def.short_name.c_str(), ns); + &type->def.ShortName()[0], ns); origin->def.kind = type->def.kind; } // TODO The name may be assigned in |ResolveToDeclarationType| but diff --git a/src/indexer.h b/src/indexer.h index 28560fed..83a19a5c 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -154,7 +154,6 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) { template struct TypeDefDefinitionData { // General metadata. - std::string short_name; std::string detailed_name; ClangSymbolKind kind = ClangSymbolKind::Unknown; optional hover; @@ -184,6 +183,9 @@ struct TypeDefDefinitionData { std::vector funcs; std::vector vars; + int16_t short_name_offset = 0; + int16_t short_name_size = 0; + bool operator==( const TypeDefDefinitionData& other) const { return detailed_name == other.detailed_name && hover == other.hover && @@ -198,6 +200,11 @@ struct TypeDefDefinitionData { const TypeDefDefinitionData& other) const { return !(*this == other); } + + std::string_view ShortName() const { + return std::string_view(detailed_name.c_str() + short_name_offset, + short_name_size); + } }; template & value) { REFLECT_MEMBER_START(); - REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); + REFLECT_MEMBER(short_name_offset); + REFLECT_MEMBER(short_name_size); REFLECT_MEMBER(kind); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); @@ -273,8 +281,8 @@ struct FuncDefDefinitionData { // Functions that this function calls. std::vector callees; - int16_t short_name_offset; - int16_t short_name_size; + int16_t short_name_offset = 0; + int16_t short_name_size = 0; ClangSymbolKind kind = ClangSymbolKind::Unknown; StorageClass storage = StorageClass::Invalid; @@ -392,8 +400,8 @@ struct VarDefDefinitionData { // Function/type which declares this one. size_t parent_id = size_t(-1); - int16_t short_name_offset; - int16_t short_name_size; + int16_t short_name_offset = 0; + int16_t short_name_size = 0; SymbolKind parent_kind = SymbolKind::Invalid; ClangSymbolKind kind = ClangSymbolKind::Unknown; diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index 59cb9fa2..08ad4d0e 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -47,7 +47,7 @@ BuildInitial(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) { return {}; Out_CqueryMemberHierarchy::Entry entry; - entry.name = root_type.def->short_name; + entry.name = std::string(root_type.def->ShortName()); entry.type_id = root.id; entry.location = *def_loc; return {entry}; diff --git a/src/messages/text_document_code_action.cc b/src/messages/text_document_code_action.cc index d8724575..78cf6aad 100644 --- a/src/messages/text_document_code_action.cc +++ b/src/messages/text_document_code_action.cc @@ -161,7 +161,7 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, if (func.def->declaring_type) { QueryType& declaring_type = db->types[func.def->declaring_type->id]; if (declaring_type.def) { - type_name = declaring_type.def->short_name; + type_name = std::string(declaring_type.def->ShortName()); optional ls_type_def_extent = GetLsRange( working_file, declaring_type.def->definition_extent->range); if (ls_type_def_extent) { @@ -390,7 +390,7 @@ struct TextDocumentCodeActionHandler command.arguments.textDocumentUri = *impl_uri; command.title = "Auto-Implement " + std::to_string(num_edits) + - " methods on " + type.def->short_name; + " methods on " + std::string(type.def->ShortName()); command.command = "cquery._autoImplement"; out.result.push_back(command); break; diff --git a/src/query.cc b/src/query.cc index e961dd92..27a41c4d 100644 --- a/src/query.cc +++ b/src/query.cc @@ -36,8 +36,9 @@ optional ToQuery(const IdMap& id_map, return nullopt; QueryType::Def result; - result.short_name = type.short_name; result.detailed_name = type.detailed_name; + result.short_name_offset = type.short_name_offset; + result.short_name_size = type.short_name_size; result.kind = type.kind; result.hover = type.hover; result.comments = type.comments; @@ -883,7 +884,7 @@ void QueryDatabase::ImportOrUpdate( existing.def = def.value; UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Type, - it->second.id, def.value.short_name, + it->second.id, std::string(def.value.ShortName()), def.value.detailed_name); } } diff --git a/src/query_utils.cc b/src/query_utils.cc index cd349d91..f6b472c0 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -440,8 +440,8 @@ optional GetSymbolInfo(QueryDatabase* db, lsSymbolInformation info; info.name = - use_short_name ? type.def->short_name : type.def->detailed_name; - if (type.def->detailed_name != type.def->short_name) + use_short_name ? std::string(type.def->ShortName()) : type.def->detailed_name; + if (type.def->detailed_name != type.def->ShortName()) info.containerName = type.def->detailed_name; // TODO ClangSymbolKind -> lsSymbolKind switch (type.def->kind) { diff --git a/src/serializer.cc b/src/serializer.cc index 8c6e1340..3876ee44 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -155,8 +155,9 @@ void Reflect(TVisitor& visitor, IndexType& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER2("id", value.id); REFLECT_MEMBER2("usr", value.usr); - REFLECT_MEMBER2("short_name", value.def.short_name); REFLECT_MEMBER2("detailed_name", value.def.detailed_name); + REFLECT_MEMBER2("short_name_offset", value.def.short_name_offset); + REFLECT_MEMBER2("short_name_size", value.def.short_name_size); REFLECT_MEMBER2("kind", value.def.kind); REFLECT_MEMBER2("hover", value.def.hover); REFLECT_MEMBER2("comments", value.def.comments); @@ -224,7 +225,7 @@ bool ReflectMemberStart(Writer& visitor, IndexFile& value) { // FIXME auto it = value.id_cache.usr_to_type_id.find(HashUsr("")); if (it != value.id_cache.usr_to_type_id.end()) { - value.Resolve(it->second)->def.short_name = ""; + value.Resolve(it->second)->def.detailed_name = ""; assert(value.Resolve(it->second)->uses.size() == 0); }