Change std::string VarDefDefinition::short_name to ShortName()

This commit is contained in:
Fangrui Song 2018-01-30 20:59:31 -08:00
parent 8ff634d557
commit 41f87887bb
8 changed files with 76 additions and 48 deletions

View File

@ -460,6 +460,7 @@ optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db,
} }
void SetVarDetail(IndexVar* var, void SetVarDetail(IndexVar* var,
std::string_view short_name,
const ClangCursor& cursor, const ClangCursor& cursor,
const CXIdxContainerInfo* semanticContainer, const CXIdxContainerInfo* semanticContainer,
bool is_first_seen, bool is_first_seen,
@ -476,7 +477,7 @@ void SetVarDetail(IndexVar* var,
def.storage = GetStorageClass(clang_Cursor_getStorageClass(cursor.cx_cursor)); def.storage = GetStorageClass(clang_Cursor_getStorageClass(cursor.cx_cursor));
std::string qualified_name = std::string qualified_name =
param->ns.QualifiedName(semanticContainer, def.short_name); param->ns.QualifiedName(semanticContainer, short_name);
if (cursor.get_kind() == CXCursor_EnumConstantDecl && semanticContainer) { if (cursor.get_kind() == CXCursor_EnumConstantDecl && semanticContainer) {
CXType enum_type = clang_getCanonicalType( CXType enum_type = clang_getCanonicalType(
@ -515,6 +516,11 @@ void SetVarDetail(IndexVar* var,
fc.content.substr(*spell_end, *extent_end - *spell_end); fc.content.substr(*spell_end, *extent_end - *spell_end);
} }
} }
// FIXME QualifiedName should return index
auto idx = def.detailed_name.find(short_name.begin(), 0, short_name.size());
assert(idx != std::string::npos);
def.short_name_offset = idx;
def.short_name_size = short_name.size();
if (is_first_seen) { if (is_first_seen) {
optional<IndexTypeId> var_type = optional<IndexTypeId> var_type =
@ -1139,8 +1145,9 @@ ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor,
IndexVar* var_def = db->Resolve(db->ToVarId(decl_usr)); IndexVar* var_def = db->Resolve(db->ToVarId(decl_usr));
if (cursor.get_kind() == CXCursor_MacroDefinition) { if (cursor.get_kind() == CXCursor_MacroDefinition) {
CXSourceRange cx_extent = clang_getCursorExtent(cursor.cx_cursor); CXSourceRange cx_extent = clang_getCursorExtent(cursor.cx_cursor);
var_def->def.short_name = cursor.get_display_name();
var_def->def.detailed_name = cursor.get_display_name(); var_def->def.detailed_name = cursor.get_display_name();
var_def->def.short_name_offset = 0;
var_def->def.short_name_size = int(var_def->def.detailed_name.size());
var_def->def.hover = var_def->def.hover =
"#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent); "#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent);
var_def->def.kind = ClangSymbolKind::Macro; var_def->def.kind = ClangSymbolKind::Macro;
@ -1181,11 +1188,11 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) { if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) {
IndexVar* ref_index = IndexVar* ref_index =
db->Resolve(db->ToVarId(ref_cursor.get_usr_hash())); db->Resolve(db->ToVarId(ref_cursor.get_usr_hash()));
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_spelling = ref_cursor.get_spelling_range();
ref_index->def.definition_extent = ref_cursor.get_extent(); ref_index->def.definition_extent = ref_cursor.get_extent();
ref_index->def.short_name = ref_cursor.get_spelling(); SetVarDetail(ref_index, ref_cursor.get_spelling(), ref_cursor,
SetVarDetail(ref_index, ref_cursor, nullptr, true, db, data->param); nullptr, true, db, data->param);
ClangType ref_type = clang_getCursorType(ref_cursor.cx_cursor); ClangType ref_type = clang_getCursorType(ref_cursor.cx_cursor);
// TODO optimize // TODO optimize
@ -1270,9 +1277,9 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
} // namespace } // namespace
std::string NamespaceHelper::QualifiedName(const CXIdxContainerInfo* container, std::string NamespaceHelper::QualifiedName(const CXIdxContainerInfo* container,
std::string unqualified_name) { std::string_view unqualified_name) {
if (!container) if (!container)
return unqualified_name; return std::string(unqualified_name);
// Anonymous namespaces are not processed by indexDeclaration. We trace // Anonymous namespaces are not processed by indexDeclaration. We trace
// nested namespaces bottom-up through clang_getCursorSemanticParent until // nested namespaces bottom-up through clang_getCursorSemanticParent until
// one that we know its qualified name. Then do another trace top-down and // one that we know its qualified name. Then do another trace top-down and
@ -1318,7 +1325,8 @@ std::string NamespaceHelper::QualifiedName(const CXIdxContainerInfo* container,
qualifier += "::"; qualifier += "::";
container_cursor_to_qualified_name[namespaces[i]] = qualifier; container_cursor_to_qualified_name[namespaces[i]] = qualifier;
} }
return qualifier + unqualified_name; // C++17 string::append
return qualifier + std::string(unqualified_name);
} }
void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
@ -1409,10 +1417,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// this may shadow. // this may shadow.
// TODO: Verify this gets called multiple times // TODO: Verify this gets called multiple times
// if (!decl->isRedeclaration) { // if (!decl->isRedeclaration) {
var->def.short_name = decl->entityInfo->name; SetVarDetail(var, std::string(decl->entityInfo->name), decl->cursor,
decl->semanticContainer, !decl->isRedeclaration, db, param);
SetVarDetail(var, decl->cursor, decl->semanticContainer,
!decl->isRedeclaration, db, param);
// FIXME https://github.com/jacobdufault/cquery/issues/239 // FIXME https://github.com/jacobdufault/cquery/issues/239
var->def.kind = GetSymbolKind(decl->entityInfo->kind); var->def.kind = GetSymbolKind(decl->entityInfo->kind);
@ -1847,7 +1853,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
// may not have a short_name yet. Note that we only process the lambda // may not have a short_name yet. Note that we only process the lambda
// parameter as a definition if it is in the same file as the reference, // parameter as a definition if it is in the same file as the reference,
// as lambdas cannot be split across files. // as lambdas cannot be split across files.
if (var->def.short_name.empty()) { if (var->def.detailed_name.empty()) {
CXFile referenced_file; CXFile referenced_file;
Range spelling = referenced.get_spelling_range(&referenced_file); Range spelling = referenced.get_spelling_range(&referenced_file);
if (file == referenced_file) { if (file == referenced_file) {
@ -1857,8 +1863,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
// TODO Some of the logic here duplicates CXIdxEntity_Variable branch // TODO Some of the logic here duplicates CXIdxEntity_Variable branch
// of OnIndexDeclaration. But there `decl` is of type CXIdxDeclInfo // of OnIndexDeclaration. But there `decl` is of type CXIdxDeclInfo
// and has more information, thus not easy to reuse the code. // and has more information, thus not easy to reuse the code.
var->def.short_name = referenced.get_spelling(); SetVarDetail(var, referenced.get_spelling(), referenced, nullptr,
SetVarDetail(var, referenced, nullptr, true, db, param); true, db, param);
var->def.kind = ClangSymbolKind::Parameter; var->def.kind = ClangSymbolKind::Parameter;
} }
} }

View File

@ -18,6 +18,7 @@
#include <rapidjson/prettywriter.h> #include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h> #include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include <string_view.h>
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
@ -185,8 +186,7 @@ struct TypeDefDefinitionData {
bool operator==( bool operator==(
const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const { const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return short_name == other.short_name && return detailed_name == other.detailed_name && hover == other.hover &&
detailed_name == other.detailed_name && hover == other.hover &&
definition_spelling == other.definition_spelling && definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent && definition_extent == other.definition_extent &&
alias_of == other.alias_of && parents == other.parents && alias_of == other.alias_of && parents == other.parents &&
@ -279,8 +279,7 @@ struct FuncDefDefinitionData {
bool operator==( bool operator==(
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& other) const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& other)
const { const {
return short_name == other.short_name && return detailed_name == other.detailed_name && hover == other.hover &&
detailed_name == other.detailed_name && hover == other.hover &&
definition_spelling == other.definition_spelling && definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent && definition_extent == other.definition_extent &&
declaring_type == other.declaring_type && base == other.base && declaring_type == other.declaring_type && base == other.base &&
@ -372,7 +371,6 @@ MAKE_REFLECT_STRUCT(IndexFunc::Declaration,
template <typename TypeId, typename FuncId, typename VarId, typename Range> template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct VarDefDefinitionData { struct VarDefDefinitionData {
// General metadata. // General metadata.
std::string short_name;
std::string detailed_name; std::string detailed_name;
optional<std::string> hover; optional<std::string> hover;
optional<std::string> comments; optional<std::string> comments;
@ -386,6 +384,8 @@ struct VarDefDefinitionData {
// Function/type which declares this one. // Function/type which declares this one.
size_t parent_id = size_t(-1); size_t parent_id = size_t(-1);
int16_t short_name_offset;
int16_t short_name_size;
SymbolKind parent_kind = SymbolKind::Invalid; SymbolKind parent_kind = SymbolKind::Invalid;
ClangSymbolKind kind = ClangSymbolKind::Unknown; ClangSymbolKind kind = ClangSymbolKind::Unknown;
@ -401,8 +401,7 @@ struct VarDefDefinitionData {
bool operator==( bool operator==(
const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const { const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return short_name == other.short_name && return detailed_name == other.detailed_name && hover == other.hover &&
detailed_name == other.detailed_name && hover == other.hover &&
definition_spelling == other.definition_spelling && definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent && definition_extent == other.definition_extent &&
variable_type == other.variable_type && comments == other.comments; variable_type == other.variable_type && comments == other.comments;
@ -411,6 +410,11 @@ struct VarDefDefinitionData {
const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const { const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return !(*this == other); 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, template <typename TVisitor,
@ -421,8 +425,9 @@ template <typename TVisitor,
void Reflect(TVisitor& visitor, void Reflect(TVisitor& visitor,
VarDefDefinitionData<TypeId, FuncId, VarId, Range>& value) { VarDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(short_name_size);
REFLECT_MEMBER(short_name_offset);
REFLECT_MEMBER(hover); REFLECT_MEMBER(hover);
REFLECT_MEMBER(comments); REFLECT_MEMBER(comments);
REFLECT_MEMBER(definition_spelling); REFLECT_MEMBER(definition_spelling);
@ -538,7 +543,7 @@ struct NamespaceHelper {
container_cursor_to_qualified_name; container_cursor_to_qualified_name;
std::string QualifiedName(const CXIdxContainerInfo* container, std::string QualifiedName(const CXIdxContainerInfo* container,
std::string unqualified_name); std::string_view unqualified_name);
}; };
// |import_file| is the cc file which is what gets passed to clang. // |import_file| is the cc file which is what gets passed to clang.

View File

@ -110,7 +110,7 @@ void EmitSemanticHighlighting(QueryDatabase* db,
std::unordered_map<SymbolIdx, Out_CqueryPublishSemanticHighlighting::Symbol> std::unordered_map<SymbolIdx, Out_CqueryPublishSemanticHighlighting::Symbol>
grouped_symbols; grouped_symbols;
for (SymbolRef sym : file->def->all_symbols) { for (SymbolRef sym : file->def->all_symbols) {
std::string detailed_name; std::string_view detailed_name;
SymbolKind parent_kind = SymbolKind::Invalid; SymbolKind parent_kind = SymbolKind::Invalid;
ClangSymbolKind kind = ClangSymbolKind::Unknown; ClangSymbolKind kind = ClangSymbolKind::Unknown;
StorageClass storage = StorageClass::Invalid; StorageClass storage = StorageClass::Invalid;
@ -137,7 +137,7 @@ void EmitSemanticHighlighting(QueryDatabase* db,
parent_kind = var->def->parent_kind; parent_kind = var->def->parent_kind;
kind = var->def->kind; kind = var->def->kind;
storage = var->def->storage; storage = var->def->storage;
detailed_name = var->def->short_name; detailed_name = var->def->ShortName();
break; break;
} }
case SymbolKind::Type: { case SymbolKind::Type: {
@ -159,8 +159,8 @@ void EmitSemanticHighlighting(QueryDatabase* db,
it->second.ranges.push_back(*loc); it->second.ranges.push_back(*loc);
} else { } else {
Out_CqueryPublishSemanticHighlighting::Symbol symbol; Out_CqueryPublishSemanticHighlighting::Symbol symbol;
symbol.stableId = symbol.stableId = semantic_cache_for_file->GetStableId(
semantic_cache_for_file->GetStableId(sym.idx.kind, detailed_name); sym.idx.kind, std::string(detailed_name));
symbol.parentKind = parent_kind; symbol.parentKind = parent_kind;
symbol.kind = kind; symbol.kind = kind;
symbol.storage = storage; symbol.storage = storage;

View File

@ -63,7 +63,7 @@ ExpandNode(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) {
for (auto& var_id : root_type.def->vars) { for (auto& var_id : root_type.def->vars) {
QueryVar& var = db->vars[var_id.id]; QueryVar& var = db->vars[var_id.id];
Out_CqueryMemberHierarchy::Entry entry; Out_CqueryMemberHierarchy::Entry entry;
entry.name = var.def->short_name; entry.name = std::string(var.def->ShortName());
entry.type_id = entry.type_id =
var.def->variable_type ? var.def->variable_type->id : size_t(-1); var.def->variable_type ? var.def->variable_type->id : size_t(-1);
if (var.def->definition_spelling) { if (var.def->definition_spelling) {

View File

@ -77,8 +77,9 @@ optional<QueryVar::Def> ToQuery(const IdMap& id_map, const IndexVar::Def& var) {
return nullopt; return nullopt;
QueryVar::Def result; QueryVar::Def result;
result.short_name = var.short_name;
result.detailed_name = var.detailed_name; result.detailed_name = var.detailed_name;
result.short_name_offset = var.short_name_offset;
result.short_name_size = var.short_name_size;
result.hover = var.hover; result.hover = var.hover;
result.comments = var.comments; result.comments = var.comments;
result.definition_spelling = id_map.ToQuery(var.definition_spelling); result.definition_spelling = id_map.ToQuery(var.definition_spelling);
@ -932,7 +933,7 @@ void QueryDatabase::ImportOrUpdate(
existing.def = def.value; existing.def = def.value;
if (!def.value.is_local()) if (!def.value.is_local())
UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Var, UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Var,
it->second.id, def.value.short_name, it->second.id, std::string(def.value.ShortName()),
def.value.detailed_name); def.value.detailed_name);
} }
} }

View File

@ -479,7 +479,7 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
return nullopt; return nullopt;
lsSymbolInformation info; lsSymbolInformation info;
info.name = use_short_name ? var.def->short_name : var.def->detailed_name; info.name = use_short_name ? std::string(var.def->ShortName()) : var.def->detailed_name;
info.containerName = var.def->detailed_name; info.containerName = var.def->detailed_name;
info.kind = lsSymbolKind::Variable; info.kind = lsSymbolKind::Variable;
return info; return info;

View File

@ -106,7 +106,6 @@ void Reflect(Writer& visitor, bool& value) {
visitor.Bool(value); visitor.Bool(value);
} }
// std::string
void Reflect(Reader& visitor, std::string& value) { void Reflect(Reader& visitor, std::string& value) {
if (!visitor.IsString()) if (!visitor.IsString())
throw std::invalid_argument("std::string"); throw std::invalid_argument("std::string");
@ -116,6 +115,14 @@ void Reflect(Writer& visitor, std::string& value) {
visitor.String(value.c_str(), (rapidjson::SizeType)value.size()); visitor.String(value.c_str(), (rapidjson::SizeType)value.size());
} }
void Reflect(Reader&, std::string_view&) {
assert(0);
}
void Reflect(Writer& visitor, std::string_view& data) {
visitor.String(&data[0], (rapidjson::SizeType)data.size());
}
// ReflectMember // ReflectMember
void ReflectMember(Writer& visitor, const char* name, std::string& value) { void ReflectMember(Writer& visitor, const char* name, std::string& value) {
visitor.Key(name); visitor.Key(name);
@ -194,8 +201,9 @@ void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER2("id", value.id); REFLECT_MEMBER2("id", value.id);
REFLECT_MEMBER2("usr", value.usr); REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("short_name", value.def.short_name);
REFLECT_MEMBER2("detailed_name", value.def.detailed_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("hover", value.def.hover); REFLECT_MEMBER2("hover", value.def.hover);
REFLECT_MEMBER2("comments", value.def.comments); REFLECT_MEMBER2("comments", value.def.comments);
REFLECT_MEMBER2("declarations", value.declarations); REFLECT_MEMBER2("declarations", value.declarations);

View File

@ -4,6 +4,7 @@
#include <macro_map.h> #include <macro_map.h>
#include <optional.h> #include <optional.h>
#include <string_view.h>
#include <variant.h> #include <variant.h>
#include <cassert> #include <cassert>
@ -181,6 +182,9 @@ void Reflect(Writer& visitor, bool& value);
void Reflect(Reader& visitor, std::string& value); void Reflect(Reader& visitor, std::string& value);
void Reflect(Writer& visitor, std::string& value); void Reflect(Writer& visitor, std::string& value);
void Reflect(Reader& visitor, std::string& view, const std::string& data);
void Reflect(Writer& visitor, std::string& view, const std::string& data);
// std::monostate is used to represent JSON null // std::monostate is used to represent JSON null
void Reflect(Reader& visitor, std::monostate&); void Reflect(Reader& visitor, std::monostate&);
void Reflect(Writer& visitor, std::monostate&); void Reflect(Writer& visitor, std::monostate&);
@ -306,40 +310,44 @@ void Reflect(Writer& visitor, std::vector<T>& values) {
visitor.EndArray(); visitor.EndArray();
} }
// Writer: // ReflectMember
inline void DefaultReflectMemberStart(Writer& visitor) { inline void DefaultReflectMemberStart(Writer& visitor) {
visitor.StartObject(); visitor.StartObject();
} }
inline void DefaultReflectMemberStart(Reader& visitor) {}
template <typename T>
bool ReflectMemberStart(Reader& visitor, T& value) {
return true;
}
template <typename T> template <typename T>
bool ReflectMemberStart(Writer& visitor, T& value) { bool ReflectMemberStart(Writer& visitor, T& value) {
visitor.StartObject(); visitor.StartObject();
return true; return true;
} }
template <typename T>
void ReflectMemberEnd(Reader& visitor, T& value) {}
template <typename T> template <typename T>
void ReflectMemberEnd(Writer& visitor, T& value) { void ReflectMemberEnd(Writer& visitor, T& value) {
visitor.EndObject(); visitor.EndObject();
} }
template <typename T>
void ReflectMember(Reader& visitor, const char* name, T& value) {
visitor.DoMember(name, [&](Reader& child) { Reflect(child, value); });
}
template <typename T> template <typename T>
void ReflectMember(Writer& visitor, const char* name, T& value) { void ReflectMember(Writer& visitor, const char* name, T& value) {
visitor.Key(name); visitor.Key(name);
Reflect(visitor, value); Reflect(visitor, value);
} }
void ReflectMember(Writer& visitor, const char* name, std::string& value); void ReflectMember(Writer& visitor, const char* name, std::string& value);
// Reader: void ReflectMember(Reader& visitor, const char* name, std::string_view& view);
void ReflectMember(Writer& visitor, const char* name, std::string_view& view);
inline void DefaultReflectMemberStart(Reader& visitor) {}
template <typename T>
bool ReflectMemberStart(Reader& visitor, T& value) {
return true;
}
template <typename T>
void ReflectMemberEnd(Reader& visitor, T& value) {}
template <typename T>
void ReflectMember(Reader& visitor, const char* name, T& value) {
visitor.DoMember(name, [&](Reader& child) { Reflect(child, value); });
}
// API // API