[PATCH] D20561: Warn when taking address of packed member

Roger Ferrer Ibanez via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 07:47:32 PDT 2016


rogfer01 marked 3 inline comments as done.

================
Comment at: lib/Sema/SemaExpr.cpp:10527
@@ +10526,3 @@
+    if (RD->hasAttr<PackedAttr>() ||
+        ME->getMemberDecl()->hasAttr<PackedAttr>()) {
+      Diag(OpLoc, diag::warn_taking_address_of_packed_member)
----------------
aaron.ballman wrote:
> Ah, I forgot that the attribute also affected the alignment of the struct itself. Amending my false-positive idea, the following should be fine, shouldn't it?:
> ```
> struct __attribute__((packed)) S {
>   char c;
>   int i;
>   char c2;
> };
> 
> void f(S s) {
>   char *cp = &s.c;
>   char *cp2 = &s.c2;
> }
> ```
> I think perhaps we should not diagnose if the member's type also has a one-byte alignment (or, for instance, uses alignas(1) to achieve that). What do you think?
Yes. I like the idea since the packed attribute has no effect on already 1-aligned data (like char). But note that C++11 does not allow reducing the alignment through `alignas`.


http://reviews.llvm.org/D20561





More information about the cfe-commits mailing list