From c166f3bca8a6cad2234756932e44b7b5d07f3192 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 25 Feb 2018 19:10:02 -0800 Subject: [PATCH] Simplify hierarchies --- src/messages/cquery_call_hierarchy.cc | 48 ++++++++------------ src/messages/cquery_inheritance_hierarchy.cc | 8 ++-- src/messages/cquery_member_hierarchy.cc | 19 ++++---- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/messages/cquery_call_hierarchy.cc b/src/messages/cquery_call_hierarchy.cc index 9b8e2c63..7b5b66d5 100644 --- a/src/messages/cquery_call_hierarchy.cc +++ b/src/messages/cquery_call_hierarchy.cc @@ -83,7 +83,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy::Entry, children); MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result); -void Expand(MessageHandler* m, +bool Expand(MessageHandler* m, Out_CqueryCallHierarchy::Entry* entry, bool callee, CallType call_type, @@ -93,27 +93,15 @@ void Expand(MessageHandler* m, const QueryFunc::Def* def = func.AnyDef(); entry->numChildren = 0; if (!def) - return; + return false; auto handle = [&](Use use, CallType call_type) { entry->numChildren++; if (levels > 0) { - QueryFunc& rel_func = m->db->GetFunc(use); - const QueryFunc::Def* rel_def = rel_func.AnyDef(); - if (!rel_def) - return; - if (optional loc = - GetLsLocation(m->db, m->working_files, use)) { - Out_CqueryCallHierarchy::Entry entry1; - entry1.id = QueryFuncId(use.id); - if (detailed_name) - entry1.name = rel_def->detailed_name; - else - entry1.name = rel_def->ShortName(); - entry1.location = *loc; - entry1.callType = call_type; - Expand(m, &entry1, callee, call_type, detailed_name, levels - 1); - entry->children.push_back(std::move(entry1)); - } + Out_CqueryCallHierarchy::Entry entry1; + entry1.id = QueryFuncId(use.id); + entry1.callType = call_type; + if (Expand(m, &entry1, callee, call_type, detailed_name, levels - 1)) + entry->children.push_back(std::move(entry1)); } }; auto handle_uses = [&](const QueryFunc& func, CallType call_type) { @@ -131,6 +119,15 @@ void Expand(MessageHandler* m, std::unordered_set seen; std::vector stack; + if (detailed_name) + entry->name = def->detailed_name; + else + entry->name = def->ShortName(); + if (def->spell) { + if (optional loc = + GetLsLocation(m->db, m->working_files, *def->spell)) + entry->location = *loc; + } handle_uses(func, CallType::Direct); // Callers/callees of base functions. @@ -167,6 +164,7 @@ void Expand(MessageHandler* m, }); } } + return true; } struct CqueryCallHierarchyInitialHandler @@ -182,15 +180,7 @@ struct CqueryCallHierarchyInitialHandler Out_CqueryCallHierarchy::Entry entry; entry.id = root_id; - if (detailed_name) - entry.name = def->detailed_name; - else - entry.name = def->ShortName(); - if (def->spell) { - if (optional loc = - GetLsLocation(db, working_files, *def->spell)) - entry.location = *loc; - } + entry.callType = CallType::Direct; Expand(this, &entry, callee, call_type, detailed_name, levels); return entry; } @@ -231,7 +221,7 @@ struct CqueryCallHierarchyExpandHandler if (params.id) { Out_CqueryCallHierarchy::Entry entry; entry.id = *params.id; - // entry.name is empty as it is known by the client. + entry.callType = CallType::Direct; if (entry.id.id < db->funcs.size()) Expand(this, &entry, params.callee, params.callType, params.detailedName, params.levels); diff --git a/src/messages/cquery_inheritance_hierarchy.cc b/src/messages/cquery_inheritance_hierarchy.cc index 6e7c1e38..b7860c1b 100644 --- a/src/messages/cquery_inheritance_hierarchy.cc +++ b/src/messages/cquery_inheritance_hierarchy.cc @@ -100,7 +100,6 @@ bool ExpandHelper(MessageHandler* m, entry->location = *loc; } if (derived) { - entry->numChildren = int(entity.derived.size()); if (levels > 0) { for (auto id : entity.derived) { Out_CqueryInheritanceHierarchy::Entry entry1; @@ -110,9 +109,9 @@ bool ExpandHelper(MessageHandler* m, entry->children.push_back(std::move(entry1)); } entry->numChildren = int(entry->children.size()); - } + } else + entry->numChildren = int(entity.derived.size()); } else { - entry->numChildren = int(def->bases.size()); if (levels > 0) { for (auto id : def->bases) { Out_CqueryInheritanceHierarchy::Entry entry1; @@ -122,7 +121,8 @@ bool ExpandHelper(MessageHandler* m, entry->children.push_back(std::move(entry1)); } entry->numChildren = int(entry->children.size()); - } + } else + entry->numChildren = int(def->bases.size()); } return true; } diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index a530e05b..96aee653 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -62,7 +62,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry, children); MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result); -void Expand(MessageHandler* m, +bool Expand(MessageHandler* m, Out_CqueryMemberHierarchy::Entry* entry, bool detailed_name, int levels) { @@ -100,13 +100,12 @@ void Expand(MessageHandler* m, } if (!def) { entry->numChildren = 0; - return; + return false; } if (detailed_name) entry->name = def->detailed_name; else entry->name = def->ShortName(); - entry->numChildren = int(def->vars.size()); if (levels > 0) { EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) { const QueryVar::Def* def1 = var.AnyDef(); @@ -123,11 +122,13 @@ void Expand(MessageHandler* m, GetLsLocation(m->db, m->working_files, *def1->spell)) entry1.location = *loc; } - Expand(m, &entry1, detailed_name, levels - 1); - entry->children.push_back(std::move(entry1)); + if (Expand(m, &entry1, detailed_name, levels - 1)) + entry->children.push_back(std::move(entry1)); }); entry->numChildren = int(entry->children.size()); - } + } else + entry->numChildren = int(def->vars.size()); + return true; } struct CqueryMemberHierarchyInitialHandler @@ -193,9 +194,9 @@ struct CqueryMemberHierarchyExpandHandler Out_CqueryMemberHierarchy::Entry entry; entry.id = *request->params.id; // entry.name is empty as it is known by the client. - if (entry.id.id < db->types.size()) - Expand(this, &entry, params.detailedName, params.levels); - out.result = std::move(entry); + if (entry.id.id < db->types.size() && + Expand(this, &entry, params.detailedName, params.levels)) + out.result = std::move(entry); } QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out);