Skip to content

Commit 4f075f5

Browse files
[Clang][TypePrinter] Make printNestedNameSpecifier look at typedefs
This is to resolve a regression caused by llvm#168534. Now when we have an anonymous object like a struct or union that has a typedef attached, we print the typedef name instead of listing it as anonymous.
1 parent f4ba8e3 commit 4f075f5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/lib/AST/Decl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,9 @@ void NamedDecl::printNestedNameSpecifier(raw_ostream &OS,
17901790
else
17911791
OS << *ND;
17921792
} else if (const auto *RD = dyn_cast<RecordDecl>(DC)) {
1793-
if (!RD->getIdentifier())
1793+
if (TypedefNameDecl *Typedef = RD->getTypedefNameForAnonDecl())
1794+
OS << Typedef->getIdentifier()->getName();
1795+
else if (!RD->getIdentifier())
17941796
OS << "(anonymous " << RD->getKindName() << ')';
17951797
else
17961798
OS << *RD;

clang/unittests/AST/TypePrinterTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,23 @@ TEST(TypePrinter, NestedNameSpecifiers) {
341341
Policy.AnonymousTagLocations = false;
342342
}));
343343
}
344+
345+
TEST(TypePrinter, NestedNameSpecifiersTypedef) {
346+
constexpr char Code[] = R"cpp(
347+
typedef union {
348+
struct {
349+
struct {
350+
unsigned int baz;
351+
} bar;
352+
};
353+
} foo;
354+
)cpp";
355+
356+
ASSERT_TRUE(PrintedTypeMatches(
357+
Code, {}, fieldDecl(hasName("bar"), hasType(qualType().bind("id"))),
358+
"struct foo::(anonymous struct)::(unnamed)",
359+
[](PrintingPolicy &Policy) {
360+
Policy.FullyQualifiedName = true;
361+
Policy.AnonymousTagLocations = false;
362+
}));
363+
}

0 commit comments

Comments
 (0)