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

View File

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