From 09dd20226fb8b70ab564c6909dea60bdad707cb9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 26 Feb 2018 17:23:45 -0800 Subject: [PATCH] Rename FuncDefDefinitionData::locals to vars, remove constants from EnumDecl's vars --- src/clang_cursor.cc | 9 +------ src/clang_cursor.h | 7 +++++- src/clang_utils.cc | 31 +++++++++++++++++++++++++ src/clang_utils.h | 2 ++ src/indexer.cc | 21 ++++++++++------- src/indexer.h | 8 +++---- src/messages/cquery_call_hierarchy.cc | 2 +- src/messages/cquery_member_hierarchy.cc | 28 +--------------------- src/query.cc | 2 +- src/serializer.cc | 2 +- 10 files changed, 61 insertions(+), 51 deletions(-) diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index 43a9d439..8ce4c262 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -29,13 +29,6 @@ bool ClangType::operator==(const ClangType& rhs) const { return clang_equalTypes(cx_type, rhs.cx_type); } -bool ClangType::is_fundamental() const { - // NOTE: This will return false for pointed types. Should we call - // strip_qualifiers for the user? - return cx_type.kind >= CXType_FirstBuiltin && - cx_type.kind <= CXType_LastBuiltin; -} - ClangCursor ClangType::get_declaration() const { return clang_getTypeDeclaration(cx_type); } @@ -45,7 +38,7 @@ std::string ClangType::get_usr() const { } Usr ClangType::get_usr_hash() const { - if (is_fundamental()) + if (is_builtin()) return static_cast(cx_type.kind); return ClangCursor(clang_getTypeDeclaration(cx_type)).get_usr_hash(); } diff --git a/src/clang_cursor.h b/src/clang_cursor.h index ea753a1a..f7129b9d 100644 --- a/src/clang_cursor.h +++ b/src/clang_cursor.h @@ -25,7 +25,12 @@ class ClangType { bool operator==(const ClangType& rhs) const; // Returns true if this is a fundamental type like int. - bool is_fundamental() const; + bool is_builtin() const { + // NOTE: This will return false for pointed types. Should we call + // strip_qualifiers for the user? + return cx_type.kind >= CXType_FirstBuiltin && + cx_type.kind <= CXType_LastBuiltin; + } ClangCursor get_declaration() const; std::string get_usr() const; diff --git a/src/clang_utils.cc b/src/clang_utils.cc index cddd2ffc..8be4a15c 100644 --- a/src/clang_utils.cc +++ b/src/clang_utils.cc @@ -150,6 +150,37 @@ std::string ToString(CXCursorKind kind) { return ToString(clang_getCursorKindSpelling(kind)); } +const char* ClangBuiltinTypeName(CXTypeKind kind) { + switch (kind) { + // clang-format off + case CXType_Bool: return "bool"; + case CXType_Char_U: return "char"; + case CXType_UChar: return "unsigned char"; + case CXType_UShort: return "unsigned short"; + case CXType_UInt: return "unsigned int"; + case CXType_ULong: return "unsigned long"; + case CXType_ULongLong: return "unsigned long long"; + case CXType_UInt128: return "unsigned __int128"; + case CXType_Char_S: return "char"; + case CXType_SChar: return "signed char"; + case CXType_WChar: return "wchar_t"; + case CXType_Int: return "int"; + case CXType_Long: return "long"; + case CXType_LongLong: return "long long"; + case CXType_Int128: return "__int128"; + case CXType_Float: return "float"; + case CXType_Double: return "double"; + case CXType_LongDouble: return "long double"; + case CXType_Float128: return "__float128"; +#if CINDEX_VERSION_MINOR >= 43 + case CXType_Half: return "_Float16"; +#endif + case CXType_NullPtr: return "nullptr"; + default: return ""; + // clang-format on + } +} + #if USE_CLANG_CXX TEST_SUITE("ClangUtils") { TEST_CASE("replacements") { diff --git a/src/clang_utils.h b/src/clang_utils.h index 7f240e8b..24175efd 100644 --- a/src/clang_utils.h +++ b/src/clang_utils.h @@ -20,6 +20,8 @@ std::string ToString(CXString cx_string); std::string ToString(CXCursorKind cursor_kind); +const char* ClangBuiltinTypeName(CXTypeKind); + // Converts Clang formatting replacement operations into LSP text edits. #if USE_CLANG_CXX std::vector ConvertClangReplacementsIntoTextEdits( diff --git a/src/indexer.cc b/src/indexer.cc index e4aa5120..9f3a398e 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -539,7 +539,7 @@ optional ResolveToDeclarationType(IndexFile* db, type = type.strip_qualifiers(); - if (type.is_fundamental()) { + if (type.is_builtin()) { // For builtin types, use type kinds as USR hash. return db->ToTypeId(type.cx_type.kind); } @@ -1602,14 +1602,19 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { if (decl->isDefinition && decl->semanticContainer) { switch (GetSymbolKind(decl->semanticContainer->cursor.kind)) { - default: - break; - case SymbolKind::Type: { - IndexTypeId parent_type_id = - db->ToTypeId(decl->semanticContainer->cursor); - db->Resolve(parent_type_id)->def.vars.push_back(var_id); + case SymbolKind::Func: { + db->Resolve(db->ToFuncId(decl->semanticContainer->cursor)) + ->def.vars.push_back(var_id); break; } + case SymbolKind::Type: + if (decl->semanticContainer->cursor.kind != CXCursor_EnumDecl) { + db->Resolve(db->ToTypeId(decl->semanticContainer->cursor)) + ->def.vars.push_back(var_id); + } + break; + default: + break; } } @@ -1842,7 +1847,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { if (decl_cursor.get_kind() == CXCursor_EnumDecl) { ClangType enum_type = clang_getEnumDeclIntegerType(decl->cursor); - if (!enum_type.is_fundamental()) { + if (!enum_type.is_builtin()) { IndexType* int_type = db->Resolve(db->ToTypeId(enum_type.get_usr_hash())); AddUse(db, int_type->uses, spell, diff --git a/src/indexer.h b/src/indexer.h index 17116089..75a1c40e 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -256,8 +256,8 @@ struct FuncDefDefinitionData { // Method this method overrides. std::vector bases; - // Local variables defined in this function. - std::vector locals; + // Local variables or parameters. + std::vector vars; // Functions that this function calls. std::vector callees; @@ -273,7 +273,7 @@ struct FuncDefDefinitionData { bool operator==(const FuncDefDefinitionData& o) const { return detailed_name == o.detailed_name && spell == o.spell && extent == o.extent && declaring_type == o.declaring_type && - bases == o.bases && locals == o.locals && callees == o.callees && + bases == o.bases && vars == o.vars && callees == o.callees && kind == o.kind && storage == o.storage && hover == o.hover && comments == o.comments; } @@ -308,7 +308,7 @@ void Reflect(TVisitor& visitor, FuncDefDefinitionData& value) { REFLECT_MEMBER(file); REFLECT_MEMBER(declaring_type); REFLECT_MEMBER(bases); - REFLECT_MEMBER(locals); + REFLECT_MEMBER(vars); REFLECT_MEMBER(callees); REFLECT_MEMBER_END(); } diff --git a/src/messages/cquery_call_hierarchy.cc b/src/messages/cquery_call_hierarchy.cc index 7b5b66d5..381cba67 100644 --- a/src/messages/cquery_call_hierarchy.cc +++ b/src/messages/cquery_call_hierarchy.cc @@ -118,6 +118,7 @@ bool Expand(MessageHandler* m, }; std::unordered_set seen; + seen.insert(func.usr); std::vector stack; if (detailed_name) entry->name = def->detailed_name; @@ -132,7 +133,6 @@ bool Expand(MessageHandler* m, // Callers/callees of base functions. if (call_type & CallType::Base) { - seen.insert(func.usr); stack.push_back(&func); while (stack.size()) { const QueryFunc& func1 = *stack.back(); diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index fa215bbc..c6e1d8d0 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -70,33 +70,7 @@ bool Expand(MessageHandler* m, const QueryType::Def* def = type.AnyDef(); // builtin types have no declaration and empty |detailed_name|. if (CXType_FirstBuiltin <= type.usr && type.usr <= CXType_LastBuiltin) { - switch (type.usr) { - // clang-format off - case CXType_Bool: entry->name = "bool"; break; - case CXType_Char_U: entry->name = "char"; break; - case CXType_UChar: entry->name = "unsigned char"; break; - case CXType_UShort: entry->name = "unsigned short"; break; - case CXType_UInt: entry->name = "unsigned int"; break; - case CXType_ULong: entry->name = "unsigned long"; break; - case CXType_ULongLong: entry->name = "unsigned long long"; break; - case CXType_UInt128: entry->name = "unsigned __int128"; break; - case CXType_Char_S: entry->name = "char"; break; - case CXType_SChar: entry->name = "signed char"; break; - case CXType_WChar: entry->name = "wchar_t"; break; - case CXType_Int: entry->name = "int"; break; - case CXType_Long: entry->name = "long"; break; - case CXType_LongLong: entry->name = "long long"; break; - case CXType_Int128: entry->name = "__int128"; break; - case CXType_Float: entry->name = "float"; break; - case CXType_Double: entry->name = "double"; break; - case CXType_LongDouble: entry->name = "long double"; break; - case CXType_Float128: entry->name = "__float128"; break; -#if CINDEX_VERSION_MINOR >= 43 - case CXType_Half: entry->name = "_Float16"; break; -#endif - case CXType_NullPtr: entry->name = "nullptr"; break; - // clang-format on - } + entry->name = ClangBuiltinTypeName(CXTypeKind(type.usr)); entry->numChildren = 0; return true; } diff --git a/src/query.cc b/src/query.cc index f5f4e446..bb0d897b 100644 --- a/src/query.cc +++ b/src/query.cc @@ -84,7 +84,7 @@ optional ToQuery(const IdMap& id_map, result.extent = id_map.ToQuery(func.extent); result.declaring_type = id_map.ToQuery(func.declaring_type); result.bases = id_map.ToQuery(func.bases); - result.locals = id_map.ToQuery(func.locals); + result.vars = id_map.ToQuery(func.vars); result.callees = id_map.ToQuery(func.callees); return result; } diff --git a/src/serializer.cc b/src/serializer.cc index f4df4ac3..b03ce1eb 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -245,7 +245,7 @@ void Reflect(TVisitor& visitor, IndexFunc& value) { REFLECT_MEMBER2("declaring_type", value.def.declaring_type); REFLECT_MEMBER2("bases", value.def.bases); REFLECT_MEMBER2("derived", value.derived); - REFLECT_MEMBER2("locals", value.def.locals); + REFLECT_MEMBER2("vars", value.def.vars); REFLECT_MEMBER2("uses", value.uses); REFLECT_MEMBER2("callees", value.def.callees); REFLECT_MEMBER_END();