diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -1507,6 +1507,9 @@ functionally equivalent to copying the underlying bytes and then dropping the source object on the floor. This is true of trivial types and types which were made trivially relocatable via the ``clang::trivial_abi`` attribute. +* ``__is_trivially_equality_comparable`` (Clang): Returns true if comparing two + objects of the provided type is known to be equivalent to comparing their + value representations. * ``__is_unbounded_array`` (C++, GNU, Microsoft, Embarcadero) * ``__is_union`` (C++, GNU, Microsoft, Embarcadero) * ``__is_unsigned`` (C++, Embarcadero): @@ -4928,7 +4931,7 @@ `dump` ------ Accepts either a single identifier or an expression. When a single identifier is passed, -the lookup results for the identifier are printed to `stderr`. When an expression is passed, +the lookup results for the identifier are printed to `stderr`. When an expression is passed, the AST for the expression is printed to `stderr`. The expression is an unevaluated operand, so things like overload resolution and template instantiations are performed, but the expression has no runtime effects. diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -148,6 +148,9 @@ - Clang now supports expressions in ``#pragma clang __debug dump``. - Clang now supports declaration of multi-dimensional arrays with ``__declspec(property)``. +- A new builtin type trait ``__is_trivially_equaltiy_comparable`` has been added, + which checks whether comparing two instances of a type is equivalent to + ``memcmp(&lhs, &rhs, sizeof(T)) == 0``. New Compiler Flags ------------------ @@ -282,7 +285,7 @@ - Fix crash when handling nested immediate invocations in initializers of global variables. (`#58207 `_) -- Fix crash when generating code coverage information for `PseudoObjectExpr` in +- Fix crash when generating code coverage information for `PseudoObjectExpr` in Clang AST. (`#45481 `_) - Fix the assertion hit when a template consteval function appears in a nested diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -2492,7 +2492,9 @@ /// Return true if the specified type has unique object representations /// according to (C++17 [meta.unary.prop]p9) - bool hasUniqueObjectRepresentations(QualType Ty) const; + bool + hasUniqueObjectRepresentations(QualType Ty, + bool CheckIfTriviallyCopyable = true) const; //===--------------------------------------------------------------------===// // Type Operators diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -899,6 +899,9 @@ /// Return true if this is a trivially relocatable type. bool isTriviallyRelocatableType(const ASTContext &Context) const; + /// Return true if this is a trivially equality comparable type. + bool isTriviallyEqualityComparableType(const ASTContext &Context) const; + /// Returns true if it is a class and it might be dynamic. bool mayBeDynamicClass() const; diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def --- a/clang/include/clang/Basic/TokenKinds.def +++ b/clang/include/clang/Basic/TokenKinds.def @@ -519,6 +519,7 @@ // Clang-only C++ Type Traits TYPE_TRAIT_1(__is_trivially_relocatable, IsTriviallyRelocatable, KEYCXX) +TYPE_TRAIT_1(__is_trivially_equality_comparable, IsTriviallyEqualityComparable, KEYCXX) TYPE_TRAIT_1(__is_bounded_array, IsBoundedArray, KEYCXX) TYPE_TRAIT_1(__is_unbounded_array, IsUnboundedArray, KEYCXX) TYPE_TRAIT_1(__is_nullptr, IsNullPointer, KEYCXX) diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -2668,12 +2668,14 @@ } static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, - const RecordDecl *RD) { + const RecordDecl *RD, + bool CheckIfTriviallyCopyable) { assert(RD->isUnion() && "Must be union type"); CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); for (const auto *Field : RD->fields()) { - if (!Context.hasUniqueObjectRepresentations(Field->getType())) + if (!Context.hasUniqueObjectRepresentations(Field->getType(), + CheckIfTriviallyCopyable)) return false; CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); if (FieldSize != UnionSize) @@ -2696,21 +2698,25 @@ static std::optional structHasUniqueObjectRepresentations(const ASTContext &Context, - const RecordDecl *RD); + const RecordDecl *RD, + bool CheckIfTriviallyCopyable); static std::optional -getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context) { +getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context, + bool CheckIfTriviallyCopyable) { if (Field->getType()->isRecordType()) { const RecordDecl *RD = Field->getType()->getAsRecordDecl(); if (!RD->isUnion()) - return structHasUniqueObjectRepresentations(Context, RD); + return structHasUniqueObjectRepresentations(Context, RD, + CheckIfTriviallyCopyable); } // A _BitInt type may not be unique if it has padding bits // but if it is a bitfield the padding bits are not used. bool IsBitIntType = Field->getType()->isBitIntType(); if (!Field->getType()->isReferenceType() && !IsBitIntType && - !Context.hasUniqueObjectRepresentations(Field->getType())) + !Context.hasUniqueObjectRepresentations(Field->getType(), + CheckIfTriviallyCopyable)) return std::nullopt; int64_t FieldSizeInBits = @@ -2730,25 +2736,28 @@ return std::nullopt; } FieldSizeInBits = BitfieldSize; - } else if (IsBitIntType && - !Context.hasUniqueObjectRepresentations(Field->getType())) { + } else if (IsBitIntType && !Context.hasUniqueObjectRepresentations( + Field->getType(), CheckIfTriviallyCopyable)) { return std::nullopt; } return FieldSizeInBits; } static std::optional -getSubobjectSizeInBits(const CXXRecordDecl *RD, const ASTContext &Context) { - return structHasUniqueObjectRepresentations(Context, RD); +getSubobjectSizeInBits(const CXXRecordDecl *RD, const ASTContext &Context, + bool CheckIfTriviallyCopyable) { + return structHasUniqueObjectRepresentations(Context, RD, + CheckIfTriviallyCopyable); } template static std::optional structSubobjectsHaveUniqueObjectRepresentations( const RangeT &Subobjects, int64_t CurOffsetInBits, - const ASTContext &Context, const clang::ASTRecordLayout &Layout) { + const ASTContext &Context, const clang::ASTRecordLayout &Layout, + bool CheckIfTriviallyCopyable) { for (const auto *Subobject : Subobjects) { std::optional SizeInBits = - getSubobjectSizeInBits(Subobject, Context); + getSubobjectSizeInBits(Subobject, Context, CheckIfTriviallyCopyable); if (!SizeInBits) return std::nullopt; if (*SizeInBits != 0) { @@ -2763,7 +2772,8 @@ static std::optional structHasUniqueObjectRepresentations(const ASTContext &Context, - const RecordDecl *RD) { + const RecordDecl *RD, + bool CheckIfTriviallyCopyable) { assert(!RD->isUnion() && "Must be struct/class type"); const auto &Layout = Context.getASTRecordLayout(RD); @@ -2784,8 +2794,8 @@ }); std::optional OffsetAfterBases = - structSubobjectsHaveUniqueObjectRepresentations(Bases, CurOffsetInBits, - Context, Layout); + structSubobjectsHaveUniqueObjectRepresentations( + Bases, CurOffsetInBits, Context, Layout, CheckIfTriviallyCopyable); if (!OffsetAfterBases) return std::nullopt; CurOffsetInBits = *OffsetAfterBases; @@ -2793,7 +2803,8 @@ std::optional OffsetAfterFields = structSubobjectsHaveUniqueObjectRepresentations( - RD->fields(), CurOffsetInBits, Context, Layout); + RD->fields(), CurOffsetInBits, Context, Layout, + CheckIfTriviallyCopyable); if (!OffsetAfterFields) return std::nullopt; CurOffsetInBits = *OffsetAfterFields; @@ -2801,7 +2812,8 @@ return CurOffsetInBits; } -bool ASTContext::hasUniqueObjectRepresentations(QualType Ty) const { +bool ASTContext::hasUniqueObjectRepresentations( + QualType Ty, bool CheckIfTriviallyCopyable) const { // C++17 [meta.unary.prop]: // The predicate condition for a template specialization // has_unique_object_representations shall be @@ -2823,10 +2835,11 @@ // Arrays are unique only if their element type is unique. if (Ty->isArrayType()) - return hasUniqueObjectRepresentations(getBaseElementType(Ty)); + return hasUniqueObjectRepresentations(getBaseElementType(Ty), + CheckIfTriviallyCopyable); // (9.1) - T is trivially copyable... - if (!Ty.isTriviallyCopyableType(*this)) + if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this)) return false; // All integrals and enums are unique. @@ -2854,10 +2867,11 @@ return false; if (Record->isUnion()) - return unionHasUniqueObjectRepresentations(*this, Record); + return unionHasUniqueObjectRepresentations(*this, Record, + CheckIfTriviallyCopyable); - std::optional StructSize = - structHasUniqueObjectRepresentations(*this, Record); + std::optional StructSize = structHasUniqueObjectRepresentations( + *this, Record, CheckIfTriviallyCopyable); return StructSize && *StructSize == static_cast(getTypeSize(Ty)); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -2594,6 +2594,50 @@ } } +static bool +HasNonDeletedDefaultedEqualityComparison(const CXXRecordDecl *Decl) { + if (Decl->isUnion()) + return false; + + if (llvm::none_of(Decl->methods(), [](const CXXMethodDecl *MemberFunction) { + return MemberFunction->isOverloadedOperator() && + MemberFunction->getOverloadedOperator() == + OverloadedOperatorKind::OO_EqualEqual && + MemberFunction->isDefaulted(); + })) + return false; + + return llvm::all_of(Decl->bases(), + [](const CXXBaseSpecifier &BS) { + if (const auto *RD = BS.getType()->getAsCXXRecordDecl()) + HasNonDeletedDefaultedEqualityComparison(RD); + return true; + }) && + llvm::all_of(Decl->fields(), [](const FieldDecl *FD) { + auto Type = FD->getType(); + if (Type->isReferenceType() || Type->isEnumeralType()) + return false; + if (const auto *RD = Type->getAsCXXRecordDecl()) + return HasNonDeletedDefaultedEqualityComparison(RD); + return true; + }); +} + +bool QualType::isTriviallyEqualityComparableType( + const ASTContext &Context) const { + QualType CanonicalType = getCanonicalType(); + if (CanonicalType->isIncompleteType() || CanonicalType->isDependentType() || + CanonicalType->isEnumeralType()) + return false; + + if (const auto *RD = CanonicalType->getAsCXXRecordDecl()) { + if (!HasNonDeletedDefaultedEqualityComparison(RD)) + return false; + } + + return Context.hasUniqueObjectRepresentations(CanonicalType); +} + bool QualType::isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const { return !Context.getLangOpts().ObjCAutoRefCount && Context.getLangOpts().ObjCWeak && diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -4876,8 +4876,10 @@ case UTT_IsStandardLayout: case UTT_IsPOD: case UTT_IsLiteral: - // By analogy, is_trivially_relocatable imposes the same constraints. + // By analogy, is_trivially_relocatable and is_trivially_equality_comparable + // impose the same constraints. case UTT_IsTriviallyRelocatable: + case UTT_IsTriviallyEqualityComparable: case UTT_CanPassInRegs: // Per the GCC type traits documentation, T shall be a complete type, cv void, // or an array of unknown bound. But GCC actually imposes the same constraints @@ -5372,6 +5374,8 @@ return RD->canPassInRegisters(); Self.Diag(KeyLoc, diag::err_builtin_pass_in_regs_non_class) << T; return false; + case UTT_IsTriviallyEqualityComparable: + return T.isTriviallyEqualityComparableType(C); } } diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp --- a/clang/test/SemaCXX/type-traits.cpp +++ b/clang/test/SemaCXX/type-traits.cpp @@ -1,6 +1,7 @@ -// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++11 -fblocks -Wno-deprecated-builtins %s -// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++14 -fblocks -Wno-deprecated-builtins %s -// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++1z -fblocks -Wno-deprecated-builtins %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++11 -fblocks -Wno-deprecated-builtins -Wno-defaulted-function-deleted %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++14 -fblocks -Wno-deprecated-builtins -Wno-defaulted-function-deleted %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++17 -fblocks -Wno-deprecated-builtins -Wno-defaulted-function-deleted %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=gnu++20 -fblocks -Wno-deprecated-builtins -Wno-defaulted-function-deleted %s #define T(b) (b) ? 1 : -1 #define F(b) (b) ? -1 : 1 @@ -570,7 +571,11 @@ static_assert(__is_aggregate(DerivesAr), ""); static_assert(__is_aggregate(DerivesArNB), ""); static_assert(!__is_aggregate(HasCons), ""); +#if __cplusplus >= 202002L + static_assert(!__is_aggregate(HasDefaultCons), ""); +#else static_assert(__is_aggregate(HasDefaultCons), ""); +#endif static_assert(!__is_aggregate(HasExplicitDefaultCons), ""); static_assert(!__is_aggregate(HasInheritedCons), ""); static_assert(__is_aggregate(HasNoInheritedCons) == TrueAfterCpp14, ""); @@ -3092,6 +3097,168 @@ } // namespace is_trivially_relocatable +namespace is_trivially_equality_comparable { +struct ForwardDeclared; // expected-note {{forward declaration of 'is_trivially_equality_comparable::ForwardDeclared'}} +static_assert(!__is_trivially_equality_comparable(ForwardDeclared), ""); // expected-error {{incomplete type 'ForwardDeclared' used in type trait expression}} + +static_assert(!__is_trivially_equality_comparable(void), ""); +static_assert(__is_trivially_equality_comparable(int), ""); +static_assert(!__is_trivially_equality_comparable(int[]), ""); +static_assert(__is_trivially_equality_comparable(int[3]), ""); +static_assert(!__is_trivially_equality_comparable(float), ""); +static_assert(!__is_trivially_equality_comparable(double), ""); +static_assert(!__is_trivially_equality_comparable(long double), ""); + +struct TriviallyEqualityComparableNoDefaultedComparator { + int i; + int j; +}; +static_assert(!__is_trivially_equality_comparable(TriviallyEqualityComparableNoDefaultedComparator), ""); + +#if __cplusplus >= 202002L + +struct TriviallyEqualityComparable { + int i; + int j; + + void func(); + bool operator==(int) const { return false; } + + bool operator==(const TriviallyEqualityComparable&) const = default; +}; +static_assert(__is_trivially_equality_comparable(TriviallyEqualityComparable), ""); + +struct NotTriviallyEqualityComparableHasPadding { + short i; + int j; + + bool operator==(const NotTriviallyEqualityComparableHasPadding&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableHasPadding), ""); + +struct NotTriviallyEqualityComparableHasFloat { + float i; + int j; + + bool operator==(const NotTriviallyEqualityComparableHasFloat&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableHasFloat), ""); + +struct NotTriviallyEqualityComparableHasTailPadding { + int i; + char j; + + bool operator==(const NotTriviallyEqualityComparableHasTailPadding&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableHasTailPadding), ""); + +struct NotTriviallyEqualityComparableBase : NotTriviallyEqualityComparableHasTailPadding { + char j; + + bool operator==(const NotTriviallyEqualityComparableBase&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableBase), ""); + +class TriviallyEqualityComparablePaddedOutBase { + int i; + char c; + +public: + bool operator==(const TriviallyEqualityComparablePaddedOutBase&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(TriviallyEqualityComparablePaddedOutBase), ""); + +struct TriviallyEqualityComparablePaddedOut : TriviallyEqualityComparablePaddedOutBase { + char j[3]; + + bool operator==(const TriviallyEqualityComparablePaddedOut&) const = default; +}; +static_assert(__is_trivially_equality_comparable(TriviallyEqualityComparablePaddedOut), ""); + +struct TriviallyEqualityComparable1 { + char i; + + bool operator==(const TriviallyEqualityComparable1&) const = default; +}; +static_assert(__is_trivially_equality_comparable(TriviallyEqualityComparable1)); + +struct TriviallyEqualityComparable2 { + int i; + + bool operator==(const TriviallyEqualityComparable2&) const = default; +}; +static_assert(__is_trivially_equality_comparable(TriviallyEqualityComparable2)); + +struct NotTriviallyEqualityComparableTriviallyEqualityComparableBases + : TriviallyEqualityComparable1, TriviallyEqualityComparable2 { + bool operator==(const NotTriviallyEqualityComparableTriviallyEqualityComparableBases&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableTriviallyEqualityComparableBases)); + +struct NotTriviallyEqualityComparableBitfield { + int i : 1; + + bool operator==(const NotTriviallyEqualityComparableBitfield&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableBitfield)); + +// TODO: This is trivially equality comparable +struct NotTriviallyEqualityComparableBitfieldFilled { + char i : __CHAR_BIT__; + + bool operator==(const NotTriviallyEqualityComparableBitfieldFilled&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableBitfield)); + +union U { + int i; + + bool operator==(const U&) const = default; +}; + +struct NotTriviallyEqualityComparableImplicitlyDeletedOperatorByUnion { + U u; + + bool operator==(const NotTriviallyEqualityComparableImplicitlyDeletedOperatorByUnion&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableImplicitlyDeletedOperatorByUnion)); + +struct NotTriviallyEqualityComparableExplicitlyDeleted { + int i; + + bool operator==(const NotTriviallyEqualityComparableExplicitlyDeleted&) const = delete; +}; + +struct NotTriviallyEqualityComparableImplicitlyDeletedOperatorByStruct { + NotTriviallyEqualityComparableExplicitlyDeleted u; + + bool operator==(const NotTriviallyEqualityComparableImplicitlyDeletedOperatorByStruct&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableImplicitlyDeletedOperatorByStruct)); + +struct NotTriviallyEqualityComparableHasReferenceMember { + int& i; + + bool operator==(const NotTriviallyEqualityComparableHasReferenceMember&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableHasReferenceMember)); + +enum E { + a, + b +}; +bool operator==(E, E) { return false; } +static_assert(!__is_trivially_equality_comparable(E)); + +struct NotTriviallyEqualityComparableHasEnum { + E e; + bool operator==(const NotTriviallyEqualityComparableHasEnum&) const = default; +}; +static_assert(!__is_trivially_equality_comparable(NotTriviallyEqualityComparableHasEnum)); + +#endif // __cplusplus >= 202002L +}; + namespace can_pass_in_regs { struct A { };