https://github.com/bolshakov-a created https://github.com/llvm/llvm-project/pull/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; ``` >From 9e29e4d36f8f6c2aa1d2f2462024be784d34a163 Mon Sep 17 00:00:00 2001 From: Bolshakov <[email protected]> Date: Sat, 4 Oct 2025 12:09:19 +0300 Subject: [PATCH] [clang][AST] Don't print inherited default template args 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; --- clang/lib/AST/DeclPrinter.cpp | 4 ++-- clang/test/AST/ast-print-record-decl.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
