[PATCH] D25106: Packed member warning: Use the typedef name for anonymous structures when it is available

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 30 09:39:16 PDT 2016


arphaman created this revision.
arphaman added reviewers: aaron.ballman, rogfer01.
arphaman added a subscriber: cfe-commits.
arphaman set the repository for this revision to rL LLVM.

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


Repository:
  rL LLVM

https://reviews.llvm.org/D25106

Files:
  Sema/SemaChecking.cpp
  Sema/address-packed.c


Index: Sema/address-packed.c
===================================================================
--- Sema/address-packed.c
+++ Sema/address-packed.c
@@ -161,3 +161,27 @@
 {
     return (struct AlignedTo2Bis*)&s->x; // no-warning
 }
+
+typedef struct {
+  char c;
+  int x;
+} __attribute__((packed)) TypedefStructArguable;
+
+typedef union {
+  char c;
+  int x;
+} __attribute((packed)) TypedefUnionArguable;
+
+typedef TypedefStructArguable TypedefStructArguableTheSecond;
+
+int *typedef1(TypedefStructArguable *s) {
+    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
+}
+
+int *typedef2(TypedefStructArguableTheSecond *s) {
+    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
+}
+
+int *typedef3(TypedefUnionArguable *s) {
+    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefUnionArguable'}}
+}
Index: Sema/SemaChecking.cpp
===================================================================
--- Sema/SemaChecking.cpp
+++ Sema/SemaChecking.cpp
@@ -11279,8 +11279,13 @@
 
 void Sema::DiagnoseMisalignedMembers() {
   for (MisalignedMember &m : MisalignedMembers) {
+    const NamedDecl *ND = m.RD;
+    if (ND->getName().empty()) {
+      if (const auto *TypedefDecl = m.RD->getTypedefNameForAnonDecl())
+        ND = TypedefDecl;
+    }
     Diag(m.E->getLocStart(), diag::warn_taking_address_of_packed_member)
-        << m.MD << m.RD << m.E->getSourceRange();
+        << m.MD << ND << m.E->getSourceRange();
   }
   MisalignedMembers.clear();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25106.73070.patch
Type: text/x-patch
Size: 1601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160930/e4b51725/attachment.bin>


More information about the cfe-commits mailing list