-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Conversation
✅ With the latest revision this PR passed the C/C++ code formatter. |
✅ With the latest revision this PR passed the Python code formatter. |
@llvm/pr-subscribers-clang Author: Zahira Ammarguellat (zahiraam) ChangesFixed the printing of templated argument list and added test case. Full diff: https://github.com/llvm/llvm-project/pull/86339.diff 2 Files Affected:
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, LGTM!
@AaronBallman Thanks! |
I'm getting errors like:
This file being a clang test, I am wondering if this is actually part of the test. Shall we put |
@kazutakahirata When do you get this error? |
Creating a PR with |
I get this error while running By the way:
would result in:
would result in:
I don't know where to put |
Looking at other examples, should the lambda expression be the last argument to |
This also fails on PPC, as well: https://lab.llvm.org/buildbot/#/builders/36/builds/43998/steps/12/logs/stdio |
oh! I think I know what the issue is. I have a fix for it. PR following up. See the )). |
@zahiraam I still get
This has been broken for quite a while now, can you please fix or revert? |
|
Fixed the printing of templated argument list and added test case.