[llvm] [LLVM][ADT] Explicitly convert size_t values to SmallVector's size type (PR #77939)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 12 07:33:43 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Andrei Golubev (andrey-golubev)
<details>
<summary>Changes</summary>
Multiple places rely on implicit conversion when assigning 'size_t' values to the member fields (size or capacity) of SmallVector.
Depending on the platform / compiler configuration, this may result in narrowing conversion warnings (especially given that the size type of SmallVector's member fields is determined based on type T - in SmallVector<T>). To avoid the problem altogether, make the conversions explicit.
---
Full diff: https://github.com/llvm/llvm-project/pull/77939.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/SmallVector.h (+5-4)
``````````diff
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 2e6d2dc6ce90a2..65e862911522a8 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -61,7 +61,7 @@ template <class Size_T> class SmallVectorBase {
SmallVectorBase() = delete;
SmallVectorBase(void *FirstEl, size_t TotalCapacity)
- : BeginX(FirstEl), Capacity(TotalCapacity) {}
+ : BeginX(FirstEl), Capacity(static_cast<Size_T>(TotalCapacity)) {}
/// This is a helper for \a grow() that's out of line to reduce code
/// duplication. This function will report a fatal error if it can't grow at
@@ -99,8 +99,9 @@ template <class Size_T> class SmallVectorBase {
///
/// This does not construct or destroy any elements in the vector.
void set_size(size_t N) {
- assert(N <= capacity());
- Size = N;
+ auto n = static_cast<Size_T>(N);
+ assert(n <= capacity());
+ Size = n;
}
};
@@ -468,7 +469,7 @@ void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow(
free(this->begin());
this->BeginX = NewElts;
- this->Capacity = NewCapacity;
+ this->Capacity = static_cast<SmallVectorSizeType<T>>(NewCapacity);
}
/// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
``````````
</details>
https://github.com/llvm/llvm-project/pull/77939
More information about the llvm-commits
mailing list