Remove ClangSymbolKind in favor of lsSymbolKind

This commit is contained in:
Fangrui Song 2018-02-18 11:29:38 -08:00
parent f3134d564c
commit 58d6547411
7 changed files with 61 additions and 125 deletions

View File

@ -89,60 +89,59 @@ SymbolKind GetSymbolKind(CXCursorKind kind) {
} }
// Inverse of libclang/CXIndexDataConsumer.cpp getEntityKindFromSymbolKind // Inverse of libclang/CXIndexDataConsumer.cpp getEntityKindFromSymbolKind
ClangSymbolKind GetSymbolKind(CXIdxEntityKind kind) { lsSymbolKind GetSymbolKind(CXIdxEntityKind kind) {
switch (kind) { switch (kind) {
default: default:
return ClangSymbolKind::Unknown; return lsSymbolKind::Unknown;
case CXIdxEntity_Enum: case CXIdxEntity_Enum:
return ClangSymbolKind::Enum; return lsSymbolKind::Enum;
case CXIdxEntity_Struct: case CXIdxEntity_Struct:
return ClangSymbolKind::Struct;
case CXIdxEntity_Union: case CXIdxEntity_Union:
return ClangSymbolKind::Union; return lsSymbolKind::Struct;
case CXIdxEntity_CXXTypeAlias: case CXIdxEntity_CXXTypeAlias:
case CXIdxEntity_Typedef: case CXIdxEntity_Typedef:
return ClangSymbolKind::TypeAlias; return lsSymbolKind::TypeParameter;
case CXIdxEntity_Function: case CXIdxEntity_Function:
return ClangSymbolKind::Function; return lsSymbolKind::Function;
case CXIdxEntity_Variable: case CXIdxEntity_Variable:
// Can also be Parameter // Can also be Parameter
return ClangSymbolKind::Variable; return lsSymbolKind::Variable;
case CXIdxEntity_Field: case CXIdxEntity_Field:
case CXIdxEntity_ObjCIvar: case CXIdxEntity_ObjCIvar:
return ClangSymbolKind::Field; return lsSymbolKind::Field;
case CXIdxEntity_EnumConstant: case CXIdxEntity_EnumConstant:
return ClangSymbolKind::EnumConstant; return lsSymbolKind::EnumMember;
case CXIdxEntity_CXXClass: case CXIdxEntity_CXXClass:
case CXIdxEntity_ObjCClass: case CXIdxEntity_ObjCClass:
return ClangSymbolKind::Class; return lsSymbolKind::Class;
case CXIdxEntity_CXXInterface: case CXIdxEntity_CXXInterface:
case CXIdxEntity_ObjCProtocol: case CXIdxEntity_ObjCProtocol:
return ClangSymbolKind::Protocol; return lsSymbolKind::Interface;
case CXIdxEntity_ObjCCategory: case CXIdxEntity_ObjCCategory:
return ClangSymbolKind::Extension; return lsSymbolKind::Interface;
case CXIdxEntity_CXXInstanceMethod: case CXIdxEntity_CXXInstanceMethod:
case CXIdxEntity_ObjCInstanceMethod: case CXIdxEntity_ObjCInstanceMethod:
return ClangSymbolKind::InstanceMethod; return lsSymbolKind::Method;
case CXIdxEntity_ObjCClassMethod: case CXIdxEntity_ObjCClassMethod:
return ClangSymbolKind::ClassMethod; return lsSymbolKind::Method;
case CXIdxEntity_CXXStaticMethod: case CXIdxEntity_CXXStaticMethod:
return ClangSymbolKind::StaticMethod; return lsSymbolKind::Method;
case CXIdxEntity_ObjCProperty: case CXIdxEntity_ObjCProperty:
return ClangSymbolKind::InstanceProperty; return lsSymbolKind::Property;
case CXIdxEntity_CXXStaticVariable: case CXIdxEntity_CXXStaticVariable:
return ClangSymbolKind::StaticProperty; return lsSymbolKind::Field;
case CXIdxEntity_CXXNamespace: case CXIdxEntity_CXXNamespace:
return ClangSymbolKind::Namespace; return lsSymbolKind::Namespace;
case CXIdxEntity_CXXNamespaceAlias: case CXIdxEntity_CXXNamespaceAlias:
return ClangSymbolKind::NamespaceAlias; return lsSymbolKind::Namespace;
case CXIdxEntity_CXXConstructor: case CXIdxEntity_CXXConstructor:
return ClangSymbolKind::Constructor; return lsSymbolKind::Constructor;
case CXIdxEntity_CXXDestructor: case CXIdxEntity_CXXDestructor:
return ClangSymbolKind::Destructor; return lsSymbolKind::Method;
case CXIdxEntity_CXXConversionFunction: case CXIdxEntity_CXXConversionFunction:
return ClangSymbolKind::ConversionFunction; return lsSymbolKind::Constructor;
} }
} }
@ -448,39 +447,33 @@ std::string GetDocumentContentInRange(CXTranslationUnit cx_tu,
void SetUsePreflight(IndexFile* db, ClangCursor parent) { void SetUsePreflight(IndexFile* db, ClangCursor parent) {
switch (GetSymbolKind(parent.get_kind())) { switch (GetSymbolKind(parent.get_kind())) {
case SymbolKind::Func: { case SymbolKind::Func:
(void)db->ToFuncId(parent.cx_cursor); (void)db->ToFuncId(parent.cx_cursor);
break; break;
} case SymbolKind::Type:
case SymbolKind::Type: {
(void)db->ToTypeId(parent.cx_cursor); (void)db->ToTypeId(parent.cx_cursor);
break; break;
} case SymbolKind::Var:
case SymbolKind::Var: {
(void)db->ToVarId(parent.cx_cursor); (void)db->ToVarId(parent.cx_cursor);
break; break;
default: default:
break; break;
} }
}
} }
// |parent| should be resolved before using |SetUsePreflight| so that |def| will // |parent| should be resolved before using |SetUsePreflight| so that |def| will
// not be invalidated by |To{Func,Type,Var}Id|. // not be invalidated by |To{Func,Type,Var}Id|.
Use SetUse(IndexFile* db, Range range, ClangCursor parent, Role role) { Use SetUse(IndexFile* db, Range range, ClangCursor parent, Role role) {
switch (GetSymbolKind(parent.get_kind())) { switch (GetSymbolKind(parent.get_kind())) {
case SymbolKind::Func: { case SymbolKind::Func:
IndexFuncId id = db->ToFuncId(parent.cx_cursor); return Use(range, db->ToFuncId(parent.cx_cursor), SymbolKind::Func, role,
return Use(range, id, SymbolKind::Func, Role::Definition, {}); {});
} case SymbolKind::Type:
case SymbolKind::Type: { return Use(range, db->ToTypeId(parent.cx_cursor), SymbolKind::Type, role,
IndexTypeId id = db->ToTypeId(parent.cx_cursor); {});
return Use(range, id, SymbolKind::Type, Role::Definition, {}); case SymbolKind::Var:
} return Use(range, db->ToVarId(parent.cx_cursor), SymbolKind::Var, role,
case SymbolKind::Var: { {});
IndexVarId id = db->ToVarId(parent.cx_cursor);
return Use(range, id, SymbolKind::Var, Role::Definition, {});
}
default: default:
return Use(range, Id<void>(), SymbolKind::File, role, {}); return Use(range, Id<void>(), SymbolKind::File, role, {});
} }
@ -1237,7 +1230,7 @@ ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor,
int16_t(strlen(var_def->def.detailed_name.c_str())); int16_t(strlen(var_def->def.detailed_name.c_str()));
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 = lsSymbolKind::Macro;
var_def->def.comments = cursor.get_comments(); var_def->def.comments = cursor.get_comments();
var_def->def.spell = var_def->def.spell =
SetUse(db, decl_loc_spelling, parent, Role::Definition); SetUse(db, decl_loc_spelling, parent, Role::Definition);
@ -1288,7 +1281,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
ref_var->def.extent = ref_var->def.extent =
SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None); SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
ref_var = db->Resolve(ref_var_id); ref_var = db->Resolve(ref_var_id);
ref_var->def.kind = ClangSymbolKind::Parameter; ref_var->def.kind = lsSymbolKind::Parameter;
SetVarDetail(ref_var, ref_cursor.get_spell_name(), ref_cursor, SetVarDetail(ref_var, ref_cursor.get_spell_name(), ref_cursor,
nullptr, true, db, param); nullptr, true, db, param);
@ -1356,7 +1349,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
ref_type->def.short_name_offset = 0; ref_type->def.short_name_offset = 0;
ref_type->def.short_name_size = ref_type->def.short_name_size =
int16_t(strlen(ref_type->def.detailed_name.c_str())); int16_t(strlen(ref_type->def.detailed_name.c_str()));
ref_type->def.kind = ClangSymbolKind::Parameter; ref_type->def.kind = lsSymbolKind::TypeParameter;
} }
UniqueAddUseSpell(db, ref_type->uses, cursor); UniqueAddUseSpell(db, ref_type->uses, cursor);
} }
@ -1390,7 +1383,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
ref_type->def.short_name_offset = 0; ref_type->def.short_name_offset = 0;
ref_type->def.short_name_size = ref_type->def.short_name_size =
int16_t(strlen(ref_type->def.detailed_name.c_str())); int16_t(strlen(ref_type->def.detailed_name.c_str()));
ref_type->def.kind = ClangSymbolKind::Parameter; ref_type->def.kind = lsSymbolKind::TypeParameter;
} }
UniqueAddUseSpell(db, ref_type->uses, cursor); UniqueAddUseSpell(db, ref_type->uses, cursor);
} }
@ -1552,9 +1545,9 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// 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);
if (var->def.kind == ClangSymbolKind::Variable && if (var->def.kind == lsSymbolKind::Variable &&
decl->cursor.kind == CXCursor_ParmDecl) decl->cursor.kind == CXCursor_ParmDecl)
var->def.kind = ClangSymbolKind::Parameter; var->def.kind = lsSymbolKind::Parameter;
//} //}
if (decl->isDefinition) { if (decl->isDefinition) {
@ -2022,7 +2015,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
// and has more information, thus not easy to reuse the code. // and has more information, thus not easy to reuse the code.
SetVarDetail(var, referenced.get_spell_name(), referenced, nullptr, SetVarDetail(var, referenced.get_spell_name(), referenced, nullptr,
true, db, param); true, db, param);
var->def.kind = ClangSymbolKind::Parameter; var->def.kind = lsSymbolKind::Parameter;
} }
} }
UniqueAddUse(db, var->uses, loc, fromContainer(ref->container), GetRole(ref, Role::Reference)); UniqueAddUse(db, var->uses, loc, fromContainer(ref->container), GetRole(ref, Role::Reference));

View File

@ -187,7 +187,7 @@ struct TypeDefDefinitionData {
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
ClangSymbolKind kind = ClangSymbolKind::Unknown; lsSymbolKind kind = lsSymbolKind::Unknown;
bool operator==(const TypeDefDefinitionData& o) const { bool operator==(const TypeDefDefinitionData& o) const {
return detailed_name == o.detailed_name && return detailed_name == o.detailed_name &&
@ -274,7 +274,7 @@ struct FuncDefDefinitionData {
Maybe<typename F::TypeId> declaring_type; Maybe<typename F::TypeId> declaring_type;
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
ClangSymbolKind kind = ClangSymbolKind::Unknown; lsSymbolKind kind = lsSymbolKind::Unknown;
StorageClass storage = StorageClass::Invalid; StorageClass storage = StorageClass::Invalid;
bool operator==(const FuncDefDefinitionData& o) const { bool operator==(const FuncDefDefinitionData& o) const {
@ -372,16 +372,15 @@ struct VarDefDefinitionData {
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
ClangSymbolKind kind = ClangSymbolKind::Unknown; lsSymbolKind kind = lsSymbolKind::Unknown;
// Note a variable may have instances of both |None| and |Extern| // Note a variable may have instances of both |None| and |Extern|
// (declaration). // (declaration).
StorageClass storage = StorageClass::Invalid; StorageClass storage = StorageClass::Invalid;
bool is_local() const { bool is_local() const {
return kind == ClangSymbolKind::Parameter || return kind == lsSymbolKind::Variable;
kind == ClangSymbolKind::Variable;
} }
bool is_macro() const { return kind == ClangSymbolKind::Macro; } bool is_macro() const { return kind == lsSymbolKind::Macro; }
bool operator==(const VarDefDefinitionData& o) const { bool operator==(const VarDefDefinitionData& o) const {
return detailed_name == o.detailed_name && spell == o.spell && return detailed_name == o.detailed_name && spell == o.spell &&

View File

@ -112,7 +112,7 @@ void EmitSemanticHighlighting(QueryDatabase* db,
for (SymbolRef sym : file->def->all_symbols) { for (SymbolRef sym : file->def->all_symbols) {
std::string_view detailed_name; std::string_view detailed_name;
SymbolKind parent_kind = SymbolKind::Invalid; SymbolKind parent_kind = SymbolKind::Invalid;
ClangSymbolKind kind = ClangSymbolKind::Unknown; lsSymbolKind kind = lsSymbolKind::Unknown;
StorageClass storage = StorageClass::Invalid; StorageClass storage = StorageClass::Invalid;
// This switch statement also filters out symbols that are not highlighted. // This switch statement also filters out symbols that are not highlighted.
switch (sym.kind) { switch (sym.kind) {

View File

@ -29,7 +29,7 @@ struct Out_CqueryPublishSemanticHighlighting
struct Symbol { struct Symbol {
int stableId = 0; int stableId = 0;
SymbolKind parentKind; SymbolKind parentKind;
ClangSymbolKind kind; lsSymbolKind kind;
StorageClass storage; StorageClass storage;
std::vector<lsRange> ranges; std::vector<lsRange> ranges;
}; };

View File

@ -155,7 +155,7 @@ struct TextDocumentCodeLensHandler
case SymbolKind::Type: { case SymbolKind::Type: {
QueryType& type = db->GetType(sym); QueryType& type = db->GetType(sym);
const QueryType::Def* def = type.AnyDef(); const QueryType::Def* def = type.AnyDef();
if (!def || def->kind == ClangSymbolKind::Namespace) if (!def || def->kind == lsSymbolKind::Namespace)
continue; continue;
AddCodeLens("ref", "refs", &common, OffsetStartColumn(use, 0), AddCodeLens("ref", "refs", &common, OffsetStartColumn(use, 0),
type.uses, true /*force_display*/); type.uses, true /*force_display*/);

View File

@ -459,17 +459,9 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
info.name = def->ShortName(); info.name = def->ShortName();
else else
info.name = def->detailed_name; info.name = def->detailed_name;
info.kind = def->kind;
if (def->detailed_name.c_str() != def->ShortName()) if (def->detailed_name.c_str() != def->ShortName())
info.containerName = def->detailed_name; info.containerName = def->detailed_name;
// TODO ClangSymbolKind -> lsSymbolKind
switch (def->kind) {
default:
info.kind = lsSymbolKind::Class;
break;
case ClangSymbolKind::Namespace:
info.kind = lsSymbolKind::Namespace;
break;
}
return info; return info;
} }
case SymbolKind::Func: { case SymbolKind::Func: {
@ -482,16 +474,8 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
info.name = def->ShortName(); info.name = def->ShortName();
else else
info.name = def->detailed_name; info.name = def->detailed_name;
info.kind = def->kind;
info.containerName = def->detailed_name; info.containerName = def->detailed_name;
switch (def->kind) {
default:
info.kind = lsSymbolKind::Function;
break;
case ClangSymbolKind::InstanceMethod:
case ClangSymbolKind::StaticMethod:
info.kind = lsSymbolKind::Method;
break;
}
return info; return info;
} }
case SymbolKind::Var: { case SymbolKind::Var: {
@ -504,15 +488,8 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
info.name = def->ShortName(); info.name = def->ShortName();
else else
info.name = def->detailed_name; info.name = def->detailed_name;
info.kind = def->kind;
info.containerName = def->detailed_name; info.containerName = def->detailed_name;
switch (def->kind) {
default:
info.kind = lsSymbolKind::Variable;
break;
case ClangSymbolKind::EnumConstant:
info.kind = lsSymbolKind::EnumMember;
break;
}
return info; return info;
} }
case SymbolKind::Invalid: case SymbolKind::Invalid:

View File

@ -8,47 +8,6 @@ enum class SymbolKind : uint8_t { Invalid, File, Type, Func, Var };
MAKE_REFLECT_TYPE_PROXY(SymbolKind); MAKE_REFLECT_TYPE_PROXY(SymbolKind);
MAKE_ENUM_HASHABLE(SymbolKind); MAKE_ENUM_HASHABLE(SymbolKind);
// TODO Rename query.h:SymbolKind to another name
// clang/Index/IndexSymbol.h clang::index::SymbolKind
enum class ClangSymbolKind : uint8_t {
Unknown,
Module = 1,
Namespace,
NamespaceAlias,
Macro,
Enum = 5,
Struct,
Class,
Protocol,
Extension,
Union,
TypeAlias,
Function = 12,
Variable,
Field,
EnumConstant,
InstanceMethod = 16,
ClassMethod,
StaticMethod,
InstanceProperty,
ClassProperty,
StaticProperty,
Constructor = 22,
Destructor,
ConversionFunction,
// Used for both function and template parameters.
// Clients can differentiate based on StorageClass.
Parameter = 25,
Using,
};
MAKE_REFLECT_TYPE_PROXY(ClangSymbolKind);
// clang/Basic/Specifiers.h clang::StorageClass // clang/Basic/Specifiers.h clang::StorageClass
enum class StorageClass : uint8_t { enum class StorageClass : uint8_t {
// In |CX_StorageClass| but not in |clang::StorageClass| // In |CX_StorageClass| but not in |clang::StorageClass|
@ -119,7 +78,9 @@ struct lsDocumentHighlight {
}; };
MAKE_REFLECT_STRUCT(lsDocumentHighlight, range, kind, role); MAKE_REFLECT_STRUCT(lsDocumentHighlight, range, kind, role);
enum class lsSymbolKind : int { enum class lsSymbolKind : uint8_t {
Unknown = 0,
File = 1, File = 1,
Module = 2, Module = 2,
Namespace = 3, Namespace = 3,
@ -146,6 +107,12 @@ enum class lsSymbolKind : int {
Event = 24, Event = 24,
Operator = 25, Operator = 25,
TypeParameter = 26, TypeParameter = 26,
// cquery extensions
// See also https://github.com/Microsoft/language-server-protocol/issues/344 for new SymbolKind
// clang/Index/IndexSymbol.h clang::index::SymbolKind
Parameter = 13,
Macro = 255,
}; };
MAKE_REFLECT_TYPE_PROXY(lsSymbolKind); MAKE_REFLECT_TYPE_PROXY(lsSymbolKind);