diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -9719,10 +9719,12 @@ // Ill-formed if the copy and move constructors are deleted. auto HasNonDeletedCopyOrMoveConstructor = [&]() { - if (RD.needsImplicitCopyConstructor() && + // Only check if the type is non-templated because Sema may not have the + // information yet. + if (!RD.isDependentType() && RD.needsImplicitCopyConstructor() && !RD.defaultedCopyConstructorIsDeleted()) return true; - if (RD.needsImplicitMoveConstructor() && + if (!RD.isDependentType() && RD.needsImplicitMoveConstructor() && !RD.defaultedMoveConstructorIsDeleted()) return true; for (const CXXConstructorDecl *CD : RD.ctors()) diff --git a/clang/test/SemaCXX/attr-trivial-abi.cpp b/clang/test/SemaCXX/attr-trivial-abi.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/attr-trivial-abi.cpp @@ -0,0 +1,104 @@ +// 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 __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; +}; + +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; +}; + +template +struct __attribute__((trivial_abi)) S10 { // expected-warning {{'trivial_abi' cannot be applied to 'S10}}' expected-note {{copy constructors and move constructors are all deleted}} + T p; +}; + +S10 p1; + +template +struct S14 { + T a; +}; + +template +struct __attribute__((trivial_abi)) S15 : S14 { // expected-warning {{'trivial_abi' cannot be applied to 'S15'}} expected-note {{copy constructors and move constructors are all deleted}} +}; + +S15 s15; + +template +struct __attribute__((trivial_abi)) S16 { // expected-warning {{'trivial_abi' cannot be applied to 'S16'}} expected-note {{copy constructors and move constructors are all deleted}} + S14 a; +}; + +S16 s16; + +template +struct __attribute__((trivial_abi)) S17 { // expected-warning {{'trivial_abi' cannot be applied to 'S17'}} expected-note {{copy constructors and move constructors are all deleted}} +}; + +S17 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 --- a/clang/test/SemaObjCXX/attr-trivial-abi.mm +++ b/clang/test/SemaObjCXX/attr-trivial-abi.mm @@ -57,8 +57,8 @@ }; // Do not warn when 'trivial_abi' is used to annotate a template class. -template -struct __attribute__((trivial_abi)) S10 { +template +struct __attribute__((trivial_abi)) S10 { // expected-warning {{'trivial_abi' cannot be applied to 'S10}}' expected-note {{copy constructors and move constructors are all deleted}} T p; }; @@ -76,21 +76,25 @@ __weak id b; }; -template -struct __attribute__((trivial_abi)) S15 : S14 { +template +struct __attribute__((trivial_abi)) S15 : S14 { // expected-warning {{'trivial_abi' cannot be applied to 'S15'}} expected-note {{copy constructors and move constructors are all deleted}} }; S15 s15; -template -struct __attribute__((trivial_abi)) S16 { +template +struct __attribute__((trivial_abi)) S16 { // expected-warning {{'trivial_abi' cannot be applied to 'S16'}} expected-note {{copy constructors and move constructors are all deleted}} S14 a; }; S16 s16; -template -struct __attribute__((trivial_abi)) S17 { // expected-warning {{'trivial_abi' cannot be applied to 'S17'}} expected-note {{has a __weak field}} +template +struct __attribute__((trivial_abi)) S17 { // expected-warning {{'trivial_abi' cannot be applied to 'S17'}} expected-note {{copy constructors and move constructors are all deleted}} + __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; };