[PATCH] D105478: [clang] Make CXXRecrdDecl invalid if it contains any undeduced fields.

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 6 06:35:14 PDT 2021


adamcz created this revision.
adamcz added a reviewer: hokein.
adamcz requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Undeduced fields in a valid record cause crashes when trying to obtain
the size and/or layout of the record.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105478

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/cxx11-crashes.cpp


Index: clang/test/SemaCXX/cxx11-crashes.cpp
===================================================================
--- clang/test/SemaCXX/cxx11-crashes.cpp
+++ clang/test/SemaCXX/cxx11-crashes.cpp
@@ -104,3 +104,20 @@
   bool baz() { return __has_nothrow_constructor(B); }
   bool qux() { return __has_nothrow_copy(B); }
 }
+
+namespace undeduced_field {
+template<class T>
+struct Foo {
+  typedef T type;
+};
+
+struct Bar {
+  Bar();
+  // The missing expression makes A undeduced.
+  static constexpr auto A = ;  // expected-error {{expected expression}}
+  Foo<decltype(A)>::type B;  // The type of B is also undeduced (wrapped in Elaborated).
+};
+
+// This used to crash when trying to get the layout of B.
+Bar x;
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -17498,8 +17498,13 @@
         Record->setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
     }
 
-    if (Record && FD->getType().isVolatileQualified())
-      Record->setHasVolatileMember(true);
+    if (Record) {
+      if (FD->getType().isVolatileQualified())
+        Record->setHasVolatileMember(true);
+      if (const auto *T = FD->getType()->getUnqualifiedDesugaredType())
+        if (T->isUndeducedType())
+          Record->setInvalidDecl();
+    }
     // Keep track of the number of named members.
     if (FD->getIdentifier())
       ++NumNamedMembers;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105478.356704.patch
Type: text/x-patch
Size: 1473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210706/df9e7ace/attachment.bin>


More information about the cfe-commits mailing list