Author: Aiden Grossman
Date: 2025-11-24T18:01:17Z
New Revision: 3843a50c69063a9440ccd65ff9a167be75baf442

URL: 
https://github.com/llvm/llvm-project/commit/3843a50c69063a9440ccd65ff9a167be75baf442
DIFF: 
https://github.com/llvm/llvm-project/commit/3843a50c69063a9440ccd65ff9a167be75baf442.diff

LOG: [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.

Added: 
    

Modified: 
    clang/lib/AST/Decl.cpp
    clang/unittests/AST/TypePrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 555aa5c050ffd..591457b1d66b4 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -1790,7 +1790,9 @@ void NamedDecl::printNestedNameSpecifier(raw_ostream &OS,
       else
         OS << *ND;
     } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) {
-      if (!RD->getIdentifier())
+      if (TypedefNameDecl *TD = RD->getTypedefNameForAnonDecl())
+        OS << *TD;
+      else if (!RD->getIdentifier())
         OS << "(anonymous " << RD->getKindName() << ')';
       else
         OS << *RD;

diff  --git a/clang/unittests/AST/TypePrinterTest.cpp 
b/clang/unittests/AST/TypePrinterTest.cpp
index 410ec021d6e72..3cadf9b265bd1 100644
--- a/clang/unittests/AST/TypePrinterTest.cpp
+++ b/clang/unittests/AST/TypePrinterTest.cpp
@@ -341,3 +341,22 @@ TEST(TypePrinter, NestedNameSpecifiers) {
         Policy.AnonymousTagLocations = false;
       }));
 }
+
+TEST(TypePrinter, NestedNameSpecifiersTypedef) {
+  constexpr char Code[] = R"cpp(
+    typedef union {
+      struct {
+        struct {
+          unsigned int baz;
+        } bar;
+      };
+    } foo;
+  )cpp";
+
+  ASSERT_TRUE(PrintedTypeMatches(
+      Code, {}, fieldDecl(hasName("bar"), hasType(qualType().bind("id"))),
+      "struct foo::(anonymous struct)::(unnamed)", [](PrintingPolicy &Policy) {
+        Policy.FullyQualifiedName = true;
+        Policy.AnonymousTagLocations = false;
+      }));
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to