Skip to content

Commit a5c33bd

Browse files
committed
[Clang][AST] Let DeclPrinter print trailing requires expressions for 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.
1 parent 5548ea3 commit a5c33bd

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ AST Dumping Potentially Breaking Changes
9999
----------------------------------------
100100

101101
- The text ast-dumper has improved printing of TemplateArguments.
102+
- The text decl-dumper prints template parameters' trailing requires expressions now.
102103

103104
Clang Frontend Potentially Breaking Changes
104105
-------------------------------------------

clang/lib/AST/DeclPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,16 @@ void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
11891189
Out << '>';
11901190
if (!OmitTemplateKW)
11911191
Out << ' ';
1192+
1193+
if (const Expr *RequiresClause = Params->getRequiresClause()) {
1194+
if (OmitTemplateKW)
1195+
Out << ' ';
1196+
Out << "requires ";
1197+
RequiresClause->printPretty(Out, nullptr, Policy, Indentation, "\n",
1198+
&Context);
1199+
if (!OmitTemplateKW)
1200+
Out << ' ';
1201+
}
11921202
}
11931203

11941204
void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args,

clang/test/PCH/cxx2a-requires-expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ bool f() {
2222
requires C<typename T::val> || (C<typename T::val> || C<T>);
2323
};
2424
}
25+
26+
namespace trailing_requires_expression {
27+
28+
template <typename T> requires C<T> && C2<T, T>
29+
// CHECK: template <typename T> requires C<T> && C2<T, T> void g();
30+
void g();
31+
32+
template <typename T> requires C<T> || C2<T, T>
33+
// CHECK: template <typename T> requires C<T> || C2<T, T> constexpr int h = sizeof(T);
34+
constexpr int h = sizeof(T);
35+
36+
template <typename T> requires C<T>
37+
// CHECK: template <typename T> requires C<T> class i {
38+
// CHECK-NEXT: };
39+
class i {};
40+
41+
}

0 commit comments

Comments
 (0)