Skip to content

Commit 3843a50

Browse files
[Clang][TypePrinter] Make printNestedNameSpecifier look at typedefs (#169364)
This is to resolve a regression caused by #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 c1f24a5 commit 3843a50

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-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 *TD = RD->getTypedefNameForAnonDecl())
1794+
OS << *TD;
1795+
else if (!RD->getIdentifier())
17941796
OS << "(anonymous " << RD->getKindName() << ')';
17951797
else
17961798
OS << *RD;

clang/unittests/AST/TypePrinterTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,22 @@ 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)", [](PrintingPolicy &Policy) {
359+
Policy.FullyQualifiedName = true;
360+
Policy.AnonymousTagLocations = false;
361+
}));
362+
}

0 commit comments

Comments
 (0)