r202088 - Sema: Make getPreferredTypeAlign respect alignments specified with an aligned attribute on a typedef

David Majnemer david.majnemer at gmail.com
Mon Feb 24 15:34:17 PST 2014


Author: majnemer
Date: Mon Feb 24 17:34:17 2014
New Revision: 202088

URL: http://llvm.org/viewvc/llvm-project?rev=202088&view=rev
Log:
Sema: Make getPreferredTypeAlign respect alignments specified with an aligned attribute on a typedef

When calculating the preferred alignment of a type, consider if a alignment
attribute came from a typedef declaration.  If one did, do not naturally align
the type.

Patch by Stephan Tolksdorf, with a little tweaking and an additional testcase by me.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/test/Sema/attr-aligned.c

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=202088&r1=202087&r2=202088&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Feb 24 17:34:17 2014
@@ -1761,13 +1761,18 @@ unsigned ASTContext::getPreferredTypeAli
   if (Target->getTriple().getArch() == llvm::Triple::xcore)
     return ABIAlign;  // Never overalign on XCore.
 
+  const TypedefType *TT = T->getAs<TypedefType>();
+
   // Double and long long should be naturally aligned if possible.
   if (const ComplexType* CT = T->getAs<ComplexType>())
     T = CT->getElementType().getTypePtr();
   if (T->isSpecificBuiltinType(BuiltinType::Double) ||
       T->isSpecificBuiltinType(BuiltinType::LongLong) ||
       T->isSpecificBuiltinType(BuiltinType::ULongLong))
-    return std::max(ABIAlign, (unsigned)getTypeSize(T));
+    // Don't increase the alignment if an alignment attribute was specified on a
+    // typedef declaration.
+    if (!TT || !TT->getDecl()->getMaxAlignment())
+      return std::max(ABIAlign, (unsigned)getTypeSize(T));
 
   return ABIAlign;
 }

Modified: cfe/trunk/test/Sema/attr-aligned.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-aligned.c?rev=202088&r1=202087&r2=202088&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-aligned.c (original)
+++ cfe/trunk/test/Sema/attr-aligned.c Mon Feb 24 17:34:17 2014
@@ -21,6 +21,12 @@ char a1[__alignof__(struct struct_with_u
 char a2[__alignof__(a) == 1? : -1] = { 0 };
 char a3[sizeof(a) == 1? : -1] = { 0 };
 
+typedef long long __attribute__((aligned(1))) underaligned_longlong;
+char a4[__alignof__(underaligned_longlong) == 1 ?: -1] = {0};
+
+typedef long long __attribute__((aligned(1))) underaligned_complex_longlong;
+char a5[__alignof__(underaligned_complex_longlong) == 1 ?: -1] = {0};
+
 // rdar://problem/8335865
 int b __attribute__((aligned(2)));
 char b1[__alignof__(b) == 2 ?: -1] = {0};





More information about the cfe-commits mailing list