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-23 20:31:06 +00:00
|
|
|
#include "clang_tu.hh"
|
2018-09-13 07:18:37 +00:00
|
|
|
#include "hierarchy.hh"
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-01-18 07:14:29 +00:00
|
|
|
#include "query_utils.h"
|
|
|
|
|
2018-07-09 06:31:40 +00:00
|
|
|
#include <clang/AST/Type.h>
|
2018-09-23 05:55:17 +00:00
|
|
|
#include <llvm/ADT/DenseSet.h>
|
2018-07-09 06:31:40 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-09-23 19:10:40 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
2018-01-18 07:14:29 +00:00
|
|
|
namespace {
|
2018-10-28 17:49:31 +00:00
|
|
|
struct Param : TextDocumentPositionParam {
|
|
|
|
// If id is specified, expand a node; otherwise textDocument+position should
|
|
|
|
// be specified for building the root and |levels| of nodes below.
|
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
bool qualified = false;
|
|
|
|
int levels = 1;
|
|
|
|
// If SymbolKind::Func and the point is at a type, list member functions
|
|
|
|
// instead of member variables.
|
|
|
|
SymbolKind kind = SymbolKind::Var;
|
|
|
|
bool hierarchy = false;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Param, textDocument, position, id, qualified, levels, kind,
|
|
|
|
hierarchy);
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
struct Out_cclsMember {
|
|
|
|
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.
|
|
|
|
int numChildren = 0;
|
|
|
|
// Empty if the |levels| limit is reached.
|
|
|
|
std::vector<Out_cclsMember> children;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2019-01-09 07:19:17 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_cclsMember, id, name, fieldName, location, numChildren,
|
|
|
|
children);
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
|
|
|
|
int levels, SymbolKind memberKind);
|
2018-02-27 04:31:08 +00:00
|
|
|
|
|
|
|
// Add a field to |entry| which is a Func/Type.
|
2019-01-09 07:19:17 +00:00
|
|
|
void DoField(MessageHandler *m, Out_cclsMember *entry, const QueryVar &var,
|
|
|
|
int64_t offset, bool qualified, int levels) {
|
2018-08-09 17:08:14 +00:00
|
|
|
const QueryVar::Def *def1 = var.AnyDef();
|
2018-02-27 04:31:08 +00:00
|
|
|
if (!def1)
|
|
|
|
return;
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsMember entry1;
|
2018-05-29 00:03:14 +00:00
|
|
|
// 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 += ' ';
|
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
if (qualified)
|
2018-05-29 00:03:14 +00:00
|
|
|
entry1.fieldName += def1->detailed_name;
|
2018-06-08 04:53:41 +00:00
|
|
|
else {
|
|
|
|
entry1.fieldName +=
|
|
|
|
std::string_view(def1->detailed_name).substr(0, def1->qual_name_offset);
|
|
|
|
entry1.fieldName += def1->Name(false);
|
|
|
|
}
|
2018-02-27 04:31:08 +00:00
|
|
|
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))
|
2018-02-27 04:31:08 +00:00
|
|
|
entry1.location = *loc;
|
|
|
|
}
|
|
|
|
if (def1->type) {
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = std::to_string(def1->type);
|
|
|
|
entry1.usr = def1->type;
|
2018-09-23 05:55:17 +00:00
|
|
|
if (Expand(m, &entry1, qualified, levels, SymbolKind::Var))
|
2018-02-27 04:31:08 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
} else {
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = "0";
|
|
|
|
entry1.usr = 0;
|
2018-02-27 04:31:08 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand a type node by adding members recursively to it.
|
2019-01-09 07:19:17 +00:00
|
|
|
bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
|
|
|
|
int levels, SymbolKind memberKind) {
|
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-09-23 05:55:17 +00:00
|
|
|
const QueryType *type = &m->db->Type(entry->usr);
|
|
|
|
const QueryType::Def *def = type->AnyDef();
|
2018-04-30 04:49:03 +00:00
|
|
|
// 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;
|
2018-04-06 00:00:07 +00:00
|
|
|
entry->name = def->Name(qualified);
|
2018-02-26 08:07:01 +00:00
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-25 17:23:38 +00:00
|
|
|
if (levels > 0) {
|
2018-08-09 17:08:14 +00:00
|
|
|
std::vector<const QueryType *> stack;
|
2018-09-23 05:55:17 +00:00
|
|
|
seen.insert(type->usr);
|
|
|
|
stack.push_back(type);
|
2018-02-26 06:55:17 +00:00
|
|
|
while (stack.size()) {
|
2018-09-23 05:55:17 +00:00
|
|
|
type = stack.back();
|
2018-02-26 06:55:17 +00:00
|
|
|
stack.pop_back();
|
2018-09-23 05:55:17 +00:00
|
|
|
const auto *def = type->AnyDef();
|
2018-10-10 05:29:51 +00:00
|
|
|
if (!def)
|
|
|
|
continue;
|
|
|
|
if (def->kind != lsSymbolKind::Namespace)
|
|
|
|
for (Usr usr : def->bases) {
|
|
|
|
auto &type1 = m->db->Type(usr);
|
|
|
|
if (type1.def.size()) {
|
|
|
|
seen.insert(type1.usr);
|
|
|
|
stack.push_back(&type1);
|
|
|
|
}
|
2018-09-13 07:18:37 +00:00
|
|
|
}
|
2018-09-23 05:55:17 +00:00
|
|
|
if (def->alias_of) {
|
|
|
|
const QueryType::Def *def1 = m->db->Type(def->alias_of).AnyDef();
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsMember entry1;
|
2018-09-23 05:55:17 +00:00
|
|
|
entry1.id = std::to_string(def->alias_of);
|
|
|
|
entry1.usr = def->alias_of;
|
|
|
|
if (def1 && def1->spell) {
|
|
|
|
// The declaration of target type.
|
|
|
|
if (std::optional<lsLocation> loc =
|
|
|
|
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.
|
|
|
|
if (std::optional<lsLocation> loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry1.location = *loc;
|
2018-02-26 08:07:01 +00:00
|
|
|
}
|
2018-09-23 05:55:17 +00:00
|
|
|
if (def1 && qualified)
|
|
|
|
entry1.fieldName = def1->detailed_name;
|
|
|
|
if (Expand(m, &entry1, qualified, levels - 1, memberKind)) {
|
|
|
|
// For builtin types |name| is set.
|
|
|
|
if (entry1.fieldName.empty())
|
|
|
|
entry1.fieldName = std::string(entry1.name);
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
} else if (memberKind == SymbolKind::Func) {
|
|
|
|
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
|
|
|
|
for (auto &def : type->def)
|
|
|
|
for (Usr usr : def.funcs)
|
|
|
|
if (seen1.insert(usr).second) {
|
|
|
|
QueryFunc &func1 = m->db->Func(usr);
|
|
|
|
if (const QueryFunc::Def *def1 = func1.AnyDef()) {
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsMember entry1;
|
2018-09-23 05:55:17 +00:00
|
|
|
entry1.fieldName = def1->Name(false);
|
|
|
|
if (def1->spell) {
|
|
|
|
if (auto loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
|
|
|
entry1.location = *loc;
|
|
|
|
} else if (func1.declarations.size()) {
|
|
|
|
if (auto loc = GetLsLocation(m->db, m->working_files,
|
|
|
|
func1.declarations[0]))
|
|
|
|
entry1.location = *loc;
|
|
|
|
}
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (memberKind == SymbolKind::Type) {
|
|
|
|
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
|
|
|
|
for (auto &def : type->def)
|
|
|
|
for (Usr usr : def.types)
|
|
|
|
if (seen1.insert(usr).second) {
|
|
|
|
QueryType &type1 = m->db->Type(usr);
|
|
|
|
if (const QueryType::Def *def1 = type1.AnyDef()) {
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsMember entry1;
|
2018-09-23 05:55:17 +00:00
|
|
|
entry1.fieldName = def1->Name(false);
|
|
|
|
if (def1->spell) {
|
|
|
|
if (auto loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
|
|
|
entry1.location = *loc;
|
|
|
|
} else if (type1.declarations.size()) {
|
|
|
|
if (auto loc = GetLsLocation(m->db, m->working_files,
|
|
|
|
type1.declarations[0]))
|
|
|
|
entry1.location = *loc;
|
|
|
|
}
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
|
|
|
|
for (auto &def : type->def)
|
|
|
|
for (auto it : def.vars)
|
|
|
|
if (seen1.insert(it.first).second) {
|
|
|
|
QueryVar &var = m->db->Var(it.first);
|
|
|
|
if (!var.def.empty())
|
|
|
|
DoField(m, entry, var, it.second, qualified, levels - 1);
|
|
|
|
}
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
2018-02-26 06:55:17 +00:00
|
|
|
}
|
2018-02-25 22:53:57 +00:00
|
|
|
entry->numChildren = int(entry->children.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
} else
|
2018-02-26 08:07:01 +00:00
|
|
|
entry->numChildren = def->alias_of ? 1 : int(def->vars.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
return true;
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
std::optional<Out_cclsMember> BuildInitial(MessageHandler *m, SymbolKind kind,
|
|
|
|
Usr root_usr, bool qualified,
|
|
|
|
int levels, SymbolKind memberKind) {
|
|
|
|
switch (kind) {
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
case SymbolKind::Func: {
|
|
|
|
const auto *def = m->db->Func(root_usr).AnyDef();
|
|
|
|
if (!def)
|
2018-02-27 04:31:08 +00:00
|
|
|
return {};
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
Out_cclsMember entry;
|
|
|
|
// Not type, |id| is invalid.
|
|
|
|
entry.name = def->Name(qualified);
|
|
|
|
if (def->spell) {
|
|
|
|
if (auto loc = GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
2018-02-27 04:31:08 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
for (Usr usr : def->vars) {
|
|
|
|
auto &var = m->db->Var(usr);
|
|
|
|
if (var.def.size())
|
|
|
|
DoField(m, &entry, var, -1, qualified, levels - 1);
|
2018-04-30 04:49:03 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
case SymbolKind::Type: {
|
|
|
|
const auto *def = m->db->Type(root_usr).AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Out_cclsMember entry;
|
|
|
|
entry.id = std::to_string(root_usr);
|
|
|
|
entry.usr = root_usr;
|
|
|
|
if (def->spell) {
|
|
|
|
if (auto loc = GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
Expand(m, &entry, qualified, levels, memberKind);
|
|
|
|
return entry;
|
2018-02-25 17:23:38 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
void MessageHandler::ccls_member(Reader &reader, ReplyOnce &reply) {
|
|
|
|
Param param;
|
|
|
|
Reflect(reader, param);
|
|
|
|
std::optional<Out_cclsMember> result;
|
|
|
|
if (param.id.size()) {
|
|
|
|
try {
|
|
|
|
param.usr = std::stoull(param.id);
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result.emplace();
|
|
|
|
result->id = std::to_string(param.usr);
|
|
|
|
result->usr = param.usr;
|
|
|
|
// entry.name is empty as it is known by the client.
|
|
|
|
if (!(db->HasType(param.usr) && Expand(this, &*result, param.qualified,
|
|
|
|
param.levels, param.kind)))
|
|
|
|
result.reset();
|
|
|
|
} else {
|
|
|
|
QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath());
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
|
|
|
|
for (SymbolRef sym :
|
|
|
|
FindSymbolsAtLocation(wfile, file, param.position)) {
|
|
|
|
switch (sym.kind) {
|
|
|
|
case SymbolKind::Func:
|
|
|
|
case SymbolKind::Type:
|
|
|
|
result = BuildInitial(this, sym.kind, sym.usr, param.qualified,
|
|
|
|
param.levels, param.kind);
|
|
|
|
break;
|
|
|
|
case SymbolKind::Var: {
|
|
|
|
const QueryVar::Def *def = db->GetVar(sym).AnyDef();
|
|
|
|
if (def && def->type)
|
|
|
|
result = BuildInitial(this, SymbolKind::Type, def->type, param.qualified,
|
|
|
|
param.levels, param.kind);
|
2018-02-28 05:56:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2018-08-24 03:33:58 +00:00
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
if (param.hierarchy)
|
|
|
|
reply(result);
|
|
|
|
else {
|
|
|
|
auto out = FlattenHierarchy(result);
|
|
|
|
reply(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace ccls
|