From 21e5fdd89e8a906f9c4e4bf00151691a0ccbb2f3 Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Wed, 30 Nov 2016 19:42:03 +0000 Subject: [PATCH] [Sema] Teach -Wcast-align to look at the aligned attribute of the declared variables. Teach Sema to check the aligned attribute attached to variable declarations so that it doesn't issue spurious warnings. rdar://problem/26517471 Differential revision: https://reviews.llvm.org/D21099 llvm-svn: 288267 --- clang/lib/Sema/SemaChecking.cpp | 22 ++++++++++++++++++++++ clang/test/Sema/warn-cast-align.c | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 4fe3053..9e16554 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -10264,6 +10264,19 @@ bool Sema::CheckParmsForFunctionDef(ArrayRef Parameters, return HasInvalidParm; } +/// A helper function to get the alignment of a Decl referred to by DeclRefExpr +/// or MemberExpr. +static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign, + ASTContext &Context) { + if (const auto *DRE = dyn_cast(E)) + return Context.getDeclAlign(DRE->getDecl()); + + if (const auto *ME = dyn_cast(E)) + return Context.getDeclAlign(ME->getMemberDecl()); + + return TypeAlign; +} + /// CheckCastAlign - Implements -Wcast-align, which warns when a /// pointer cast increases the alignment requirements. void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { @@ -10298,6 +10311,15 @@ void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { if (SrcPointee->isIncompleteType()) return; CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); + + if (auto *CE = dyn_cast(Op)) { + if (CE->getCastKind() == CK_ArrayToPointerDecay) + SrcAlign = getDeclAlign(CE->getSubExpr(), SrcAlign, Context); + } else if (auto *UO = dyn_cast(Op)) { + if (UO->getOpcode() == UO_AddrOf) + SrcAlign = getDeclAlign(UO->getSubExpr(), SrcAlign, Context); + } + if (SrcAlign >= DestAlign) return; Diag(TRange.getBegin(), diag::warn_cast_align) diff --git a/clang/test/Sema/warn-cast-align.c b/clang/test/Sema/warn-cast-align.c index 9d64699..e8f85bc 100644 --- a/clang/test/Sema/warn-cast-align.c +++ b/clang/test/Sema/warn-cast-align.c @@ -39,3 +39,23 @@ void test2(char *P) { 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; +} -- 2.7.4