PR47138: Don't crash if the preferred alignment of an invalid record
authorRichard Smith <richard@metafoo.co.uk>
Wed, 12 Aug 2020 19:24:08 +0000 (12:24 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Wed, 12 Aug 2020 19:31:20 +0000 (12:31 -0700)
type is requested.

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

index 4d708d5..544bbb5 100644 (file)
@@ -2457,7 +2457,7 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
     return ABIAlign;
 
   if (const auto *RT = T->getAs<RecordType>()) {
-    if (TI.AlignIsRequired)
+    if (TI.AlignIsRequired || RT->getDecl()->isInvalidDecl())
       return ABIAlign;
 
     unsigned PreferredAlign = static_cast<unsigned>(
index f285402..64986d3 100644 (file)
@@ -102,3 +102,8 @@ typedef int __attribute__((aligned(16))) aligned_int;
 template <typename>
 using template_alias = aligned_int;
 static_assert(alignof(template_alias<void>) == 16, "Expected alignment of 16" );
+
+struct PR47138 {
+  invalid_type a; // expected-error {{unknown type}}
+};
+static_assert(__alignof__(PR47138) == 1, ""); // Don't crash.