Skip to content

Commit

Permalink
[C11] Update the status of N1365 on constant expression handling
Browse files Browse the repository at this point in the history
This paper is about constant expression handling in the presence of
FLT_EVAL_METHOD, which we handle via insertion of implicit cast nodes
in the AST.
  • Loading branch information
AaronBallman authored and chencha3 committed Mar 22, 2024
1 parent a7a2e9a commit c4d5141
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions clang/test/C/C11/n1365.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %clang_cc1 -ast-dump %s | FileCheck %s

/* WG14 N1365: Clang 16
* Constant expressions
*/

// Note: we don't allow you to expand __FLT_EVAL_METHOD__ in the presence of a
// pragma that changes its value. However, we can test that we have the correct
// constant expression behavior by testing that the AST has the correct implicit
// casts, which also specify that the cast was inserted due to an evaluation
// method requirement.
void func(void) {
{
#pragma clang fp eval_method(double)
_Static_assert(123.0F * 2.0F == 246.0F, "");
// CHECK: StaticAssertDecl
// CHECK-NEXT: ImplicitCastExpr {{.*}} '_Bool' <IntegralToBoolean>
// CHECK-NEXT: BinaryOperator {{.*}} 'int' '=='
// CHECK-NEXT: BinaryOperator {{.*}} 'double' '*' FPEvalMethod=1
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double' <FloatingCast> FPEvalMethod=1
// CHECK-NEXT: FloatingLiteral
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double' <FloatingCast> FPEvalMethod=1
// CHECK-NEXT: FloatingLiteral

// Ensure that a cast removes the extra precision.
_Static_assert((float)(123.0F * 2.0F) == 246.0F, "");
// CHECK: StaticAssertDecl
// CHECK-NEXT: ImplicitCastExpr {{.*}} '_Bool' <IntegralToBoolean>
// CHECK-NEXT: BinaryOperator {{.*}} 'int' '=='
// CHECK-NEXT: BinaryOperator {{.*}} 'double' '*' FPEvalMethod=1
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double' <FloatingCast> FPEvalMethod=1
// CHECK-NEXT: FloatingLiteral
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double' <FloatingCast> FPEvalMethod=1
// CHECK-NEXT: FloatingLiteral
}

{
#pragma clang fp eval_method(extended)
_Static_assert(123.0F * 2.0F == 246.0F, "");
// CHECK: StaticAssertDecl
// CHECK-NEXT: ImplicitCastExpr {{.*}} '_Bool' <IntegralToBoolean>
// CHECK-NEXT: BinaryOperator {{.*}} 'int' '=='
// CHECK-NEXT: BinaryOperator {{.*}} 'long double' '*' FPEvalMethod=2
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'long double' <FloatingCast> FPEvalMethod=2
// CHECK-NEXT: FloatingLiteral
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'long double' <FloatingCast> FPEvalMethod=2
// CHECK-NEXT: FloatingLiteral
}

{
#pragma clang fp eval_method(source)
_Static_assert(123.0F * 2.0F == 246.0F, "");
// CHECK: StaticAssertDecl
// CHECK-NEXT: ImplicitCastExpr {{.*}} '_Bool' <IntegralToBoolean>
// CHECK-NEXT: BinaryOperator {{.*}} 'int' '=='
// CHECK-NEXT: BinaryOperator {{.*}} 'float' '*' FPEvalMethod=0
// CHECK-NEXT: FloatingLiteral
// CHECK-NEXT: FloatingLiteral
}
}
2 changes: 1 addition & 1 deletion clang/www/c_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ <h2 id="c11">C11 implementation status</h2>
<tr>
<td>Constant expressions</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1365.htm">N1365</a></td>
<td class="unknown" align="center">Unknown</td>
<td class="Clang 16" align="center">Full</td>
</tr>
<tr>
<td>Contractions and expression evaluation methods</td>
Expand Down

0 comments on commit c4d5141

Please sign in to comment.