[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
This commit is contained in:
Akira Hatanaka 2020-07-06 19:48:21 -07:00
parent ec04ce4623
commit 04027052a7
2 changed files with 20 additions and 3 deletions

View File

@ -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)

View File

@ -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 {