-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" #160004
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
[ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" #160004
Conversation
This patch consolidates two implementations of assertSafeToAddRange into a single template function. The new implementation uses "constexpr if" to check for pointer iterators, preserving the original behavior while simplifying the code.
|
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesThis patch consolidates two implementations of assertSafeToAddRange The new implementation uses "constexpr if" to check for pointer Full diff: https://github.com/llvm/llvm-project/pull/160004.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 80f7734b86907..5577b09fee89c 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -212,17 +212,16 @@ class SmallVectorTemplateCommon
void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
/// Check whether any part of the range will be invalidated by growing.
- void assertSafeToAddRange(const T *From, const T *To) {
- if (From == To)
- return;
- this->assertSafeToAdd(From, To - From);
- this->assertSafeToAdd(To - 1, To - From);
+ template <class ItTy> void assertSafeToAddRange(ItTy From, ItTy To) {
+ if constexpr (std::is_pointer_v<ItTy> &&
+ std::is_same_v<std::remove_cv_t<std::remove_pointer_t<ItTy>>,
+ T>) {
+ if (From == To)
+ return;
+ this->assertSafeToAdd(From, To - From);
+ this->assertSafeToAdd(To - 1, To - From);
+ }
}
- template <
- class ItTy,
- std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value,
- bool> = false>
- void assertSafeToAddRange(ItTy, ItTy) {}
/// Reserve enough space to add one element, and return the updated element
/// pointer in case it was a reference to the storage.
|
|
This commit is breaking my flang builds. This is on x86-64 with GCC 9.3.0. Please advise, fix, or revert. |
You can add |
|
Is that a permanent change that you're telling me to make, or a temporary patch until you can resolve this build failure? |
|
This is a proper fix to the warning you pasted. The code is fine in general, it's just this warning is a bit overzealous while you run with warnings as errors. |
|
Here you go: 2b5e29e |
This patch consolidates two implementations of assertSafeToAddRange
into a single template function.
The new implementation uses "constexpr if" to check for pointer
iterators, preserving the original behavior while simplifying the
code.