Fix a failing assertion with -Wcast-align

When there is unknown type in a struct in code compiled with
-Wcast-align, the compiler crashes due to
clang::ASTContext::getASTRecordLayout() failing an assert.

Added check that the RecordDecl is valid before calling
getASTRecordLayout().
This commit is contained in:
Queen Dela Cruz 2021-07-07 14:00:31 -04:00 committed by Aaron Ballman
parent e81ba28313
commit 3c5721d772
2 changed files with 10 additions and 1 deletions

View File

@ -14477,7 +14477,8 @@ static getBaseAlignmentAndOffsetFromLValue(const Expr *E, ASTContext &Ctx) {
case Stmt::MemberExprClass: {
auto *ME = cast<MemberExpr>(E);
auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
if (!FD || FD->getType()->isReferenceType())
if (!FD || FD->getType()->isReferenceType() ||
FD->getParent()->isInvalidDecl())
break;
Optional<std::pair<CharUnits, CharUnits>> P;
if (ME->isArrow())

View File

@ -67,3 +67,11 @@ unsigned int func5(void);
FnTy test5(void) {
return (FnTy)&func5;
}
void test6() {
struct {
int hello;
doesnotexist world; // expected-error {{unknown type name 'doesnotexist'}}
} foo;
void **repro = (void **)&foo.hello; // expected-warning {{cast from 'int *' to 'void **' increases required alignment from 4 to 8}}
}