[PATCH] D21099: [Sema] Teach -Wcast-align to look at the aligned attribute of the declared variables

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 13:33:47 PDT 2016


ahatanak created this revision.
ahatanak added a subscriber: cfe-commits.

Sema::CheckCastAlign currently ignores the aligned attribute attached to the declared variables, which causes clang to issue spurious warnings. This patch fixes the bug.

http://reviews.llvm.org/D21099

Files:
  lib/Sema/SemaChecking.cpp
  test/Sema/warn-cast-align.c

Index: test/Sema/warn-cast-align.c
===================================================================
--- test/Sema/warn-cast-align.c
+++ test/Sema/warn-cast-align.c
@@ -39,3 +39,23 @@
 void test3(char *P) {
   struct B *b = (struct B*) P;
 }
+
+// Do not issue a warning. The aligned attribute changes the alignment of the
+// variables and fields.
+char __attribute__((aligned(4))) a[16];
+
+struct S0 {
+  char a[16];
+};
+
+struct S {
+  char __attribute__((aligned(4))) a[16];
+  struct S0 __attribute__((aligned(4))) s0;
+};
+
+void test4() {
+  struct S s;
+  int *i = (int *)s.a;
+  i = (int *)&s.s0;
+  i = (int *)a;
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -9304,6 +9304,23 @@
   if (SrcPointee->isIncompleteType()) return;
 
   CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
+  Expr *SE = nullptr;
+
+  if (auto *CE = dyn_cast<CastExpr>(Op)) {
+    if (CE->getCastKind() == CK_ArrayToPointerDecay)
+      SE = CE->getSubExpr();
+  } else if (auto *UO = dyn_cast<UnaryOperator>(Op)) {
+    if (UO->getOpcode() == UO_AddrOf)
+      SE = UO->getSubExpr();
+  }
+
+  if (SE) {
+    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(SE))
+      SrcAlign = Context.getDeclAlign(DRE->getDecl());
+    else if (const MemberExpr *ME = dyn_cast<MemberExpr>(SE))
+      SrcAlign = Context.getDeclAlign(ME->getMemberDecl());
+  }
+
   if (SrcAlign >= DestAlign) return;
 
   Diag(TRange.getBegin(), diag::warn_cast_align)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21099.59942.patch
Type: text/x-patch
Size: 1564 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160607/b974286d/attachment.bin>


More information about the cfe-commits mailing list