[llvm] 7bf39a5 - [ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" (#160004)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 21 19:16:46 PDT 2025


Author: Kazu Hirata
Date: 2025-09-21T19:16:42-07:00
New Revision: 7bf39a5585342b152888104205a11f3c0235b7a9

URL: https://github.com/llvm/llvm-project/commit/7bf39a5585342b152888104205a11f3c0235b7a9
DIFF: https://github.com/llvm/llvm-project/commit/7bf39a5585342b152888104205a11f3c0235b7a9.diff

LOG: [ADT] Refactor SmallVector::assertSafeToAddRange with "constexpr if" (#160004)

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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallVector.h

Removed: 
    


################################################################################
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.


        


More information about the llvm-commits mailing list