From 42bcf2b58f14b70a77f7f15e34b5642c63eb309c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 28 May 2018 17:03:14 -0700 Subject: [PATCH] $ccls/memberHierarchy: add field offset --- .../declaration_vs_definition/class_member.cc | 5 ++- .../class_member_static.cc | 5 ++- index_tests/namespaces/namespace_alias.cc | 5 ++- index_tests/namespaces/namespace_reference.cc | 5 ++- .../implicit_variable_instantiation.cc | 13 +++++- .../templates/member_ref_in_template.cc | 5 ++- ...ass_template_func_usage_folded_into_one.cc | 8 +++- ...ace_template_type_usage_folded_into_one.cc | 8 +++- index_tests/types/anonymous_struct.cc | 16 ++++++- index_tests/unions/union_decl.cc | 8 +++- index_tests/unions/union_usage.cc | 8 +++- .../usage/func_usage_class_inline_var_def.cc | 5 ++- index_tests/usage/type_usage_declare_field.cc | 8 +++- index_tests/usage/usage_inside_of_call.cc | 8 +++- index_tests/usage/var_usage_class_member.cc | 8 +++- index_tests/usage/var_usage_cstyle_cast.cc | 5 ++- index_tests/vars/class_member.cc | 5 ++- index_tests/vars/class_static_member.cc | 5 ++- src/indexer.cc | 5 ++- src/indexer.h | 2 +- src/messages/ccls_member_hierarchy.cc | 28 +++++++++--- src/messages/ccls_random.cc | 2 +- src/project.cc | 5 +-- src/serializer.h | 45 +++++++++++-------- src/serializers/binary.h | 12 ++--- src/serializers/json.h | 4 +- 26 files changed, 173 insertions(+), 60 deletions(-) diff --git a/index_tests/declaration_vs_definition/class_member.cc b/index_tests/declaration_vs_definition/class_member.cc index 02495c02..b0feb85b 100644 --- a/index_tests/declaration_vs_definition/class_member.cc +++ b/index_tests/declaration_vs_definition/class_member.cc @@ -37,7 +37,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [9736582033442720743], + "vars": [{ + "L": 9736582033442720743, + "R": 0 + }], "instances": [], "uses": [] }], diff --git a/index_tests/declaration_vs_definition/class_member_static.cc b/index_tests/declaration_vs_definition/class_member_static.cc index 7b4e00ec..4abc4523 100644 --- a/index_tests/declaration_vs_definition/class_member_static.cc +++ b/index_tests/declaration_vs_definition/class_member_static.cc @@ -39,7 +39,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [8942920329766232482], + "vars": [{ + "L": 8942920329766232482, + "R": -1 + }], "instances": [], "uses": ["5:5-5:8|0|1|4"] }], diff --git a/index_tests/namespaces/namespace_alias.cc b/index_tests/namespaces/namespace_alias.cc index 8a7e8ef6..50af6291 100644 --- a/index_tests/namespaces/namespace_alias.cc +++ b/index_tests/namespaces/namespace_alias.cc @@ -113,7 +113,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [15042442838933090518], + "vars": [{ + "L": 15042442838933090518, + "R": -1 + }], "instances": [], "uses": ["3:20-3:23|17805385787823406700|2|4", "9:27-9:30|0|1|4", "12:21-12:24|10818727483146447186|3|4"] }, { diff --git a/index_tests/namespaces/namespace_reference.cc b/index_tests/namespaces/namespace_reference.cc index 3ffedd6d..73d50bd9 100644 --- a/index_tests/namespaces/namespace_reference.cc +++ b/index_tests/namespaces/namespace_reference.cc @@ -76,7 +76,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [17328473273923617489], - "vars": [12898699035586282159], + "vars": [{ + "L": 12898699035586282159, + "R": -1 + }], "instances": [], "uses": ["1:11-1:13|0|1|4", "7:3-7:5|631910859630953711|3|4", "7:14-7:16|631910859630953711|3|4", "8:19-8:21|631910859630953711|3|4"] }, { diff --git a/index_tests/templates/implicit_variable_instantiation.cc b/index_tests/templates/implicit_variable_instantiation.cc index 845721cb..b45508b8 100644 --- a/index_tests/templates/implicit_variable_instantiation.cc +++ b/index_tests/templates/implicit_variable_instantiation.cc @@ -81,7 +81,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [12898699035586282159, 9008550860229740818], + "vars": [{ + "L": 12898699035586282159, + "R": -1 + }, { + "L": 9008550860229740818, + "R": -1 + }], "instances": [], "uses": ["1:11-1:13|0|1|4"] }, { @@ -98,7 +104,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [4731849186641714451], + "vars": [{ + "L": 4731849186641714451, + "R": -1 + }], "instances": [], "uses": ["10:26-10:32|0|1|4", "13:13-13:19|0|1|4", "14:14-14:20|0|1|4"] }, { diff --git a/index_tests/templates/member_ref_in_template.cc b/index_tests/templates/member_ref_in_template.cc index 43c19f79..fd71e2db 100644 --- a/index_tests/templates/member_ref_in_template.cc +++ b/index_tests/templates/member_ref_in_template.cc @@ -70,7 +70,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [8905286151237717330], - "vars": [5866801090710377175], + "vars": [{ + "L": 5866801090710377175, + "R": -1 + }], "instances": [], "uses": [] }, { diff --git a/index_tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc b/index_tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc index 3880a11d..a6a609ce 100644 --- a/index_tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc +++ b/index_tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc @@ -62,7 +62,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [15768138241775955040, 3182917058194750998], + "vars": [{ + "L": 15768138241775955040, + "R": -1 + }, { + "L": 3182917058194750998, + "R": -1 + }], "instances": [], "uses": ["1:11-1:13|0|1|4"] }, { diff --git a/index_tests/templates/namespace_template_type_usage_folded_into_one.cc b/index_tests/templates/namespace_template_type_usage_folded_into_one.cc index 4c00e09a..3b7f23e7 100644 --- a/index_tests/templates/namespace_template_type_usage_folded_into_one.cc +++ b/index_tests/templates/namespace_template_type_usage_folded_into_one.cc @@ -26,7 +26,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [15768138241775955040, 3182917058194750998], + "vars": [{ + "L": 15768138241775955040, + "R": -1 + }, { + "L": 3182917058194750998, + "R": -1 + }], "instances": [], "uses": ["1:11-1:13|0|1|4"] }, { diff --git a/index_tests/types/anonymous_struct.cc b/index_tests/types/anonymous_struct.cc index 362b11a4..087cd8de 100644 --- a/index_tests/types/anonymous_struct.cc +++ b/index_tests/types/anonymous_struct.cc @@ -38,7 +38,16 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [3348817847649945564, 4821094820988543895, 15292551660437765731], + "vars": [{ + "L": 3348817847649945564, + "R": 0 + }, { + "L": 4821094820988543895, + "R": 32 + }, { + "L": 15292551660437765731, + "R": 64 + }], "instances": [], "uses": [] }, { @@ -55,7 +64,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [1963212417280098348], + "vars": [{ + "L": 1963212417280098348, + "R": 0 + }], "instances": [], "uses": [] }], diff --git a/index_tests/unions/union_decl.cc b/index_tests/unions/union_decl.cc index ad1c9e7e..87949e04 100644 --- a/index_tests/unions/union_decl.cc +++ b/index_tests/unions/union_decl.cc @@ -53,7 +53,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [9529311430721959843, 8804696910588009104], + "vars": [{ + "L": 9529311430721959843, + "R": 0 + }, { + "L": 8804696910588009104, + "R": 0 + }], "instances": [], "uses": [] }], diff --git a/index_tests/unions/union_usage.cc b/index_tests/unions/union_usage.cc index 3de937b6..3329ecd5 100644 --- a/index_tests/unions/union_usage.cc +++ b/index_tests/unions/union_usage.cc @@ -77,7 +77,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [9529311430721959843, 8804696910588009104], + "vars": [{ + "L": 9529311430721959843, + "R": 0 + }, { + "L": 8804696910588009104, + "R": 0 + }], "instances": [2933643612409209903], "uses": ["6:1-6:4|0|1|4", "8:10-8:13|0|1|4"] }], diff --git a/index_tests/usage/func_usage_class_inline_var_def.cc b/index_tests/usage/func_usage_class_inline_var_def.cc index 2c5c3db3..3d62897e 100644 --- a/index_tests/usage/func_usage_class_inline_var_def.cc +++ b/index_tests/usage/func_usage_class_inline_var_def.cc @@ -57,7 +57,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [4220150017963593039], + "vars": [{ + "L": 4220150017963593039, + "R": 0 + }], "instances": [], "uses": [] }], diff --git a/index_tests/usage/type_usage_declare_field.cc b/index_tests/usage/type_usage_declare_field.cc index d5814ff9..e2401832 100644 --- a/index_tests/usage/type_usage_declare_field.cc +++ b/index_tests/usage/type_usage_declare_field.cc @@ -58,7 +58,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [14314859014962085433, 14727441168849658842], + "vars": [{ + "L": 14314859014962085433, + "R": 0 + }, { + "L": 14727441168849658842, + "R": 64 + }], "instances": [], "uses": [] }], diff --git a/index_tests/usage/usage_inside_of_call.cc b/index_tests/usage/usage_inside_of_call.cc index 93875baf..9642d1d9 100644 --- a/index_tests/usage/usage_inside_of_call.cc +++ b/index_tests/usage/usage_inside_of_call.cc @@ -93,7 +93,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [9648311402855509901, 11489549839875479478], + "vars": [{ + "L": 9648311402855509901, + "R": 0 + }, { + "L": 11489549839875479478, + "R": -1 + }], "instances": [], "uses": ["10:5-10:8|0|1|4", "14:22-14:25|0|1|4", "14:40-14:43|0|1|4"] }], diff --git a/index_tests/usage/var_usage_class_member.cc b/index_tests/usage/var_usage_class_member.cc index 9eeeb6e8..d10749eb 100644 --- a/index_tests/usage/var_usage_class_member.cc +++ b/index_tests/usage/var_usage_class_member.cc @@ -96,7 +96,13 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [4220150017963593039, 3873837747174060388], + "vars": [{ + "L": 4220150017963593039, + "R": 0 + }, { + "L": 3873837747174060388, + "R": 32 + }], "instances": [14669930844300034456], "uses": ["11:3-11:6|0|1|4"] }], diff --git a/index_tests/usage/var_usage_cstyle_cast.cc b/index_tests/usage/var_usage_cstyle_cast.cc index 21839e12..b554c29e 100644 --- a/index_tests/usage/var_usage_cstyle_cast.cc +++ b/index_tests/usage/var_usage_cstyle_cast.cc @@ -44,7 +44,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [7057400933868440116], + "vars": [{ + "L": 7057400933868440116, + "R": -1 + }], "instances": [], "uses": ["7:15-7:21|0|1|4"] }], diff --git a/index_tests/vars/class_member.cc b/index_tests/vars/class_member.cc index 548da980..191c62ca 100644 --- a/index_tests/vars/class_member.cc +++ b/index_tests/vars/class_member.cc @@ -21,7 +21,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [13799811842374292251], + "vars": [{ + "L": 13799811842374292251, + "R": 0 + }], "instances": [13799811842374292251], "uses": ["2:3-2:6|0|1|4"] }], diff --git a/index_tests/vars/class_static_member.cc b/index_tests/vars/class_static_member.cc index ba6fbbe7..426090c8 100644 --- a/index_tests/vars/class_static_member.cc +++ b/index_tests/vars/class_static_member.cc @@ -23,7 +23,10 @@ OUTPUT: "derived": [], "types": [], "funcs": [], - "vars": [5844987037615239736], + "vars": [{ + "L": 5844987037615239736, + "R": -1 + }], "instances": [5844987037615239736], "uses": ["2:10-2:13|0|1|4", "4:1-4:4|0|1|4", "4:6-4:9|0|1|4"] }], diff --git a/src/indexer.cc b/src/indexer.cc index 0794a5f3..48dffa65 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -681,7 +681,7 @@ void OnIndexReference_Function(IndexFile* db, } // namespace // static -const int IndexFile::kMajorVersion = 15; +const int IndexFile::kMajorVersion = 16; const int IndexFile::kMinorVersion = 0; IndexFile::IndexFile(const std::string& path, const std::string& contents) @@ -1491,8 +1491,9 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { } case SymbolKind::Type: if (decl->semanticContainer->cursor.kind != CXCursor_EnumDecl) { + long offset = clang_Cursor_getOffsetOfField(cursor.cx_cursor); db->ToType(decl->semanticContainer->cursor) - .def.vars.push_back(var.usr); + .def.vars.emplace_back(var.usr, offset); } break; default: diff --git a/src/indexer.h b/src/indexer.h index ae29f918..d79cd66e 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -171,7 +171,7 @@ struct TypeDef : NameMixin { // Types, functions, and variables defined in this type. std::vector types; std::vector funcs; - std::vector vars; + std::vector> vars; // If set, then this is the same underlying type as the given value (ie, this // type comes from a using or typedef statement). diff --git a/src/messages/ccls_member_hierarchy.cc b/src/messages/ccls_member_hierarchy.cc index 2ac6045d..6cc9b755 100644 --- a/src/messages/ccls_member_hierarchy.cc +++ b/src/messages/ccls_member_hierarchy.cc @@ -74,17 +74,29 @@ bool Expand(MessageHandler* m, void DoField(MessageHandler* m, Out_CclsMemberHierarchy::Entry* entry, const QueryVar& var, + int64_t offset, bool qualified, int levels) { const QueryVar::Def* def1 = var.AnyDef(); if (!def1) return; Out_CclsMemberHierarchy::Entry entry1; + // With multiple inheritance, the offset is incorrect. + if (offset >= 0) { + if (offset / 8 < 10) + entry1.fieldName += ' '; + entry1.fieldName += std::to_string(offset / 8); + if (offset % 8) { + entry1.fieldName += '.'; + entry1.fieldName += std::to_string(offset % 8); + } + entry1.fieldName += ' '; + } if (qualified) - entry1.fieldName = def1->detailed_name; + entry1.fieldName += def1->detailed_name; else - entry1.fieldName = def1->detailed_name.substr(0, def1->qual_name_offset) + - std::string(def1->Name(false)); + entry1.fieldName += def1->detailed_name.substr(0, def1->qual_name_offset) + + std::string(def1->Name(false)); if (def1->spell) { if (std::optional loc = GetLsLocation(m->db, m->working_files, *def1->spell)) @@ -158,9 +170,11 @@ bool Expand(MessageHandler* m, entry->children.push_back(std::move(entry1)); } } else { - EachDefinedEntity(m->db->usr2var, def->vars, [&](QueryVar& var) { - DoField(m, entry, var, qualified, levels - 1); - }); + for (auto it : def->vars) { + QueryVar& var = m->db->usr2var[it.first]; + if (!var.def.empty()) + DoField(m, entry, var, it.second, qualified, levels - 1); + } } } } @@ -195,7 +209,7 @@ struct Handler_CclsMemberHierarchy entry.location = *loc; } EachDefinedEntity(db->usr2var, def->vars, [&](QueryVar& var) { - DoField(this, &entry, var, qualified, levels - 1); + DoField(this, &entry, var, -1, qualified, levels - 1); }); return entry; } diff --git a/src/messages/ccls_random.cc b/src/messages/ccls_random.cc index 382bba34..a5fabdb2 100644 --- a/src/messages/ccls_random.cc +++ b/src/messages/ccls_random.cc @@ -80,7 +80,7 @@ struct Handler_CclsRandom : BaseMessageHandler { Add(sym2id, adj, it.second.instances, n); Add(sym2id, adj, def->funcs, n); Add(sym2id, adj, def->types, n); - Add(sym2id, adj, def->vars, n); + //Add(sym2id, adj, def->vars, n); n++; } for (auto& it : db->usr2var) diff --git a/src/project.cc b/src/project.cc index 90abbb8e..a7d0534b 100644 --- a/src/project.cc +++ b/src/project.cc @@ -269,9 +269,8 @@ std::vector LoadCompilationEntriesFromDirectory( #endif } if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) { - LOG_S(INFO) << "unable to load " << Path.c_str() - << "; using directory listing instead."; - return LoadFromDirectoryListing(project); + LOG_S(WARNING) << "unable to load " << Path.c_str(); + return {}; } LOG_S(INFO) << "loaded " << Path.c_str(); diff --git a/src/serializer.h b/src/serializer.h index 28f28481..d2ee1191 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -48,7 +48,7 @@ class Reader { virtual std::unique_ptr operator[](const char* x) = 0; virtual void IterArray(std::function fn) = 0; - virtual void DoMember(const char* name, std::function fn) = 0; + virtual void Member(const char* name, std::function fn) = 0; }; class Writer { @@ -75,8 +75,8 @@ class Writer { struct IndexFile; -#define REFLECT_MEMBER_START() ReflectMemberStart(visitor, value) -#define REFLECT_MEMBER_END() ReflectMemberEnd(visitor, value); +#define REFLECT_MEMBER_START() ReflectMemberStart(visitor) +#define REFLECT_MEMBER_END() ReflectMemberEnd(visitor); #define REFLECT_MEMBER(name) ReflectMember(visitor, #name, value.name) #define REFLECT_MEMBER_MANDATORY_OPTIONAL(name) \ ReflectMember(visitor, #name, value.name, mandatory_optional_tag{}) @@ -265,6 +265,19 @@ void ReflectMember(Writer& visitor, Reflect(visitor, value); } +template +void Reflect(Reader& vis, std::pair& v) { + vis.Member("L", [&]() { Reflect(vis, v.first); }); + vis.Member("R", [&]() { Reflect(vis, v.second); }); +} +template +void Reflect(Writer& vis, std::pair& v) { + vis.StartObject(); + ReflectMember(vis, "L", v.first); + ReflectMember(vis, "R", v.second); + vis.EndObject(); +} + // std::vector template void Reflect(Reader& visitor, std::vector& values) { @@ -284,31 +297,27 @@ void Reflect(Writer& visitor, std::vector& values) { // ReflectMember -template -bool ReflectMemberStart(Reader& visitor, T& value) { +inline bool ReflectMemberStart(Reader& vis) { return false; } -template -bool ReflectMemberStart(Writer& visitor, T& value) { - visitor.StartObject(); +inline bool ReflectMemberStart(Writer& vis) { + vis.StartObject(); return true; } -template -void ReflectMemberEnd(Reader& visitor, T& value) {} -template -void ReflectMemberEnd(Writer& visitor, T& value) { - visitor.EndObject(); +inline void ReflectMemberEnd(Reader& vis) {} +inline void ReflectMemberEnd(Writer& vis) { + vis.EndObject(); } template -void ReflectMember(Reader& visitor, const char* name, T& value) { - visitor.DoMember(name, [&](Reader& child) { Reflect(child, value); }); +void ReflectMember(Reader& vis, const char* name, T& v) { + vis.Member(name, [&]() { Reflect(vis, v); }); } template -void ReflectMember(Writer& visitor, const char* name, T& value) { - visitor.Key(name); - Reflect(visitor, value); +void ReflectMember(Writer& vis, const char* name, T& v) { + vis.Key(name); + Reflect(vis, v); } // API diff --git a/src/serializers/binary.h b/src/serializers/binary.h index ad6d64fe..821c4367 100644 --- a/src/serializers/binary.h +++ b/src/serializers/binary.h @@ -39,10 +39,10 @@ class BinaryReader : public Reader { // Abuse how the function is called in serializer.h bool IsNull() override { return !*p_++; } bool IsInt() override { return true; } - bool IsInt64() override {return true;} - bool IsUInt64() override {return true;} - bool IsDouble() override {return true;}; - bool IsString() override {return true;} + bool IsInt64() override { return true; } + bool IsUInt64() override { return true; } + bool IsDouble() override { return true; } + bool IsString() override { return true; } void GetNull() override {} bool GetBool() override { return Get(); } @@ -69,8 +69,8 @@ class BinaryReader : public Reader { fn(*this); } - void DoMember(const char*, std::function fn) override { - fn(*this); + void Member(const char*, std::function fn) override { + fn(); } }; diff --git a/src/serializers/json.h b/src/serializers/json.h index d4f25f48..b5c84c04 100644 --- a/src/serializers/json.h +++ b/src/serializers/json.h @@ -51,13 +51,13 @@ class JsonReader : public Reader { path_.pop_back(); } - void DoMember(const char* name, std::function fn) override { + void Member(const char* name, std::function fn) override { path_.push_back(name); auto it = m_->FindMember(name); if (it != m_->MemberEnd()) { auto saved = m_; m_ = &it->value; - fn(*this); + fn(); m_ = saved; } path_.pop_back();