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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 13:56:37 PDT 2016


aaron.ballman added inline comments.

================
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)
----------------
rogfer01 wrote:
> 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`.
Hmm, okay, I was not remembering properly; I thought one of `alignas`, `__declspec(align)`, or `__attribute__((aligned))` allowed for decreasing the alignment, but documentation implies otherwise.


http://reviews.llvm.org/D20561





More information about the cfe-commits mailing list