[clang] 0402705 - [Sema] Teach -Wcast-align to compute alignment of CXXThisExpr
Akira Hatanaka via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 7 17:48:06 PDT 2020
Author: Akira Hatanaka
Date: 2020-07-07T17:45:04-07:00
New Revision: 04027052a72f982c9e09472427ec7339415fb777
URL: https://github.com/llvm/llvm-project/commit/04027052a72f982c9e09472427ec7339415fb777
DIFF: https://github.com/llvm/llvm-project/commit/04027052a72f982c9e09472427ec7339415fb777.diff
LOG: [Sema] Teach -Wcast-align to compute alignment of CXXThisExpr
This fixes https://bugs.llvm.org/show_bug.cgi?id=46605.
rdar://problem/65158878
Differential Revision: https://reviews.llvm.org/D83317
Added:
Modified:
clang/lib/Sema/SemaChecking.cpp
clang/test/SemaCXX/warn-cast-align.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 77858b17d62c..efaf36a69306 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -13560,12 +13560,14 @@ static getBaseAlignmentAndOffsetFromLValue(const Expr *E, ASTContext &Ctx) {
}
case Stmt::MemberExprClass: {
auto *ME = cast<MemberExpr>(E);
- if (ME->isArrow())
- break;
auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
if (!FD || FD->getType()->isReferenceType())
break;
- auto P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
+ Optional<std::pair<CharUnits, CharUnits>> P;
+ if (ME->isArrow())
+ P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx);
+ else
+ P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
if (!P)
break;
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent());
@@ -13629,6 +13631,11 @@ static getBaseAlignmentAndOffsetFromPtr(const Expr *E, ASTContext &Ctx) {
}
break;
}
+ case Stmt::CXXThisExprClass: {
+ auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl();
+ CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment();
+ return std::make_pair(Alignment, CharUnits::Zero());
+ }
case Stmt::UnaryOperatorClass: {
auto *UO = cast<UnaryOperator>(E);
if (UO->getOpcode() == UO_AddrOf)
diff --git a/clang/test/SemaCXX/warn-cast-align.cpp b/clang/test/SemaCXX/warn-cast-align.cpp
index 53cd75fb1405..1e84ba9cd67a 100644
--- a/clang/test/SemaCXX/warn-cast-align.cpp
+++ b/clang/test/SemaCXX/warn-cast-align.cpp
@@ -44,9 +44,16 @@ void test1(void *P) {
c = IntPtr(P);
}
+struct __attribute__((aligned(16))) AlignedS {
+ char m[16];
+};
+
struct __attribute__((aligned(16))) A {
char m0[16];
char m1[16];
+ AlignedS *getAlignedS() {
+ return (AlignedS *)m1;
+ }
};
struct B0 {
@@ -92,6 +99,9 @@ struct __attribute__((aligned(16))) D4 : virtual D2 {
struct D5 : virtual D0 {
char m0[16];
+ AlignedS *get() {
+ return (AlignedS *)m0; // expected-warning {{cast from 'char *' to 'AlignedS *'}}
+ }
};
struct D6 : virtual D5 {
More information about the cfe-commits
mailing list