Clean up SetTypeName

This commit is contained in:
Fangrui Song 2018-01-25 22:20:28 -08:00
parent 63b793619d
commit 50c6ed1dd3
2 changed files with 24 additions and 25 deletions

View File

@ -391,13 +391,18 @@ bool IsFunctionCallContext(CXCursorKind kind) {
} }
void SetTypeName(IndexType* type, void SetTypeName(IndexType* type,
const ClangCursor& cursor,
const CXIdxContainerInfo* container, const CXIdxContainerInfo* container,
const char* name, const char* name,
NamespaceHelper* ns) { NamespaceHelper* ns) {
CXIdxContainerInfo parent;
// |name| can be null in an anonymous struct (see // |name| can be null in an anonymous struct (see
// tests/types/anonymous_struct.cc). // tests/types/anonymous_struct.cc).
type->def.short_name = name ? name : "(anon)"; type->def.short_name = name ? name : "(anon)";
type->def.detailed_name = ns->QualifiedName(container, type->def.short_name); if (!container)
parent.cursor = cursor.get_semantic_parent().cx_cursor;
type->def.detailed_name =
ns->QualifiedName(container ? container : &parent, type->def.short_name);
} }
// Finds the cursor associated with the declaration type of |cursor|. This // Finds the cursor associated with the declaration type of |cursor|. This
@ -405,7 +410,8 @@ void SetTypeName(IndexType* type,
// qualifies from |cursor| (ie, Foo* => Foo) and removes template arguments // qualifies from |cursor| (ie, Foo* => Foo) and removes template arguments
// (ie, Foo<A,B> => Foo<*,*>). // (ie, Foo<A,B> => Foo<*,*>).
optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db, optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db,
ClangCursor cursor) { ClangCursor cursor,
NamespaceHelper* ns) {
ClangType type = cursor.get_type(); ClangType type = cursor.get_type();
// auto x = new Foo() will not be deduced to |Foo| if we do not use the // auto x = new Foo() will not be deduced to |Foo| if we do not use the
@ -434,9 +440,8 @@ optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db,
IndexTypeId type_id = db->ToTypeId(usr); IndexTypeId type_id = db->ToTypeId(usr);
IndexType* typ = db->Resolve(type_id); IndexType* typ = db->Resolve(type_id);
if (typ->def.short_name.empty()) { if (typ->def.short_name.empty()) {
typ->def.short_name = declaration.get_spelling(); std::string name = declaration.get_spelling();
// TODO detailed_name SetTypeName(typ, declaration, nullptr, name.c_str(), ns);
typ->def.detailed_name = declaration.get_spelling();
} }
return type_id; return type_id;
} }
@ -457,9 +462,7 @@ void SetVarDetail(IndexVar* var,
def.comments = cursor.get_comments(); def.comments = cursor.get_comments();
std::string qualified_name = std::string qualified_name =
semanticContainer param->ns.QualifiedName(semanticContainer, def.short_name);
? param->ns.QualifiedName(semanticContainer, def.short_name)
: def.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(
@ -500,7 +503,8 @@ void SetVarDetail(IndexVar* var,
} }
if (is_first_seen) { if (is_first_seen) {
optional<IndexTypeId> var_type = ResolveToDeclarationType(db, cursor); optional<IndexTypeId> var_type =
ResolveToDeclarationType(db, cursor, &param->ns);
if (var_type) { if (var_type) {
// Don't treat enum definition variables as instantiations. // Don't treat enum definition variables as instantiations.
bool is_enum_member = semanticContainer && bool is_enum_member = semanticContainer &&
@ -1258,7 +1262,7 @@ std::string NamespaceHelper::QualifiedName(const CXIdxContainerInfo* container,
// 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
// put their names into a map of USR -> qualified_name. // put their names into a map of USR -> qualified_name.
ClangCursor cursor = container->cursor; ClangCursor cursor(container->cursor);
std::vector<ClangCursor> namespaces; std::vector<ClangCursor> namespaces;
std::string qualifier; std::string qualifier;
while (cursor.get_kind() != CXCursor_TranslationUnit && while (cursor.get_kind() != CXCursor_TranslationUnit &&
@ -1352,12 +1356,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
IndexType* ns = db->Resolve(ns_id); IndexType* ns = db->Resolve(ns_id);
ns->def.kind = GetSymbolKind(decl->entityInfo->kind); ns->def.kind = GetSymbolKind(decl->entityInfo->kind);
if (ns->def.short_name.empty()) { if (ns->def.short_name.empty()) {
if (decl->entityInfo->name) SetTypeName(ns, decl_cursor, decl->semanticContainer,
ns->def.short_name = decl->entityInfo->name; decl->entityInfo->name, &param->ns);
else
ns->def.short_name = "(anon)";
ns->def.detailed_name = param->ns.QualifiedName(decl->semanticContainer,
ns->def.short_name);
ns->def.definition_spelling = decl_spell; ns->def.definition_spelling = decl_spell;
ns->def.definition_extent = decl_cursor.get_extent(); ns->def.definition_extent = decl_cursor.get_extent();
if (decl->semanticContainer) { if (decl->semanticContainer) {
@ -1607,7 +1607,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
type->def.definition_spelling = spell; type->def.definition_spelling = spell;
type->def.definition_extent = extent; type->def.definition_extent = extent;
SetTypeName(type, decl->semanticContainer, decl->entityInfo->name, &param->ns); SetTypeName(type, decl_cursor, decl->semanticContainer,
decl->entityInfo->name, &param->ns);
type->def.kind = GetSymbolKind(decl->entityInfo->kind); type->def.kind = GetSymbolKind(decl->entityInfo->kind);
type->def.comments = decl_cursor.get_comments(); type->def.comments = decl_cursor.get_comments();
@ -1652,7 +1653,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// TODO: For type section, verify if this ever runs for non definitions? // TODO: For type section, verify if this ever runs for non definitions?
// if (!decl->isRedeclaration) { // if (!decl->isRedeclaration) {
SetTypeName(type, decl->semanticContainer, decl->entityInfo->name, &param->ns); SetTypeName(type, decl_cursor, decl->semanticContainer,
decl->entityInfo->name, &param->ns);
type->def.kind = GetSymbolKind(decl->entityInfo->kind); type->def.kind = GetSymbolKind(decl->entityInfo->kind);
type->def.comments = decl_cursor.get_comments(); type->def.comments = decl_cursor.get_comments();
// } // }
@ -1698,7 +1700,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
AddDeclTypeUsages(db, base_class->cursor, nullopt, AddDeclTypeUsages(db, base_class->cursor, nullopt,
decl->semanticContainer, decl->lexicalContainer); decl->semanticContainer, decl->lexicalContainer);
optional<IndexTypeId> parent_type_id = optional<IndexTypeId> parent_type_id =
ResolveToDeclarationType(db, base_class->cursor); ResolveToDeclarationType(db, base_class->cursor, &param->ns);
// type_def ptr could be invalidated by ResolveToDeclarationType and // type_def ptr could be invalidated by ResolveToDeclarationType and
// TemplateVisitor. // TemplateVisitor.
type = db->Resolve(type_id); type = db->Resolve(type_id);
@ -1933,7 +1935,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
// template <class T> class A; // template <class T> class A;
if (ref_type->def.short_name.empty()) { if (ref_type->def.short_name.empty()) {
// TODO Replace |nullptr| with semantic container. // TODO Replace |nullptr| with semantic container.
SetTypeName(ref_type, nullptr, ref->referencedEntity->name, &param->ns); SetTypeName(ref_type, ref_cursor, nullptr, ref->referencedEntity->name,
&param->ns);
// if (!ref_type->def.definition_spelling) { // if (!ref_type->def.definition_spelling) {
// ref_type->def.definition_spelling = ref_cursor.get_spelling_range(); // ref_type->def.definition_spelling = ref_cursor.get_spelling_range();
// ref_type->def.definition_extent = ref_cursor.get_extent(); // ref_type->def.definition_extent = ref_cursor.get_extent();

View File

@ -527,10 +527,6 @@ struct NamespaceHelper {
std::unordered_map<ClangCursor, std::string> std::unordered_map<ClangCursor, std::string>
container_cursor_to_qualified_name; container_cursor_to_qualified_name;
void RegisterQualifiedName(std::string usr,
const CXIdxContainerInfo* container,
std::string qualified_name) {}
std::string QualifiedName(const CXIdxContainerInfo* container, std::string QualifiedName(const CXIdxContainerInfo* container,
std::string unqualified_name); std::string unqualified_name);
}; };