[clang] [clang] Use constant rounding mode for floating literals (PR #90877)

Joshua Cranmer via cfe-commits cfe-commits at lists.llvm.org
Fri May 3 12:18:58 PDT 2024


jcranmer-intel wrote:

I've been doing some testing, and I do want to confirm my understanding of the C standard here.

>From what I can tell, macro expansion (phase 4) happens before constants are parsed (phase 7). As a result, if you have code like this:
```c
#define CONSTANT 0.1f
```
the interpretation of `0.1f` depends on the state of the pragma at point of use of the macro, not point of declaration of the macro. Given that pragmas can be defined with `_Pragma`, with lambdas or statement expressions (albeit neither of which is standard C), it should be possible to create a macro that evaluates a constant with a given rounding mode:
```c++
#define rendevous(x) _Pragma(#x)
#define CONSTANT(RM, x) ([](){ rendevous(STDC FENV_ROUND RM); return x; })())
```

This does seem to indeed be the behavior of the current implementation, but I would like to see some tests in the test code confirming that it's the macro use, not the `#define` that determines the interpretation of the constant, and another test for the `_Pragma` case.

https://github.com/llvm/llvm-project/pull/90877


More information about the cfe-commits mailing list