]> granicus.if.org Git - clang/commitdiff
When a reference to a field of a struct/union/class is passed to the
authorCharles Davis <cdavis@mines.edu>
Tue, 23 Feb 2010 04:52:00 +0000 (04:52 +0000)
committerCharles Davis <cdavis@mines.edu>
Tue, 23 Feb 2010 04:52:00 +0000 (04:52 +0000)
__alignof__ operator, make sure to take into account the packed alignment
of the struct/union/class itself. Matches GCC's behavior and fixes PR6362.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96884 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ASTContext.cpp
test/Sema/align-x86.c

index c8caeb62b31144054ef928e6f45b2ca0c26ff356..202e3370b67e5bf1cc8e2a13fc523ffdee29e8f7 100644 (file)
@@ -563,6 +563,12 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
 
       Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
     }
+    if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
+      // In the case of a field in a packed struct, we want the minimum
+      // of the alignment of the field and the alignment of the struct.
+      Align = std::min(Align,
+        getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
+    }
   }
 
   return CharUnits::fromQuantity(Align / Target.getCharWidth());
index f67adecbf5172fdf7d5a8a452edfbfef2e3a2024..c9a63989ecbc28fb8fcf2a97ba4c2d642bda4c76 100644 (file)
@@ -12,3 +12,9 @@ short chk2[__alignof__(long long) == 8 ? 1 : -1];
 _Complex double g3;
 short chk1[__alignof__(g3) == 8 ? 1 : -1]; 
 short chk2[__alignof__(_Complex double) == 8 ? 1 : -1];
+
+// PR6362
+struct __attribute__((packed)) {unsigned int a} g4;
+short chk1[__alignof__(g4) == 1 ? 1 : -1];
+short chk2[__alignof__(g4.a) == 1 ? 1 : -1];
+