From 79ab7986b4126eb081b57f5cda60f0a9d988e472 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sun, 23 Jul 2023 23:25:27 +0800 Subject: [PATCH] add NOLINT --- .clang-tidy | 1 + paddle/ir/core/op_info_impl.cc | 25 +++++++++++++------------ paddle/ir/core/operation.cc | 24 ++++++++++++++---------- paddle/ir/core/utils.cc | 9 +++++---- paddle/ir/core/value.cc | 21 ++++++++++++--------- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 9c3f6daa4eb71..fd61e9371409b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -160,6 +160,7 @@ Checks: ' -cppcoreguidelines-no-malloc, -cppcoreguidelines-pro-type-const-cast, -cppcoreguidelines-pro-type-member-init, +cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-slicing, -hicpp-avoid-goto, -hicpp-exception-baseclass, diff --git a/paddle/ir/core/op_info_impl.cc b/paddle/ir/core/op_info_impl.cc index 0a48c98c9f85c..b63e6c6516dff 100644 --- a/paddle/ir/core/op_info_impl.cc +++ b/paddle/ir/core/op_info_impl.cc @@ -43,13 +43,14 @@ OpInfo OpInfoImpl::Create(Dialect *dialect, base_ptr += interfaces_num * sizeof(InterfaceValue); } if (traits_num > 0) { - auto p_first_trait = reinterpret_cast(base_ptr); + TypeId *p_first_trait = reinterpret_cast(base_ptr); // NOLINT memcpy(base_ptr, trait_set.data(), sizeof(TypeId) * traits_num); std::sort(p_first_trait, p_first_trait + traits_num); base_ptr += traits_num * sizeof(TypeId); } // Construct OpInfoImpl. - VLOG(6) << "Construct OpInfoImpl at " << reinterpret_cast(base_ptr) + VLOG(6) << "Construct OpInfoImpl at " + << reinterpret_cast(base_ptr) // NOLINT << " ......"; OpInfo op_info = OpInfo(new (base_ptr) OpInfoImpl(dialect, op_id, @@ -75,9 +76,9 @@ ir::IrContext *OpInfoImpl::ir_context() const { bool OpInfoImpl::HasTrait(TypeId trait_id) const { if (num_traits_ > 0) { - const TypeId *p_first_trait = - reinterpret_cast(reinterpret_cast(this) - - sizeof(ir::TypeId) * num_traits_); + const TypeId *p_first_trait = reinterpret_cast( + reinterpret_cast(this) - // NOLINT + sizeof(ir::TypeId) * num_traits_); return std::binary_search( p_first_trait, p_first_trait + num_traits_, trait_id); } @@ -87,8 +88,8 @@ bool OpInfoImpl::HasTrait(TypeId trait_id) const { bool OpInfoImpl::HasInterface(TypeId interface_id) const { if (num_interfaces_ > 0) { const InterfaceValue *p_first_interface = - reinterpret_cast( - reinterpret_cast(this) - + reinterpret_cast( // NOLINT + reinterpret_cast(this) - // NOLINT sizeof(ir::TypeId) * num_traits_ - sizeof(InterfaceValue) * num_interfaces_); return std::binary_search(p_first_interface, @@ -101,8 +102,8 @@ bool OpInfoImpl::HasInterface(TypeId interface_id) const { void *OpInfoImpl::GetInterfaceImpl(TypeId interface_id) const { if (num_interfaces_ > 0) { const InterfaceValue *p_first_interface = - reinterpret_cast( - reinterpret_cast(this) - + reinterpret_cast( // NOLINT + reinterpret_cast(this) - // NOLINT sizeof(TypeId) * num_traits_ - sizeof(InterfaceValue) * num_interfaces_); size_t left = 0, right = num_interfaces_; @@ -123,18 +124,18 @@ void *OpInfoImpl::GetInterfaceImpl(TypeId interface_id) const { void OpInfoImpl::Destroy() { VLOG(6) << "Destroy op_info impl at " << this; // (1) free interfaces - char *base_ptr = reinterpret_cast(this) - + char *base_ptr = reinterpret_cast(this) - // NOLINT sizeof(ir::TypeId) * num_traits_ - sizeof(InterfaceValue) * num_interfaces_; if (num_interfaces_ > 0) { InterfaceValue *p_interface_val = - reinterpret_cast(base_ptr); + reinterpret_cast(base_ptr); // NOLINT for (size_t i = 0; i < num_interfaces_; i++) { (p_interface_val + i)->~InterfaceValue(); } } // (2) free memeory - VLOG(6) << "Free base_ptr " << reinterpret_cast(base_ptr); + VLOG(6) << "Free base_ptr " << reinterpret_cast(base_ptr); // NOLINT free(base_ptr); } diff --git a/paddle/ir/core/operation.cc b/paddle/ir/core/operation.cc index fd0f2bf9d990d..78ac876cf7cac 100644 --- a/paddle/ir/core/operation.cc +++ b/paddle/ir/core/operation.cc @@ -64,7 +64,8 @@ Operation *Operation::Create(const std::vector &inputs, size_t base_size = result_mem_size + op_mem_size + operand_mem_size + region_mem_size; // 2. Malloc memory. - char *base_ptr = reinterpret_cast(aligned_malloc(base_size, 8)); + char *base_ptr = + reinterpret_cast(aligned_malloc(base_size, 8)); // NOLINT // 3.1. Construct OpResults. for (size_t idx = num_results; idx > 0; idx--) { if (idx > max_inline_result_num) { @@ -81,7 +82,7 @@ Operation *Operation::Create(const std::vector &inputs, Operation(attributes, op_info, num_results, num_operands, num_regions); base_ptr += sizeof(Operation); // 3.3. Construct OpOperands. - if ((reinterpret_cast(base_ptr) & 0x7) != 0) { + if ((reinterpret_cast(base_ptr) & 0x7) != 0) { // NOLINT IR_THROW("The address of OpOperandImpl must be divisible by 8."); } for (size_t idx = 0; idx < num_operands; idx++) { @@ -90,7 +91,7 @@ Operation *Operation::Create(const std::vector &inputs, } // 3.4. Construct Regions if (num_regions > 0) { - op->regions_ = reinterpret_cast(base_ptr); + op->regions_ = reinterpret_cast(base_ptr); // NOLINT for (size_t idx = 0; idx < num_regions; idx++) { new (base_ptr) Region(op); base_ptr += sizeof(Region); @@ -143,7 +144,8 @@ void Operation::Destroy() { (num_results_ - max_inline_result_num) + sizeof(detail::OpInlineResultImpl) * max_inline_result_num : sizeof(detail::OpInlineResultImpl) * num_results_; - void *aligned_ptr = reinterpret_cast(this) - result_mem_size; + void *aligned_ptr = + reinterpret_cast(this) - result_mem_size; // NOLINT VLOG(6) << "Destroy Operation [" << name() << "]: {ptr = " << aligned_ptr << ", size = " << result_mem_size << "} done."; @@ -172,17 +174,17 @@ ir::OpResult Operation::result(uint32_t index) const { uint32_t max_inline_idx = detail::OpResultImpl::GetMaxInlineResultIndex(); const char *ptr = (index > max_inline_idx) - ? reinterpret_cast(this) - + ? reinterpret_cast(this) - // NOLINT (max_inline_idx + 1) * sizeof(detail::OpInlineResultImpl) - (index - max_inline_idx) * sizeof(detail::OpOutlineResultImpl) - : reinterpret_cast(this) - + : reinterpret_cast(this) - // NOLINT (index + 1) * sizeof(detail::OpInlineResultImpl); if (index > max_inline_idx) { return ir::OpResult( - reinterpret_cast(ptr)); + reinterpret_cast(ptr)); // NOLINT } else { return ir::OpResult( - reinterpret_cast(ptr)); + reinterpret_cast(ptr)); // NOLINT } } @@ -190,9 +192,11 @@ OpOperand Operation::op_operand(uint32_t index) const { if (index >= num_operands_) { IR_THROW("index exceeds OP input range."); } - const char *ptr = reinterpret_cast(this) + sizeof(Operation) + + const char *ptr = reinterpret_cast(this) + + sizeof(Operation) + // NOLINT (index) * sizeof(detail::OpOperandImpl); - return OpOperand(reinterpret_cast(ptr)); + return OpOperand( + reinterpret_cast(ptr)); // NOLINT } Value Operation::operand(uint32_t index) const { diff --git a/paddle/ir/core/utils.cc b/paddle/ir/core/utils.cc index eec502ee10b1f..230e9e23696be 100644 --- a/paddle/ir/core/utils.cc +++ b/paddle/ir/core/utils.cc @@ -35,9 +35,10 @@ void *aligned_malloc(size_t size, size_t alignment) { if (mem == nullptr) { return nullptr; } - size_t adjust = alignment - reinterpret_cast(mem) % alignment; - void *aligned_mem = reinterpret_cast(mem) + adjust; - *(reinterpret_cast(aligned_mem) - 1) = mem; + size_t adjust = + alignment - reinterpret_cast(mem) % alignment; // NOLINT + void *aligned_mem = reinterpret_cast(mem) + adjust; // NOLINT + *(reinterpret_cast(aligned_mem) - 1) = mem; // NOLINT assert(reinterpret_cast(aligned_mem) % alignment == 0); return aligned_mem; #endif @@ -50,7 +51,7 @@ void aligned_free(void *mem_ptr) { _aligned_free(mem_ptr); #else if (mem_ptr) { - free(*(reinterpret_cast(mem_ptr) - 1)); + free(*(reinterpret_cast(mem_ptr) - 1)); // NOLINT } #endif } diff --git a/paddle/ir/core/value.cc b/paddle/ir/core/value.cc index 666be5481c418..4de7d6a52a117 100644 --- a/paddle/ir/core/value.cc +++ b/paddle/ir/core/value.cc @@ -119,7 +119,7 @@ uint32_t OpResult::GetResultIndex() const { return impl()->GetResultIndex(); } detail::OpResultImpl *OpResult::impl() const { IR_ENFORCE(impl_, "Can't use impl() interface while value is null."); - return reinterpret_cast(impl_); + return reinterpret_cast(impl_); // NOLINT } uint32_t OpResult::GetValidInlineIndex(uint32_t index) { @@ -184,10 +184,11 @@ void OpOperandImpl::RemoveFromUdChain() { OpOperandImpl::~OpOperandImpl() { RemoveFromUdChain(); } uint32_t ValueImpl::index() const { - uint32_t index = - reinterpret_cast(first_use_offseted_by_index_) & 0x07; + uint32_t index = reinterpret_cast(first_use_offseted_by_index_) & + 0x07; // NOLINT if (index < 6) return index; - return reinterpret_cast(const_cast(this)) + return reinterpret_cast( + const_cast(this)) // NOLINT ->GetResultIndex(); } @@ -196,9 +197,11 @@ std::string ValueImpl::PrintUdChain() { result << "Value[" << this << "] -> "; OpOperandImpl *tmp = first_use(); if (tmp) { - result << "OpOperand[" << reinterpret_cast(tmp) << "] -> "; + result << "OpOperand[" << reinterpret_cast(tmp) + << "] -> "; // NOLINT while (tmp->next_use() != nullptr) { - result << "OpOperand[" << reinterpret_cast(tmp->next_use()) + result << "OpOperand[" + << reinterpret_cast(tmp->next_use()) // NOLINT << "] -> "; tmp = tmp->next_use(); } @@ -218,7 +221,7 @@ ir::Operation *OpResultImpl::owner() const { // For inline result, pointer offset index to obtain the address of op. if (const auto *result = ir::dyn_cast(this)) { result += result->GetResultIndex() + 1; - return reinterpret_cast( + return reinterpret_cast( // NOLINT const_cast(result)); } // For outline result, pointer offset outline_index to obtain the address of @@ -230,9 +233,9 @@ ir::Operation *OpResultImpl::owner() const { // The offset of the maximum inline result distance op is // GetMaxInlineResultIndex. const auto *inline_result = - reinterpret_cast(outline_result); + reinterpret_cast(outline_result); // NOLINT inline_result += (GetMaxInlineResultIndex() + 1); - return reinterpret_cast( + return reinterpret_cast( // NOLINT const_cast(inline_result)); } } // namespace detail