Sirraide 69afb9d787
[Clang] [Sema] Fix bug in _Complex float+int arithmetic (#83063)
C23 6.3.1.8 ‘Usual arithmetic conversions’ p1 states (emphasis mine): 
> Otherwise, if the corresponding real type of either operand is
`float`, the other operand is converted, *without change of type
domain*, to a type whose corresponding real type is `float`.

‘type domain’ here refers to `_Complex` vs real (i.e. non-`_Complex`);
there is another clause that states the same for `double`.

Consider the following code:
```c++
_Complex float f;
int x;
f / x;
```

After talking this over with @AaronBallman, we came to the conclusion
that `x` should be converted to `float` and *not* `_Complex float` (that
is, we should perform a division of `_Complex float / float`, and *not*
`_Complex float / _Complex float`; the same also applies to `-+*`). This
was already being done correctly for cases where `x` was already a
`float`; it’s just mixed `_Complex float`+`int` operations that
currently suffer from this problem.

This pr removes the extra `FloatingRealToComplex` conversion that we
were erroneously inserting and adds some tests to make sure we’re
actually doing `_Complex float / float` and not `_Complex float /
_Complex float` (and analogously for `double` and `-+*`).

The only exception here is `float / _Complex float`, which calls a
library function (`__divsc3`) that takes 4 `float`s, so we end up having
to convert the `float` to a `_Complex float` after all (and analogously
for `double`); I don’t believe there is a way around this.

Lastly, we were also missing tests for `_Complex` arithmetic at compile
time, so this adds some tests for that as well.
2024-03-13 17:39:23 +01:00

42 lines
1.8 KiB
C++

// RUN: %clang_cc1 -O2 -triple=x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
struct agg
{
int a ;
int b ;
} t;
struct agg a;
int vt=10;
_Complex float cf;
int volatile vol =10;
void f0() {
const_cast<volatile _Complex float &>(cf) = const_cast<volatile _Complex float&>(cf) + 1;
// CHECK: [[Re1:%.*]] = load volatile float, ptr @cf
// CHECK: [[Im1:%.*]] = load volatile float, ptr getelementptr
// CHECK: [[Add1:%.*]] = fadd float [[Re1]], 1.000000e+00
// CHECK: store volatile float [[Add1]], ptr @cf
// CHECK: store volatile float [[Im1]], ptr getelementptr
static_cast<volatile _Complex float &>(cf) = static_cast<volatile _Complex float&>(cf) + 1;
// CHECK: [[Re2:%.*]] = load volatile float, ptr @cf
// CHECK: [[Im2:%.*]] = load volatile float, ptr getelementptr
// CHECK: [[Add2:%.*]] = fadd float [[Re2]], 1.000000e+00
// CHECK: store volatile float [[Add2]], ptr @cf
// CHECK: store volatile float [[Im2]], ptr getelementptr
const_cast<volatile int &>(a.a) = const_cast<volatile int &>(t.a) ;
// CHECK: [[I1:%.*]] = load volatile i32, ptr @t
// CHECK: store volatile i32 [[I1]], ptr @a
static_cast<volatile int &>(a.b) = static_cast<volatile int &>(t.a) ;
// CHECK: [[I2:%.*]] = load volatile i32, ptr @t
// CHECK: store volatile i32 [[I2]], ptr getelementptr
const_cast<volatile int&>(vt) = const_cast<volatile int&>(vt) + 1;
// CHECK: [[I3:%.*]] = load volatile i32, ptr @vt
// CHECK: [[Add3:%.*]] = add nsw i32 [[I3]], 1
// CHECK: store volatile i32 [[Add3]], ptr @vt
static_cast<volatile int&>(vt) = static_cast<volatile int&>(vt) + 1;
// CHECK: [[I4:%.*]] = load volatile i32, ptr @vt
// CHECK: [[Add4:%.*]] = add nsw i32 [[I4]], 1
// CHECK: store volatile i32 [[Add4]], ptr @vt
vt = const_cast<int&>(vol);
// [[I5:%.*]] = load i32, ptr @vol
// store i32 [[I5]], ptr @vt
}