| Issue |
237818
|
| Summary |
[clang][Frontend] -save-temps changes __builtin_LINE() for multi-line macro expansions
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
yqtian-se
|
Compiling preprocessed output can change the value of `__builtin_LINE()` and
`__builtin_source_location()` when they occur in a macro expansion that spans
multiple source lines. As a result, `-save-temps` can change observable program
behavior.
For example, glibc's C++ `assert()` uses `__builtin_LINE()`. The following
program reports a different assertion line with `-save-temps` than without it:
```cpp
#include <cassert>
int f(int move, int row) {
assert(move == 1 || move == 2
|| (row >= 0 && row < 19));
return row;
}
int main(int argc, char **) { return f(argc + 5, -1); }
```
### Reproduction
```console
$ clang++ -O2 a.cpp -o plain
$ clang++ -O2 -save-temps a.cpp -o saved
$ ./plain 2>&1 | grep -o 'a.cpp:[0-9]*'
a.cpp:4
$ ./saved 2>&1 | grep -o 'a.cpp:[0-9]*'
a.cpp:3
```
The wording of the assertion message is libc-dependent; the relevant result is
the reported line number. GCC reports the same line with and without
`-save-temps` for this input.
Reproduced on x86_64 Ubuntu 24.04 with LLVM main
`adad1a68aba2` (2026-11-02).
#### Expected behavior
Saving and recompiling the preprocessed output should not change the value of
`__builtin_LINE()` or `__builtin_source_location()`. Both compilations should
report line 4.
### potential reason
`SourceLocExpr::EvaluateInContext` uses the end of a macro expansion for these
builtins, matching Clang's `__LINE__` behavior. However, `-E` prints the macro
expansion starting on the line where the expansion begins. The saved
preprocessed file therefore places the builtin on line 3, and the subsequent
compilation observes that new position.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs