Skip to content

Commit

Permalink
[Clang] [CodeGen] Fix codegen bug in constant initialisation in C23 m…
Browse files Browse the repository at this point in the history
…ode (#84981)

Consider the following code:
```c
bool const inf =  (1.0/0.0);
``` 

When trying to emit the initialiser of this variable in C23, we end up
hitting a code path in codegen in `VarDecl::evaluateValueImpl()` where
we check for `IsConstantInitialization && (Ctx.getLangOpts().CPlusPlus
|| Ctx.getLangOpts().C23)`, and if that is the case and we emitted any
notes, constant evaluation fails, and as a result, codegen issues this
error:
```
<source>:1:12: error: cannot compile this static initializer yet
    1 | bool const inf =  (1.0/0.0);
      |        
```

As a fix, only fail in C23 mode if we’re initialising a `constexpr`
variable.

This fixes #84784.
  • Loading branch information
Sirraide committed Mar 13, 2024
1 parent 424e0a8 commit 2cf2bc4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2577,11 +2577,14 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes,
IsConstantInitialization);

// In C++/C23, this isn't a constant initializer if we produced notes. In that
// case, we can't keep the result, because it may only be correct under the
// assumption that the initializer is a constant context.
// In C++, or in C23 if we're initialising a 'constexpr' variable, this isn't
// a constant initializer if we produced notes. In that case, we can't keep
// the result, because it may only be correct under the assumption that the
// initializer is a constant context.
if (IsConstantInitialization &&
(Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23) && !Notes.empty())
(Ctx.getLangOpts().CPlusPlus ||
(isConstexpr() && Ctx.getLangOpts().C23)) &&
!Notes.empty())
Result = false;

// Ensure the computed APValue is cleaned up later if evaluation succeeded,
Expand Down
3 changes: 3 additions & 0 deletions clang/test/CodeGen/const-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,6 @@ int PR4517_x2 = PR4517_arrc[PR4517_idx];
// CHECK: @PR4517_x = global i32 42, align 4
// CHECK: @PR4517_idx = constant i32 1, align 4
// CHECK: @PR4517_x2 = global i32 42, align 4

// CHECK: @GH84784_inf = constant i8 1
_Bool const GH84784_inf = (1.0/0.0);
6 changes: 6 additions & 0 deletions clang/test/Sema/const-init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s

// Division by 0 here is an error iff the variable is 'constexpr'.
const _Bool inf1 = (1.0/0.0 == __builtin_inf());
constexpr _Bool inf2 = (1.0/0.0 == __builtin_inf()); // expected-error {{must be initialized by a constant expression}} expected-note {{division by zero}}
constexpr _Bool inf3 = __builtin_inf() == __builtin_inf();

0 comments on commit 2cf2bc4

Please sign in to comment.