Skip to content

Conversation

bolshakov-a
Copy link
Contributor

Prior to this change, for the code like this:

template <int, int = 0>
class Tpl;
template <int = 0, int>
class Tpl;

pretty-printing produced an uncompilable code:

template <int, int = 0> class Tpl;
template <int = 0, int = 0> class Tpl;

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 4, 2025

@llvm/pr-subscribers-clang

Author: Andrey Ali Khan Bolshakov (bolshakov-a)

Changes

Prior to this change, for the code like this:

template &lt;int, int = 0&gt;
class Tpl;
template &lt;int = 0, int&gt;
class Tpl;

pretty-printing produced an uncompilable code:

template &lt;int, int = 0&gt; class Tpl;
template &lt;int = 0, int = 0&gt; class Tpl;

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

2 Files Affected:

  • (modified) clang/lib/AST/DeclPrinter.cpp (+2-2)
  • (modified) clang/test/AST/ast-print-record-decl.c (+12-1)
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 196057f7b45a4..7001adeff5397 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1894,7 +1894,7 @@ void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
       Out << TTP->getDeclName();
   }
 
-  if (TTP->hasDefaultArgument()) {
+  if (TTP->hasDefaultArgument() && !TTP->defaultArgumentWasInherited()) {
     Out << " = ";
     TTP->getDefaultArgument().getArgument().print(Policy, Out,
                                                   /*IncludeType=*/false);
@@ -1909,7 +1909,7 @@ void DeclPrinter::VisitNonTypeTemplateParmDecl(
         Policy.CleanUglifiedParameters ? II->deuglifiedName() : II->getName();
   printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
 
-  if (NTTP->hasDefaultArgument()) {
+  if (NTTP->hasDefaultArgument() && !NTTP->defaultArgumentWasInherited()) {
     Out << " = ";
     NTTP->getDefaultArgument().getArgument().print(Policy, Out,
                                                    /*IncludeType=*/false);
diff --git a/clang/test/AST/ast-print-record-decl.c b/clang/test/AST/ast-print-record-decl.c
index d3717a4feab83..fd815881ebeb0 100644
--- a/clang/test/AST/ast-print-record-decl.c
+++ b/clang/test/AST/ast-print-record-decl.c
@@ -290,9 +290,9 @@ KW DeclGroupInMemberList {
 // A tag decl group in the tag decl's own member list is exercised in
 // defSelfRef above.
 
+#ifdef __cplusplus
 
 // Check out-of-line record definition
-#ifdef __cplusplus
 // PRINT-CXX-NEXT: [[KW]] OutOfLineRecord {
 KW OutOfLineRecord {
   // PRINT-CXX-NEXT: [[KW]] Inner
@@ -304,4 +304,15 @@ KW OutOfLineRecord {
 KW OutOfLineRecord::Inner {
   // PRINT-CXX-NEXT: };
 };
+
+// PRINT-CXX-NEXT: template <typename, typename = int> [[KW]] SmearedTypeDefArgs;
+template <typename, typename = int> KW SmearedTypeDefArgs;
+// PRINT-CXX-NEXT: template <typename = int, typename> [[KW]] SmearedTypeDefArgs;
+template <typename = int, typename> KW SmearedTypeDefArgs;
+
+// PRINT-CXX-NEXT: template <int, int = 0> [[KW]] SmearedNTTPDefArgs;
+template <int, int = 0> KW SmearedNTTPDefArgs;
+// PRINT-CXX-NEXT: template <int = 0, int> [[KW]] SmearedNTTPDefArgs;
+template <int = 0, int> KW SmearedNTTPDefArgs;
+
 #endif

@bolshakov-a
Copy link
Contributor Author

@erichkeane, @mizvekov, @cor3ntin

Does such a small fix in pretty-printing need a relnote?

@cor3ntin
Copy link
Contributor

cor3ntin commented Oct 4, 2025

Yeah, i think it would not hurt!

Prior to this change, for the code like this:

template <int, int = 0>
class Tpl;
template <int = 0, int>
class Tpl;

pretty-printing produced an uncompilable code:

template <int, int = 0> class Tpl;
template <int = 0, int = 0> class Tpl;
@bolshakov-a bolshakov-a force-pushed the no_inherited_tpl_arg_print branch from 9e29e4d to 66bfe49 Compare October 4, 2025 14:15
Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@bolshakov-a
Copy link
Contributor Author

Could someone merge it?

@zyn0217 zyn0217 merged commit 2121bda into llvm:main Oct 5, 2025
10 checks passed
@bolshakov-a
Copy link
Contributor Author

Thanks!

@bolshakov-a bolshakov-a deleted the no_inherited_tpl_arg_print branch October 5, 2025 17:47
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 6, 2025
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;
```
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.

5 participants