Skip to content

Fix printing of templated records. #86339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2024

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Mar 22, 2024

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

Copy link

github-actions bot commented Mar 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link

✅ With the latest revision this PR passed the Python code formatter.

@zahiraam zahiraam marked this pull request as ready for review March 25, 2024 13:57
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 25, 2024
@zahiraam zahiraam requested a review from AaronBallman March 25, 2024 13:58
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2024

@llvm/pr-subscribers-clang

Author: Zahira Ammarguellat (zahiraam)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/86339.diff

2 Files Affected:

  • (modified) clang/lib/AST/TypePrinter.cpp (-5)
  • (modified) clang/unittests/AST/DeclPrinterTest.cpp (+69)
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;

Argument.getKind() == TemplateArgument::Type &&
isa<TagType>(Argument.getAsType()))
OS << Argument.getAsType().getAsString();
else
Copy link
Contributor Author

@zahiraam zahiraam Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was wrongly added in #84014 and was not reached by any tests. Some of our internal testing did hit this and realized the code was wrong. Created reproducers from internal tests to show that the code is wrong.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, LGTM!

@zahiraam
Copy link
Contributor Author

@AaronBallman Thanks!

@zahiraam zahiraam merged commit 3a2c70b into llvm:main Mar 26, 2024
@kazutakahirata
Copy link
Contributor

I'm getting errors like:

llvm-project/clang/unittests/AST/DeclPrinterTest.cpp:1394:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };

This file being a clang test, I am wondering if this is actually part of the test. Shall we put [[maybe_unused]] here?

@zahiraam
Copy link
Contributor Author

I'm getting errors like:

llvm-project/clang/unittests/AST/DeclPrinterTest.cpp:1394:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };

This file being a clang test, I am wondering if this is actually part of the test. Shall we put [[maybe_unused]] here?

@kazutakahirata When do you get this error?

@zahiraam
Copy link
Contributor Author

Creating a PR with
[](PrintingPolicy [[maybe_unused]] & Policy) { Policy.SuppressTagKeyword = true; };
on those tests.

@kazutakahirata
Copy link
Contributor

Creating a PR with [](PrintingPolicy [[maybe_unused]] & Policy) { Policy.SuppressTagKeyword = true; }; on those tests.

I get this error while running ninja check-clang-unit. My build tree is configured with -DCMAKE_CXX_COMPILER=/usr/bin/clang++ and -DLLVM_ENABLE_WERROR=On.

By the way:

[](PrintingPolicy [[maybe_unused]] &Policy) { Policy.SuppressTagKeyword = false; };

would result in:

clang/unittests/AST/DeclPrinterTest.cpp:1394:23: error: 'maybe_unused' attribute cannot be applied to types
[[maybe_unused]] [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };

would result in:

clang/unittests/AST/DeclPrinterTest.cpp:1394:5: error: 'maybe_unused' attribute cannot be applied to a statement

I don't know where to put [[maybe_unused]].

@kazutakahirata
Copy link
Contributor

kazutakahirata commented Mar 26, 2024

Looking at other examples, should the lambda expression be the last argument to PrintedDeclCXX98Matches?

@amy-kwan
Copy link
Contributor

@zahiraam
Copy link
Contributor Author

zahiraam commented Mar 26, 2024

oh! I think I know what the issue is. I have a fix for it. PR following up.
It should be:
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; })); }
instead of:
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; };

See the )).

@mikaelholmen
Copy link
Collaborator

@zahiraam I still get

../../clang/unittests/AST/DeclPrinterTest.cpp:1394:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1405:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1416:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1435:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1455:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 errors generated.

This has been broken for quite a while now, can you please fix or revert?

@mikaelholmen
Copy link
Collaborator

@zahiraam I still get

../../clang/unittests/AST/DeclPrinterTest.cpp:1394:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1405:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1416:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1435:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/unittests/AST/DeclPrinterTest.cpp:1455:3: error: expression result unused [-Werror,-Wunused-value]
  [](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 errors generated.

This has been broken for quite a while now, can you please fix or revert?

Thank you @alinas for fixing the problem in 577e0ef !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants