Simplify hierarchies

This commit is contained in:
Fangrui Song 2018-02-25 19:10:02 -08:00
parent 95797be730
commit c166f3bca8
3 changed files with 33 additions and 42 deletions

View File

@ -83,7 +83,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy::Entry,
children); children);
MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result);
void Expand(MessageHandler* m, bool Expand(MessageHandler* m,
Out_CqueryCallHierarchy::Entry* entry, Out_CqueryCallHierarchy::Entry* entry,
bool callee, bool callee,
CallType call_type, CallType call_type,
@ -93,27 +93,15 @@ void Expand(MessageHandler* m,
const QueryFunc::Def* def = func.AnyDef(); const QueryFunc::Def* def = func.AnyDef();
entry->numChildren = 0; entry->numChildren = 0;
if (!def) if (!def)
return; return false;
auto handle = [&](Use use, CallType call_type) { auto handle = [&](Use use, CallType call_type) {
entry->numChildren++; entry->numChildren++;
if (levels > 0) { if (levels > 0) {
QueryFunc& rel_func = m->db->GetFunc(use); Out_CqueryCallHierarchy::Entry entry1;
const QueryFunc::Def* rel_def = rel_func.AnyDef(); entry1.id = QueryFuncId(use.id);
if (!rel_def) entry1.callType = call_type;
return; if (Expand(m, &entry1, callee, call_type, detailed_name, levels - 1))
if (optional<lsLocation> loc = entry->children.push_back(std::move(entry1));
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));
}
} }
}; };
auto handle_uses = [&](const QueryFunc& func, CallType call_type) { auto handle_uses = [&](const QueryFunc& func, CallType call_type) {
@ -131,6 +119,15 @@ void Expand(MessageHandler* m,
std::unordered_set<Usr> seen; std::unordered_set<Usr> seen;
std::vector<const QueryFunc*> stack; std::vector<const QueryFunc*> stack;
if (detailed_name)
entry->name = def->detailed_name;
else
entry->name = def->ShortName();
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
}
handle_uses(func, CallType::Direct); handle_uses(func, CallType::Direct);
// Callers/callees of base functions. // Callers/callees of base functions.
@ -167,6 +164,7 @@ void Expand(MessageHandler* m,
}); });
} }
} }
return true;
} }
struct CqueryCallHierarchyInitialHandler struct CqueryCallHierarchyInitialHandler
@ -182,15 +180,7 @@ struct CqueryCallHierarchyInitialHandler
Out_CqueryCallHierarchy::Entry entry; Out_CqueryCallHierarchy::Entry entry;
entry.id = root_id; entry.id = root_id;
if (detailed_name) entry.callType = CallType::Direct;
entry.name = def->detailed_name;
else
entry.name = def->ShortName();
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
Expand(this, &entry, callee, call_type, detailed_name, levels); Expand(this, &entry, callee, call_type, detailed_name, levels);
return entry; return entry;
} }
@ -231,7 +221,7 @@ struct CqueryCallHierarchyExpandHandler
if (params.id) { if (params.id) {
Out_CqueryCallHierarchy::Entry entry; Out_CqueryCallHierarchy::Entry entry;
entry.id = *params.id; 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()) if (entry.id.id < db->funcs.size())
Expand(this, &entry, params.callee, params.callType, Expand(this, &entry, params.callee, params.callType,
params.detailedName, params.levels); params.detailedName, params.levels);

View File

@ -100,7 +100,6 @@ bool ExpandHelper(MessageHandler* m,
entry->location = *loc; entry->location = *loc;
} }
if (derived) { if (derived) {
entry->numChildren = int(entity.derived.size());
if (levels > 0) { if (levels > 0) {
for (auto id : entity.derived) { for (auto id : entity.derived) {
Out_CqueryInheritanceHierarchy::Entry entry1; Out_CqueryInheritanceHierarchy::Entry entry1;
@ -110,9 +109,9 @@ bool ExpandHelper(MessageHandler* m,
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));
} }
entry->numChildren = int(entry->children.size()); entry->numChildren = int(entry->children.size());
} } else
entry->numChildren = int(entity.derived.size());
} else { } else {
entry->numChildren = int(def->bases.size());
if (levels > 0) { if (levels > 0) {
for (auto id : def->bases) { for (auto id : def->bases) {
Out_CqueryInheritanceHierarchy::Entry entry1; Out_CqueryInheritanceHierarchy::Entry entry1;
@ -122,7 +121,8 @@ bool ExpandHelper(MessageHandler* m,
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));
} }
entry->numChildren = int(entry->children.size()); entry->numChildren = int(entry->children.size());
} } else
entry->numChildren = int(def->bases.size());
} }
return true; return true;
} }

View File

@ -62,7 +62,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
children); children);
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result);
void Expand(MessageHandler* m, bool Expand(MessageHandler* m,
Out_CqueryMemberHierarchy::Entry* entry, Out_CqueryMemberHierarchy::Entry* entry,
bool detailed_name, bool detailed_name,
int levels) { int levels) {
@ -100,13 +100,12 @@ void Expand(MessageHandler* m,
} }
if (!def) { if (!def) {
entry->numChildren = 0; entry->numChildren = 0;
return; return false;
} }
if (detailed_name) if (detailed_name)
entry->name = def->detailed_name; entry->name = def->detailed_name;
else else
entry->name = def->ShortName(); entry->name = def->ShortName();
entry->numChildren = int(def->vars.size());
if (levels > 0) { if (levels > 0) {
EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) { EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) {
const QueryVar::Def* def1 = var.AnyDef(); const QueryVar::Def* def1 = var.AnyDef();
@ -123,11 +122,13 @@ void Expand(MessageHandler* m,
GetLsLocation(m->db, m->working_files, *def1->spell)) GetLsLocation(m->db, m->working_files, *def1->spell))
entry1.location = *loc; entry1.location = *loc;
} }
Expand(m, &entry1, detailed_name, levels - 1); if (Expand(m, &entry1, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));
}); });
entry->numChildren = int(entry->children.size()); entry->numChildren = int(entry->children.size());
} } else
entry->numChildren = int(def->vars.size());
return true;
} }
struct CqueryMemberHierarchyInitialHandler struct CqueryMemberHierarchyInitialHandler
@ -193,9 +194,9 @@ struct CqueryMemberHierarchyExpandHandler
Out_CqueryMemberHierarchy::Entry entry; Out_CqueryMemberHierarchy::Entry entry;
entry.id = *request->params.id; entry.id = *request->params.id;
// entry.name is empty as it is known by the client. // entry.name is empty as it is known by the client.
if (entry.id.id < db->types.size()) if (entry.id.id < db->types.size() &&
Expand(this, &entry, params.detailedName, params.levels); Expand(this, &entry, params.detailedName, params.levels))
out.result = std::move(entry); out.result = std::move(entry);
} }
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out); QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out);