[clang] 3a2c70b - Fix printing of templated records. (#86339)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 26 06:06:51 PDT 2024


Author: Zahira Ammarguellat
Date: 2024-03-26T09:06:46-04:00
New Revision: 3a2c70b3713a856ea416d92abdddb7893fca308b

URL: https://github.com/llvm/llvm-project/commit/3a2c70b3713a856ea416d92abdddb7893fca308b
DIFF: https://github.com/llvm/llvm-project/commit/3a2c70b3713a856ea416d92abdddb7893fca308b.diff

LOG: Fix printing of templated records. (#86339)

Fixed the printing of templated argument list and added test case.

Added: 
    

Modified: 
    clang/lib/AST/TypePrinter.cpp
    clang/unittests/AST/DeclPrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 7032ff2f18468c..d9504f9dcb3899 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -2303,11 +2303,6 @@ printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy,
     } else {
       if (!FirstArg)
         OS << Comma;
-      if (!Policy.SuppressTagKeyword &&
-          Argument.getKind() == TemplateArgument::Type &&
-          isa<TagType>(Argument.getAsType()))
-        OS << Argument.getAsType().getAsString();
-      else
         // Tries to print the argument with location info if exists.
         printArgument(Arg, Policy, ArgOS,
                       TemplateParameterList::shouldIncludeTypeForArgument(

diff  --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index e024c41e03b484..07fa02bd96e25d 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -1386,6 +1386,75 @@ TEST(DeclPrinter, TestTemplateArgumentList16) {
   ASSERT_TRUE(PrintedDeclCXX11Matches(Code, "NT2", "int NT2 = 5"));
 }
 
+TEST(DeclPrinter, TestCXXRecordDecl17) {
+  ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
+                                      "struct X {};"
+                                      "Z<X> A;",
+                                      "A", "Z<X> A"));
+  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
+}
+
+TEST(DeclPrinter, TestCXXRecordDecl18) {
+  ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
+                                      "struct X {};"
+                                      "Z<X> A;"
+                                      "template <typename T1, int>"
+                                      "struct Y{};"
+                                      "Y<Z<X>, 2> B;",
+                                      "B", "Y<Z<X>, 2> B"));
+  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
+}
+
+TEST(DeclPrinter, TestCXXRecordDecl19) {
+  ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
+                                      "struct X {};"
+                                      "Z<X> A;"
+                                      "template <typename T1, int>"
+                                      "struct Y{};"
+                                      "Y<Z<X>, 2> B;",
+                                      "B", "Y<Z<X>, 2> B"));
+  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
+}
+TEST(DeclPrinter, TestCXXRecordDecl20) {
+  ASSERT_TRUE(PrintedDeclCXX98Matches(
+      "template <typename T, int N> class Inner;"
+      "template <typename T, int N>"
+      "class Inner{Inner(T val){}};"
+      "template <class InnerClass, int N> class Outer {"
+      "public:"
+      "struct NestedStruct {"
+      "int nestedValue;"
+      "NestedStruct(int val) : nestedValue(val) {}"
+      "};"
+      "InnerClass innerInstance;"
+      "Outer(const InnerClass &inner) : innerInstance(inner) {}"
+      "};"
+      "Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100);",
+      "nestedInstance",
+      "Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100)"));
+  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
+}
+
+TEST(DeclPrinter, TestCXXRecordDecl21) {
+  ASSERT_TRUE(PrintedDeclCXX98Matches(
+      "template <typename T, int N> class Inner;"
+      "template <typename T, int N>"
+      "class Inner{Inner(T val){}};"
+      "template <class InnerClass, int N> class Outer {"
+      "public:"
+      "struct NestedStruct {"
+      "int nestedValue;"
+      "NestedStruct(int val) : nestedValue(val) {}"
+      "};"
+      "InnerClass innerInstance;"
+      "Outer(const InnerClass &inner) : innerInstance(inner) {}"
+      "};"
+      "Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100);",
+      "nestedInstance",
+      "Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100)"));
+  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
+}
+
 TEST(DeclPrinter, TestFunctionParamUglified) {
   llvm::StringLiteral Code = R"cpp(
     class __c;


        


More information about the cfe-commits mailing list