[clang-tools-extra] [clang-tidy] Add `modernize-use-bit-cast` check (PR #189962)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 1 09:09:59 PDT 2026
================
@@ -0,0 +1,305 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "../utils/Matchers.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"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+static const Expr *stripMemcpyArgument(const Expr *ExprNode) {
+ ExprNode = ExprNode->IgnoreParenImpCasts();
+ while (const auto *Cast = dyn_cast<ExplicitCastExpr>(ExprNode))
+ ExprNode = Cast->getSubExpr()->IgnoreParenImpCasts();
+ return ExprNode;
+}
+
+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 = stripMemcpyArgument(ExprNode);
+ 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 isSupportedMemcpyArgType(QualType Type, const ASTContext &Context,
+ bool RequireMutable) {
+ if (Type.isNull())
+ return false;
+
+ const QualType CanonicalType = Type.getCanonicalType().getNonReferenceType();
+ if (CanonicalType.isNull() || CanonicalType->isDependentType() ||
+ CanonicalType->isIncompleteType() ||
----------------
serge-sans-paille wrote:
Nor DependentType, based on the implementation: https://clang.llvm.org/doxygen/Type_8cpp_source.html#l02863
I don't see why array types are not candidates.
https://github.com/llvm/llvm-project/pull/189962
More information about the cfe-commits
mailing list