Skip to content

Commit 2121bda

Browse files
authored
[clang][AST] Don't print inherited default template args (llvm#161953)
Prior to this change, for the code like this: ```cpp template <int, int = 0> class Tpl; template <int = 0, int> class Tpl; ``` pretty-printing produced an uncompilable code: ```cpp template <int, int = 0> class Tpl; template <int = 0, int = 0> class Tpl; ```
1 parent e543ca6 commit 2121bda

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ AST Dumping Potentially Breaking Changes
123123

124124
``__atomic_test_and_set(p, 0)``
125125

126+
- Pretty-printing of templates with inherited (i.e. specified in a previous
127+
redeclaration) default arguments has been fixed.
128+
126129
Clang Frontend Potentially Breaking Changes
127130
-------------------------------------------
128131
- Members of anonymous unions/structs are now injected as ``IndirectFieldDecl``

clang/lib/AST/DeclPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
18941894
Out << TTP->getDeclName();
18951895
}
18961896

1897-
if (TTP->hasDefaultArgument()) {
1897+
if (TTP->hasDefaultArgument() && !TTP->defaultArgumentWasInherited()) {
18981898
Out << " = ";
18991899
TTP->getDefaultArgument().getArgument().print(Policy, Out,
19001900
/*IncludeType=*/false);
@@ -1909,7 +1909,7 @@ void DeclPrinter::VisitNonTypeTemplateParmDecl(
19091909
Policy.CleanUglifiedParameters ? II->deuglifiedName() : II->getName();
19101910
printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
19111911

1912-
if (NTTP->hasDefaultArgument()) {
1912+
if (NTTP->hasDefaultArgument() && !NTTP->defaultArgumentWasInherited()) {
19131913
Out << " = ";
19141914
NTTP->getDefaultArgument().getArgument().print(Policy, Out,
19151915
/*IncludeType=*/false);

clang/test/AST/ast-print-record-decl.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ KW DeclGroupInMemberList {
290290
// A tag decl group in the tag decl's own member list is exercised in
291291
// defSelfRef above.
292292

293+
#ifdef __cplusplus
293294

294295
// Check out-of-line record definition
295-
#ifdef __cplusplus
296296
// PRINT-CXX-NEXT: [[KW]] OutOfLineRecord {
297297
KW OutOfLineRecord {
298298
// PRINT-CXX-NEXT: [[KW]] Inner
@@ -304,4 +304,15 @@ KW OutOfLineRecord {
304304
KW OutOfLineRecord::Inner {
305305
// PRINT-CXX-NEXT: };
306306
};
307+
308+
// PRINT-CXX-NEXT: template <typename, typename = int> [[KW]] SmearedTypeDefArgs;
309+
template <typename, typename = int> KW SmearedTypeDefArgs;
310+
// PRINT-CXX-NEXT: template <typename = int, typename> [[KW]] SmearedTypeDefArgs;
311+
template <typename = int, typename> KW SmearedTypeDefArgs;
312+
313+
// PRINT-CXX-NEXT: template <int, int = 0> [[KW]] SmearedNTTPDefArgs;
314+
template <int, int = 0> KW SmearedNTTPDefArgs;
315+
// PRINT-CXX-NEXT: template <int = 0, int> [[KW]] SmearedNTTPDefArgs;
316+
template <int = 0, int> KW SmearedNTTPDefArgs;
317+
307318
#endif

0 commit comments

Comments
 (0)