[PATCH] D93101: [Clang][Codegen] Truncate initializers of union bitfield members

Joe Ellis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 13 03:46:23 PST 2021


joechrisellis added inline comments.


================
Comment at: clang/lib/AST/ExprConstant.cpp:9803-9804
+           (Field->isBitField() &&
+            truncateBitfieldValue(Info, InitExpr, Result.getUnionValue(),
+                                  Field));
   }
----------------
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. 😄 


================
Comment at: clang/test/CodeGenCXX/bitfield-layout.cpp:88-95
+// CHECK: define i32 @_Z10test_truncv()
+int test_trunc() {
+  union {
+    int i : 4;
+  } U = {15};
+  return U.i;
+  // CHECK: ret i32 -1
----------------
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.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D93101/new/

https://reviews.llvm.org/D93101



More information about the cfe-commits mailing list