Skip to content

Commit 4493d9f

Browse files
zahiraamsys-ce-bb
authored andcommitted
Fix printing of templated records. (#86339)
Fixed the printing of templated argument list and added test case.
1 parent 5440c10 commit 4493d9f

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

clang/lib/AST/TypePrinter.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,11 +2322,6 @@ printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy,
23222322
} else {
23232323
if (!FirstArg)
23242324
OS << Comma;
2325-
if (!Policy.SuppressTagKeyword &&
2326-
Argument.getKind() == TemplateArgument::Type &&
2327-
isa<TagType>(Argument.getAsType()))
2328-
OS << Argument.getAsType().getAsString();
2329-
else
23302325
// Tries to print the argument with location info if exists.
23312326
printArgument(Arg, Policy, ArgOS,
23322327
TemplateParameterList::shouldIncludeTypeForArgument(

clang/unittests/AST/DeclPrinterTest.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,75 @@ TEST(DeclPrinter, TestTemplateArgumentList16) {
13861386
ASSERT_TRUE(PrintedDeclCXX11Matches(Code, "NT2", "int NT2 = 5"));
13871387
}
13881388

1389+
TEST(DeclPrinter, TestCXXRecordDecl17) {
1390+
ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
1391+
"struct X {};"
1392+
"Z<X> A;",
1393+
"A", "Z<X> A"));
1394+
[](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
1395+
}
1396+
1397+
TEST(DeclPrinter, TestCXXRecordDecl18) {
1398+
ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
1399+
"struct X {};"
1400+
"Z<X> A;"
1401+
"template <typename T1, int>"
1402+
"struct Y{};"
1403+
"Y<Z<X>, 2> B;",
1404+
"B", "Y<Z<X>, 2> B"));
1405+
[](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
1406+
}
1407+
1408+
TEST(DeclPrinter, TestCXXRecordDecl19) {
1409+
ASSERT_TRUE(PrintedDeclCXX98Matches("template<typename T> struct Z {};"
1410+
"struct X {};"
1411+
"Z<X> A;"
1412+
"template <typename T1, int>"
1413+
"struct Y{};"
1414+
"Y<Z<X>, 2> B;",
1415+
"B", "Y<Z<X>, 2> B"));
1416+
[](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
1417+
}
1418+
TEST(DeclPrinter, TestCXXRecordDecl20) {
1419+
ASSERT_TRUE(PrintedDeclCXX98Matches(
1420+
"template <typename T, int N> class Inner;"
1421+
"template <typename T, int N>"
1422+
"class Inner{Inner(T val){}};"
1423+
"template <class InnerClass, int N> class Outer {"
1424+
"public:"
1425+
"struct NestedStruct {"
1426+
"int nestedValue;"
1427+
"NestedStruct(int val) : nestedValue(val) {}"
1428+
"};"
1429+
"InnerClass innerInstance;"
1430+
"Outer(const InnerClass &inner) : innerInstance(inner) {}"
1431+
"};"
1432+
"Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100);",
1433+
"nestedInstance",
1434+
"Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100)"));
1435+
[](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = false; };
1436+
}
1437+
1438+
TEST(DeclPrinter, TestCXXRecordDecl21) {
1439+
ASSERT_TRUE(PrintedDeclCXX98Matches(
1440+
"template <typename T, int N> class Inner;"
1441+
"template <typename T, int N>"
1442+
"class Inner{Inner(T val){}};"
1443+
"template <class InnerClass, int N> class Outer {"
1444+
"public:"
1445+
"struct NestedStruct {"
1446+
"int nestedValue;"
1447+
"NestedStruct(int val) : nestedValue(val) {}"
1448+
"};"
1449+
"InnerClass innerInstance;"
1450+
"Outer(const InnerClass &inner) : innerInstance(inner) {}"
1451+
"};"
1452+
"Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100);",
1453+
"nestedInstance",
1454+
"Outer<Inner<int, 10>, 5>::NestedStruct nestedInstance(100)"));
1455+
[](PrintingPolicy &Policy) { Policy.SuppressTagKeyword = true; };
1456+
}
1457+
13891458
TEST(DeclPrinter, TestFunctionParamUglified) {
13901459
llvm::StringLiteral Code = R"cpp(
13911460
class __c;

0 commit comments

Comments
 (0)