ccls/src/messages/ccls_memberHierarchy.cc

308 lines
9.9 KiB
C++
Raw Normal View History

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.
==============================================================================*/
#include "message_handler.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
#include "query_utils.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
2018-07-09 06:31:40 +00:00
#include <clang/AST/Type.h>
using namespace clang;
#include <queue>
#include <unordered_set>
namespace {
2018-03-31 03:16:33 +00:00
MethodType kMethodType = "$ccls/memberHierarchy";
2018-03-31 03:16:33 +00:00
struct In_CclsMemberHierarchy : 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;
// Type
Usr usr;
std::string id;
bool qualified = false;
int levels = 1;
bool flat = false;
} params;
};
2018-08-09 17:08:14 +00:00
MAKE_REFLECT_STRUCT(In_CclsMemberHierarchy::Params, textDocument, position, id,
qualified, levels, flat);
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(In_CclsMemberHierarchy, id, params);
REGISTER_IN_MESSAGE(In_CclsMemberHierarchy);
2018-08-09 17:08:14 +00:00
struct Out_CclsMemberHierarchy : public lsOutMessage<Out_CclsMemberHierarchy> {
struct Entry {
Usr usr;
std::string id;
std::string_view name;
std::string 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.
2018-02-27 01:23:45 +00:00
int numChildren = 0;
// 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-08-09 17:08:14 +00:00
MAKE_REFLECT_STRUCT(Out_CclsMemberHierarchy::Entry, id, name, fieldName,
location, numChildren, children);
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsMemberHierarchy, jsonrpc, id,
2018-05-08 03:59:08 +00:00
result);
2018-08-09 17:08:14 +00:00
bool Expand(MessageHandler *m, Out_CclsMemberHierarchy::Entry *entry,
bool qualified, int levels);
// Add a field to |entry| which is a Func/Type.
2018-08-09 17:08:14 +00:00
void DoField(MessageHandler *m, Out_CclsMemberHierarchy::Entry *entry,
const QueryVar &var, int64_t offset, bool qualified, int levels) {
const QueryVar::Def *def1 = var.AnyDef();
if (!def1)
return;
2018-03-31 03:16:33 +00:00
Out_CclsMemberHierarchy::Entry entry1;
// With multiple inheritance, the offset is incorrect.
if (offset >= 0) {
if (offset / 8 < 10)
entry1.fieldName += ' ';
entry1.fieldName += std::to_string(offset / 8);
if (offset % 8) {
entry1.fieldName += '.';
entry1.fieldName += std::to_string(offset % 8);
}
entry1.fieldName += ' ';
}
if (qualified)
entry1.fieldName += def1->detailed_name;
else {
entry1.fieldName +=
std::string_view(def1->detailed_name).substr(0, def1->qual_name_offset);
entry1.fieldName += def1->Name(false);
}
if (def1->spell) {
2018-03-31 03:16:33 +00:00
if (std::optional<lsLocation> loc =
2018-03-20 02:51:42 +00:00
GetLsLocation(m->db, m->working_files, *def1->spell))
entry1.location = *loc;
}
if (def1->type) {
entry1.id = std::to_string(def1->type);
entry1.usr = def1->type;
if (Expand(m, &entry1, qualified, levels))
entry->children.push_back(std::move(entry1));
} else {
entry1.id = "0";
entry1.usr = 0;
entry->children.push_back(std::move(entry1));
}
}
// Expand a type node by adding members recursively to it.
2018-08-09 17:08:14 +00:00
bool Expand(MessageHandler *m, Out_CclsMemberHierarchy::Entry *entry,
bool qualified, int levels) {
2018-07-09 06:31:40 +00:00
if (0 < entry->usr && entry->usr <= BuiltinType::LastKind) {
2018-07-15 07:45:51 +00:00
entry->name = ClangBuiltinTypeName(int(entry->usr));
2018-02-26 07:14:03 +00:00
return true;
2018-02-25 23:24:51 +00:00
}
2018-08-09 17:08:14 +00:00
const QueryType &type = m->db->Type(entry->usr);
const QueryType::Def *def = type.AnyDef();
// builtin types have no declaration and empty |qualified|.
2018-02-27 01:23:45 +00:00
if (!def)
2018-02-26 03:10:02 +00:00
return false;
entry->name = def->Name(qualified);
std::unordered_set<Usr> seen;
if (levels > 0) {
2018-08-09 17:08:14 +00:00
std::vector<const QueryType *> stack;
2018-02-26 06:55:17 +00:00
seen.insert(type.usr);
stack.push_back(&type);
while (stack.size()) {
2018-08-09 17:08:14 +00:00
const auto *def = stack.back()->AnyDef();
2018-02-26 06:55:17 +00:00
stack.pop_back();
if (def) {
2018-08-09 17:08:14 +00:00
EachDefinedType(m->db, def->bases, [&](QueryType &type1) {
2018-02-26 06:55:17 +00:00
if (!seen.count(type1.usr)) {
seen.insert(type1.usr);
stack.push_back(&type1);
}
});
if (def->alias_of) {
2018-08-09 17:08:14 +00:00
const QueryType::Def *def1 = m->db->Type(def->alias_of).AnyDef();
2018-03-31 03:16:33 +00:00
Out_CclsMemberHierarchy::Entry entry1;
entry1.id = std::to_string(def->alias_of);
entry1.usr = def->alias_of;
if (def1 && def1->spell) {
// The declaration of target type.
2018-03-31 03:16:33 +00:00
if (std::optional<lsLocation> loc =
2018-03-20 02:51:42 +00:00
GetLsLocation(m->db, m->working_files, *def1->spell))
entry1.location = *loc;
} else if (def->spell) {
// Builtin types have no declaration but the typedef declaration
// itself is useful.
2018-03-31 03:16:33 +00:00
if (std::optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry1.location = *loc;
2018-02-26 06:55:17 +00:00
}
if (def1 && qualified)
entry1.fieldName = def1->detailed_name;
if (Expand(m, &entry1, qualified, levels - 1)) {
2018-02-27 01:23:45 +00:00
// For builtin types |name| is set.
if (entry1.fieldName.empty())
2018-02-27 01:23:45 +00:00
entry1.fieldName = std::string(entry1.name);
2018-02-26 06:55:17 +00:00
entry->children.push_back(std::move(entry1));
2018-02-27 01:23:45 +00:00
}
} else {
for (auto it : def->vars) {
2018-08-09 17:08:14 +00:00
QueryVar &var = m->db->Var(it.first);
if (!var.def.empty())
DoField(m, entry, var, it.second, qualified, levels - 1);
}
}
}
2018-02-26 06:55:17 +00:00
}
entry->numChildren = int(entry->children.size());
2018-02-26 03:10:02 +00:00
} else
entry->numChildren = def->alias_of ? 1 : int(def->vars.size());
2018-02-26 03:10:02 +00:00
return true;
}
2018-03-31 03:16:33 +00:00
struct Handler_CclsMemberHierarchy
: BaseMessageHandler<In_CclsMemberHierarchy> {
MethodType GetMethodType() const override { return kMethodType; }
2018-08-09 17:08:14 +00:00
std::optional<Out_CclsMemberHierarchy::Entry>
BuildInitial(SymbolKind kind, Usr root_usr, bool qualified, int levels) {
switch (kind) {
default:
return {};
case SymbolKind::Func: {
2018-08-09 17:08:14 +00:00
const auto *def = db->Func(root_usr).AnyDef();
if (!def)
return {};
Out_CclsMemberHierarchy::Entry entry;
// Not type, |id| is invalid.
entry.name = def->Name(qualified);
if (def->spell) {
if (std::optional<lsLocation> loc =
2018-08-09 17:08:14 +00:00
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
2018-08-09 17:08:14 +00:00
EachDefinedVar(db, def->vars, [&](QueryVar &var) {
DoField(this, &entry, var, -1, qualified, levels - 1);
});
return entry;
}
case SymbolKind::Type: {
2018-08-09 17:08:14 +00:00
const auto *def = db->Type(root_usr).AnyDef();
if (!def)
return {};
Out_CclsMemberHierarchy::Entry entry;
entry.id = std::to_string(root_usr);
entry.usr = root_usr;
if (def->spell) {
if (std::optional<lsLocation> loc =
2018-08-09 17:08:14 +00:00
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
Expand(this, &entry, qualified, levels);
return entry;
}
}
}
2018-08-09 17:08:14 +00:00
void Run(In_CclsMemberHierarchy *request) override {
auto &params = request->params;
2018-03-31 03:16:33 +00:00
Out_CclsMemberHierarchy 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_CclsMemberHierarchy::Entry entry;
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
// entry.name is empty as it is known by the client.
if (db->HasType(entry.usr) &&
Expand(this, &entry, params.qualified, params.levels))
2018-02-26 03:10:02 +00:00
out.result = std::move(entry);
} else {
2018-08-09 17:08:14 +00:00
QueryFile *file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetPath(), &file))
return;
2018-08-09 17:08:14 +00:00
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
for (SymbolRef sym :
FindSymbolsAtLocation(wfile, file, params.position)) {
switch (sym.kind) {
2018-08-09 17:08:14 +00:00
case SymbolKind::Func:
case SymbolKind::Type:
out.result =
BuildInitial(sym.kind, sym.usr, params.qualified, params.levels);
break;
case SymbolKind::Var: {
const QueryVar::Def *def = db->GetVar(sym).AnyDef();
if (def && def->type)
out.result = BuildInitial(SymbolKind::Type, def->type,
params.qualified, params.levels);
break;
}
default:
continue;
}
break;
}
}
if (!params.flat) {
pipeline::WriteStdout(kMethodType, out);
return;
}
Out_LocationList out1;
out1.id = request->id;
if (out.result) {
std::queue<Out_CclsMemberHierarchy::Entry *> q;
for (auto &entry1 : out.result->children)
q.push(&entry1);
while (q.size()) {
auto *entry = q.front();
q.pop();
if (entry->location.uri.raw_uri.size())
out1.result.push_back({entry->location});
for (auto &entry1 : entry->children)
q.push(&entry1);
}
std::sort(out1.result.begin(), out1.result.end());
out1.result.erase(std::unique(out1.result.begin(), out1.result.end()),
out1.result.end());
}
pipeline::WriteStdout(kMethodType, out1);
}
};
2018-03-31 03:16:33 +00:00
REGISTER_MESSAGE_HANDLER(Handler_CclsMemberHierarchy);
2018-08-09 17:08:14 +00:00
} // namespace