-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Clang does not report invalid arithmetic operations at compile time when doing _Complex
math.
#84871
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
Comments
@llvm/issue-subscribers-clang-frontend Author: Timm Baeder (tbaederr)
For example:
```c++
static_assert(__imag(5.0j / 0) == __builtin_inf());
```
https://godbolt.org/z/59fqKv4Kc
This works without any diagnostics in Clang, but errors out in gcc. There are actually a few tests in llvm-project/clang/test/SemaCXX/complex-folding.cpp Lines 60 to 99 in d02d8df
In this example: constexpr _Complex double D1 = {0.0, 0.0};
constexpr double D2 = __real(__builtin_inf() * D1);
static_assert(D2 == 1);
constexpr double D3 = __builtin_inf() * 0.0; https://godbolt.org/z/dPME9To8d (which is adapted and simplified from the test above), multiplying |
@AaronBallman Do you maybe have an opinion here? Should the behavior be changed? Or is this something some projects may rely on? |
In C, _Complex is morally acceptable in an arithmetic constant expression; C doesn't have a suffix for complex or imaginary numbers so it's technically not possible to have a complex constant in standard C, but we support suffixes as an extension so you can have a complex constant. In C++, So I think complex types should follow the usual expectations for use within a constant expression in that it catches UB as being non-constant, but otherwise is fine. (Because we have an extension allowing arbitrary constant expressions in static_assert rather than requiring they be integer constant expressions specifically.) So I think the behavior probably should be changed? CC @jcranmer-intel for additional opinions. |
) Use handleFloatFloatBinOp to properly diagnose NaN results and divisions by zero. Fixes #84871
…m#90588) Use handleFloatFloatBinOp to properly diagnose NaN results and divisions by zero. Fixes llvm#84871 Signed-off-by: Hafidz Muzakky <[email protected]>
For example:
https://godbolt.org/z/59fqKv4Kc
This works without any diagnostics in Clang, but errors out in gcc.
There are actually a few tests in
tests/SemaCXX/complex-folding.cpp
that rely on this behavior, although they aren't as obvious as the example above:llvm-project/clang/test/SemaCXX/complex-folding.cpp
Lines 60 to 99 in d02d8df
In this example:
https://godbolt.org/z/dPME9To8d
(which is adapted and simplified from the test above), multiplying
inf
with0
produces anan
, which is properly diagnosed when evaluating the initializer forD3
, but not at al when evaluatingD2
. Thestatic_assert
proves that D2 is indeednan
.The text was updated successfully, but these errors were encountered: