hokein created this revision.
hokein added reviewers: sammccall, rsmith.
Herald added a project: clang.
hokein requested review of this revision.

The getASTRecordLayout method requires a valid decl.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85834

Files:
  clang/lib/AST/ASTContext.cpp
  clang/test/SemaCXX/alignof.cpp


Index: clang/test/SemaCXX/alignof.cpp
===================================================================
--- clang/test/SemaCXX/alignof.cpp
+++ clang/test/SemaCXX/alignof.cpp
@@ -102,3 +102,11 @@
 template <typename>
 using template_alias = aligned_int;
 static_assert(alignof(template_alias<void>) == 16, "Expected alignment of 16" 
);
+
+namespace nocrash {
+struct ForwardDecl; // expected-note {{forward declaration of}}
+struct S {
+  ForwardDecl a; // expected-error {{field has incomplete type}}
+};
+static_assert(__alignof__(S), "");
+}
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2460,11 +2460,13 @@
     if (TI.AlignIsRequired)
       return ABIAlign;
 
-    unsigned PreferredAlign = static_cast<unsigned>(
-        toBits(getASTRecordLayout(RT->getDecl()).PreferredAlignment));
-    assert(PreferredAlign >= ABIAlign &&
-           "PreferredAlign should be at least as large as ABIAlign.");
-    return PreferredAlign;
+    if (!RT->getDecl()->isInvalidDecl()) {
+      unsigned PreferredAlign = static_cast<unsigned>(
+          toBits(getASTRecordLayout(RT->getDecl()).PreferredAlignment));
+      assert(PreferredAlign >= ABIAlign &&
+             "PreferredAlign should be at least as large as ABIAlign.");
+      return PreferredAlign;
+    }
   }
 
   // Double (and, for targets supporting AIX `power` alignment, long double) 
and


Index: clang/test/SemaCXX/alignof.cpp
===================================================================
--- clang/test/SemaCXX/alignof.cpp
+++ clang/test/SemaCXX/alignof.cpp
@@ -102,3 +102,11 @@
 template <typename>
 using template_alias = aligned_int;
 static_assert(alignof(template_alias<void>) == 16, "Expected alignment of 16" );
+
+namespace nocrash {
+struct ForwardDecl; // expected-note {{forward declaration of}}
+struct S {
+  ForwardDecl a; // expected-error {{field has incomplete type}}
+};
+static_assert(__alignof__(S), "");
+}
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2460,11 +2460,13 @@
     if (TI.AlignIsRequired)
       return ABIAlign;
 
-    unsigned PreferredAlign = static_cast<unsigned>(
-        toBits(getASTRecordLayout(RT->getDecl()).PreferredAlignment));
-    assert(PreferredAlign >= ABIAlign &&
-           "PreferredAlign should be at least as large as ABIAlign.");
-    return PreferredAlign;
+    if (!RT->getDecl()->isInvalidDecl()) {
+      unsigned PreferredAlign = static_cast<unsigned>(
+          toBits(getASTRecordLayout(RT->getDecl()).PreferredAlignment));
+      assert(PreferredAlign >= ABIAlign &&
+             "PreferredAlign should be at least as large as ABIAlign.");
+      return PreferredAlign;
+    }
   }
 
   // Double (and, for targets supporting AIX `power` alignment, long double) and
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to