2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
#include "hierarchy.hh"
|
2018-02-25 22:53:57 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-02-25 22:53:57 +00:00
|
|
|
#include "query_utils.h"
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-09-23 19:10:40 +00:00
|
|
|
using namespace ccls;
|
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
MethodType kMethodType = "$ccls/call";
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-03-20 02:51:42 +00:00
|
|
|
enum class CallType : uint8_t {
|
|
|
|
Direct = 0,
|
|
|
|
Base = 1,
|
|
|
|
Derived = 2,
|
|
|
|
All = 1 | 2
|
|
|
|
};
|
2018-02-26 00:05:03 +00:00
|
|
|
MAKE_REFLECT_TYPE_PROXY(CallType);
|
|
|
|
|
|
|
|
bool operator&(CallType lhs, CallType rhs) {
|
|
|
|
return uint8_t(lhs) & uint8_t(rhs);
|
|
|
|
}
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
struct In_CclsCall : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
struct Params {
|
2018-02-28 05:56:40 +00:00
|
|
|
// If id is specified, expand a node; otherwise textDocument+position should
|
|
|
|
// be specified for building the root and |levels| of nodes below.
|
2018-02-25 22:53:57 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-02-26 00:05:03 +00:00
|
|
|
// true: callee tree (functions called by this function); false: caller tree
|
|
|
|
// (where this function is called)
|
2018-02-25 22:53:57 +00:00
|
|
|
bool callee = false;
|
2018-02-26 00:05:03 +00:00
|
|
|
// Base: include base functions; All: include both base and derived
|
|
|
|
// functions.
|
2018-02-28 07:08:39 +00:00
|
|
|
CallType callType = CallType::All;
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified = true;
|
2018-02-25 22:53:57 +00:00
|
|
|
int levels = 1;
|
2018-09-13 07:18:37 +00:00
|
|
|
bool hierarchy = false;
|
2018-02-25 22:53:57 +00:00
|
|
|
};
|
|
|
|
Params params;
|
|
|
|
};
|
2018-09-13 07:18:37 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsCall::Params, textDocument, position, id,
|
|
|
|
callee, callType, qualified, levels, hierarchy);
|
|
|
|
MAKE_REFLECT_STRUCT(In_CclsCall, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_CclsCall);
|
2018-02-25 22:53:57 +00:00
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
struct Out_CclsCall : public lsOutMessage<Out_CclsCall> {
|
2018-02-25 22:53:57 +00:00
|
|
|
struct Entry {
|
2018-04-30 04:49:03 +00:00
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-25 22:53:57 +00:00
|
|
|
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-02-25 22:53:57 +00:00
|
|
|
};
|
2018-09-13 07:18:37 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CclsCall::Entry, id, name, location, callType,
|
2018-08-09 17:08:14 +00:00
|
|
|
numChildren, children);
|
2018-09-13 07:18:37 +00:00
|
|
|
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsCall, jsonrpc, id,
|
2018-05-08 03:59:08 +00:00
|
|
|
result);
|
2018-02-25 22:53:57 +00:00
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
bool Expand(MessageHandler *m, Out_CclsCall::Entry *entry, bool callee,
|
2018-08-09 17:08:14 +00:00
|
|
|
CallType call_type, bool qualified, int levels) {
|
|
|
|
const QueryFunc &func = m->db->Func(entry->usr);
|
|
|
|
const QueryFunc::Def *def = func.AnyDef();
|
2018-02-25 22:53:57 +00:00
|
|
|
entry->numChildren = 0;
|
|
|
|
if (!def)
|
2018-02-26 03:10:02 +00:00
|
|
|
return false;
|
2018-06-01 21:22:55 +00:00
|
|
|
auto handle = [&](Use use, CallType call_type1) {
|
2018-02-26 00:05:03 +00:00
|
|
|
entry->numChildren++;
|
|
|
|
if (levels > 0) {
|
2018-09-13 07:18:37 +00:00
|
|
|
Out_CclsCall::Entry entry1;
|
2018-04-30 04:49:03 +00:00
|
|
|
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-06-01 21:22:55 +00:00
|
|
|
entry1.callType = call_type1;
|
2018-04-06 00:00:07 +00:00
|
|
|
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));
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
auto handle_uses = [&](const QueryFunc &func, CallType call_type) {
|
2018-02-25 22:53:57 +00:00
|
|
|
if (callee) {
|
2018-08-09 17:08:14 +00:00
|
|
|
if (const auto *def = func.AnyDef())
|
2018-02-25 22:53:57 +00:00
|
|
|
for (SymbolRef ref : def->callees)
|
|
|
|
if (ref.kind == SymbolKind::Func)
|
2018-08-09 17:08:14 +00:00
|
|
|
handle(Use{{ref.range, ref.usr, ref.kind, ref.role}, def->file_id},
|
2018-03-20 02:51:42 +00:00
|
|
|
call_type);
|
2018-02-25 22:53:57 +00:00
|
|
|
} else {
|
|
|
|
for (Use use : func.uses)
|
|
|
|
if (use.kind == SymbolKind::Func)
|
|
|
|
handle(use, call_type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-27 01:23:45 +00:00
|
|
|
seen.insert(func.usr);
|
2018-08-09 17:08:14 +00:00
|
|
|
std::vector<const QueryFunc *> stack;
|
2018-04-06 00:00:07 +00:00
|
|
|
entry->name = def->Name(qualified);
|
2018-02-25 22:53:57 +00:00
|
|
|
handle_uses(func, CallType::Direct);
|
|
|
|
|
|
|
|
// Callers/callees of base functions.
|
2018-02-26 00:05:03 +00:00
|
|
|
if (call_type & CallType::Base) {
|
|
|
|
stack.push_back(&func);
|
|
|
|
while (stack.size()) {
|
2018-08-09 17:08:14 +00:00
|
|
|
const QueryFunc &func1 = *stack.back();
|
2018-02-26 00:05:03 +00:00
|
|
|
stack.pop_back();
|
2018-08-09 17:08:14 +00:00
|
|
|
if (auto *def1 = func1.AnyDef()) {
|
|
|
|
EachDefinedFunc(m->db, def1->bases, [&](QueryFunc &func2) {
|
2018-02-26 00:05:03 +00:00
|
|
|
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()) {
|
2018-08-09 17:08:14 +00:00
|
|
|
const QueryFunc &func1 = *stack.back();
|
2018-02-26 00:05:03 +00:00
|
|
|
stack.pop_back();
|
2018-08-09 17:08:14 +00:00
|
|
|
EachDefinedFunc(m->db, func1.derived, [&](QueryFunc &func2) {
|
2018-02-26 00:05:03 +00:00
|
|
|
if (!seen.count(func2.usr)) {
|
2018-02-25 22:53:57 +00:00
|
|
|
seen.insert(func2.usr);
|
|
|
|
stack.push_back(&func2);
|
2018-02-26 00:05:03 +00:00
|
|
|
handle_uses(func2, CallType::Derived);
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 03:10:02 +00:00
|
|
|
return true;
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
struct Handler_CclsCall : BaseMessageHandler<In_CclsCall> {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
std::optional<Out_CclsCall::Entry>
|
2018-08-09 17:08:14 +00:00
|
|
|
BuildInitial(Usr root_usr, bool callee, CallType call_type, bool qualified,
|
|
|
|
int levels) {
|
|
|
|
const auto *def = db->Func(root_usr).AnyDef();
|
2018-02-25 22:53:57 +00:00
|
|
|
if (!def)
|
|
|
|
return {};
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
Out_CclsCall::Entry entry;
|
2018-04-30 04:49:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
Expand(this, &entry, callee, call_type, qualified, levels);
|
2018-02-25 22:53:57 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
void Run(In_CclsCall *request) override {
|
2018-08-09 17:08:14 +00:00
|
|
|
auto ¶ms = request->params;
|
2018-09-13 07:18:37 +00:00
|
|
|
Out_CclsCall out;
|
2018-02-25 22:53:57 +00:00
|
|
|
out.id = request->id;
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
if (params.id.size()) {
|
|
|
|
try {
|
|
|
|
params.usr = std::stoull(params.id);
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-13 07:18:37 +00:00
|
|
|
Out_CclsCall::Entry entry;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry.id = std::to_string(params.usr);
|
|
|
|
entry.usr = params.usr;
|
2018-02-26 03:10:02 +00:00
|
|
|
entry.callType = CallType::Direct;
|
2018-05-30 06:56:14 +00:00
|
|
|
if (db->HasFunc(params.usr))
|
2018-04-06 00:00:07 +00:00
|
|
|
Expand(this, &entry, params.callee, params.callType, params.qualified,
|
|
|
|
params.levels);
|
2018-02-25 22:53:57 +00:00
|
|
|
out.result = std::move(entry);
|
2018-02-28 05:56:40 +00:00
|
|
|
} else {
|
2018-08-09 17:08:14 +00:00
|
|
|
QueryFile *file;
|
2018-02-28 05:56:40 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
|
|
params.textDocument.uri.GetPath(), &file))
|
|
|
|
return;
|
2018-08-09 17:08:14 +00:00
|
|
|
WorkingFile *working_file =
|
2018-02-28 05:56:40 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
for (SymbolRef sym :
|
2018-03-20 02:51:42 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, params.position)) {
|
2018-02-28 05:56:40 +00:00
|
|
|
if (sym.kind == SymbolKind::Func) {
|
2018-04-30 04:49:03 +00:00
|
|
|
out.result = BuildInitial(sym.usr, params.callee, params.callType,
|
|
|
|
params.qualified, params.levels);
|
2018-02-28 05:56:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
if (params.hierarchy) {
|
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out_LocationList out1;
|
|
|
|
out1.id = request->id;
|
|
|
|
if (out.result)
|
|
|
|
FlattenHierarchy<Out_CclsCall::Entry>(*out.result, out1);
|
|
|
|
pipeline::WriteStdout(kMethodType, out1);
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
};
|
2018-09-13 07:18:37 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_CclsCall);
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|