[clang] 3c5721d - Fix a failing assertion with -Wcast-align

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 7 11:01:39 PDT 2021


Author: Queen Dela Cruz
Date: 2021-07-07T14:00:31-04:00
New Revision: 3c5721d77275d2a7bdaeeadd0b1c3864f1166110

URL: https://github.com/llvm/llvm-project/commit/3c5721d77275d2a7bdaeeadd0b1c3864f1166110
DIFF: https://github.com/llvm/llvm-project/commit/3c5721d77275d2a7bdaeeadd0b1c3864f1166110.diff

LOG: 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().

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 0c9d2010e3777..64d838d2cd74f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -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())

diff  --git a/clang/test/Sema/warn-cast-align.c b/clang/test/Sema/warn-cast-align.c
index 389c0c17d2f7d..7df71997bf3e2 100644
--- a/clang/test/Sema/warn-cast-align.c
+++ b/clang/test/Sema/warn-cast-align.c
@@ -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}}
+}


        


More information about the cfe-commits mailing list