$cquery/{call,member}Hierarchy{Initial,Expand} work

This commit is contained in:
Fangrui Song 2018-02-25 16:05:03 -08:00
parent eda5782aa6
commit 8c32839362
2 changed files with 83 additions and 61 deletions

View File

@ -5,13 +5,25 @@
#include <loguru.hpp> #include <loguru.hpp>
namespace { namespace {
enum class CallType : uint8_t { Direct = 0, Base = 1, Derived = 2, All = 1 | 2 };
MAKE_REFLECT_TYPE_PROXY(CallType);
bool operator&(CallType lhs, CallType rhs) {
return uint8_t(lhs) & uint8_t(rhs);
}
struct Ipc_CqueryCallHierarchyInitial struct Ipc_CqueryCallHierarchyInitial
: public RequestMessage<Ipc_CqueryCallHierarchyInitial> { : public RequestMessage<Ipc_CqueryCallHierarchyInitial> {
const static IpcId kIpcId = IpcId::CqueryCallHierarchyInitial; const static IpcId kIpcId = IpcId::CqueryCallHierarchyInitial;
struct Params { struct Params {
lsTextDocumentIdentifier textDocument; lsTextDocumentIdentifier textDocument;
lsPosition position; lsPosition position;
// true: callee tree (functions called by this function); false: caller tree
// (where this function is called)
bool callee = false; bool callee = false;
// Base: include base functions; All: include both base and derived
// functions.
CallType callType = CallType::Direct;
bool detailedName = false; bool detailedName = false;
int levels = 1; int levels = 1;
}; };
@ -21,21 +33,19 @@ MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyInitial::Params,
textDocument, textDocument,
position, position,
callee, callee,
callType,
detailedName, detailedName,
levels); levels);
MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyInitial, id, params); MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyInitial, id, params);
REGISTER_IPC_MESSAGE(Ipc_CqueryCallHierarchyInitial); REGISTER_IPC_MESSAGE(Ipc_CqueryCallHierarchyInitial);
enum class CallType { Direct = 0, Base = 1, Derived = 2, All = 1 | 2 };
MAKE_REFLECT_TYPE_PROXY(CallType);
struct Ipc_CqueryCallHierarchyExpand struct Ipc_CqueryCallHierarchyExpand
: public RequestMessage<Ipc_CqueryCallHierarchyExpand> { : public RequestMessage<Ipc_CqueryCallHierarchyExpand> {
const static IpcId kIpcId = IpcId::CqueryCallHierarchyExpand; const static IpcId kIpcId = IpcId::CqueryCallHierarchyExpand;
struct Params { struct Params {
Maybe<QueryFuncId> id; Maybe<QueryFuncId> id;
// true: callee tree; false: caller tree
bool callee = false; bool callee = false;
CallType callType = CallType::Direct;
bool detailedName = false; bool detailedName = false;
int levels = 1; int levels = 1;
}; };
@ -44,6 +54,7 @@ struct Ipc_CqueryCallHierarchyExpand
MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyExpand::Params, MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyExpand::Params,
id, id,
callee, callee,
callType,
detailedName, detailedName,
levels); levels);
MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyExpand, id, params); MAKE_REFLECT_STRUCT(Ipc_CqueryCallHierarchyExpand, id, params);
@ -75,6 +86,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result);
void Expand(MessageHandler* m, void Expand(MessageHandler* m,
Out_CqueryCallHierarchy::Entry* entry, Out_CqueryCallHierarchy::Entry* entry,
bool callee, bool callee,
CallType call_type,
bool detailed_name, bool detailed_name,
int levels) { int levels) {
const QueryFunc& func = m->db->funcs[entry->id.id]; const QueryFunc& func = m->db->funcs[entry->id.id];
@ -82,30 +94,25 @@ void Expand(MessageHandler* m,
entry->numChildren = 0; entry->numChildren = 0;
if (!def) if (!def)
return; return;
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
}
auto handle = [&](Use use, CallType call_type) { auto handle = [&](Use use, CallType call_type) {
QueryFunc& rel_func = m->db->GetFunc(use); entry->numChildren++;
const QueryFunc::Def* rel_def = rel_func.AnyDef(); if (levels > 0) {
if (!rel_def) QueryFunc& rel_func = m->db->GetFunc(use);
return; const QueryFunc::Def* rel_def = rel_func.AnyDef();
if (optional<lsLocation> loc = if (!rel_def)
GetLsLocation(m->db, m->working_files, use)) { return;
entry->numChildren++; if (optional<lsLocation> loc =
if (levels > 0) { GetLsLocation(m->db, m->working_files, use)) {
Out_CqueryCallHierarchy::Entry entry1; Out_CqueryCallHierarchy::Entry entry1;
entry1.id = QueryFuncId(use.id); entry1.id = QueryFuncId(use.id);
if (detailed_name) if (detailed_name)
entry1.name = rel_def->detailed_name; entry1.name = rel_def->detailed_name;
else else
entry1.name = rel_def->ShortName(); entry1.name = rel_def->ShortName();
entry1.location = *loc; entry1.location = *loc;
entry1.callType = call_type; entry1.callType = call_type;
Expand(m, &entry1, callee, detailed_name, levels - 1); Expand(m, &entry1, callee, call_type, detailed_name, levels - 1);
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));
} }
} }
}; };
@ -127,33 +134,38 @@ void Expand(MessageHandler* m,
handle_uses(func, CallType::Direct); handle_uses(func, CallType::Direct);
// Callers/callees of base functions. // Callers/callees of base functions.
stack.push_back(&func); if (call_type & CallType::Base) {
while (stack.size()) { seen.insert(func.usr);
const QueryFunc& func1 = *stack.back(); stack.push_back(&func);
stack.pop_back(); while (stack.size()) {
if (auto* def1 = func1.AnyDef()) { const QueryFunc& func1 = *stack.back();
EachDefinedEntity(m->db->funcs, def1->base, [&](QueryFunc& func2) { stack.pop_back();
if (seen.count(func2.usr)) { if (auto* def1 = func1.AnyDef()) {
seen.insert(func2.usr); EachDefinedEntity(m->db->funcs, def1->base, [&](QueryFunc& func2) {
stack.push_back(&func2); if (!seen.count(func2.usr)) {
handle_uses(func2, CallType::Base); seen.insert(func2.usr);
} stack.push_back(&func2);
}); handle_uses(func2, CallType::Base);
}
});
}
} }
} }
// Callers/callees of derived functions. // Callers/callees of derived functions.
stack.push_back(&func); if (call_type & CallType::Derived) {
while (stack.size()) { stack.push_back(&func);
const QueryFunc& func1 = *stack.back(); while (stack.size()) {
stack.pop_back(); const QueryFunc& func1 = *stack.back();
EachDefinedEntity(m->db->funcs, func1.derived, [&](QueryFunc& func2) { stack.pop_back();
if (seen.count(func2.usr)) { EachDefinedEntity(m->db->funcs, func1.derived, [&](QueryFunc& func2) {
seen.insert(func2.usr); if (!seen.count(func2.usr)) {
stack.push_back(&func2); seen.insert(func2.usr);
handle_uses(func2, CallType::Derived); stack.push_back(&func2);
} handle_uses(func2, CallType::Derived);
}); }
});
}
} }
} }
@ -161,6 +173,7 @@ struct CqueryCallHierarchyInitialHandler
: BaseMessageHandler<Ipc_CqueryCallHierarchyInitial> { : BaseMessageHandler<Ipc_CqueryCallHierarchyInitial> {
optional<Out_CqueryCallHierarchy::Entry> BuildInitial(QueryFuncId root_id, optional<Out_CqueryCallHierarchy::Entry> BuildInitial(QueryFuncId root_id,
bool callee, bool callee,
CallType call_type,
bool detailed_name, bool detailed_name,
int levels) { int levels) {
const auto* def = db->funcs[root_id.id].AnyDef(); const auto* def = db->funcs[root_id.id].AnyDef();
@ -169,8 +182,16 @@ struct CqueryCallHierarchyInitialHandler
Out_CqueryCallHierarchy::Entry entry; Out_CqueryCallHierarchy::Entry entry;
entry.id = root_id; entry.id = root_id;
entry.name = def->ShortName(); if (detailed_name)
Expand(this, &entry, callee, detailed_name, levels); 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);
return entry; return entry;
} }
@ -189,8 +210,9 @@ struct CqueryCallHierarchyInitialHandler
for (SymbolRef sym : for (SymbolRef sym :
FindSymbolsAtLocation(working_file, file, params.position)) { FindSymbolsAtLocation(working_file, file, params.position)) {
if (sym.kind == SymbolKind::Func) { if (sym.kind == SymbolKind::Func) {
out.result = BuildInitial(QueryFuncId(sym.id), params.callee, out.result =
params.detailedName, params.levels); BuildInitial(QueryFuncId(sym.id), params.callee, params.callType,
params.detailedName, params.levels);
break; break;
} }
} }
@ -209,10 +231,10 @@ 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 and it is known by the client. // entry.name is empty as it is known by the client.
if (entry.id.id < db->funcs.size()) if (entry.id.id < db->funcs.size())
Expand(this, &entry, params.callee, params.detailedName, Expand(this, &entry, params.callee, params.callType,
params.levels); params.detailedName, params.levels);
out.result = std::move(entry); out.result = std::move(entry);
} }

View File

@ -42,7 +42,7 @@ struct Out_CqueryMemberHierarchy
struct Entry { struct Entry {
QueryTypeId id; QueryTypeId id;
std::string_view name; std::string_view name;
std::string_view field_name; std::string_view fieldName;
lsLocation location; lsLocation location;
// For unexpanded nodes, this is an upper bound because some entities may be // For unexpanded nodes, this is an upper bound because some entities may be
// undefined. If it is 0, there are no members. // undefined. If it is 0, there are no members.
@ -56,7 +56,7 @@ struct Out_CqueryMemberHierarchy
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry, MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
id, id,
name, name,
field_name, fieldName,
location, location,
numChildren, numChildren,
children); children);
@ -115,7 +115,7 @@ void Expand(MessageHandler* m,
const QueryVar::Def* def1 = var.AnyDef(); const QueryVar::Def* def1 = var.AnyDef();
Out_CqueryMemberHierarchy::Entry entry1; Out_CqueryMemberHierarchy::Entry entry1;
entry1.id = def1->type ? *def1->type : QueryTypeId(); entry1.id = def1->type ? *def1->type : QueryTypeId();
entry1.field_name = def1->ShortName(); entry1.fieldName = def1->ShortName();
Expand(m, &entry1, detailed_name, levels - 1); Expand(m, &entry1, detailed_name, levels - 1);
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));
}); });
@ -180,7 +180,7 @@ struct CqueryMemberHierarchyExpandHandler
if (params.id) { if (params.id) {
Out_CqueryMemberHierarchy::Entry entry; Out_CqueryMemberHierarchy::Entry entry;
entry.id = *request->params.id; entry.id = *request->params.id;
// entry.name is empty and 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);