2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-01-10 08:21:55 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2018-01-13 06:13:08 +00:00
|
|
|
// FIXME Interop with VSCode, change std::string usr to Usr (uint64_t)
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Ipc_CqueryCallTreeInitial
|
2018-01-19 09:01:56 +00:00
|
|
|
: public RequestMessage<Ipc_CqueryCallTreeInitial> {
|
2017-12-06 04:39:44 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryCallTreeInitial;
|
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryCallTreeInitial, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryCallTreeInitial);
|
|
|
|
|
2018-01-30 00:27:43 +00:00
|
|
|
struct Ipc_CqueryCallTreeExpand
|
|
|
|
: public RequestMessage<Ipc_CqueryCallTreeExpand> {
|
2018-01-19 09:01:56 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryCallTreeExpand;
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Params {
|
|
|
|
std::string usr;
|
|
|
|
};
|
|
|
|
Params params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryCallTreeExpand::Params, usr);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryCallTreeExpand, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryCallTreeExpand);
|
|
|
|
|
|
|
|
struct Out_CqueryCallTree : public lsOutMessage<Out_CqueryCallTree> {
|
|
|
|
enum class CallType { Direct = 0, Base = 1, Derived = 2 };
|
|
|
|
struct CallEntry {
|
2018-01-31 06:39:39 +00:00
|
|
|
std::string_view name;
|
2017-12-06 04:39:44 +00:00
|
|
|
std::string usr;
|
|
|
|
lsLocation location;
|
|
|
|
bool hasCallers = true;
|
|
|
|
CallType callType = CallType::Direct;
|
|
|
|
};
|
|
|
|
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<CallEntry> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-01-30 00:35:01 +00:00
|
|
|
MAKE_REFLECT_TYPE_PROXY(Out_CqueryCallTree::CallType);
|
2017-12-06 04:39:44 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryCallTree::CallEntry,
|
|
|
|
name,
|
|
|
|
usr,
|
|
|
|
location,
|
|
|
|
hasCallers,
|
|
|
|
callType);
|
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryCallTree, jsonrpc, id, result);
|
|
|
|
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(
|
2017-12-06 04:39:44 +00:00
|
|
|
QueryDatabase* db,
|
|
|
|
WorkingFiles* working_files,
|
|
|
|
QueryFuncId root) {
|
|
|
|
QueryFunc& root_func = db->funcs[root.id];
|
2018-02-11 04:01:10 +00:00
|
|
|
if (!root_func.def || !root_func.def->spell)
|
2017-12-06 04:39:44 +00:00
|
|
|
return {};
|
|
|
|
optional<lsLocation> def_loc =
|
2018-02-11 04:01:10 +00:00
|
|
|
GetLsLocation(db, working_files, *root_func.def->spell);
|
2017-12-06 04:39:44 +00:00
|
|
|
if (!def_loc)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Out_CqueryCallTree::CallEntry entry;
|
2018-01-31 06:39:39 +00:00
|
|
|
entry.name = root_func.def->ShortName();
|
2018-01-13 06:13:08 +00:00
|
|
|
entry.usr = std::to_string(root_func.usr);
|
2017-12-06 04:39:44 +00:00
|
|
|
entry.location = *def_loc;
|
|
|
|
entry.hasCallers = HasCallersOnSelfOrBaseOrDerived(db, root_func);
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<Out_CqueryCallTree::CallEntry> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
result.push_back(entry);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(
|
2017-12-06 04:39:44 +00:00
|
|
|
QueryDatabase* db,
|
|
|
|
WorkingFiles* working_files,
|
|
|
|
QueryFuncId root) {
|
|
|
|
QueryFunc& root_func = db->funcs[root.id];
|
|
|
|
if (!root_func.def)
|
|
|
|
return {};
|
|
|
|
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<Out_CqueryCallTree::CallEntry> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
auto handle_caller = [&](Use caller,
|
2017-12-06 04:39:44 +00:00
|
|
|
Out_CqueryCallTree::CallType call_type) {
|
|
|
|
optional<lsLocation> call_location =
|
2018-02-09 07:10:54 +00:00
|
|
|
GetLsLocation(db, working_files, caller);
|
2017-12-06 04:39:44 +00:00
|
|
|
if (!call_location)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: REMOVE |seen_locations| once we fix the querydb update bugs
|
|
|
|
// TODO: basically, querydb gets duplicate references inserted into it.
|
2018-01-11 05:16:46 +00:00
|
|
|
// if (!seen_locations.insert(caller.loc).second) {
|
|
|
|
// LOG_S(ERROR) << "!!!! FIXME DUPLICATE REFERENCE IN QUERYDB" <<
|
|
|
|
// std::endl; return;
|
|
|
|
//}
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
if (caller.kind == SymbolKind::Func) {
|
|
|
|
QueryFunc& call_func = db->GetFunc(caller);
|
2017-12-06 04:39:44 +00:00
|
|
|
if (!call_func.def)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Out_CqueryCallTree::CallEntry call_entry;
|
2018-01-31 06:39:39 +00:00
|
|
|
call_entry.name = call_func.def->ShortName();
|
2018-01-13 06:13:08 +00:00
|
|
|
call_entry.usr = std::to_string(call_func.usr);
|
2017-12-06 04:39:44 +00:00
|
|
|
call_entry.location = *call_location;
|
|
|
|
call_entry.hasCallers = HasCallersOnSelfOrBaseOrDerived(db, call_func);
|
|
|
|
call_entry.callType = call_type;
|
|
|
|
result.push_back(call_entry);
|
|
|
|
} else {
|
|
|
|
// TODO: See if we can do a better job here. Need more information from
|
|
|
|
// the indexer.
|
|
|
|
Out_CqueryCallTree::CallEntry call_entry;
|
|
|
|
call_entry.name = "Likely Constructor";
|
|
|
|
call_entry.usr = "no_usr";
|
|
|
|
call_entry.location = *call_location;
|
|
|
|
call_entry.hasCallers = false;
|
|
|
|
call_entry.callType = call_type;
|
|
|
|
result.push_back(call_entry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
std::vector<Use> base_callers =
|
2017-12-06 04:39:44 +00:00
|
|
|
GetCallersForAllBaseFunctions(db, root_func);
|
2018-02-12 04:22:47 +00:00
|
|
|
std::vector<Use> derived_callers =
|
2017-12-06 04:39:44 +00:00
|
|
|
GetCallersForAllDerivedFunctions(db, root_func);
|
2018-02-10 20:53:18 +00:00
|
|
|
result.reserve(root_func.uses.size() + base_callers.size() +
|
2017-12-06 04:39:44 +00:00
|
|
|
derived_callers.size());
|
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use caller : root_func.uses)
|
2017-12-06 04:39:44 +00:00
|
|
|
handle_caller(caller, Out_CqueryCallTree::CallType::Direct);
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use caller : base_callers)
|
2018-02-10 20:53:18 +00:00
|
|
|
if (caller.kind == SymbolKind::Func && caller.id != Id<void>(root)) {
|
|
|
|
// Do not show calls to the base function coming from this function.
|
|
|
|
handle_caller(caller, Out_CqueryCallTree::CallType::Base);
|
|
|
|
}
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use caller : derived_callers)
|
2017-12-06 04:39:44 +00:00
|
|
|
handle_caller(caller, Out_CqueryCallTree::CallType::Derived);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct CqueryCallTreeInitialHandler
|
|
|
|
: BaseMessageHandler<Ipc_CqueryCallTreeInitial> {
|
|
|
|
void Run(Ipc_CqueryCallTreeInitial* request) override {
|
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_CqueryCallTree out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
for (SymbolRef sym :
|
2017-12-06 03:32:33 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
2018-02-09 17:42:10 +00:00
|
|
|
if (sym.kind == SymbolKind::Func) {
|
2017-12-06 03:32:33 +00:00
|
|
|
out.result =
|
2018-02-11 18:25:37 +00:00
|
|
|
BuildInitialCallTree(db, working_files, QueryFuncId(sym.id));
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::CqueryCallTreeInitial, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryCallTreeInitialHandler);
|
|
|
|
|
|
|
|
struct CqueryCallTreeExpandHandler
|
|
|
|
: BaseMessageHandler<Ipc_CqueryCallTreeExpand> {
|
|
|
|
void Run(Ipc_CqueryCallTreeExpand* request) override {
|
|
|
|
Out_CqueryCallTree out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-01-20 18:32:39 +00:00
|
|
|
// FIXME
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryFuncId> func_id =
|
2018-01-20 18:32:39 +00:00
|
|
|
db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr));
|
|
|
|
if (func_id)
|
2018-02-02 07:10:37 +00:00
|
|
|
out.result = BuildExpandCallTree(db, working_files, *func_id);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryCallTreeExpandHandler);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|