[10/N] Fixes clang-tidy warnings in c10/util/*.h (#116326)

Still a continued work for clean up c10/util/*.h
Pull Request resolved: https://github.com/pytorch/pytorch/pull/116326
Approved by: https://github.com/Skylion007
This commit is contained in:
cyy 2023-12-23 04:59:55 +00:00 committed by PyTorch MergeBot
parent 84b2a32359
commit 7663ffb673
15 changed files with 46 additions and 53 deletions

View File

@ -35,6 +35,7 @@ namespace c10 {
template <size_t Alignment, size_t Size> template <size_t Alignment, size_t Size>
struct AlignedCharArray { struct AlignedCharArray {
// NOLINTNEXTLINE(*c-arrays)
alignas(Alignment) char buffer[Size]; alignas(Alignment) char buffer[Size];
}; };
@ -142,6 +143,7 @@ template <
typename T9 = char, typename T9 = char,
typename T10 = char> typename T10 = char>
union SizerImpl { union SizerImpl {
// NOLINTNEXTLINE(*c-arrays)
char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)], char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)], arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
arr9[sizeof(T9)], arr10[sizeof(T10)]; arr9[sizeof(T9)], arr10[sizeof(T10)];

View File

@ -5,8 +5,7 @@
#include <intrin.h> #include <intrin.h>
#endif #endif
namespace c10 { namespace c10::utils {
namespace utils {
/** /**
* This is a simple bitset class with sizeof(long long int) bits. * This is a simple bitset class with sizeof(long long int) bits.
@ -114,5 +113,4 @@ inline bool operator!=(bitset lhs, bitset rhs) noexcept {
return !(lhs == rhs); return !(lhs == rhs);
} }
} // namespace utils } // namespace c10::utils
} // namespace c10

View File

@ -47,7 +47,7 @@ class once_flag {
if (init_.load(std::memory_order_relaxed)) { if (init_.load(std::memory_order_relaxed)) {
return; return;
} }
c10::guts::invoke(f, std::forward<Args>(args)...); c10::guts::invoke(std::forward<F>(f), std::forward<Args>(args)...);
init_.store(true, std::memory_order_release); init_.store(true, std::memory_order_release);
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <c10/core/TensorImpl.h> #include <c10/core/TensorImpl.h>
#include <c10/core/UndefinedTensorImpl.h>
#include <utility> #include <utility>

View File

@ -1,10 +1,11 @@
#include <c10/util/Float8_e4m3fn.h> #include <c10/util/Float8_e4m3fn.h>
#include <iostream> #include <iostream>
#include <type_traits>
namespace c10 { namespace c10 {
static_assert( static_assert(
std::is_standard_layout<Float8_e4m3fn>::value, std::is_standard_layout_v<Float8_e4m3fn>,
"c10::Float8_e4m3fn must be standard layout."); "c10::Float8_e4m3fn must be standard layout.");
std::ostream& operator<<(std::ostream& out, const Float8_e4m3fn& value) { std::ostream& operator<<(std::ostream& out, const Float8_e4m3fn& value) {

View File

@ -50,14 +50,15 @@ class function_ref<Ret(Params...)> {
template <typename Callable> template <typename Callable>
function_ref( function_ref(
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
Callable&& callable, Callable&& callable,
typename std::enable_if<!std::is_same< std::enable_if_t<
typename std::remove_reference<Callable>::type, !std::is_same_v<std::remove_reference_t<Callable>, function_ref>>* =
function_ref>::value>::type* = nullptr, nullptr,
typename std::enable_if<std::is_convertible< std::enable_if_t<std::is_convertible_v<
typename std::invoke_result_t<Callable, Params...>, typename std::invoke_result_t<Callable, Params...>,
Ret>::value>::type* = nullptr) Ret>>* = nullptr)
: callback(callback_fn<typename std::remove_reference<Callable>::type>), : callback(callback_fn<std::remove_reference_t<Callable>>),
callable(reinterpret_cast<intptr_t>(&callable)) {} callable(reinterpret_cast<intptr_t>(&callable)) {}
Ret operator()(Params... params) const { Ret operator()(Params... params) const {

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <c10/util/TypeList.h> #include <c10/util/TypeList.h>
#include <functional>
#include <type_traits> #include <type_traits>
namespace c10::guts { namespace c10::guts {
@ -207,6 +206,7 @@ constexpr auto tuple_slice(Tuple t) {
namespace detail { namespace detail {
template <class Mapper, class... Args, size_t... Indices> template <class Mapper, class... Args, size_t... Indices>
auto tuple_map( auto tuple_map(
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
std::tuple<Args...>&& tuple, std::tuple<Args...>&& tuple,
const Mapper& mapper, const Mapper& mapper,
std::index_sequence<Indices...>) { std::index_sequence<Indices...>) {

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <array> #include <array>
#include <cstddef>
#include <cstdint>
#include <type_traits> #include <type_traits>
/** Helper class for allocating temporary fixed size arrays with SBO. /** Helper class for allocating temporary fixed size arrays with SBO.
@ -16,9 +18,7 @@ namespace c10 {
template <typename T, size_t N> template <typename T, size_t N>
class SmallBuffer { class SmallBuffer {
static_assert( static_assert(std::is_trivial_v<T>, "SmallBuffer is intended for POD types");
std::is_trivial<T>::value,
"SmallBuffer is intended for POD types");
std::array<T, N> storage_; std::array<T, N> storage_;
size_t size_{}; size_t size_{};

View File

@ -201,7 +201,7 @@ struct all {
}; };
template <template <class> class Condition, class... Types> template <template <class> class Condition, class... Types>
struct all<Condition, typelist<Types...>> struct all<Condition, typelist<Types...>>
: guts::conjunction<Condition<Types>...> { : std::conjunction<Condition<Types>...> {
static_assert( static_assert(
is_type_condition<Condition>::value, is_type_condition<Condition>::value,
"In typelist::all<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member."); "In typelist::all<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
@ -223,7 +223,7 @@ struct true_for_any_type final {
}; };
template <template <class> class Condition, class... Types> template <template <class> class Condition, class... Types>
struct true_for_any_type<Condition, typelist<Types...>> final struct true_for_any_type<Condition, typelist<Types...>> final
: guts::disjunction<Condition<Types>...> { : std::disjunction<Condition<Types>...> {
static_assert( static_assert(
is_type_condition<Condition>::value, is_type_condition<Condition>::value,
"In typelist::true_for_any_type<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member."); "In typelist::true_for_any_type<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");

View File

@ -5,8 +5,7 @@
#include <cstring> #include <cstring>
#include <optional> #include <optional>
namespace c10 { namespace c10::utils {
namespace utils {
// Reads an environment variable and returns // Reads an environment variable and returns
// - optional<true>, if set equal to "1" // - optional<true>, if set equal to "1"
// - optional<false>, if set equal to "0" // - optional<false>, if set equal to "0"
@ -39,5 +38,4 @@ inline std::optional<bool> check_env(const char* name) {
} }
return std::nullopt; return std::nullopt;
} }
} // namespace utils } // namespace c10::utils
} // namespace c10

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <c10/macros/Macros.h>
#include <cstdint> #include <cstdint>
namespace c10::detail { namespace c10::detail {

View File

@ -33,6 +33,7 @@
#pragma once #pragma once
#include <c10/macros/Export.h> #include <c10/macros/Export.h>
#include <cstdint>
#include <iosfwd> #include <iosfwd>
namespace c10 { namespace c10 {

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
namespace c10 { namespace c10 {
namespace detail { namespace detail {

View File

@ -14,16 +14,12 @@
#pragma once #pragma once
#include <c10/macros/Macros.h> #include <c10/macros/Macros.h>
#include <c10/util/llvmMathExtras.h> #include <c10/util/llvmMathExtras.h>
#include <array>
#include <cassert> #include <cassert>
#include <climits> #include <climits>
#include <cstring>
#include <iterator> #include <iterator>
#include <list> #include <list>
#include <ostream>
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wshorten-64-to-32")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wshorten-64-to-32")
#endif
namespace c10 { namespace c10 {
@ -54,18 +50,12 @@ struct SparseBitVectorElement {
private: private:
// Index of Element in terms of where first bit starts. // Index of Element in terms of where first bit starts.
unsigned ElementIndex; unsigned ElementIndex;
BitWord Bits[BITWORDS_PER_ELEMENT]; std::array<BitWord, BITWORDS_PER_ELEMENT> Bits{};
SparseBitVectorElement() { SparseBitVectorElement() : ElementIndex(~0U) {}
ElementIndex = ~0U;
memset(&Bits[0], 0, sizeof(BitWord) * BITWORDS_PER_ELEMENT);
}
public: public:
explicit SparseBitVectorElement(unsigned Idx) { explicit SparseBitVectorElement(unsigned Idx) : ElementIndex(Idx) {}
ElementIndex = Idx;
memset(&Bits[0], 0, sizeof(BitWord) * BITWORDS_PER_ELEMENT);
}
// Comparison. // Comparison.
bool operator==(const SparseBitVectorElement& RHS) const { bool operator==(const SparseBitVectorElement& RHS) const {
@ -274,8 +264,10 @@ class SparseBitVector {
// 'this' is always const in this particular function and we sort out the // 'this' is always const in this particular function and we sort out the
// difference in FindLowerBound and FindLowerBoundConst. // difference in FindLowerBound and FindLowerBoundConst.
ElementListIter Begin = ElementListIter Begin =
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<SparseBitVector<ElementSize>*>(this)->Elements.begin(); const_cast<SparseBitVector<ElementSize>*>(this)->Elements.begin();
ElementListIter End = ElementListIter End =
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<SparseBitVector<ElementSize>*>(this)->Elements.end(); const_cast<SparseBitVector<ElementSize>*>(this)->Elements.end();
if (Elements.empty()) { if (Elements.empty()) {
@ -313,7 +305,7 @@ class SparseBitVector {
// than it would be, in order to be efficient. // than it would be, in order to be efficient.
class SparseBitVectorIterator { class SparseBitVectorIterator {
private: private:
bool AtEnd; bool AtEnd{false};
const SparseBitVector<ElementSize>* BitVector = nullptr; const SparseBitVector<ElementSize>* BitVector = nullptr;
@ -321,13 +313,13 @@ class SparseBitVector {
ElementListConstIter Iter; ElementListConstIter Iter;
// Current bit number inside of our bitmap. // Current bit number inside of our bitmap.
unsigned BitNumber; unsigned BitNumber{0};
// Current word number inside of our element. // Current word number inside of our element.
unsigned WordNumber; unsigned WordNumber{0};
// Current bits from the element. // Current bits from the element.
typename SparseBitVectorElement<ElementSize>::BitWord Bits; typename SparseBitVectorElement<ElementSize>::BitWord Bits{0};
// Move our iterator to the first non-zero bit in the bitmap. // Move our iterator to the first non-zero bit in the bitmap.
void AdvanceToFirstNonZero() { void AdvanceToFirstNonZero() {
@ -392,12 +384,10 @@ class SparseBitVector {
SparseBitVectorIterator( SparseBitVectorIterator(
const SparseBitVector<ElementSize>* RHS, const SparseBitVector<ElementSize>* RHS,
bool end = false) bool end = false)
: BitVector(RHS) { : AtEnd(end),
Iter = BitVector->Elements.begin(); BitVector(RHS),
BitNumber = 0; Iter(BitVector->Elements.begin()),
Bits = 0; WordNumber(~0) {
WordNumber = ~0;
AtEnd = end;
AdvanceToFirstNonZero(); AdvanceToFirstNonZero();
} }
@ -442,7 +432,7 @@ class SparseBitVector {
SparseBitVector(const SparseBitVector& RHS) SparseBitVector(const SparseBitVector& RHS)
: Elements(RHS.Elements), CurrElementIter(Elements.begin()) {} : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
SparseBitVector(SparseBitVector&& RHS) SparseBitVector(SparseBitVector&& RHS) noexcept
: Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {} : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
// Clear. // Clear.
@ -459,7 +449,7 @@ class SparseBitVector {
CurrElementIter = Elements.begin(); CurrElementIter = Elements.begin();
return *this; return *this;
} }
SparseBitVector& operator=(SparseBitVector&& RHS) { SparseBitVector& operator=(SparseBitVector&& RHS) noexcept {
Elements = std::move(RHS.Elements); Elements = std::move(RHS.Elements);
CurrElementIter = Elements.begin(); CurrElementIter = Elements.begin();
return *this; return *this;
@ -612,7 +602,7 @@ class SparseBitVector {
if (Iter1->index() > Iter2->index()) { if (Iter1->index() > Iter2->index()) {
++Iter2; ++Iter2;
} else if (Iter1->index() == Iter2->index()) { } else if (Iter1->index() == Iter2->index()) {
bool BecameZero; bool BecameZero = false;
changed |= Iter1->intersectWith(*Iter2, BecameZero); changed |= Iter1->intersectWith(*Iter2, BecameZero);
if (BecameZero) { if (BecameZero) {
ElementListIter IterTmp = Iter1; ElementListIter IterTmp = Iter1;
@ -666,7 +656,7 @@ class SparseBitVector {
if (Iter1->index() > Iter2->index()) { if (Iter1->index() > Iter2->index()) {
++Iter2; ++Iter2;
} else if (Iter1->index() == Iter2->index()) { } else if (Iter1->index() == Iter2->index()) {
bool BecameZero; bool BecameZero = false;
changed |= Iter1->intersectWithComplement(*Iter2, BecameZero); changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
if (BecameZero) { if (BecameZero) {
ElementListIter IterTmp = Iter1; ElementListIter IterTmp = Iter1;
@ -900,5 +890,3 @@ std::ostream& operator<<(
} }
} // end namespace c10 } // end namespace c10
C10_CLANG_DIAGNOSTIC_POP()

View File

@ -38,6 +38,7 @@ constexpr auto ssize(const C& c) -> std::
} }
template <typename T, std::ptrdiff_t N> template <typename T, std::ptrdiff_t N>
// NOLINTNEXTLINE(*-c-arrays)
constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t { constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t {
return N; return N;
} }