File tree 4 files changed +11
-8
lines changed
4 files changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -4045,18 +4045,18 @@ A type trait is used to check whether a type can be safely copied by memcpy.
4045
4045
4046
4046
**Description **:
4047
4047
4048
- It is common for library owners to perform memcpy / memmove on types that aren't
4049
- trivally copyable for performance reason. However, according to the C++ standard,
4050
- it is undefined bheavior to mempcy non-trivially-copyable types, even though
4051
- it may work in pratice. This builtin is designed to bridge that gap .
4048
+ This trait is similar to ` std::is_trivially_copyable `, but additionally allows
4049
+ to have user-defined constructors, virtual functions and virtual bases. It is up
4050
+ to the user code to guarantee that a bitwise copy results in non-broken object
4051
+ and that the lifetime of an object is properly started .
4052
4052
4053
4053
Objects of bitwise cloneable types can be bitwise copied by memcpy /memmove . The
4054
4054
Clang compiler warrants that this behavior is well defined, and won't be
4055
4055
broken by compiler optimizations.
4056
4056
4057
4057
After the copy, the lifetime of the new object isn't started yet (unless the
4058
- type is trivially copyable). Users must explicitly start its lifetime by the
4059
- ` __builtin_start_object_lifetime ` mechanism to avoid undefined behavior.
4058
+ type is trivially copyable). Users must ensure its lifetime is started to avoid
4059
+ undefined behavior.
4060
4060
4061
4061
This builtin can be used in constant expressions.
4062
4062
Original file line number Diff line number Diff line change @@ -1126,6 +1126,8 @@ class QualType {
1126
1126
// / copyable types, their underlying bytes can be safely copied by memcpy or
1127
1127
// / memmove. Clang guarantees that the destination has the same **object**
1128
1128
// / representations after the copy.
1129
+ // /
1130
+ // FIXME: each call triggers a full computation, cache the result.
1129
1131
bool isBitwiseCloneableType (const ASTContext &Context) const ;
1130
1132
1131
1133
// / Return true if this is a trivially copyable type
Original file line number Diff line number Diff line change @@ -2752,8 +2752,7 @@ bool QualType::isBitwiseCloneableType(const ASTContext & Context) const {
2752
2752
return false ;
2753
2753
2754
2754
for (auto *const Field : RD->fields ()) {
2755
- QualType T = Context.getBaseElementType (Field->getType ());
2756
- if (!T.isBitwiseCloneableType (Context))
2755
+ if (!Field->getType ().isBitwiseCloneableType (Context))
2757
2756
return false ;
2758
2757
}
2759
2758
return true ;
Original file line number Diff line number Diff line change @@ -17,8 +17,10 @@ struct Foo { int a; };
17
17
static_assert (__is_bitwise_cloneable(Foo));
18
18
19
19
struct DynamicClass { virtual int Foo (); };
20
+ static_assert (!__is_trivially_copyable(DynamicClass));
20
21
static_assert (__is_bitwise_cloneable(DynamicClass));
21
22
23
+ // Trivially copyable types are always bitwise cloneable.
22
24
struct Bar { int & b; }; // trivially copyable
23
25
static_assert (__is_trivially_copyable(Bar));
24
26
static_assert (__is_bitwise_cloneable(Bar));
You can’t perform that action at this time.
0 commit comments