-
Notifications
You must be signed in to change notification settings - Fork 13k
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
[clang] Miscompilation concerning const pointer to file-scope compound literal #72784
Comments
Interestingly removing the I am not sure what is going on here but @AaronBallman probably has a better idea |
Wow, that is a neat one! The behavior seems to have changed around Clang 4.0.0: https://godbolt.org/z/8s4ffY5Pf and the IR changed drastically. If I had to make a guess at what caused this change, I would guess it was this patch: 9648288 -- changing the literal from a global to a local also resolves the issue, so this seems to be related to lifetimes. I continue to believe that the way we model compound literals is wrong in C (see #68746 (comment) for details) and I think this may be another demonstration of it. CC @rjmccall @efriedma-quic for opinions or ideas on how to resolve this. Perhaps there's a more narrow fix we can make, but I suspect we should be putting time into refactoring compound literals more broadly because I'm not certain our current approach works for C23's ability to add storage class specifiers to the compound literal. |
This is not a CodeGen thing; it's constant evaluation misbehaving. For example:
I'm not sure how much it helps to introduce CompouldLiteralVarDecls; you still need an expression in the AST to represent the point at which the contents of the CompoundLiteral are evaluated, and nothing else can actually refer to the CompouldLiteralVarDecl directly, so I don't see when you'd ever use a DeclRefExpr to refer to a CompoundLiteral. You could introduce a CompouldLiteralVarDecl anyway, but that doesn't seem to get you much benefit unless you want to introduce decls for everything that allocates space. I mean, do you want MaterializeTemporaryVarDecl for C++? |
My thinking was that this would be modeled after That said, I'm not tied to this strategy specifically. Do you have other ideas on how we would cleanly associate a storage class like |
CompoundLiteralExpr already has an "isFileScope()" bit, which is effectively the storage class. We can extend it to return a proper storage class if we want. It would require a slight extension of the code to support thread_local.
That seems mostly like a refactoring. You can always just write the relevant code to handle "a VarDecl or a CompoundLiteralExpr". We don't have very much code specific to CompoundLiteral anyway. |
@llvm/issue-subscribers-clang-frontend Author: Vélimir Chakhnovski (vchakhno)
### Minimal reproducible example:
`main.c`
```c
#include <stdio.h>
typedef struct { global_t *const global = &(global_t){0}; int main(void) {
|
How do we handle l-value references to temporaries in C++ constant evaluation? I don't think we make |
Minimal reproducible example:
main.c
Expected behaviour:
The above code should print 10. The value being assigned also appears in the generated assembly, but isn't printed at execution.
Actual behaviour:
This code compiles successfully without warnings and prints 0.
Compiled with:
clang main.c -Wall -Wextra -Werror -pedantic -fsanitize=undefined,address
17.0.0.1
, targetx86_64-unknown-linux-gnu
on godbolt (link)14.0.0-1ubuntu1.1
, targetx86_64-pc-linux-gnu
12.0.1-19ubuntu3
, targetx86_64-pc-linux-gnu
All three versions are affected.
I also compiled it on gcc in order to compare:
gcc main.c -Wall -Wextra -Werror -pedantic -fsanitize=undefined,address
13.2.0
on godbolt (link)11.4.0-1ubuntu1~22.04
Both versions compile successfully and produce
10
, the expected behaviour.The text was updated successfully, but these errors were encountered: