Change TypeDefDefinition::short_name from string to string_view

This commit is contained in:
Fangrui Song 2018-01-30 23:01:45 -08:00
parent 93269ecfd4
commit 77dca1936b
7 changed files with 45 additions and 28 deletions

View File

@ -411,11 +411,16 @@ void SetTypeName(IndexType* type,
CXIdxContainerInfo parent;
// |name| can be null in an anonymous struct (see
// tests/types/anonymous_struct.cc).
type->def.short_name = name ? name : "(anon)";
if (!name)
name = "(anon)";
if (!container)
parent.cursor = cursor.get_semantic_parent().cx_cursor;
type->def.detailed_name =
ns->QualifiedName(container ? container : &parent, type->def.short_name);
ns->QualifiedName(container ? container : &parent, name);
auto idx = type->def.detailed_name.find(name);
assert(idx != std::string::npos);
type->def.short_name_offset = idx;
type->def.short_name_size = strlen(name);
}
// Finds the cursor associated with the declaration type of |cursor|. This
@ -452,7 +457,7 @@ optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db,
clang_disposeString(cx_usr);
IndexTypeId type_id = db->ToTypeId(usr);
IndexType* typ = db->Resolve(type_id);
if (typ->def.short_name.empty()) {
if (typ->def.detailed_name.empty()) {
std::string name = declaration.get_spelling();
SetTypeName(typ, declaration, nullptr, name.c_str(), ns);
}
@ -846,7 +851,7 @@ void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor,
if (param->toplevel_type) {
IndexType* ref_type = db->Resolve(*param->toplevel_type);
std::string name = cursor.get_referenced().get_spelling();
if (name == ref_type->def.short_name) {
if (name == ref_type->def.ShortName()) {
UniqueAdd(ref_type->uses, cursor.get_spelling_range());
param->toplevel_type = nullopt;
return;
@ -1240,11 +1245,12 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
// CXCursor_TemplateTemplateParameter can be visited by visiting
// CXCursor_TranslationUnit, but not (confirm this) by visiting
// {Class,Function}Template. Thus we need to initialize it here.
if (ref_index->def.short_name.empty()) {
if (ref_index->def.detailed_name.empty()) {
ref_index->def.definition_spelling = ref_cursor.get_spelling_range();
ref_index->def.definition_extent = ref_cursor.get_extent();
ref_index->def.short_name = ref_cursor.get_spelling();
ref_index->def.detailed_name = ref_index->def.short_name;
ref_index->def.detailed_name = ref_cursor.get_spelling();
ref_index->def.short_name_offset = 0;
ref_index->def.short_name_size = ref_index->def.detailed_name.size();
}
UniqueAdd(ref_index->uses, cursor.get_spelling_range());
}
@ -1260,11 +1266,12 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
// CXCursor_TemplateTypeParameter can be visited by visiting
// CXCursor_TranslationUnit, but not (confirm this) by visiting
// {Class,Function}Template. Thus we need to initialize it here.
if (ref_index->def.short_name.empty()) {
if (ref_index->def.detailed_name.empty()) {
ref_index->def.definition_spelling = ref_cursor.get_spelling_range();
ref_index->def.definition_extent = ref_cursor.get_extent();
ref_index->def.short_name = ref_cursor.get_spelling();
ref_index->def.detailed_name = ref_index->def.short_name;
ref_index->def.detailed_name = ref_cursor.get_spelling();
ref_index->def.short_name_offset = 0;
ref_index->def.short_name_size = ref_index->def.detailed_name.size();
}
UniqueAdd(ref_index->uses, cursor.get_spelling_range());
}
@ -1378,7 +1385,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
IndexTypeId ns_id = db->ToTypeId(HashUsr(decl->entityInfo->USR));
IndexType* ns = db->Resolve(ns_id);
ns->def.kind = GetSymbolKind(decl->entityInfo->kind);
if (ns->def.short_name.empty()) {
if (ns->def.detailed_name.empty()) {
SetTypeName(ns, decl_cursor, decl->semanticContainer,
decl->entityInfo->name, &param->ns);
ns->def.definition_spelling = decl_spell;
@ -1717,9 +1724,9 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// template<class T> class function; // not visited by
// OnIndexDeclaration template<> class function<int> {}; // current
// cursor
if (origin->def.short_name.empty()) {
if (origin->def.detailed_name.empty()) {
SetTypeName(origin, origin_cursor, nullptr,
type->def.short_name.c_str(), ns);
&type->def.ShortName()[0], ns);
origin->def.kind = type->def.kind;
}
// TODO The name may be assigned in |ResolveToDeclarationType| but

View File

@ -154,7 +154,6 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) {
template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct TypeDefDefinitionData {
// General metadata.
std::string short_name;
std::string detailed_name;
ClangSymbolKind kind = ClangSymbolKind::Unknown;
optional<std::string> hover;
@ -184,6 +183,9 @@ struct TypeDefDefinitionData {
std::vector<FuncId> funcs;
std::vector<VarId> vars;
int16_t short_name_offset = 0;
int16_t short_name_size = 0;
bool operator==(
const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return detailed_name == other.detailed_name && hover == other.hover &&
@ -198,6 +200,11 @@ struct TypeDefDefinitionData {
const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return !(*this == other);
}
std::string_view ShortName() const {
return std::string_view(detailed_name.c_str() + short_name_offset,
short_name_size);
}
};
template <typename TVisitor,
typename TypeId,
@ -207,8 +214,9 @@ template <typename TVisitor,
void Reflect(TVisitor& visitor,
TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(short_name_offset);
REFLECT_MEMBER(short_name_size);
REFLECT_MEMBER(kind);
REFLECT_MEMBER(hover);
REFLECT_MEMBER(comments);
@ -273,8 +281,8 @@ struct FuncDefDefinitionData {
// Functions that this function calls.
std::vector<FuncRef> callees;
int16_t short_name_offset;
int16_t short_name_size;
int16_t short_name_offset = 0;
int16_t short_name_size = 0;
ClangSymbolKind kind = ClangSymbolKind::Unknown;
StorageClass storage = StorageClass::Invalid;
@ -392,8 +400,8 @@ struct VarDefDefinitionData {
// Function/type which declares this one.
size_t parent_id = size_t(-1);
int16_t short_name_offset;
int16_t short_name_size;
int16_t short_name_offset = 0;
int16_t short_name_size = 0;
SymbolKind parent_kind = SymbolKind::Invalid;
ClangSymbolKind kind = ClangSymbolKind::Unknown;

View File

@ -47,7 +47,7 @@ BuildInitial(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) {
return {};
Out_CqueryMemberHierarchy::Entry entry;
entry.name = root_type.def->short_name;
entry.name = std::string(root_type.def->ShortName());
entry.type_id = root.id;
entry.location = *def_loc;
return {entry};

View File

@ -161,7 +161,7 @@ optional<lsTextEdit> BuildAutoImplementForFunction(QueryDatabase* db,
if (func.def->declaring_type) {
QueryType& declaring_type = db->types[func.def->declaring_type->id];
if (declaring_type.def) {
type_name = declaring_type.def->short_name;
type_name = std::string(declaring_type.def->ShortName());
optional<lsRange> ls_type_def_extent = GetLsRange(
working_file, declaring_type.def->definition_extent->range);
if (ls_type_def_extent) {
@ -390,7 +390,7 @@ struct TextDocumentCodeActionHandler
command.arguments.textDocumentUri = *impl_uri;
command.title = "Auto-Implement " + std::to_string(num_edits) +
" methods on " + type.def->short_name;
" methods on " + std::string(type.def->ShortName());
command.command = "cquery._autoImplement";
out.result.push_back(command);
break;

View File

@ -36,8 +36,9 @@ optional<QueryType::Def> ToQuery(const IdMap& id_map,
return nullopt;
QueryType::Def result;
result.short_name = type.short_name;
result.detailed_name = type.detailed_name;
result.short_name_offset = type.short_name_offset;
result.short_name_size = type.short_name_size;
result.kind = type.kind;
result.hover = type.hover;
result.comments = type.comments;
@ -883,7 +884,7 @@ void QueryDatabase::ImportOrUpdate(
existing.def = def.value;
UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Type,
it->second.id, def.value.short_name,
it->second.id, std::string(def.value.ShortName()),
def.value.detailed_name);
}
}

View File

@ -440,8 +440,8 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
lsSymbolInformation info;
info.name =
use_short_name ? type.def->short_name : type.def->detailed_name;
if (type.def->detailed_name != type.def->short_name)
use_short_name ? std::string(type.def->ShortName()) : type.def->detailed_name;
if (type.def->detailed_name != type.def->ShortName())
info.containerName = type.def->detailed_name;
// TODO ClangSymbolKind -> lsSymbolKind
switch (type.def->kind) {

View File

@ -155,8 +155,9 @@ void Reflect(TVisitor& visitor, IndexType& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER2("id", value.id);
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("short_name", value.def.short_name);
REFLECT_MEMBER2("detailed_name", value.def.detailed_name);
REFLECT_MEMBER2("short_name_offset", value.def.short_name_offset);
REFLECT_MEMBER2("short_name_size", value.def.short_name_size);
REFLECT_MEMBER2("kind", value.def.kind);
REFLECT_MEMBER2("hover", value.def.hover);
REFLECT_MEMBER2("comments", value.def.comments);
@ -224,7 +225,7 @@ bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
// FIXME
auto it = value.id_cache.usr_to_type_id.find(HashUsr(""));
if (it != value.id_cache.usr_to_type_id.end()) {
value.Resolve(it->second)->def.short_name = "<fundamental>";
value.Resolve(it->second)->def.detailed_name = "<fundamental>";
assert(value.Resolve(it->second)->uses.size() == 0);
}