[clang] a707da4 - Clang crashed while checking for deletion of copy and move ctors

Vy Nguyen via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 7 11:40:59 PDT 2020


Author: Vy Nguyen
Date: 2020-07-07T14:40:37-04:00
New Revision: a707da4728dea51c1446cf582a46fb271f3969c3

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

LOG: Clang crashed while checking for deletion of copy and move ctors

    Crash:
       @     0x559d129463fc  clang::CXXRecordDecl::defaultedCopyConstructorIsDeleted()
        @     0x559d1288d3e5  clang::Sema::checkIllFormedTrivialABIStruct()::$_7::operator()()
        @     0x559d12884c34  clang::Sema::checkIllFormedTrivialABIStruct()
        @     0x559d1288412e  clang::Sema::CheckCompletedCXXClass()
        @     0x559d1288d843  clang::Sema::ActOnFinishCXXMemberSpecification()
        @     0x559d12020109  clang::Parser::ParseCXXMemberSpecification()
        @     0x559d1201e80c  clang::Parser::ParseClassSpecifier()
        @     0x559d1204e807  clang::Parser::ParseDeclarationSpecifiers()
        @     0x559d120e9aa9  clang::Parser::ParseSingleDeclarationAfterTemplate()
        @     0x559d120e8f21  clang::Parser::ParseTemplateDeclarationOrSpecialization()
        @     0x559d120e8886  clang::Parser::ParseDeclarationStartingWithTemplate()
        @     0x559d1204a1d4  clang::Parser::ParseDeclaration()
        @     0x559d12004b1d  clang::Parser::ParseExternalDeclaration()
        @     0x559d12017689  clang::Parser::ParseInnerNamespace()
        @     0x559d12017024  clang::Parser::ParseNamespace()
        @     0x559d1204a29b  clang::Parser::ParseDeclaration()
        @     0x559d12004c74  clang::Parser::ParseExternalDeclaration()

    Subscribers: cfe-commits

    Tags: #clang

    Differential Revision: https://reviews.llvm.org/D83263

Added: 
    clang/test/SemaCXX/attr-trivial-abi.cpp

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/SemaObjCXX/attr-trivial-abi.mm

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index ef555788ee0e..eb001a77518b 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9719,6 +9719,10 @@ void Sema::checkIllFormedTrivialABIStruct(CXXRecordDecl &RD) {
 
   // Ill-formed if the copy and move constructors are deleted.
   auto HasNonDeletedCopyOrMoveConstructor = [&]() {
+    // If the type is dependent, then assume it might have
+    // implicit copy or move ctor because we won't know yet at this point.
+    if (RD.isDependentType())
+      return true;
     if (RD.needsImplicitCopyConstructor() &&
         !RD.defaultedCopyConstructorIsDeleted())
       return true;

diff  --git a/clang/test/SemaCXX/attr-trivial-abi.cpp b/clang/test/SemaCXX/attr-trivial-abi.cpp
new file mode 100644
index 000000000000..334b471d2ab8
--- /dev/null
+++ b/clang/test/SemaCXX/attr-trivial-abi.cpp
@@ -0,0 +1,112 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
+
+void __attribute__((trivial_abi)) foo(); // expected-warning {{'trivial_abi' attribute only applies to classes}}
+
+// Should not crash.
+template <class>
+class __attribute__((trivial_abi)) a { a(a &&); };
+
+struct [[clang::trivial_abi]] S0 {
+  int a;
+};
+
+struct __attribute__((trivial_abi)) S1 {
+  int a;
+};
+
+struct __attribute__((trivial_abi)) S3 { // expected-warning {{'trivial_abi' cannot be applied to 'S3'}} expected-note {{is polymorphic}}
+  virtual void m();
+};
+
+struct S3_2 {
+  virtual void m();
+} __attribute__((trivial_abi)); // expected-warning {{'trivial_abi' cannot be applied to 'S3_2'}} expected-note {{is polymorphic}}
+
+struct __attribute__((trivial_abi)) S3_3 { // expected-warning {{'trivial_abi' cannot be applied to 'S3_3'}} expected-note {{has a field of a non-trivial class type}}
+  S3_3(S3_3 &&);
+  S3_2 s32;
+};
+
+// Diagnose invalid trivial_abi even when the type is templated because it has a non-trivial field.
+template <class T>
+struct __attribute__((trivial_abi)) S3_4 { // expected-warning {{'trivial_abi' cannot be applied to 'S3_4'}} expected-note {{has a field of a non-trivial class type}}
+  S3_4(S3_4 &&);
+  S3_2 s32;
+};
+
+struct S4 {
+  int a;
+};
+
+struct __attribute__((trivial_abi)) S5 : public virtual S4 { // expected-warning {{'trivial_abi' cannot be applied to 'S5'}} expected-note {{has a virtual base}}
+};
+
+struct __attribute__((trivial_abi)) S9 : public S4 {
+};
+
+struct __attribute__((trivial_abi(1))) S8 { // expected-error {{'trivial_abi' attribute takes no arguments}}
+  int a;
+};
+
+// Do not warn about deleted ctors  when 'trivial_abi' is used to annotate a template class.
+template <class T>
+struct __attribute__((trivial_abi)) S10 {
+  T p;
+};
+
+S10<int *> p1;
+
+template <class T>
+struct S14 {
+  T a;
+};
+
+template <class T>
+struct __attribute__((trivial_abi)) S15 : S14<T> {
+};
+
+S15<int> s15;
+
+template <class T>
+struct __attribute__((trivial_abi)) S16 {
+  S14<T> a;
+};
+
+S16<int> s16;
+
+template <class T>
+struct __attribute__((trivial_abi)) S17 {
+};
+
+S17<int> s17;
+
+namespace deletedCopyMoveConstructor {
+struct __attribute__((trivial_abi)) CopyMoveDeleted { // expected-warning {{'trivial_abi' cannot be applied to 'CopyMoveDeleted'}} expected-note {{copy constructors and move constructors are all deleted}}
+  CopyMoveDeleted(const CopyMoveDeleted &) = delete;
+  CopyMoveDeleted(CopyMoveDeleted &&) = delete;
+};
+
+struct __attribute__((trivial_abi)) S18 { // expected-warning {{'trivial_abi' cannot be applied to 'S18'}} expected-note {{copy constructors and move constructors are all deleted}}
+  CopyMoveDeleted a;
+};
+
+struct __attribute__((trivial_abi)) CopyDeleted {
+  CopyDeleted(const CopyDeleted &) = delete;
+  CopyDeleted(CopyDeleted &&) = default;
+};
+
+struct __attribute__((trivial_abi)) MoveDeleted {
+  MoveDeleted(const MoveDeleted &) = default;
+  MoveDeleted(MoveDeleted &&) = delete;
+};
+
+struct __attribute__((trivial_abi)) S19 { // expected-warning {{'trivial_abi' cannot be applied to 'S19'}} expected-note {{copy constructors and move constructors are all deleted}}
+  CopyDeleted a;
+  MoveDeleted b;
+};
+
+// This is fine since the move constructor isn't deleted.
+struct __attribute__((trivial_abi)) S20 {
+  int &&a; // a member of rvalue reference type deletes the copy constructor.
+};
+} // namespace deletedCopyMoveConstructor

diff  --git a/clang/test/SemaObjCXX/attr-trivial-abi.mm b/clang/test/SemaObjCXX/attr-trivial-abi.mm
index 537c1390a54a..87b79c14d07a 100644
--- a/clang/test/SemaObjCXX/attr-trivial-abi.mm
+++ b/clang/test/SemaObjCXX/attr-trivial-abi.mm
@@ -57,7 +57,7 @@ struct __attribute__((trivial_abi(1))) S8 { // expected-error {{'trivial_abi' at
 };
 
 // Do not warn when 'trivial_abi' is used to annotate a template class.
-template<class T>
+template <class T>
 struct __attribute__((trivial_abi)) S10 {
   T p;
 };
@@ -76,21 +76,27 @@ struct __attribute__((trivial_abi)) S10<id> { // expected-warning {{'trivial_abi
   __weak id b;
 };
 
-template<class T>
+template <class T>
 struct __attribute__((trivial_abi)) S15 : S14<T> {
 };
 
 S15<int> s15;
 
-template<class T>
+template <class T>
 struct __attribute__((trivial_abi)) S16 {
   S14<T> a;
 };
 
 S16<int> s16;
 
-template<class T>
+template <class T>
 struct __attribute__((trivial_abi)) S17 { // expected-warning {{'trivial_abi' cannot be applied to 'S17'}} expected-note {{has a __weak field}}
+  S17();
+  S17(S17 &&);
+  __weak id a;
+};
+
+struct __attribute__((trivial_abi)) S18 { // expected-warning {{'trivial_abi' cannot be applied to 'S18'}} expected-note {{has a __weak field}}
   __weak id a;
 };
 


        


More information about the cfe-commits mailing list