Skip to content

[Clang][AST] Let DeclPrinter print trailing requires expressions for template parameters #96864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2024

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Jun 27, 2024

As discussed in #96084 (comment), it would be nice to present these trailing constraints on template parameters when printing CTAD decls through a DeclPrinter.

…template parameters

As discussed in llvm#96084 (comment),
it would be nice to present these trailing constraints on template
parameters when printing CTAD decls through a DeclPrinter.
@zyn0217 zyn0217 requested review from mizvekov and hokein June 27, 2024 08:08
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

As discussed in #96084 (comment), it would be nice to present these trailing constraints on template parameters when printing CTAD decls through a DeclPrinter.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/AST/DeclPrinter.cpp (+10)
  • (modified) clang/test/PCH/cxx2a-requires-expr.cpp (+17)
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 {};
+
+}

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

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

thanks, looks good.

@zyn0217 zyn0217 merged commit 2a948d1 into llvm:main Jun 27, 2024
6 of 7 checks passed
@zyn0217 zyn0217 deleted the decl-dumper-trailing-requires branch June 27, 2024 10:50
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…template parameters (llvm#96864)

As discussed in
llvm#96084 (comment),
it would be nice to present these trailing constraints on template
parameters when printing CTAD decls through a DeclPrinter.
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
…template parameters (llvm#96864)

As discussed in
llvm#96084 (comment),
it would be nice to present these trailing constraints on template
parameters when printing CTAD decls through a DeclPrinter.
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.

3 participants