[PATCH] D85834: [AST] Fix a crash on calling getASTRecordLayout on an invalid RecordDecl.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 12 06:15:45 PDT 2020


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85834.285061.patch
Type: text/x-patch
Size: 1481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200812/44fbd5da/attachment.bin>


More information about the cfe-commits mailing list