This is an archive of the discontinued LLVM Phabricator instance.

Packed member warning: Use the typedef name for anonymous structures when it is available
ClosedPublic

Authored by arphaman on Sep 30 2016, 9:39 AM.

Details

Summary

This patch improves the packed member warning by showing the name of the anonymous structure/union when it was defined with a typedef, e.g. the code below:

typedef struct {
  char c;
  int x;
} __attribute__((packed)) Foo;
int *foo(Foo *f) { return &f->x; }

Would now produce the following warning:

taking address of packed member 'x' of class or structure 'Foo' may result in an unaligned pointer value

Diff Detail

Event Timeline

arphaman updated this revision to Diff 73070.Sep 30 2016, 9:39 AM
arphaman retitled this revision from to Packed member warning: Use the typedef name for anonymous structures when it is available.
arphaman updated this object.
arphaman added reviewers: aaron.ballman, rogfer01.
arphaman set the repository for this revision to rL LLVM.
arphaman added a subscriber: cfe-commits.
arphaman updated this revision to Diff 73076.Sep 30 2016, 9:49 AM
arphaman removed rL LLVM as the repository for this revision.

I had to reuploaded the diff as Phabricator seemed to have removed the 'lib' and 'test' paths prefixes from the filenames in the original diff.

aaron.ballman added inline comments.Oct 3 2016, 1:44 PM
lib/Sema/SemaChecking.cpp
11284

Please don't use auto here since the type is not spelled directly in the initialization.

test/Sema/address-packed.c
187

Can you add a test along the lines of:

struct S {
  union {
    char c;
    int x;
  } __attribute__((packed));
};

int *ugh(S *s) {
  return &s->x;   
}
arphaman updated this revision to Diff 73338.Oct 3 2016, 2:08 PM
arphaman set the repository for this revision to rL LLVM.
arphaman marked 2 inline comments as done.

Update to the patch that follows Aaron's comments:

  • Uses the explicit type name instead of auto.
  • Expands the test by adding a scenario where an inner anonymous union is contained in a structure.
aaron.ballman accepted this revision.Oct 4 2016, 6:24 AM
aaron.ballman edited edge metadata.

LGTM, thank you!

This revision is now accepted and ready to land.Oct 4 2016, 6:24 AM
This revision was automatically updated to reflect the committed changes.