From c1de52de64725945e5ae87e6f99ddedf161856e5 Mon Sep 17 00:00:00 2001
From: Chris Lattner <sabre@nondot.org>
Date: Sat, 19 Feb 2011 22:55:41 +0000
Subject: [PATCH] Fix PR9253, allowing attribute(aligned) to reduce the
 alignment of a typedef.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126059 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/AST/ASTContext.cpp          |  8 +++++++-
 test/Sema/struct-packed-align.c | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 94f3b337275..7da2f348439 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -882,7 +882,13 @@ ASTContext::getTypeInfo(const Type *T) const {
     const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
     std::pair<uint64_t, unsigned> Info
       = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
-    Align = std::max(Typedef->getMaxAlignment(), Info.second);
+    // If the typedef has an aligned attribute on it, it overrides any computed
+    // alignment we have.  This violates the GCC documentation (which says that
+    // attribute(aligned) can only round up) but matches its implementation.
+    if (unsigned AttrAlign = Typedef->getMaxAlignment())
+      Align = AttrAlign;
+    else
+      Align = Info.second;
     Width = Info.first;
     break;
   }
diff --git a/test/Sema/struct-packed-align.c b/test/Sema/struct-packed-align.c
index 2b9456703c7..6ca6a6096c4 100644
--- a/test/Sema/struct-packed-align.c
+++ b/test/Sema/struct-packed-align.c
@@ -117,3 +117,18 @@ struct packed_fas2 {
 
 extern int m1[sizeof(struct packed_fas2) == 1 ? 1 : -1];
 extern int m2[__alignof(struct packed_fas2) == 1 ? 1 : -1];
+
+// Attribute aligned can round down typedefs.  PR9253
+typedef long long  __attribute__((aligned(1))) nt;
+
+struct nS {
+  char buf_nr;
+  nt start_lba;
+};
+
+extern int n1[sizeof(struct nS) == 9 ? 1 : -1];
+extern int n2[__alignof(struct nS) == 1 ? 1 : -1];
+
+
+
+
-- 
GitLab