https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/96864
As discussed in https://github.com/llvm/llvm-project/pull/96084#discussion_r1654629993, it would be nice to present these trailing constraints on template parameters when printing CTAD decls through a DeclPrinter. >From a5c33bd413d8150d1688240c6b5253b1760cafe1 Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Thu, 27 Jun 2024 15:59:48 +0800 Subject: [PATCH] [Clang][AST] Let DeclPrinter print trailing requires expressions for template parameters As discussed in https://github.com/llvm/llvm-project/pull/96084#discussion_r1654629993, it would be nice to present these trailing constraints on template parameters when printing CTAD decls through a DeclPrinter. --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/AST/DeclPrinter.cpp | 10 ++++++++++ clang/test/PCH/cxx2a-requires-expr.cpp | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 69aea6c21ad39..03b1daa6597cd 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -99,6 +99,7 @@ AST Dumping Potentially Breaking Changes ---------------------------------------- - The text ast-dumper has improved printing of TemplateArguments. +- The text decl-dumper prints template parameters' trailing requires expressions now. Clang Frontend Potentially Breaking Changes ------------------------------------------- diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp index 0cf4e64f83b8d..0a081e7e07ca8 100644 --- a/clang/lib/AST/DeclPrinter.cpp +++ b/clang/lib/AST/DeclPrinter.cpp @@ -1189,6 +1189,16 @@ void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params, Out << '>'; if (!OmitTemplateKW) Out << ' '; + + if (const Expr *RequiresClause = Params->getRequiresClause()) { + if (OmitTemplateKW) + Out << ' '; + Out << "requires "; + RequiresClause->printPretty(Out, nullptr, Policy, Indentation, "\n", + &Context); + if (!OmitTemplateKW) + Out << ' '; + } } void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args, diff --git a/clang/test/PCH/cxx2a-requires-expr.cpp b/clang/test/PCH/cxx2a-requires-expr.cpp index 7f8f258a0f8f3..936f601685463 100644 --- a/clang/test/PCH/cxx2a-requires-expr.cpp +++ b/clang/test/PCH/cxx2a-requires-expr.cpp @@ -22,3 +22,20 @@ bool f() { requires C<typename T::val> || (C<typename T::val> || C<T>); }; } + +namespace trailing_requires_expression { + +template <typename T> requires C<T> && C2<T, T> +// CHECK: template <typename T> requires C<T> && C2<T, T> void g(); +void g(); + +template <typename T> requires C<T> || C2<T, T> +// CHECK: template <typename T> requires C<T> || C2<T, T> constexpr int h = sizeof(T); +constexpr int h = sizeof(T); + +template <typename T> requires C<T> +// CHECK: template <typename T> requires C<T> class i { +// CHECK-NEXT: }; +class i {}; + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits