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
Unit Tests
Time | Test | |
---|---|---|
50 ms | x64 windows > LLVM.CodeGen/XCore::threads.ll |
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 | ||
88–95 | 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–9804 | 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). |
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. 😄