[cfe-commits] r96884 - in /cfe/trunk: lib/AST/ASTContext.cpp test/Sema/align-x86.c

Charles Davis cdavis at mines.edu
Mon Feb 22 20:52:00 PST 2010


Author: cdavis
Date: Mon Feb 22 22:52:00 2010
New Revision: 96884

URL: http://llvm.org/viewvc/llvm-project?rev=96884&view=rev
Log:
When a reference to a field of a struct/union/class is passed to the
__alignof__ operator, make sure to take into account the packed alignment
of the struct/union/class itself. Matches GCC's behavior and fixes PR6362.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/test/Sema/align-x86.c

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=96884&r1=96883&r2=96884&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Feb 22 22:52:00 2010
@@ -563,6 +563,12 @@
 
       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());

Modified: cfe/trunk/test/Sema/align-x86.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/align-x86.c?rev=96884&r1=96883&r2=96884&view=diff
==============================================================================
--- cfe/trunk/test/Sema/align-x86.c (original)
+++ cfe/trunk/test/Sema/align-x86.c Mon Feb 22 22:52:00 2010
@@ -12,3 +12,9 @@
 _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];
+





More information about the cfe-commits mailing list