Skip to content

[LLVM][ADT] Explicitly convert size_t values to SmallVector's size type #77939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions llvm/include/llvm/ADT/SmallVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,8 +99,18 @@ 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;
assert(N <= capacity()); // implies no overflow in assignment
Size = static_cast<Size_T>(N);
}

/// Set the array data pointer to \p Begin and capacity to \p N.
///
/// This does not construct or destroy any elements in the vector.
// This does not clean up any existing allocation.
void set_allocation_range(void *Begin, size_t N) {
assert(N <= SizeTypeMax());
BeginX = Begin;
Capacity = static_cast<Size_T>(N);
}
};

Expand Down Expand Up @@ -467,8 +477,7 @@ void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow(
if (!this->isSmall())
free(this->begin());

this->BeginX = NewElts;
this->Capacity = NewCapacity;
this->set_allocation_range(NewElts, NewCapacity);
}

/// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Support/SmallVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize,
NewElts = replaceAllocation(NewElts, TSize, NewCapacity, size());
}

this->BeginX = NewElts;
this->Capacity = NewCapacity;
this->set_allocation_range(NewElts, NewCapacity);
}

template class llvm::SmallVectorBase<uint32_t>;
Expand Down