[clang] [clang] Implement a bitwise_copyable builtin type trait. (PR #86512)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue May 14 09:30:11 PDT 2024


================
@@ -2718,6 +2718,36 @@ bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
                                      /*IsCopyConstructible=*/false);
 }
 
+bool QualType::isBitwiseCloneableType(const ASTContext & Context) const {
+  QualType CanonicalType = getCanonicalType();
+  if (CanonicalType->isIncompleteType() || CanonicalType->isDependentType())
+    return false;
+  // Trivially copyable types are bitwise clonable, e.g. scalar types.
+  if (CanonicalType.isTriviallyCopyableType(Context))
+    return true;
+
+  if (CanonicalType->isArrayType())
+    return Context.getBaseElementType(CanonicalType)
+        .isBitwiseCloneableType(Context);
+
+  if (const auto *RD = CanonicalType->getAsCXXRecordDecl()) {
+    for (auto Base : RD->bases())
+      if (!Base.getType().isBitwiseCloneableType(Context))
----------------
ilya-biryukov wrote:

I wanted to note that because we aren't caching results of the computations for classes, the running time may end up expontential for some examples.

A synthetic examples would be:
```cpp
struct F1 { /*...*/ };
struct F2 : F1 {}
struct F3 : F1, F2 {}
struct F4 : F2, F3 {}
...
```

I don't think this would matter much in practice, but at least leaving a FIXME for the future would be useful.

https://github.com/llvm/llvm-project/pull/86512


More information about the cfe-commits mailing list