From ce65f5ac3cbf897bb5e3de1a51d80fd00866abaa Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 22 May 2019 05:06:50 -0700 Subject: [PATCH] Export of internal Abseil changes. -- 1edfe05ddddca43e7650b2d790df7c8498c0e588 by Abseil Team : Adding an assert to catch various misuses of std::optional. PiperOrigin-RevId: 249427865 -- 45463bbb7e59dfbc584b2f024368a63db98bd7a8 by CJ Johnson : Migrates internal member function GetAllocator() to GetAllocPtr() and changes the return type to pointer instead of reference to avoid unnecessary copy in DestroyElements(...) PiperOrigin-RevId: 249319571 -- 507835be22af85676143ee0c43a80a52bc32094c by Abseil Team : Fix -Wstring-conversion in GetEnvVar (Windows implementation). PiperOrigin-RevId: 249201897 GitOrigin-RevId: 1edfe05ddddca43e7650b2d790df7c8498c0e588 Change-Id: I9300131887ee507cf80d399c724cf87341e4f11a --- absl/container/inlined_vector.h | 44 +++++++++++------------- absl/container/internal/inlined_vector.h | 12 ++++--- absl/flags/parse.cc | 2 +- absl/types/optional.h | 4 ++- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 6c2d08571..564c95351 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -155,7 +155,7 @@ class InlinedVector { // Creates a copy of an `other` inlined vector using `other`'s allocator. InlinedVector(const InlinedVector& other) - : InlinedVector(other, other.storage_.GetAllocator()) {} + : InlinedVector(other, *other.storage_.GetAllocPtr()) {} // Creates a copy of an `other` inlined vector using a specified allocator. InlinedVector(const InlinedVector& other, const allocator_type& alloc) @@ -189,7 +189,7 @@ class InlinedVector { InlinedVector(InlinedVector&& other) noexcept( absl::allocator_is_nothrow::value || std::is_nothrow_move_constructible::value) - : storage_(other.storage_.GetAllocator()) { + : storage_(*other.storage_.GetAllocPtr()) { if (other.storage_.GetIsAllocated()) { // We can just steal the underlying buffer from the source. // That leaves the source empty, so we clear its size. @@ -224,7 +224,7 @@ class InlinedVector { absl::allocator_is_nothrow::value) : storage_(alloc) { if (other.storage_.GetIsAllocated()) { - if (alloc == other.storage_.GetAllocator()) { + if (*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) { // We can just steal the allocation from the source. storage_.SetAllocatedSize(other.size()); storage_.SetAllocatedData(other.storage_.GetAllocatedData()); @@ -437,7 +437,7 @@ class InlinedVector { // `InlinedVector::get_allocator()` // // Returns a copy of the allocator of the inlined vector. - allocator_type get_allocator() const { return storage_.GetAllocator(); } + allocator_type get_allocator() const { return *storage_.GetAllocPtr(); } // --------------------------------------------------------------------------- // InlinedVector Member Mutators @@ -794,19 +794,15 @@ class InlinedVector { // deallocates the heap allocation if the inlined vector was allocated. void clear() noexcept { const bool is_allocated = storage_.GetIsAllocated(); - pointer the_data = is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData(); - - inlined_vector_internal::DestroyElements(storage_.GetAllocator(), the_data, + inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), the_data, storage_.GetSize()); - + storage_.SetInlinedSize(0); if (is_allocated) { - AllocatorTraits::deallocate(storage_.GetAllocator(), the_data, + AllocatorTraits::deallocate(*storage_.GetAllocPtr(), the_data, storage_.GetAllocatedCapacity()); } - - storage_.SetInlinedSize(/* size = */ 0); } // `InlinedVector::reserve()` @@ -854,7 +850,7 @@ class InlinedVector { // Reallocate storage and move elements. // We can't simply use the same approach as above, because `assign()` would // call into `reserve()` internally and reserve larger capacity than we need - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), s); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), s); UninitializedCopy(std::make_move_iterator(storage_.GetAllocatedData()), std::make_move_iterator(storage_.GetAllocatedData() + s), new_data); @@ -880,7 +876,7 @@ class InlinedVector { Destroy(storage_.GetAllocatedData(), storage_.GetAllocatedData() + size()); assert(begin() == storage_.GetAllocatedData()); - AllocatorTraits::deallocate(storage_.GetAllocator(), + AllocatorTraits::deallocate(*storage_.GetAllocPtr(), storage_.GetAllocatedData(), storage_.GetAllocatedCapacity()); } else { @@ -895,7 +891,7 @@ class InlinedVector { template reference Construct(pointer p, Args&&... args) { std::allocator_traits::construct( - storage_.GetAllocator(), p, std::forward(args)...); + *storage_.GetAllocPtr(), p, std::forward(args)...); return *p; } @@ -912,7 +908,7 @@ class InlinedVector { // Destroy [`from`, `to`) in place. void Destroy(pointer from, pointer to) { for (pointer cur = from; cur != to; ++cur) { - std::allocator_traits::destroy(storage_.GetAllocator(), + std::allocator_traits::destroy(*storage_.GetAllocPtr(), cur); } #if !defined(NDEBUG) @@ -943,7 +939,7 @@ class InlinedVector { } pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); UninitializedCopy(std::make_move_iterator(data()), std::make_move_iterator(data() + s), new_data); @@ -975,7 +971,7 @@ class InlinedVector { // Move everyone into the new allocation, leaving a gap of `n` for the // requested shift. pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); size_type index = position - begin(); UninitializedCopy(std::make_move_iterator(data()), std::make_move_iterator(data() + index), new_data); @@ -1024,7 +1020,7 @@ class InlinedVector { size_type new_capacity = 2 * capacity(); pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); reference new_element = Construct(new_data + s, std::forward(args)...); @@ -1038,7 +1034,7 @@ class InlinedVector { void InitAssign(size_type n) { if (n > static_cast(N)) { - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), n); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), n); storage_.SetAllocatedData(new_data); storage_.SetAllocatedCapacity(n); UninitializedFill(storage_.GetAllocatedData(), @@ -1053,7 +1049,7 @@ class InlinedVector { void InitAssign(size_type n, const_reference v) { if (n > static_cast(N)) { - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), n); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), n); storage_.SetAllocatedData(new_data); storage_.SetAllocatedCapacity(n); UninitializedFill(storage_.GetAllocatedData(), @@ -1126,7 +1122,7 @@ class InlinedVector { // Both out of line, so just swap the tag, allocation, and allocator. storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); storage_.SwapAllocatedSizeAndCapacity(std::addressof(other.storage_)); - swap(storage_.GetAllocator(), other.storage_.GetAllocator()); + swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr()); return; } @@ -1156,7 +1152,7 @@ class InlinedVector { a->storage_.GetInlinedData() + a_size); storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); - swap(storage_.GetAllocator(), other.storage_.GetAllocator()); + swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr()); assert(b->size() == a_size); assert(a->size() == b_size); @@ -1198,8 +1194,8 @@ class InlinedVector { a->storage_.SetAllocatedData(b_data); a->storage_.SetAllocatedCapacity(b_capacity); - if (a->storage_.GetAllocator() != b->storage_.GetAllocator()) { - swap(a->storage_.GetAllocator(), b->storage_.GetAllocator()); + if (*a->storage_.GetAllocPtr() != *b->storage_.GetAllocPtr()) { + swap(*a->storage_.GetAllocPtr(), *b->storage_.GetAllocPtr()); } assert(b->size() == a_size); diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 4589ce083..a2b3d24d9 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -33,7 +33,7 @@ using IsAtLeastForwardIterator = std::is_convertible< std::forward_iterator_tag>; template -void DestroyElements(AllocatorType alloc, ValueType* destroy_first, +void DestroyElements(AllocatorType* alloc_ptr, ValueType* destroy_first, SizeType destroy_size) { using AllocatorTraits = std::allocator_traits; @@ -45,7 +45,7 @@ void DestroyElements(AllocatorType alloc, ValueType* destroy_first, // NOTE: We assume destructors do not throw and thus make no attempt to roll // back. for (SizeType i = 0; i < destroy_size; ++i) { - AllocatorTraits::destroy(alloc, destroy_first + i); + AllocatorTraits::destroy(*alloc_ptr, destroy_first + i); } #ifndef NDEBUG @@ -104,10 +104,12 @@ class Storage { return data_.allocated.allocated_capacity; } - allocator_type& GetAllocator() { return metadata_.template get<0>(); } + allocator_type* GetAllocPtr() { + return std::addressof(metadata_.template get<0>()); + } - const allocator_type& GetAllocator() const { - return metadata_.template get<0>(); + const allocator_type* GetAllocPtr() const { + return std::addressof(metadata_.template get<0>()); } void SetAllocatedSize(size_type size) { diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index b19619602..3caaa1c25 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -190,7 +190,7 @@ bool GetEnvVar(const char* var_name, std::string* var_value) { char buf[1024]; auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf)); if (get_res >= sizeof(buf)) { - return ""; + return false; } if (get_res == 0) { diff --git a/absl/types/optional.h b/absl/types/optional.h index 1670022dd..142dc83a2 100644 --- a/absl/types/optional.h +++ b/absl/types/optional.h @@ -421,7 +421,9 @@ class optional : private optional_internal::optional_data, // // Accesses the underlying `T` value of an `optional`. If the `optional` is // empty, behavior is undefined. - constexpr const T& operator*() const & { return reference(); } + constexpr const T& operator*() const& { + return ABSL_ASSERT(this->engaged_), reference(); + } T& operator*() & { assert(this->engaged_); return reference();