From 516b94e98242ed612bf1df9c2c782b54845825e0 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 5 Jan 2018 22:33:31 -0800 Subject: [PATCH] Change VarClass::cls to ClangSymbolKind::kind ClangSymbolKind is ported from clang::index::SymbolKind --- src/indexer.cc | 25 +++++++--------- src/indexer.h | 67 ++++++++++++++++++++++++++++++------------ src/message_handler.cc | 9 +++--- src/query.cc | 2 +- src/serializer.cc | 2 +- 5 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 32f2bbab..3ed3ddc9 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -408,7 +408,7 @@ void OnIndexReference_Function(IndexFile* db, } // namespace // static -int IndexFile::kCurrentVersion = 6; +int IndexFile::kCurrentVersion = 7; IndexFile::IndexFile(const std::string& path) : id_cache(path), path(path) { // TODO: Reconsider if we should still be reusing the same id_cache. @@ -965,7 +965,7 @@ ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor, var_def->def.detailed_name = cursor.get_display_name(); var_def->def.hover = "#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent); - var_def->def.cls = VarClass::Macro; + var_def->def.kind = ClangSymbolKind::Macro; var_def->def.comments = cursor.get_comments(); var_def->def.definition_spelling = decl_loc_spelling; var_def->def.definition_extent = ResolveCXSourceRange(cx_extent, nullptr); @@ -1213,18 +1213,13 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { SetVarDetail(var, decl->cursor, decl->semanticContainer, !decl->isRedeclaration, db, param); - bool is_system = clang_Location_isInSystemHeader( - clang_indexLoc_getCXSourceLocation(decl->loc)); - if (is_system) - var->def.cls = VarClass::Unknown; - else { - if (IsGlobalContainer(decl->semanticContainer)) - var->def.cls = VarClass::Global; - else if (IsTypeDefinition(decl->semanticContainer)) - var->def.cls = VarClass::Member; - else - var->def.cls = VarClass::Local; - } + // FIXME https://github.com/jacobdufault/cquery/issues/239 + if (IsGlobalContainer(decl->semanticContainer)) + var->def.kind = ClangSymbolKind::Module; + else if (IsTypeDefinition(decl->semanticContainer)) + var->def.kind = ClangSymbolKind::Field; + else + var->def.kind = ClangSymbolKind::Variable; //} if (decl->isDefinition) { @@ -1635,7 +1630,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { // and has more information, thus not easy to reuse the code. var->def.short_name = referenced.get_spelling(); SetVarDetail(var, referenced, nullptr, true, db, param); - var->def.cls = VarClass::Local; + var->def.kind = ClangSymbolKind::Parameter; UniqueAdd(var->uses, referenced.get_spelling_range()); } } diff --git a/src/indexer.h b/src/indexer.h index 466c29b1..5e4eaaab 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -78,6 +78,45 @@ using IndexVarId = Id; struct IdCache; +// TODO Rename query.h:SymbolKind to another name; change int16_t to uint8_t +// clang/Index/IndexSymbol.h clang::index::SymbolKind +enum class ClangSymbolKind : int16_t { + Unknown, + + Module, + Namespace, + NamespaceAlias, + Macro, + + Enum, + Struct, + Class, + Protocol, + Extension, + Union, + TypeAlias, + + Function, + Variable, + Field, + EnumConstant, + + InstanceMethod, + ClassMethod, + StaticMethod, + InstanceProperty, + ClassProperty, + StaticProperty, + + Constructor, + Destructor, + ConversionFunction, + + Parameter, + Using, +}; +MAKE_REFLECT_TYPE_PROXY(ClangSymbolKind, std::underlying_type::type); + struct IndexFuncRef { // NOTE: id can be -1 if the function call is not coming from a function. IndexFuncId id; @@ -149,6 +188,7 @@ struct TypeDefDefinitionData { // General metadata. std::string short_name; std::string detailed_name; + ClangSymbolKind kind = ClangSymbolKind::Unknown; optional hover; optional comments; @@ -202,6 +242,7 @@ void Reflect(TVisitor& visitor, REFLECT_MEMBER_START(); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); + REFLECT_MEMBER(kind); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); REFLECT_MEMBER(definition_spelling); @@ -249,6 +290,7 @@ struct FuncDefDefinitionData { // General metadata. std::string short_name; std::string detailed_name; + ClangSymbolKind kind = ClangSymbolKind::Unknown; optional hover; optional comments; optional definition_spelling; @@ -299,6 +341,7 @@ void Reflect( REFLECT_MEMBER_START(); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); + REFLECT_MEMBER(kind); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); REFLECT_MEMBER(definition_spelling); @@ -361,25 +404,12 @@ MAKE_REFLECT_STRUCT(IndexFunc::Declaration, content, param_spellings); -enum class VarClass { - // probably a variable in system headers - Unknown = 0, - // a parameter or function variable - Local = 1, - // a macro, ie, #define FOO - Macro = 2, - // a global variable - Global = 3, - // a member variable of struct/union/class/enum - Member = 4 -}; -MAKE_REFLECT_TYPE_PROXY(VarClass, std::underlying_type::type); - template struct VarDefDefinitionData { // General metadata. std::string short_name; std::string detailed_name; + ClangSymbolKind kind = ClangSymbolKind::Unknown; optional hover; optional comments; optional declaration; @@ -394,10 +424,9 @@ struct VarDefDefinitionData { // Type which declares this one. optional declaring_type; - VarClass cls = VarClass::Unknown; - - bool is_local() const { return cls == VarClass::Local; } - bool is_macro() const { return cls == VarClass::Macro; } + // FIXME + bool is_local() const { return kind == ClangSymbolKind::Variable; } + bool is_macro() const { return kind == ClangSymbolKind::Macro; } bool operator==( const VarDefDefinitionData& other) const { @@ -426,13 +455,13 @@ void Reflect(TVisitor& visitor, REFLECT_MEMBER_START(); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); + REFLECT_MEMBER(kind); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); REFLECT_MEMBER(definition_spelling); REFLECT_MEMBER(definition_extent); REFLECT_MEMBER(variable_type); REFLECT_MEMBER(declaring_type); - REFLECT_MEMBER(cls); REFLECT_MEMBER_END(); } diff --git a/src/message_handler.cc b/src/message_handler.cc index 72a556f6..72ee844b 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -126,10 +126,11 @@ void EmitSemanticHighlighting(QueryDatabase* db, QueryVar* var = &db->vars[sym.idx.idx]; if (!var->def) continue; // applies to for loop - switch (var->def->cls) { - case VarClass::Local: - case VarClass::Global: - case VarClass::Member: + switch (var->def->kind) { + case ClangSymbolKind::Field: + case ClangSymbolKind::Macro: + case ClangSymbolKind::Module: + case ClangSymbolKind::Variable: break; default: continue; // applies to for loop diff --git a/src/query.cc b/src/query.cc index 39e54c47..8775a03d 100644 --- a/src/query.cc +++ b/src/query.cc @@ -72,7 +72,7 @@ optional ToQuery(const IdMap& id_map, const IndexVar::Def& var) { result.definition_extent = id_map.ToQuery(var.definition_extent); result.variable_type = id_map.ToQuery(var.variable_type); result.declaring_type = id_map.ToQuery(var.declaring_type); - result.cls = var.cls; + result.kind = var.kind; return result; } diff --git a/src/serializer.cc b/src/serializer.cc index 60b4c363..5f59dadb 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -150,7 +150,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) { REFLECT_MEMBER2("definition_extent", value.def.definition_extent); REFLECT_MEMBER2("variable_type", value.def.variable_type); REFLECT_MEMBER2("declaring_type", value.def.declaring_type); - REFLECT_MEMBER2("cls", value.def.cls); + REFLECT_MEMBER2("kind", value.def.kind); REFLECT_MEMBER2("uses", value.uses); REFLECT_MEMBER_END(); }