If an initial value is given for a bitfield that does not fit in the
bitfield, the value should be truncated. Constant folding for
expressions did not account for this truncation in the case of union
member functions, despite a warning being emitted. In some contexts,
evaluation of expressions was not enabled unless C++11, ROPI or RWPI
was enabled.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang/lib/AST/ExprConstant.cpp | ||
---|---|---|
9803β9804 | nit: I would prefer this: if (Field->isBitField() && truncateBitfieldValue(Info, InitExpr, Result.getUnionValue(), Field)) return true; return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); It feels more in-line with the rest of the function. But it is okay if you want to ignore this too. π | |
clang/test/CodeGenCXX/bitfield-layout.cpp | ||
89β96 | I'd like to see some more tests that check the truncation behaviour. My understanding is that this is trucating to -1 because of two's complement? How about something like: int test_trunc() { union { int i : 4; } U = {80}; return U.i; // CHECK: ret i32 0 } Am I understanding the behaviour correctly? Some comments about what is actually happening on the bit-level to get this result would also be nice. |
clang/lib/AST/ExprConstant.cpp | ||
---|---|---|
9801β9809 | Shouldn't this be && not ||? These functions return true if they succeed (unlike the convention in Sema where true means an error diagnostic was produced). |
clang/lib/AST/ExprConstant.cpp | ||
---|---|---|
9801β9809 | You are correct. I have updated the logic to be simpler to follow as well. | |
clang/test/CodeGenCXX/bitfield-layout.cpp | ||
89β96 | Yes that is correct. I have added some new test cases and comments to make it clear what they are testing for. |
The truncate conditions look a lot better and the test coverage seems reasonable now.
LGTM.
nit: I would prefer this:
It feels more in-line with the rest of the function. But it is okay if you want to ignore this too. π