Fix warnings in SmallVector (#127250)

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/127250
Approved by: https://github.com/ezyang
This commit is contained in:
cyy
2024-05-30 21:13:17 +00:00
committed by PyTorch MergeBot
parent ad1b18ab2f
commit bf2f5e70dd
3 changed files with 17 additions and 26 deletions

View File

@ -576,8 +576,8 @@ TYPED_TEST(SmallVectorTest, EraseTest) {
SCOPED_TRACE("EraseTest"); SCOPED_TRACE("EraseTest");
this->makeSequence(this->theVector, 1, 3); this->makeSequence(this->theVector, 1, 3);
const auto& theConstVector = this->theVector; auto& theVector = this->theVector;
this->theVector.erase(theConstVector.begin()); this->theVector.erase(theVector.begin());
this->assertValuesInOrder(this->theVector, 2u, 2, 3); this->assertValuesInOrder(this->theVector, 2u, 2, 3);
} }
@ -586,8 +586,8 @@ TYPED_TEST(SmallVectorTest, EraseRangeTest) {
SCOPED_TRACE("EraseRangeTest"); SCOPED_TRACE("EraseRangeTest");
this->makeSequence(this->theVector, 1, 3); this->makeSequence(this->theVector, 1, 3);
const auto& theConstVector = this->theVector; auto& theVector = this->theVector;
this->theVector.erase(theConstVector.begin(), theConstVector.begin() + 2); this->theVector.erase(theVector.begin(), theVector.begin() + 2);
this->assertValuesInOrder(this->theVector, 1u, 3); this->assertValuesInOrder(this->theVector, 1u, 3);
} }

View File

@ -123,7 +123,7 @@ void* SmallVectorBase<Size_T>::mallocForGrow(
// Note: Moving this function into the header may cause performance regression. // Note: Moving this function into the header may cause performance regression.
template <class Size_T> template <class Size_T>
void SmallVectorBase<Size_T>::grow_pod( void SmallVectorBase<Size_T>::grow_pod(
void* FirstEl, const void* FirstEl,
size_t MinSize, size_t MinSize,
size_t TSize) { size_t TSize) {
size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity()); size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());

View File

@ -38,11 +38,6 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
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 {
/// This is all the stuff common to all SmallVectors. /// This is all the stuff common to all SmallVectors.
@ -75,7 +70,7 @@ class C10_API SmallVectorBase {
/// This is an implementation of the grow() method which only works /// This is an implementation of the grow() method which only works
/// on POD-like data types and is out of line to reduce code duplication. /// on POD-like data types and is out of line to reduce code duplication.
/// This function will report a fatal error if it cannot increase capacity. /// This function will report a fatal error if it cannot increase capacity.
void grow_pod(void* FirstEl, size_t MinSize, size_t TSize); void grow_pod(const void* FirstEl, size_t MinSize, size_t TSize);
public: public:
SmallVectorBase() = delete; SmallVectorBase() = delete;
@ -112,8 +107,10 @@ using SmallVectorSizeType =
/// Figure out the offset of the first element. /// Figure out the offset of the first element.
template <class T, typename = void> template <class T, typename = void>
struct SmallVectorAlignmentAndSize { struct SmallVectorAlignmentAndSize {
// NOLINTNEXTLINE(*c-arrays*)
alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof( alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof(
SmallVectorBase<SmallVectorSizeType<T>>)]; SmallVectorBase<SmallVectorSizeType<T>>)];
// NOLINTNEXTLINE(*c-arrays*)
alignas(T) char FirstEl[sizeof(T)]; alignas(T) char FirstEl[sizeof(T)];
}; };
@ -246,7 +243,7 @@ class SmallVectorTemplateCommon
bool ReferencesStorage = false; bool ReferencesStorage = false;
int64_t Index = -1; int64_t Index = -1;
if (!U::TakesParamByValue) { if constexpr (!U::TakesParamByValue) {
if (C10_UNLIKELY(This->isReferenceToStorage(&Elt))) { if (C10_UNLIKELY(This->isReferenceToStorage(&Elt))) {
ReferencesStorage = true; ReferencesStorage = true;
Index = &Elt - This->begin(); Index = &Elt - This->begin();
@ -306,7 +303,7 @@ class SmallVectorTemplateCommon
size_type size_in_bytes() const { size_type size_in_bytes() const {
return size() * sizeof(T); return size() * sizeof(T);
} }
size_type max_size() const { constexpr size_type max_size() const {
return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T)); return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T));
} }
@ -475,6 +472,7 @@ class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
this->set_size(this->size() + 1); this->set_size(this->size() + 1);
} }
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
void push_back(T&& Elt) { void push_back(T&& Elt) {
T* EltPtr = reserveForParamAndGetAddress(Elt); T* EltPtr = reserveForParamAndGetAddress(Elt);
::new ((void*)this->end()) T(::std::move(*EltPtr)); ::new ((void*)this->end()) T(::std::move(*EltPtr));
@ -788,13 +786,9 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
assign(RHS.begin(), RHS.end()); assign(RHS.begin(), RHS.end());
} }
iterator erase(const_iterator CI) { iterator erase(iterator I) {
// Just cast away constness because this is a non-const member function.
iterator I = const_cast<iterator>(CI);
assert( assert(
this->isReferenceToStorage(CI) && this->isReferenceToStorage(I) && "Iterator to erase is out of bounds.");
"Iterator to erase is out of bounds.");
iterator N = I; iterator N = I;
// Shift all elts down one. // Shift all elts down one.
@ -804,11 +798,7 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
return (N); return (N);
} }
iterator erase(const_iterator CS, const_iterator CE) { iterator erase(iterator S, iterator E) {
// Just cast away constness because this is a non-const member function.
iterator S = const_cast<iterator>(CS);
iterator E = const_cast<iterator>(CE);
assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds."); assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds.");
iterator N = S; iterator N = S;
@ -1402,6 +1392,7 @@ class /* LLVM_GSL_OWNER */ SmallVector : public SmallVectorImpl<T>,
.end())>::iterator_category, .end())>::iterator_category,
std::input_iterator_tag>, std::input_iterator_tag>,
int> = 0> int> = 0>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
SmallVector& operator=(Container&& C) { SmallVector& operator=(Container&& C) {
this->assign(C.begin(), C.end()); this->assign(C.begin(), C.end());
return *this; return *this;
@ -1439,6 +1430,7 @@ using ValueTypeFromRangeType = std::remove_const_t<
/// SmallVector with elements of the vector. This is useful, for example, /// SmallVector with elements of the vector. This is useful, for example,
/// when you want to iterate a range and then sort the results. /// when you want to iterate a range and then sort the results.
template <unsigned Size, typename R> template <unsigned Size, typename R>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R&& Range) { SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R&& Range) {
return {std::begin(Range), std::end(Range)}; return {std::begin(Range), std::end(Range)};
} }
@ -1447,6 +1439,7 @@ SmallVector<
ValueTypeFromRangeType<R>, ValueTypeFromRangeType<R>,
CalculateSmallVectorDefaultInlinedElements< CalculateSmallVectorDefaultInlinedElements<
ValueTypeFromRangeType<R>>::value> ValueTypeFromRangeType<R>>::value>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
to_vector(R&& Range) { to_vector(R&& Range) {
return {std::begin(Range), std::end(Range)}; return {std::begin(Range), std::end(Range)};
} }
@ -1472,5 +1465,3 @@ inline void swap(
} }
} // end namespace std } // end namespace std
C10_CLANG_DIAGNOSTIC_POP()