[clang-tools-extra] [clang-tidy] Add `modernize-use-bit-cast` check (PR #189962)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Sun May 3 12:39:43 PDT 2026
================
@@ -0,0 +1,279 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseBitCastCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/Twine.h"
+#include <cassert>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+static bool isSupportedMemcpyObjectExpr(const Expr *ExprNode) {
+ ExprNode = ExprNode->IgnoreParenImpCasts();
+
+ if (isa<DeclRefExpr>(ExprNode))
+ return true;
+
+ const auto *Member = dyn_cast<MemberExpr>(ExprNode);
+ if (!Member || !isa<FieldDecl>(Member->getMemberDecl()))
+ if (const auto *MemberPointer = dyn_cast<BinaryOperator>(ExprNode))
+ if (MemberPointer->getOpcode() == BO_PtrMemD ||
+ MemberPointer->getOpcode() == BO_PtrMemI)
+ return isSupportedMemcpyObjectExpr(MemberPointer->getLHS());
+
+ return Member && isSupportedMemcpyObjectExpr(Member->getBase());
+}
+
+static const Expr *extractMemcpyObjectExpr(const Expr *ExprNode) {
+ ExprNode = ExprNode->IgnoreParenCasts();
+ const auto *AddressOf = dyn_cast<UnaryOperator>(ExprNode);
+ if (!AddressOf || AddressOf->getOpcode() != UO_AddrOf)
+ return nullptr;
+
+ const Expr *ObjectExpr = AddressOf->getSubExpr()->IgnoreParenImpCasts();
+ return isSupportedMemcpyObjectExpr(ObjectExpr) ? ObjectExpr : nullptr;
+}
+
+static bool isBitCastableMemcpyObjectType(QualType Type,
+ const ASTContext &Context) {
+ Type = Type.getCanonicalType().getNonReferenceType();
+ return !Type.isNull() && !Type.isVolatileQualified() &&
+ !Type->isAnyPointerType() && !Type->isFunctionType() &&
+ Type.isTriviallyCopyableType(Context) &&
----------------
unterumarmung wrote:
I kept both because neither predicate is a substitute for the other.
`isTriviallyCopyableType()` is the C++ requirement for `std::bit_cast`: both `To` and `From` must be trivially copyable. `isBitwiseCloneableType()` is a Clang-specific byte-copy safety predicate for memcpy/memmove; for records it checks fields/bases recursively and rejects Clang-known unsafe byte-copy cases, but it does not itself enforce C++ trivial copyability.
For example, if we removed `isTriviallyCopyableType()`, a non-trivially-copyable record with only simple data members could still be considered bitwise-cloneable by Clang’s predicate:
```cpp
struct NonTrivial {
NonTrivial(const NonTrivial &);
int value;
};
unsigned int dst;
NonTrivial src;
std::memcpy(&dst, &src, sizeof(src));
```
Rewriting this to `std::bit_cast<unsigned int>(src)` would be a false positive because `std::bit_cast` is ill-formed when From is not trivially copyable.
On the other hand, if we removed `isBitwiseCloneableType()` and kept only `isTriviallyCopyableType()`, we would lose Clang’s extra byte-copy safety filter. For example, on pointer-auth targets Clang’s bitwise-cloneable check rejects types that contain address-discriminated pointer-auth data transitively, even when the top-level trivial-copyability check would otherwise not model that byte-copy hazard. That would make the check more permissive than the memcpy safety predicate it is trying to recognize.
So the intended condition is: `std::bit_cast` must be well-formed (`isTriviallyCopyableType()`), and the original byte-copy pattern should also pass Clang’s memcpy/memmove safety predicate (`isBitwiseCloneableType()`). That said, if we decide one of these checks is unnecessary, `isBitwiseCloneableType()` is probably the better candidate to drop: `isTriviallyCopyableType()` is required for the replacement to be valid C++, while `isBitwiseCloneableType()` is an additional conservativeness filter.
https://github.com/llvm/llvm-project/pull/189962
More information about the cfe-commits
mailing list