[Objective-C] Fix __weak type traits with -fobjc-weak

Summary: Similar to ARC, in ObjCWeak Objective-C object pointers qualified with a weak lifetime are not POD or trivial types. Update the type trait code to reflect this. Copy and adapt the arc-type-traits.mm test case to verify correctness.

Reviewers: rsmith, doug.gregor, rjmccall

Reviewed By: rjmccall

Subscribers: cfe-commits

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

llvm-svn: 299010
This commit is contained in:
Brian Kelley 2017-03-29 17:40:35 +00:00
parent ce539b54e3
commit 93c640b986
3 changed files with 224 additions and 87 deletions

View File

@ -2023,20 +2023,8 @@ bool QualType::isCXX98PODType(const ASTContext &Context) const {
if ((*this)->isIncompleteType())
return false;
if (Context.getLangOpts().ObjCAutoRefCount) {
switch (getObjCLifetime()) {
case Qualifiers::OCL_ExplicitNone:
return true;
case Qualifiers::OCL_Strong:
case Qualifiers::OCL_Weak:
case Qualifiers::OCL_Autoreleasing:
return false;
case Qualifiers::OCL_None:
break;
}
}
if (hasNonTrivialObjCLifetime())
return false;
QualType CanonicalType = getTypePtr()->CanonicalType;
switch (CanonicalType->getTypeClass()) {
@ -2085,22 +2073,8 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
if ((*this)->isIncompleteType())
return false;
if (Context.getLangOpts().ObjCAutoRefCount) {
switch (getObjCLifetime()) {
case Qualifiers::OCL_ExplicitNone:
return true;
case Qualifiers::OCL_Strong:
case Qualifiers::OCL_Weak:
case Qualifiers::OCL_Autoreleasing:
return false;
case Qualifiers::OCL_None:
if ((*this)->isObjCLifetimeType())
return false;
break;
}
}
if (hasNonTrivialObjCLifetime())
return false;
QualType CanonicalType = getTypePtr()->CanonicalType;
if (CanonicalType->isDependentType())
@ -2137,22 +2111,8 @@ bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
if ((*this)->isArrayType())
return Context.getBaseElementType(*this).isTriviallyCopyableType(Context);
if (Context.getLangOpts().ObjCAutoRefCount) {
switch (getObjCLifetime()) {
case Qualifiers::OCL_ExplicitNone:
return true;
case Qualifiers::OCL_Strong:
case Qualifiers::OCL_Weak:
case Qualifiers::OCL_Autoreleasing:
return false;
case Qualifiers::OCL_None:
if ((*this)->isObjCLifetimeType())
return false;
break;
}
}
if (hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9
// Scalar types, trivially copyable class types, arrays of such types, and
@ -2298,20 +2258,8 @@ bool QualType::isCXX11PODType(const ASTContext &Context) const {
if (ty->isDependentType())
return false;
if (Context.getLangOpts().ObjCAutoRefCount) {
switch (getObjCLifetime()) {
case Qualifiers::OCL_ExplicitNone:
return true;
case Qualifiers::OCL_Strong:
case Qualifiers::OCL_Weak:
case Qualifiers::OCL_Autoreleasing:
return false;
case Qualifiers::OCL_None:
break;
}
}
if (hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9:
// Scalar types, POD classes, arrays of such types, and cv-qualified

View File

@ -4518,25 +4518,6 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
}
}
/// \brief Determine whether T has a non-trivial Objective-C lifetime in
/// ARC mode.
static bool hasNontrivialObjCLifetime(QualType T) {
switch (T.getObjCLifetime()) {
case Qualifiers::OCL_ExplicitNone:
return false;
case Qualifiers::OCL_Strong:
case Qualifiers::OCL_Weak:
case Qualifiers::OCL_Autoreleasing:
return true;
case Qualifiers::OCL_None:
return T->isObjCLifetimeType();
}
llvm_unreachable("Unknown ObjC lifetime qualifier");
}
static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
QualType RhsT, SourceLocation KeyLoc);
@ -4630,10 +4611,9 @@ static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc,
return S.canThrow(Result.get()) == CT_Cannot;
if (Kind == clang::TT_IsTriviallyConstructible) {
// Under Objective-C ARC, if the destination has non-trivial Objective-C
// lifetime, this is a non-trivial construction.
if (S.getLangOpts().ObjCAutoRefCount &&
hasNontrivialObjCLifetime(T.getNonReferenceType()))
// Under Objective-C ARC and Weak, if the destination has non-trivial
// Objective-C lifetime, this is a non-trivial construction.
if (T.getNonReferenceType().hasNonTrivialObjCLifetime())
return false;
// The initialization succeeded; now make sure there are no non-trivial
@ -4852,10 +4832,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
return Self.canThrow(Result.get()) == CT_Cannot;
if (BTT == BTT_IsTriviallyAssignable) {
// Under Objective-C ARC, if the destination has non-trivial Objective-C
// lifetime, this is a non-trivial assignment.
if (Self.getLangOpts().ObjCAutoRefCount &&
hasNontrivialObjCLifetime(LhsT.getNonReferenceType()))
// Under Objective-C ARC and Weak, if the destination has non-trivial
// Objective-C lifetime, this is a non-trivial assignment.
if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime())
return false;
return !Result.get()->hasNonTrivialCall(Self.Context);

View File

@ -0,0 +1,210 @@
// RUN: %clang_cc1 -fsyntax-only -fobjc-weak -fobjc-runtime-has-weak -verify -std=c++11 %s
// expected-no-diagnostics
// Check the results of the various type-trait query functions on
// lifetime-qualified types in ObjC Weak.
#define TRAIT_IS_TRUE(Trait, Type) static_assert(Trait(Type), "")
#define TRAIT_IS_FALSE(Trait, Type) static_assert(!Trait(Type), "")
#define TRAIT_IS_TRUE_2(Trait, Type1, Type2) static_assert(Trait(Type1, Type2), "")
#define TRAIT_IS_FALSE_2(Trait, Type1, Type2) static_assert(!Trait(Type1, Type2), "")
struct HasStrong { id obj; };
struct HasWeak { __weak id obj; };
struct HasUnsafeUnretained { __unsafe_unretained id obj; };
// __has_nothrow_assign
TRAIT_IS_TRUE(__has_nothrow_assign, __strong id);
TRAIT_IS_TRUE(__has_nothrow_assign, __weak id);
TRAIT_IS_TRUE(__has_nothrow_assign, __autoreleasing id);
TRAIT_IS_TRUE(__has_nothrow_assign, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_nothrow_assign, HasStrong);
TRAIT_IS_TRUE(__has_nothrow_assign, HasWeak);
TRAIT_IS_TRUE(__has_nothrow_assign, HasUnsafeUnretained);
// __has_nothrow_copy
TRAIT_IS_TRUE(__has_nothrow_copy, __strong id);
TRAIT_IS_TRUE(__has_nothrow_copy, __weak id);
TRAIT_IS_TRUE(__has_nothrow_copy, __autoreleasing id);
TRAIT_IS_TRUE(__has_nothrow_copy, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_nothrow_copy, HasStrong);
TRAIT_IS_TRUE(__has_nothrow_copy, HasWeak);
TRAIT_IS_TRUE(__has_nothrow_copy, HasUnsafeUnretained);
// __has_nothrow_constructor
TRAIT_IS_TRUE(__has_nothrow_constructor, __strong id);
TRAIT_IS_TRUE(__has_nothrow_constructor, __weak id);
TRAIT_IS_TRUE(__has_nothrow_constructor, __autoreleasing id);
TRAIT_IS_TRUE(__has_nothrow_constructor, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_nothrow_constructor, HasStrong);
TRAIT_IS_TRUE(__has_nothrow_constructor, HasWeak);
TRAIT_IS_TRUE(__has_nothrow_constructor, HasUnsafeUnretained);
// __has_trivial_assign
TRAIT_IS_TRUE(__has_trivial_assign, __strong id);
TRAIT_IS_FALSE(__has_trivial_assign, __weak id);
TRAIT_IS_TRUE(__has_trivial_assign, __autoreleasing id);
TRAIT_IS_TRUE(__has_trivial_assign, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_trivial_assign, HasStrong);
TRAIT_IS_FALSE(__has_trivial_assign, HasWeak);
TRAIT_IS_TRUE(__has_trivial_assign, HasUnsafeUnretained);
// __has_trivial_copy
TRAIT_IS_TRUE(__has_trivial_copy, __strong id);
TRAIT_IS_FALSE(__has_trivial_copy, __weak id);
TRAIT_IS_TRUE(__has_trivial_copy, __autoreleasing id);
TRAIT_IS_TRUE(__has_trivial_copy, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_trivial_copy, HasStrong);
TRAIT_IS_FALSE(__has_trivial_copy, HasWeak);
TRAIT_IS_TRUE(__has_trivial_copy, HasUnsafeUnretained);
// __has_trivial_constructor
TRAIT_IS_TRUE(__has_trivial_constructor, __strong id);
TRAIT_IS_FALSE(__has_trivial_constructor, __weak id);
TRAIT_IS_TRUE(__has_trivial_constructor, __autoreleasing id);
TRAIT_IS_TRUE(__has_trivial_constructor, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_trivial_constructor, HasStrong);
TRAIT_IS_FALSE(__has_trivial_constructor, HasWeak);
TRAIT_IS_TRUE(__has_trivial_constructor, HasUnsafeUnretained);
// __has_trivial_destructor
TRAIT_IS_TRUE(__has_trivial_destructor, __strong id);
TRAIT_IS_FALSE(__has_trivial_destructor, __weak id);
TRAIT_IS_TRUE(__has_trivial_destructor, __autoreleasing id);
TRAIT_IS_TRUE(__has_trivial_destructor, __unsafe_unretained id);
TRAIT_IS_TRUE(__has_trivial_destructor, HasStrong);
TRAIT_IS_FALSE(__has_trivial_destructor, HasWeak);
TRAIT_IS_TRUE(__has_trivial_destructor, HasUnsafeUnretained);
// __is_literal
TRAIT_IS_TRUE(__is_literal, __strong id);
TRAIT_IS_TRUE(__is_literal, __weak id);
TRAIT_IS_TRUE(__is_literal, __autoreleasing id);
TRAIT_IS_TRUE(__is_literal, __unsafe_unretained id);
// __is_literal_type
TRAIT_IS_TRUE(__is_literal_type, __strong id);
TRAIT_IS_TRUE(__is_literal_type, __weak id);
TRAIT_IS_TRUE(__is_literal_type, __autoreleasing id);
TRAIT_IS_TRUE(__is_literal_type, __unsafe_unretained id);
// __is_pod
TRAIT_IS_TRUE(__is_pod, __strong id);
TRAIT_IS_FALSE(__is_pod, __weak id);
TRAIT_IS_TRUE(__is_pod, __autoreleasing id);
TRAIT_IS_TRUE(__is_pod, __unsafe_unretained id);
TRAIT_IS_TRUE(__is_pod, HasStrong);
TRAIT_IS_FALSE(__is_pod, HasWeak);
TRAIT_IS_TRUE(__is_pod, HasUnsafeUnretained);
// __is_trivial
TRAIT_IS_TRUE(__is_trivial, __strong id);
TRAIT_IS_FALSE(__is_trivial, __weak id);
TRAIT_IS_TRUE(__is_trivial, __autoreleasing id);
TRAIT_IS_TRUE(__is_trivial, __unsafe_unretained id);
TRAIT_IS_TRUE(__is_trivial, HasStrong);
TRAIT_IS_FALSE(__is_trivial, HasWeak);
TRAIT_IS_TRUE(__is_trivial, HasUnsafeUnretained);
// __is_scalar
TRAIT_IS_TRUE(__is_scalar, __strong id);
TRAIT_IS_FALSE(__is_scalar, __weak id);
TRAIT_IS_TRUE(__is_scalar, __autoreleasing id);
TRAIT_IS_TRUE(__is_scalar, __unsafe_unretained id);
// __is_standard_layout
TRAIT_IS_TRUE(__is_standard_layout, __strong id);
TRAIT_IS_TRUE(__is_standard_layout, __weak id);
TRAIT_IS_TRUE(__is_standard_layout, __autoreleasing id);
TRAIT_IS_TRUE(__is_standard_layout, __unsafe_unretained id);
// __is_trivally_assignable
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __unsafe_unretained id&&);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __strong id);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __weak id);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __autoreleasing id);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __unsafe_unretained id);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __strong id&&);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __weak id&&);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __autoreleasing id&&);
TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, HasStrong&, HasStrong);
TRAIT_IS_TRUE_2(__is_trivially_assignable, HasStrong&, HasStrong&&);
TRAIT_IS_FALSE_2(__is_trivially_assignable, HasWeak&, HasWeak);
TRAIT_IS_FALSE_2(__is_trivially_assignable, HasWeak&, HasWeak&&);
TRAIT_IS_TRUE_2(__is_trivially_assignable, HasUnsafeUnretained&, HasUnsafeUnretained);
TRAIT_IS_TRUE_2(__is_trivially_assignable, HasUnsafeUnretained&, HasUnsafeUnretained&&);
// __is_trivally_constructible
TRAIT_IS_TRUE(__is_trivially_constructible, __strong id);
TRAIT_IS_FALSE(__is_trivially_constructible, __weak id);
TRAIT_IS_TRUE(__is_trivially_constructible, __autoreleasing id);
TRAIT_IS_TRUE(__is_trivially_constructible, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __unsafe_unretained id&&);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __strong id);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __weak id);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __autoreleasing id);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __unsafe_unretained id);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __strong id&&);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __weak id&&);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __autoreleasing id&&);
TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __strong id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __weak id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __autoreleasing id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __unsafe_unretained id);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __strong id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __weak id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __autoreleasing id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __unsafe_unretained id&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, HasStrong, HasStrong);
TRAIT_IS_TRUE_2(__is_trivially_constructible, HasStrong, HasStrong&&);
TRAIT_IS_FALSE_2(__is_trivially_constructible, HasWeak, HasWeak);
TRAIT_IS_FALSE_2(__is_trivially_constructible, HasWeak, HasWeak&&);
TRAIT_IS_TRUE_2(__is_trivially_constructible, HasUnsafeUnretained, HasUnsafeUnretained);
TRAIT_IS_TRUE_2(__is_trivially_constructible, HasUnsafeUnretained, HasUnsafeUnretained&&);