ccls/src/messages/ccls_call_hierarchy.cc

236 lines
7.0 KiB
C++
Raw Normal View History

#include "message_handler.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
using namespace ccls;
#include "query_utils.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
#include <unordered_set>
namespace {
2018-03-31 03:16:33 +00:00
MethodType kMethodType = "$ccls/callHierarchy";
2018-03-20 02:51:42 +00:00
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);
}
2018-03-31 03:16:33 +00:00
struct In_CclsCallHierarchy : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
struct Params {
// If id is specified, expand a node; otherwise textDocument+position should
// be specified for building the root and |levels| of nodes below.
lsTextDocumentIdentifier textDocument;
lsPosition position;
Usr usr;
std::string id;
// 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::All;
bool qualified = true;
int levels = 1;
};
Params params;
};
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(In_CclsCallHierarchy::Params,
textDocument,
position,
2018-02-25 23:24:51 +00:00
id,
callee,
callType,
qualified,
2018-02-25 23:24:51 +00:00
levels);
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(In_CclsCallHierarchy, id, params);
REGISTER_IN_MESSAGE(In_CclsCallHierarchy);
2018-03-31 03:16:33 +00:00
struct Out_CclsCallHierarchy : public lsOutMessage<Out_CclsCallHierarchy> {
struct Entry {
Usr usr;
std::string id;
std::string_view name;
lsLocation location;
CallType callType = CallType::Direct;
int numChildren;
// Empty if the |levels| limit is reached.
std::vector<Entry> children;
};
lsRequestId id;
2018-03-31 03:16:33 +00:00
std::optional<Entry> result;
};
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(Out_CclsCallHierarchy::Entry,
id,
name,
location,
callType,
numChildren,
children);
2018-05-08 03:59:08 +00:00
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsCallHierarchy,
jsonrpc,
id,
result);
2018-02-26 03:10:02 +00:00
bool Expand(MessageHandler* m,
2018-03-31 03:16:33 +00:00
Out_CclsCallHierarchy::Entry* entry,
bool callee,
CallType call_type,
bool qualified,
int levels) {
const QueryFunc& func = m->db->Func(entry->usr);
const QueryFunc::Def* def = func.AnyDef();
entry->numChildren = 0;
if (!def)
2018-02-26 03:10:02 +00:00
return false;
auto handle = [&](Use use, CallType call_type) {
entry->numChildren++;
if (levels > 0) {
2018-03-31 03:16:33 +00:00
Out_CclsCallHierarchy::Entry entry1;
entry1.id = std::to_string(use.usr);
entry1.usr = use.usr;
2018-03-01 01:53:43 +00:00
if (auto loc = GetLsLocation(m->db, m->working_files, use))
entry1.location = *loc;
2018-02-26 03:10:02 +00:00
entry1.callType = call_type;
if (Expand(m, &entry1, callee, call_type, qualified, levels - 1))
2018-02-26 03:10:02 +00:00
entry->children.push_back(std::move(entry1));
}
};
auto handle_uses = [&](const QueryFunc& func, CallType call_type) {
if (callee) {
if (const auto* def = func.AnyDef())
for (SymbolRef ref : def->callees)
if (ref.kind == SymbolKind::Func)
2018-05-05 03:40:52 +00:00
handle(Use{{ref.range, ref.usr, ref.kind, ref.role},
def->spell->file_id},
2018-03-20 02:51:42 +00:00
call_type);
} else {
for (Use use : func.uses)
if (use.kind == SymbolKind::Func)
handle(use, call_type);
}
};
std::unordered_set<Usr> seen;
seen.insert(func.usr);
std::vector<const QueryFunc*> stack;
entry->name = def->Name(qualified);
handle_uses(func, CallType::Direct);
// Callers/callees of base functions.
if (call_type & CallType::Base) {
stack.push_back(&func);
while (stack.size()) {
const QueryFunc& func1 = *stack.back();
stack.pop_back();
if (auto* def1 = func1.AnyDef()) {
EachDefinedEntity(m->db->usr2func, def1->bases, [&](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.
if (call_type & CallType::Derived) {
stack.push_back(&func);
while (stack.size()) {
const QueryFunc& func1 = *stack.back();
stack.pop_back();
EachDefinedEntity(m->db->usr2func, func1.derived, [&](QueryFunc& func2) {
if (!seen.count(func2.usr)) {
seen.insert(func2.usr);
stack.push_back(&func2);
handle_uses(func2, CallType::Derived);
}
});
}
}
2018-02-26 03:10:02 +00:00
return true;
}
2018-03-31 03:16:33 +00:00
struct Handler_CclsCallHierarchy
: BaseMessageHandler<In_CclsCallHierarchy> {
MethodType GetMethodType() const override { return kMethodType; }
std::optional<Out_CclsCallHierarchy::Entry> BuildInitial(Usr root_usr,
bool callee,
CallType call_type,
bool qualified,
int levels) {
const auto* def = db->Func(root_usr).AnyDef();
if (!def)
return {};
2018-03-31 03:16:33 +00:00
Out_CclsCallHierarchy::Entry entry;
entry.id = std::to_string(root_usr);
entry.usr = root_usr;
2018-02-26 03:10:02 +00:00
entry.callType = CallType::Direct;
2018-03-01 01:53:43 +00:00
if (def->spell) {
2018-03-31 03:16:33 +00:00
if (std::optional<lsLocation> loc =
2018-03-20 02:51:42 +00:00
GetLsLocation(db, working_files, *def->spell))
2018-03-01 01:53:43 +00:00
entry.location = *loc;
}
Expand(this, &entry, callee, call_type, qualified, levels);
return entry;
}
2018-03-31 03:16:33 +00:00
void Run(In_CclsCallHierarchy* request) override {
auto& params = request->params;
2018-03-31 03:16:33 +00:00
Out_CclsCallHierarchy out;
out.id = request->id;
if (params.id.size()) {
try {
params.usr = std::stoull(params.id);
} catch (...) {
return;
}
2018-03-31 03:16:33 +00:00
Out_CclsCallHierarchy::Entry entry;
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
2018-02-26 03:10:02 +00:00
entry.callType = CallType::Direct;
if (db->usr2func.count(params.usr))
Expand(this, &entry, params.callee, params.callType, params.qualified,
params.levels);
out.result = std::move(entry);
} else {
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetPath(), &file))
return;
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
for (SymbolRef sym :
2018-03-20 02:51:42 +00:00
FindSymbolsAtLocation(working_file, file, params.position)) {
if (sym.kind == SymbolKind::Func) {
out.result = BuildInitial(sym.usr, params.callee, params.callType,
params.qualified, params.levels);
break;
}
}
}
2018-05-28 00:50:02 +00:00
pipeline::WriteStdout(kMethodType, out);
}
};
2018-03-31 03:16:33 +00:00
REGISTER_MESSAGE_HANDLER(Handler_CclsCallHierarchy);
} // namespace