From ae83eca8d28ac331d927c74ce90066e14b6591a5 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 10 Jun 2014 23:43:44 +0000 Subject: [PATCH] Teach __alignof__ to look through arrays before performing the preferred-alignment transformations. Corrects alignof(T[]) to return alignof(T) in all cases, as required by relevant standards. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210609 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTContext.cpp | 10 +--------- test/Sema/align-x86.c | 4 ++++ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index f78f2a90a5..eaf77e9625 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -1328,15 +1328,6 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { MinWidth <= getTypeSize(cast(arrayType))) Align = std::max(Align, Target->getLargeArrayAlign()); } - - // Keep track of extra alignment requirements on the array itself, then - // work with the element type. - // - // FIXME: Computing the preferred type alignment for the array element - // type should not be necessary, but getPreferredTypeAlign returns the - // wrong thing in some cases (such as 'long long[]' on x86_64). - Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); - T = BaseT; } Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); if (const VarDecl *VD = dyn_cast(D)) { @@ -1788,6 +1779,7 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { const TypedefType *TT = T->getAs(); // Double and long long should be naturally aligned if possible. + T = T->getBaseElementTypeUnsafe(); if (const ComplexType *CT = T->getAs()) T = CT->getElementType().getTypePtr(); if (T->isSpecificBuiltinType(BuiltinType::Double) || diff --git a/test/Sema/align-x86.c b/test/Sema/align-x86.c index 6b93a4893d..f112c6398c 100644 --- a/test/Sema/align-x86.c +++ b/test/Sema/align-x86.c @@ -23,6 +23,10 @@ struct __attribute__((packed)) {unsigned int a;} g4; short chk1[__alignof__(g4) == 1 ? 1 : -1]; short chk2[__alignof__(g4.a) == 1 ? 1 : -1]; +double g6[3]; +short chk1[__alignof__(g6) == 8 ? 1 : -1]; +short chk2[__alignof__(double[3]) == 8 ? 1 : -1]; + // PR5637 -- 2.50.1