[llvm] 1fe24a6 - ADT: Split out isSafeToReferenceAfterResize helper to use early returns, NFC
Duncan P. N. Exon Smith via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 19 17:55:13 PST 2020
Author: Duncan P. N. Exon Smith
Date: 2020-11-19T17:55:04-08:00
New Revision: 1fe24a640c2837fbbfb03add122cb3b045914416
URL: https://github.com/llvm/llvm-project/commit/1fe24a640c2837fbbfb03add122cb3b045914416
DIFF: https://github.com/llvm/llvm-project/commit/1fe24a640c2837fbbfb03add122cb3b045914416.diff
LOG: ADT: Split out isSafeToReferenceAfterResize helper to use early returns, NFC
The assertion logic in SmallVector::assertSafeToReferenceAfterResize is
hard to follow; split out SmallVector::isSafeToReferenceAfterResize and
add early returns and comments. No functionality change here.
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 ed329aa02e34..c5bb1ece0667 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -136,12 +136,24 @@ class SmallVectorTemplateCommon
this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
}
+ /// Return true unless Elt will be invalidated by resizing the vector to
+ /// NewSize.
+ bool isSafeToReferenceAfterResize(const void *Elt, size_t NewSize) {
+ // Past the end.
+ if (LLVM_LIKELY(Elt < this->begin() || Elt >= this->end()))
+ return true;
+
+ // Return false if Elt will be destroyed by shrinking.
+ if (NewSize <= this->size())
+ return Elt < this->begin() + NewSize;
+
+ // Return false if we need to grow.
+ return NewSize <= this->capacity();
+ }
+
/// Check whether Elt will be invalidated by resizing the vector to NewSize.
void assertSafeToReferenceAfterResize(const void *Elt, size_t NewSize) {
- assert((Elt >= this->end() ||
- (NewSize <= this->size()
- ? Elt < this->begin() + NewSize
- : (Elt < this->begin() || NewSize <= this->capacity()))) &&
+ assert(isSafeToReferenceAfterResize(Elt, NewSize) &&
"Attempting to reference an element of the vector in an operation "
"that invalidates it");
}
More information about the llvm-commits
mailing list