Skip to content

Commit d7f9e01

Browse files
committed
add review comments
1 parent 3f74c49 commit d7f9e01

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,18 +4045,18 @@ A type trait is used to check whether a type can be safely copied by memcpy.
40454045
40464046
**Description**:
40474047
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.
40524052
40534053
Objects of bitwise cloneable types can be bitwise copied by memcpy/memmove. The
40544054
Clang compiler warrants that this behavior is well defined, and won't be
40554055
broken by compiler optimizations.
40564056
40574057
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.
40604060
40614061
This builtin can be used in constant expressions.
40624062

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,8 @@ class QualType {
11261126
/// copyable types, their underlying bytes can be safely copied by memcpy or
11271127
/// memmove. Clang guarantees that the destination has the same **object**
11281128
/// representations after the copy.
1129+
///
1130+
// FIXME: each call triggers a full computation, cache the result.
11291131
bool isBitwiseCloneableType(const ASTContext &Context) const;
11301132

11311133
/// Return true if this is a trivially copyable type

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,8 +2752,7 @@ bool QualType::isBitwiseCloneableType(const ASTContext & Context) const {
27522752
return false;
27532753

27542754
for (auto *const Field : RD->fields()) {
2755-
QualType T = Context.getBaseElementType(Field->getType());
2756-
if (!T.isBitwiseCloneableType(Context))
2755+
if (!Field->getType().isBitwiseCloneableType(Context))
27572756
return false;
27582757
}
27592758
return true;

clang/test/SemaCXX/builtin-is-bitwise-cloneable.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ struct Foo { int a; };
1717
static_assert(__is_bitwise_cloneable(Foo));
1818

1919
struct DynamicClass { virtual int Foo(); };
20+
static_assert(!__is_trivially_copyable(DynamicClass));
2021
static_assert(__is_bitwise_cloneable(DynamicClass));
2122

23+
// Trivially copyable types are always bitwise cloneable.
2224
struct Bar { int& b; }; // trivially copyable
2325
static_assert(__is_trivially_copyable(Bar));
2426
static_assert(__is_bitwise_cloneable(Bar));

0 commit comments

Comments
 (0)