aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ABSEIL_ISSUE_TEMPLATE.md2
-rw-r--r--CMakeLists.txt2
-rw-r--r--absl/algorithm/container.h10
-rw-r--r--absl/algorithm/container_test.cc13
-rw-r--r--absl/base/BUILD.bazel21
-rw-r--r--absl/base/CMakeLists.txt43
-rw-r--r--absl/base/attributes.h12
-rw-r--r--absl/base/config.h7
-rw-r--r--absl/base/exception_safety_testing_test.cc25
-rw-r--r--absl/base/inline_variable_test.cc62
-rw-r--r--absl/base/inline_variable_test_a.cc (renamed from absl/utility/utility.cc)14
-rw-r--r--absl/base/inline_variable_test_b.cc (renamed from absl/base/internal/log_severity.cc)12
-rw-r--r--absl/base/internal/exception_safety_testing.cc7
-rw-r--r--absl/base/internal/exception_safety_testing.h61
-rw-r--r--absl/base/internal/inline_variable.h127
-rw-r--r--absl/base/internal/inline_variable_testing.h44
-rw-r--r--absl/base/internal/malloc_extension.cc39
-rw-r--r--absl/base/internal/malloc_extension_test.cc23
-rw-r--r--absl/base/internal/malloc_hook.cc161
-rw-r--r--absl/base/internal/malloc_hook.h111
-rw-r--r--absl/base/internal/malloc_hook_c.h88
-rw-r--r--absl/base/internal/per_thread_tls.h2
-rw-r--r--absl/base/internal/raw_logging.cc2
-rw-r--r--absl/base/internal/raw_logging.h2
-rw-r--r--absl/base/internal/spinlock.h2
-rw-r--r--absl/base/internal/thread_identity.h2
-rw-r--r--absl/base/log_severity.h (renamed from absl/base/internal/log_severity.h)0
-rw-r--r--absl/base/macros.h2
-rw-r--r--absl/container/BUILD.bazel8
-rw-r--r--absl/container/fixed_array.h27
-rw-r--r--absl/container/fixed_array_test.cc38
-rw-r--r--absl/container/inlined_vector.h36
-rw-r--r--absl/container/inlined_vector_test.cc77
-rw-r--r--absl/copts.bzl26
-rw-r--r--absl/debugging/internal/stacktrace_aarch64-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_arm-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_generic-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_powerpc-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_unimplemented-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_win32-inl.inc8
-rw-r--r--absl/debugging/internal/stacktrace_x86-inl.inc8
-rw-r--r--absl/debugging/stacktrace.h7
-rw-r--r--absl/memory/memory.h14
-rw-r--r--absl/memory/memory_test.cc14
-rw-r--r--absl/meta/type_traits.h66
-rw-r--r--absl/meta/type_traits_test.cc337
-rw-r--r--absl/numeric/int128.cc34
-rw-r--r--absl/numeric/int128.h99
-rw-r--r--absl/numeric/int128_have_intrinsic.inc19
-rw-r--r--absl/numeric/int128_no_intrinsic.inc19
-rw-r--r--absl/numeric/int128_test.cc6
-rw-r--r--absl/strings/BUILD.bazel1
-rw-r--r--absl/strings/CMakeLists.txt1
-rw-r--r--absl/strings/escaping.h2
-rw-r--r--absl/strings/internal/stl_type_traits.h246
-rw-r--r--absl/strings/internal/str_split_internal.h24
-rw-r--r--absl/strings/str_cat.h3
-rw-r--r--absl/strings/str_split_test.cc12
-rw-r--r--absl/strings/string_view.h2
-rw-r--r--absl/strings/string_view_test.cc25
-rw-r--r--absl/strings/strip.h8
-rw-r--r--absl/strings/strip_test.cc20
-rw-r--r--absl/strings/substitute.h16
-rw-r--r--absl/synchronization/lifetime_test.cc22
-rw-r--r--absl/synchronization/mutex.cc5
-rw-r--r--absl/synchronization/mutex.h45
-rw-r--r--absl/synchronization/mutex_test.cc10
-rw-r--r--absl/types/BUILD.bazel15
-rw-r--r--absl/types/CMakeLists.txt17
-rw-r--r--absl/types/any.h1
-rw-r--r--absl/types/any_exception_safety_test.cc201
-rw-r--r--absl/types/optional.h12
-rw-r--r--absl/types/span.h8
-rw-r--r--absl/utility/BUILD.bazel4
-rw-r--r--absl/utility/CMakeLists.txt14
-rw-r--r--absl/utility/utility.h83
-rw-r--r--absl/utility/utility_test.cc178
77 files changed, 2015 insertions, 739 deletions
diff --git a/ABSEIL_ISSUE_TEMPLATE.md b/ABSEIL_ISSUE_TEMPLATE.md
index 53e0000..9dfd4b8 100644
--- a/ABSEIL_ISSUE_TEMPLATE.md
+++ b/ABSEIL_ISSUE_TEMPLATE.md
@@ -19,4 +19,4 @@ possible in this section.]
[Please clearly describe the API change(s) being proposed. If multiple changes,
please keep them clearly distinguished. When possible, **use example code
snippets to illustrate before–after API usages**. List pros-n-cons. Highlight
-the main questions that you want to be answered.Given the Abseil project compatibility requirements, describe why the API change is safe."]
+the main questions that you want to be answered. Given the Abseil project compatibility requirements, describe why the API change is safe.]
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 142898c..6d3789e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,7 +33,7 @@ if (MSVC)
# /wd4267 conversion from 'size_t' to 'type2'
# /wd4800 force value to bool 'true' or 'false' (performance warning)
add_compile_options(/W3 /WX /wd4005 /wd4068 /wd4244 /wd4267 /wd4800)
- add_definitions(/DNOMINMAX /DWIN32_LEAN_AND_MEAN=1 /D_CRT_SECURE_NO_WARNINGS)
+ add_definitions(/DNOMINMAX /DWIN32_LEAN_AND_MEAN=1 /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS)
else()
set(ABSL_STD_CXX_FLAG "-std=c++11" CACHE STRING "c++ std flag (default: c++11)")
endif()
diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h
index 839a6ad..8d0d403 100644
--- a/absl/algorithm/container.h
+++ b/absl/algorithm/container.h
@@ -499,16 +499,6 @@ OutputIterator c_move(C& src, OutputIterator dest) {
container_algorithm_internal::c_end(src), dest);
}
-// c_move_backward()
-//
-// Container-based version of the <algorithm> `std::move_backward()` function to
-// move a container's elements into an iterator in reverse order.
-template <typename C, typename BidirectionalIterator>
-BidirectionalIterator c_move_backward(C& src, BidirectionalIterator dest) {
- return std::move_backward(container_algorithm_internal::c_begin(src),
- container_algorithm_internal::c_end(src), dest);
-}
-
// c_swap_ranges()
//
// Container-based version of the <algorithm> `std::swap_ranges()` function to
diff --git a/absl/algorithm/container_test.cc b/absl/algorithm/container_test.cc
index 093b281..de66f14 100644
--- a/absl/algorithm/container_test.cc
+++ b/absl/algorithm/container_test.cc
@@ -636,19 +636,6 @@ TEST(MutatingTest, Move) {
Pointee(5)));
}
-TEST(MutatingTest, MoveBackward) {
- std::vector<std::unique_ptr<int>> actual;
- actual.emplace_back(absl::make_unique<int>(1));
- actual.emplace_back(absl::make_unique<int>(2));
- actual.emplace_back(absl::make_unique<int>(3));
- actual.emplace_back(absl::make_unique<int>(4));
- actual.emplace_back(absl::make_unique<int>(5));
- auto subrange = absl::MakeSpan(actual.data(), 3);
- absl::c_move_backward(subrange, actual.end());
- EXPECT_THAT(actual, ElementsAre(IsNull(), IsNull(), Pointee(1), Pointee(2),
- Pointee(3)));
-}
-
TEST(MutatingTest, SwapRanges) {
std::vector<int> odds = {2, 4, 6};
std::vector<int> evens = {1, 3, 5};
diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel
index 743add7..9bb7bfa 100644
--- a/absl/base/BUILD.bazel
+++ b/absl/base/BUILD.bazel
@@ -82,7 +82,6 @@ cc_library(
srcs = ["internal/malloc_extension.cc"],
hdrs = [
"internal/malloc_extension.h",
- "internal/malloc_extension_c.h",
],
copts = ABSL_DEFAULT_COPTS,
visibility = [
@@ -129,6 +128,7 @@ cc_library(
name = "base_internal",
hdrs = [
"internal/identity.h",
+ "internal/inline_variable.h",
"internal/invoke.h",
],
copts = ABSL_DEFAULT_COPTS,
@@ -152,7 +152,6 @@ cc_library(
"casts.h",
"internal/atomic_hook.h",
"internal/cycleclock.h",
- "internal/log_severity.h",
"internal/low_level_scheduling.h",
"internal/per_thread_tls.h",
"internal/raw_logging.h",
@@ -161,6 +160,7 @@ cc_library(
"internal/thread_identity.h",
"internal/tsan_mutex_interface.h",
"internal/unscaledcycleclock.h",
+ "log_severity.h",
],
copts = ABSL_DEFAULT_COPTS,
deps = [
@@ -236,6 +236,7 @@ cc_library(
hdrs = ["internal/exception_safety_testing.h"],
copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
deps = [
+ ":base",
":config",
":pretty_function",
"//absl/memory",
@@ -258,6 +259,22 @@ cc_test(
)
cc_test(
+ name = "inline_variable_test",
+ size = "small",
+ srcs = [
+ "inline_variable_test.cc",
+ "inline_variable_test_a.cc",
+ "inline_variable_test_b.cc",
+ "internal/inline_variable_testing.h",
+ ],
+ copts = ABSL_TEST_COPTS,
+ deps = [
+ ":base_internal",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
+
+cc_test(
name = "invoke_test",
size = "small",
srcs = ["invoke_test.cc"],
diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt
index 3e94d51..9d2de55 100644
--- a/absl/base/CMakeLists.txt
+++ b/absl/base/CMakeLists.txt
@@ -20,6 +20,7 @@ list(APPEND BASE_PUBLIC_HEADERS
"casts.h"
"config.h"
"dynamic_annotations.h"
+ "log_severity.h"
"macros.h"
"optimization.h"
"policy_checks.h"
@@ -33,17 +34,18 @@ list(APPEND BASE_INTERNAL_HEADERS
"internal/cycleclock.h"
"internal/endian.h"
"internal/exception_testing.h"
+ "internal/exception_safety_testing.h"
"internal/identity.h"
"internal/invoke.h"
- "internal/log_severity.h"
+ "internal/inline_variable.h"
"internal/low_level_alloc.h"
"internal/low_level_scheduling.h"
- "internal/malloc_extension_c.h"
"internal/malloc_extension.h"
"internal/malloc_hook_c.h"
"internal/malloc_hook.h"
"internal/malloc_hook_invoke.h"
"internal/per_thread_tls.h"
+ "internal/pretty_function.h"
"internal/raw_logging.h"
"internal/scheduling_mode.h"
"internal/spinlock.h"
@@ -58,8 +60,9 @@ list(APPEND BASE_INTERNAL_HEADERS
# absl_base main library
-list(APPEND BASE_SRC
+list(APPEND BASE_SRC
"internal/cycleclock.cc"
+ "internal/exception_safety_testing.cc"
"internal/raw_logging.cc"
"internal/spinlock.cc"
"internal/sysinfo.cc"
@@ -212,6 +215,26 @@ absl_test(
)
+# test inline_variable_test
+list(APPEND INLINE_VARIABLE_TEST_SRC
+ "internal/inline_variable_testing.h"
+ "inline_variable_test.cc"
+ "inline_variable_test_a.cc"
+ "inline_variable_test_b.cc"
+)
+
+set(INLINE_VARIABLE_TEST_PUBLIC_LIBRARIES absl::base)
+
+absl_test(
+ TARGET
+ inline_variable_test
+ SOURCES
+ ${INLINE_VARIABLE_TEST_SRC}
+ PUBLIC_LIBRARIES
+ ${INLINE_VARIABLE_TEST_PUBLIC_LIBRARIES}
+)
+
+
# test spinlock_test_common
set(SPINLOCK_TEST_COMMON_SRC "spinlock_test_common.cc")
set(SPINLOCK_TEST_COMMON_PUBLIC_LIBRARIES absl::base absl::synchronization)
@@ -319,6 +342,20 @@ absl_test(
${THREAD_IDENTITY_TEST_PUBLIC_LIBRARIES}
)
+#test exceptions_safety_testing_test
+set(EXCEPTION_SAFETY_TESTING_TEST_SRC "exception_safety_testing_test.cc")
+set(EXCEPTION_SAFETY_TESTING_TEST_PUBLIC_LIBRARIES absl::base absl::memory absl::meta absl::strings absl::optional)
+
+absl_test(
+ TARGET
+ absl_exception_safety_testing_test
+ SOURCES
+ ${EXCEPTION_SAFETY_TESTING_TEST_SRC}
+ PUBLIC_LIBRARIES
+ ${EXCEPTION_SAFETY_TESTING_TEST_PUBLIC_LIBRARIES}
+ PRIVATE_COMPILE_FLAGS
+ ${ABSL_EXCEPTIONS_FLAG}
+)
# test absl_malloc_extension_system_malloc_test
set(MALLOC_EXTENSION_SYSTEM_MALLOC_TEST_SRC "internal/malloc_extension_test.cc")
diff --git a/absl/base/attributes.h b/absl/base/attributes.h
index 02bb030..4e1fc8b 100644
--- a/absl/base/attributes.h
+++ b/absl/base/attributes.h
@@ -281,6 +281,18 @@
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
#endif
+// ABSL_ATTRIBUTE_RETURNS_NONNULL
+//
+// Tells the compiler that a particular function never returns a null pointer.
+#if ABSL_HAVE_ATTRIBUTE(returns_nonnull) || \
+ (defined(__GNUC__) && \
+ (__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
+ !defined(__clang__))
+#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
+#else
+#define ABSL_ATTRIBUTE_RETURNS_NONNULL
+#endif
+
// ABSL_HAVE_ATTRIBUTE_SECTION
//
// Indicates whether labeled sections are supported. Labeled sections are not
diff --git a/absl/base/config.h b/absl/base/config.h
index 3f3b8b3..6703d0e 100644
--- a/absl/base/config.h
+++ b/absl/base/config.h
@@ -138,9 +138,10 @@
// supported.
#ifdef ABSL_HAVE_THREAD_LOCAL
#error ABSL_HAVE_THREAD_LOCAL cannot be directly set
-#elif !defined(__apple_build_version__) || \
- ((__apple_build_version__ >= 8000042) && \
- !(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0))
+#elif (!defined(__apple_build_version__) || \
+ (__apple_build_version__ >= 8000042)) && \
+ !(defined(__APPLE__) && TARGET_OS_IPHONE && \
+ __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
// Notes: Xcode's clang did not support `thread_local` until version
// 8, and even then not for all iOS < 9.0.
#define ABSL_HAVE_THREAD_LOCAL 1
diff --git a/absl/base/exception_safety_testing_test.cc b/absl/base/exception_safety_testing_test.cc
index 1fc0386..5477b40 100644
--- a/absl/base/exception_safety_testing_test.cc
+++ b/absl/base/exception_safety_testing_test.cc
@@ -63,7 +63,7 @@ TEST_F(ThrowingValueTest, Throws) {
// the countdown doesn't hit 0, and doesn't modify the state of the
// ThrowingValue if it throws
template <typename F>
-void TestOp(F&& f) {
+void TestOp(const F& f) {
UnsetCountdown();
ExpectNoThrow(f);
@@ -153,11 +153,21 @@ TEST_F(ThrowingValueTest, ThrowingStreamOps) {
TestOp([&]() { std::cout << bomb; });
}
+template <typename F>
+void TestAllocatingOp(const F& f) {
+ UnsetCountdown();
+ ExpectNoThrow(f);
+
+ SetCountdown();
+ EXPECT_THROW(f(), exceptions_internal::TestBadAllocException);
+ UnsetCountdown();
+}
+
TEST_F(ThrowingValueTest, ThrowingAllocatingOps) {
// make_unique calls unqualified operator new, so these exercise the
// ThrowingValue overloads.
- TestOp([]() { return absl::make_unique<ThrowingValue<>>(1); });
- TestOp([]() { return absl::make_unique<ThrowingValue<>[]>(2); });
+ TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>>(1); });
+ TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>[]>(2); });
}
TEST_F(ThrowingValueTest, NonThrowingMoveCtor) {
@@ -399,7 +409,8 @@ struct CallOperator {
};
struct NonNegative {
- friend testing::AssertionResult AbslCheckInvariants(NonNegative* g) {
+ friend testing::AssertionResult AbslCheckInvariants(
+ NonNegative* g, absl::InternalAbslNamespaceFinder) {
if (g->i >= 0) return testing::AssertionSuccess();
return testing::AssertionFailure()
<< "i should be non-negative but is " << g->i;
@@ -503,7 +514,8 @@ struct HasReset : public NonNegative {
void reset() { i = 0; }
- friend bool AbslCheckInvariants(HasReset* h) {
+ friend bool AbslCheckInvariants(HasReset* h,
+ absl::InternalAbslNamespaceFinder) {
h->reset();
return h->i == 0;
}
@@ -591,7 +603,8 @@ struct ExhaustivenessTester {
return true;
}
- friend testing::AssertionResult AbslCheckInvariants(ExhaustivenessTester*) {
+ friend testing::AssertionResult AbslCheckInvariants(
+ ExhaustivenessTester*, absl::InternalAbslNamespaceFinder) {
return testing::AssertionSuccess();
}
diff --git a/absl/base/inline_variable_test.cc b/absl/base/inline_variable_test.cc
new file mode 100644
index 0000000..5499189
--- /dev/null
+++ b/absl/base/inline_variable_test.cc
@@ -0,0 +1,62 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <type_traits>
+
+#include "absl/base/internal/inline_variable.h"
+#include "absl/base/internal/inline_variable_testing.h"
+
+#include "gtest/gtest.h"
+
+namespace absl {
+namespace inline_variable_testing_internal {
+namespace {
+
+TEST(InlineVariableTest, Constexpr) {
+ static_assert(inline_variable_foo.value == 5, "");
+ static_assert(other_inline_variable_foo.value == 5, "");
+ static_assert(inline_variable_int == 5, "");
+ static_assert(other_inline_variable_int == 5, "");
+}
+
+TEST(InlineVariableTest, DefaultConstructedIdentityEquality) {
+ EXPECT_EQ(get_foo_a().value, 5);
+ EXPECT_EQ(get_foo_b().value, 5);
+ EXPECT_EQ(&get_foo_a(), &get_foo_b());
+}
+
+TEST(InlineVariableTest, DefaultConstructedIdentityInequality) {
+ EXPECT_NE(&inline_variable_foo, &other_inline_variable_foo);
+}
+
+TEST(InlineVariableTest, InitializedIdentityEquality) {
+ EXPECT_EQ(get_int_a(), 5);
+ EXPECT_EQ(get_int_b(), 5);
+ EXPECT_EQ(&get_int_a(), &get_int_b());
+}
+
+TEST(InlineVariableTest, InitializedIdentityInequality) {
+ EXPECT_NE(&inline_variable_int, &other_inline_variable_int);
+}
+
+TEST(InlineVariableTest, FunPtrType) {
+ static_assert(
+ std::is_same<void(*)(),
+ std::decay<decltype(inline_variable_fun_ptr)>::type>::value,
+ "");
+}
+
+} // namespace
+} // namespace inline_variable_testing_internal
+} // namespace absl
diff --git a/absl/utility/utility.cc b/absl/base/inline_variable_test_a.cc
index fce79f5..a3bf3b6 100644
--- a/absl/utility/utility.cc
+++ b/absl/base/inline_variable_test_a.cc
@@ -12,16 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-// -----------------------------------------------------------------------------
-// File: ascii.h
-// -----------------------------------------------------------------------------
-//
-#include "absl/utility/utility.h"
+#include "absl/base/internal/inline_variable_testing.h"
namespace absl {
+namespace inline_variable_testing_internal {
+
+const Foo& get_foo_a() { return inline_variable_foo; }
-#ifndef ABSL_HAVE_STD_OPTIONAL
-const in_place_t in_place{};
-#endif
+const int& get_int_a() { return inline_variable_int; }
+} // namespace inline_variable_testing_internal
} // namespace absl
diff --git a/absl/base/internal/log_severity.cc b/absl/base/inline_variable_test_b.cc
index 430ac45..b4b9393 100644
--- a/absl/base/internal/log_severity.cc
+++ b/absl/base/inline_variable_test_b.cc
@@ -12,4 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "absl/base/internal/log_severity.h"
+#include "absl/base/internal/inline_variable_testing.h"
+
+namespace absl {
+namespace inline_variable_testing_internal {
+
+const Foo& get_foo_b() { return inline_variable_foo; }
+
+const int& get_int_b() { return inline_variable_int; }
+
+} // namespace inline_variable_testing_internal
+} // namespace absl
diff --git a/absl/base/internal/exception_safety_testing.cc b/absl/base/internal/exception_safety_testing.cc
index ab8d6c9..821438e 100644
--- a/absl/base/internal/exception_safety_testing.cc
+++ b/absl/base/internal/exception_safety_testing.cc
@@ -23,8 +23,11 @@ namespace exceptions_internal {
int countdown = -1;
-void MaybeThrow(absl::string_view msg) {
- if (countdown-- == 0) throw TestException(msg);
+void MaybeThrow(absl::string_view msg, bool throw_bad_alloc) {
+ if (countdown-- == 0) {
+ if (throw_bad_alloc) throw TestBadAllocException(msg);
+ throw TestException(msg);
+ }
}
testing::AssertionResult FailureMessage(const TestException& e,
diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h
index a0127a8..8eac226 100644
--- a/absl/base/internal/exception_safety_testing.h
+++ b/absl/base/internal/exception_safety_testing.h
@@ -35,6 +35,8 @@
#include "absl/types/optional.h"
namespace absl {
+struct InternalAbslNamespaceFinder {};
+
struct AllocInspector;
// A configuration enum for Throwing*. Operations whose flags are set will
@@ -71,31 +73,45 @@ constexpr bool ThrowingAllowed(NoThrow flags, NoThrow flag) {
class TestException {
public:
explicit TestException(absl::string_view msg) : msg_(msg) {}
- absl::string_view what() const { return msg_; }
+ virtual ~TestException() {}
+ virtual const char* what() const noexcept { return msg_.c_str(); }
private:
std::string msg_;
};
+// TestBadAllocException exists because allocation functions must throw an
+// exception which can be caught by a handler of std::bad_alloc. We use a child
+// class of std::bad_alloc so we can customise the error message, and also
+// derive from TestException so we don't accidentally end up catching an actual
+// bad_alloc exception in TestExceptionSafety.
+class TestBadAllocException : public std::bad_alloc, public TestException {
+ public:
+ explicit TestBadAllocException(absl::string_view msg)
+ : TestException(msg) {}
+ using TestException::what;
+};
+
extern int countdown;
-void MaybeThrow(absl::string_view msg);
+void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false);
testing::AssertionResult FailureMessage(const TestException& e,
int countdown) noexcept;
class TrackedObject {
+ public:
+ TrackedObject(const TrackedObject&) = delete;
+ TrackedObject(TrackedObject&&) = delete;
+
protected:
- explicit TrackedObject(absl::string_view child_ctor) {
+ explicit TrackedObject(const char* child_ctor) {
if (!GetAllocs().emplace(this, child_ctor).second) {
ADD_FAILURE() << "Object at address " << static_cast<void*>(this)
<< " re-constructed in ctor " << child_ctor;
}
}
- TrackedObject(const TrackedObject&) = delete;
- TrackedObject(TrackedObject&&) = delete;
-
static std::unordered_map<TrackedObject*, absl::string_view>& GetAllocs() {
static auto* m =
new std::unordered_map<TrackedObject*, absl::string_view>();
@@ -120,10 +136,10 @@ using FactoryType = typename absl::result_of_t<Factory()>::element_type;
template <typename Factory, typename Op, typename Checker>
absl::optional<testing::AssertionResult> TestCheckerAtCountdown(
Factory factory, const Op& op, int count, const Checker& check) {
- exceptions_internal::countdown = count;
auto t_ptr = factory();
absl::optional<testing::AssertionResult> out;
try {
+ exceptions_internal::countdown = count;
op(t_ptr.get());
} catch (const exceptions_internal::TestException& e) {
out.emplace(check(t_ptr.get()));
@@ -141,6 +157,10 @@ int UpdateOut(Factory factory, const Op& op, int count, const Checker& checker,
return 0;
}
+// Declare AbslCheckInvariants so that it can be found eventually via ADL.
+// Taking `...` gives it the lowest possible precedence.
+void AbslCheckInvariants(...);
+
// Returns an optional with the result of the check if op fails, or an empty
// optional if op passes
template <typename Factory, typename Op, typename... Checkers>
@@ -148,8 +168,9 @@ absl::optional<testing::AssertionResult> TestAtCountdown(
Factory factory, const Op& op, int count, const Checkers&... checkers) {
// Don't bother with the checkers if the class invariants are already broken.
auto out = TestCheckerAtCountdown(
- factory, op, count,
- [](FactoryType<Factory>* t_ptr) { return AbslCheckInvariants(t_ptr); });
+ factory, op, count, [](FactoryType<Factory>* t_ptr) {
+ return AbslCheckInvariants(t_ptr, InternalAbslNamespaceFinder());
+ });
if (!out.has_value()) return out;
// Run each checker, short circuiting after the first failure
@@ -483,7 +504,7 @@ class ThrowingValue : private exceptions_internal::TrackedObject {
static void* operator new(size_t s, Args&&... args) noexcept(
!exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) {
if (exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) {
- exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
+ exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
}
return ::operator new(s, std::forward<Args>(args)...);
}
@@ -492,7 +513,7 @@ class ThrowingValue : private exceptions_internal::TrackedObject {
static void* operator new[](size_t s, Args&&... args) noexcept(
!exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) {
if (exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) {
- exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
+ exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
}
return ::operator new[](s, std::forward<Args>(args)...);
}
@@ -630,10 +651,7 @@ class ThrowingAllocator : private exceptions_internal::TrackedObject {
p->~U();
}
- size_type max_size() const
- noexcept(!exceptions_internal::ThrowingAllowed(Flags,
- NoThrow::kNoThrow)) {
- ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
+ size_type max_size() const noexcept {
return std::numeric_limits<difference_type>::max() / sizeof(value_type);
}
@@ -720,9 +738,12 @@ T TestThrowingCtor(Args&&... args) {
// Tests that performing operation Op on a T follows exception safety
// guarantees. By default only tests the basic guarantee. There must be a
-// function, AbslCheckInvariants(T*) which returns
-// anything convertible to bool and which makes sure the invariants of the type
-// are upheld. This is called before any of the checkers.
+// function, AbslCheckInvariants(T*, absl::InternalAbslNamespaceFinder) which
+// returns anything convertible to bool and which makes sure the invariants of
+// the type are upheld. This is called before any of the checkers. The
+// InternalAbslNamespaceFinder is unused, and just helps find
+// AbslCheckInvariants for absl types which become aliases to std::types in
+// C++17.
//
// Parameters:
// * TFactory: operator() returns a unique_ptr to the type under test (T). It
@@ -740,11 +761,13 @@ template <typename TFactory, typename FunctionFromTPtrToVoid,
testing::AssertionResult TestExceptionSafety(TFactory factory,
FunctionFromTPtrToVoid&& op,
const Checkers&... checkers) {
+ struct Cleanup {
+ ~Cleanup() { UnsetCountdown(); }
+ } c;
for (int countdown = 0;; ++countdown) {
auto out = exceptions_internal::TestAtCountdown(factory, op, countdown,
checkers...);
if (!out.has_value()) {
- UnsetCountdown();
return testing::AssertionSuccess();
}
if (!*out) return *out;
diff --git a/absl/base/internal/inline_variable.h b/absl/base/internal/inline_variable.h
new file mode 100644
index 0000000..a65fe89
--- /dev/null
+++ b/absl/base/internal/inline_variable.h
@@ -0,0 +1,127 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
+#define ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
+
+#include <type_traits>
+
+#include "absl/base/internal/identity.h"
+
+// File:
+// This file define a macro that allows the creation of or emulation of C++17
+// inline variables based on whether or not the feature is supported.
+
+////////////////////////////////////////////////////////////////////////////////
+// Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init)
+//
+// Description:
+// Expands to the equivalent of an inline constexpr instance of the specified
+// `type` and `name`, initialized to the value `init`. If the compiler being
+// used is detected as supporting actual inline variables as a language
+// feature, then the macro expands to an actual inline variable definition.
+//
+// Requires:
+// `type` is a type that is usable in an extern variable declaration.
+//
+// Requires: `name` is a valid identifier
+//
+// Requires:
+// `init` is an expression that can be used in the following definition:
+// constexpr type name = init;
+//
+// Usage:
+//
+// // Equivalent to: `inline constexpr size_t variant_npos = -1;`
+// ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1);
+//
+// Differences in implementation:
+// For a direct, language-level inline variable, decltype(name) will be the
+// type that was specified along with const qualification, whereas for
+// emulated inline variables, decltype(name) may be different (in practice
+// it will likely be a reference type).
+////////////////////////////////////////////////////////////////////////////////
+
+// ABSL_INTERNAL_HAS_WARNING()
+//
+// If the compiler supports the `__has_warning` extension for detecting
+// warnings, then this macro is defined to be `__has_warning`.
+//
+// If the compiler does not support `__has_warning`, invocations expand to 0.
+//
+// For clang's documentation of `__has_warning`, see
+// https://clang.llvm.org/docs/LanguageExtensions.html#has-warning
+#if defined(__has_warning)
+#define ABSL_INTERNAL_HAS_WARNING __has_warning
+#else // Otherwise, be optimistic and assume the warning is not enabled.
+#define ABSL_INTERNAL_HAS_WARNING(warning) 0
+#endif // defined(__has_warning)
+
+// If the compiler supports inline variables and does not warn when used...
+#if defined(__cpp_inline_variables) && \
+ !ABSL_INTERNAL_HAS_WARNING("-Wc++98-c++11-c++14-compat")
+
+// Clang's -Wmissing-variable-declarations option erroneously warned that
+// inline constexpr objects need to be pre-declared. This has now been fixed,
+// but we will need to support this workaround for people building with older
+// versions of clang.
+//
+// Bug: https://bugs.llvm.org/show_bug.cgi?id=35862
+//
+// Note:
+// identity_t is used here so that the const and name are in the
+// appropriate place for pointer types, reference types, function pointer
+// types, etc..
+#if defined(__clang__) && \
+ ABSL_INTERNAL_HAS_WARNING("-Wmissing-variable-declarations")
+#define ABSL_INTERNAL_EXTERN_DECL(type, name) \
+ extern const ::absl::internal::identity_t<type> name;
+#else // Otherwise, just define the macro to do nothing.
+#define ABSL_INTERNAL_EXTERN_DECL(type, name)
+#endif // defined(__clang__) &&
+ // ABSL_INTERNAL_HAS_WARNING("-Wmissing-variable-declarations")
+
+// See above comment at top of file for details.
+#define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \
+ ABSL_INTERNAL_EXTERN_DECL(type, name) \
+ inline constexpr ::absl::internal::identity_t<type> name = init
+
+#else // Otherwise, we need to emulate inline variables...
+
+// See above comment at top of file for details.
+//
+// Note:
+// identity_t is used here so that the const and name are in the
+// appropriate place for pointer types, reference types, function pointer
+// types, etc..
+#define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \
+ template <class /*AbslInternalDummy*/ = void> \
+ struct AbslInternalInlineVariableHolder##name { \
+ static constexpr ::absl::internal::identity_t<var_type> kInstance = init; \
+ }; \
+ \
+ template <class AbslInternalDummy> \
+ constexpr ::absl::internal::identity_t<var_type> \
+ AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \
+ \
+ static constexpr const ::absl::internal::identity_t<var_type>& \
+ name = /* NOLINT */ \
+ AbslInternalInlineVariableHolder##name<>::kInstance; \
+ static_assert(sizeof(void (*)(decltype(name))) != 0, \
+ "Silence unused variable warnings.")
+
+#endif // defined(__cpp_inline_variables) &&
+ // !ABSL_INTERNAL_HAS_WARNING("-Wc++98-c++11-c++14-compat")
+
+#endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
diff --git a/absl/base/internal/inline_variable_testing.h b/absl/base/internal/inline_variable_testing.h
new file mode 100644
index 0000000..a0dd2bb
--- /dev/null
+++ b/absl/base/internal/inline_variable_testing.h
@@ -0,0 +1,44 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_BASE_INLINE_VARIABLE_TESTING_H_
+#define ABSL_BASE_INLINE_VARIABLE_TESTING_H_
+
+#include "absl/base/internal/inline_variable.h"
+
+namespace absl {
+namespace inline_variable_testing_internal {
+
+struct Foo {
+ int value = 5;
+};
+
+ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, inline_variable_foo, {});
+ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, other_inline_variable_foo, {});
+
+ABSL_INTERNAL_INLINE_CONSTEXPR(int, inline_variable_int, 5);
+ABSL_INTERNAL_INLINE_CONSTEXPR(int, other_inline_variable_int, 5);
+
+ABSL_INTERNAL_INLINE_CONSTEXPR(void(*)(), inline_variable_fun_ptr, nullptr);
+
+const Foo& get_foo_a();
+const Foo& get_foo_b();
+
+const int& get_int_a();
+const int& get_int_b();
+
+} // namespace inline_variable_testing_internal
+} // namespace absl
+
+#endif // ABSL_BASE_INLINE_VARIABLE_TESTING_H_
diff --git a/absl/base/internal/malloc_extension.cc b/absl/base/internal/malloc_extension.cc
index d48ec5b..5b90653 100644
--- a/absl/base/internal/malloc_extension.cc
+++ b/absl/base/internal/malloc_extension.cc
@@ -20,7 +20,6 @@
#include <string>
#include "absl/base/dynamic_annotations.h"
-#include "absl/base/internal/malloc_extension_c.h"
namespace absl {
namespace base_internal {
@@ -155,44 +154,6 @@ void MallocExtension::GetFragmentationProfile(MallocExtensionWriter*) {}
} // namespace base_internal
} // namespace absl
-// These are C shims that work on the current instance.
-
-#define C_SHIM(fn, retval, paramlist, arglist) \
- extern "C" retval MallocExtension_##fn paramlist { \
- return absl::base_internal::MallocExtension::instance()->fn arglist; \
- }
-
-C_SHIM(VerifyAllMemory, int, (void), ());
-C_SHIM(VerifyNewMemory, int, (const void* p), (p));
-C_SHIM(VerifyArrayNewMemory, int, (const void* p), (p));
-C_SHIM(VerifyMallocMemory, int, (const void* p), (p));
-C_SHIM(
- MallocMemoryStats, int,
- (int* blocks, size_t* total,
- int histogram[absl::base_internal::MallocExtension::kMallocHistogramSize]),
- (blocks, total, histogram));
-
-C_SHIM(GetStats, void,
- (char* buffer, int buffer_length), (buffer, buffer_length));
-C_SHIM(GetNumericProperty, int,
- (const char* property, size_t* value), (property, value));
-C_SHIM(SetNumericProperty, int,
- (const char* property, size_t value), (property, value));
-
-C_SHIM(MarkThreadIdle, void, (void), ());
-C_SHIM(MarkThreadBusy, void, (void), ());
-C_SHIM(ReleaseFreeMemory, void, (void), ());
-C_SHIM(ReleaseToSystem, void, (size_t num_bytes), (num_bytes));
-C_SHIM(GetEstimatedAllocatedSize, size_t, (size_t size), (size));
-C_SHIM(GetAllocatedSize, size_t, (const void* p), (p));
-
-// Can't use the shim here because of the need to translate the enums.
-extern "C"
-MallocExtension_Ownership MallocExtension_GetOwnership(const void* p) {
- return static_cast<MallocExtension_Ownership>(
- absl::base_internal::MallocExtension::instance()->GetOwnership(p));
-}
-
// Default implementation just returns size. The expectation is that
// the linked-in malloc implementation might provide an override of
// this weak function with a better implementation.
diff --git a/absl/base/internal/malloc_extension_test.cc b/absl/base/internal/malloc_extension_test.cc
index 246875a..76605eb 100644
--- a/absl/base/internal/malloc_extension_test.cc
+++ b/absl/base/internal/malloc_extension_test.cc
@@ -19,7 +19,6 @@
#include "gtest/gtest.h"
#include "absl/base/internal/malloc_extension.h"
-#include "absl/base/internal/malloc_extension_c.h"
namespace absl {
namespace base_internal {
@@ -35,15 +34,12 @@ TEST(MallocExtension, MallocExtension) {
} else {
ASSERT_TRUE(MallocExtension::instance()->GetNumericProperty(
"generic.current_allocated_bytes", &cxx_bytes_used));
- ASSERT_TRUE(MallocExtension_GetNumericProperty(
- "generic.current_allocated_bytes", &c_bytes_used));
#ifndef MEMORY_SANITIZER
EXPECT_GT(cxx_bytes_used, 1000);
EXPECT_GT(c_bytes_used, 1000);
#endif
EXPECT_TRUE(MallocExtension::instance()->VerifyAllMemory());
- EXPECT_TRUE(MallocExtension_VerifyAllMemory());
EXPECT_EQ(MallocExtension::kOwned,
MallocExtension::instance()->GetOwnership(a));
@@ -65,30 +61,11 @@ TEST(MallocExtension, MallocExtension) {
MallocExtension::instance()->GetEstimatedAllocatedSize(i));
free(p);
}
-
- // Check the c-shim version too.
- EXPECT_EQ(MallocExtension_kOwned, MallocExtension_GetOwnership(a));
- EXPECT_EQ(MallocExtension_kNotOwned,
- MallocExtension_GetOwnership(&cxx_bytes_used));
- EXPECT_EQ(MallocExtension_kNotOwned, MallocExtension_GetOwnership(nullptr));
- EXPECT_GE(MallocExtension_GetAllocatedSize(a), 1000);
- EXPECT_LE(MallocExtension_GetAllocatedSize(a), 5000);
- EXPECT_GE(MallocExtension_GetEstimatedAllocatedSize(1000), 1000);
}
free(a);
}
-// Verify that the .cc file and .h file have the same enum values.
-TEST(GetOwnership, EnumValuesEqualForCAndCXX) {
- EXPECT_EQ(static_cast<int>(MallocExtension::kUnknownOwnership),
- static_cast<int>(MallocExtension_kUnknownOwnership));
- EXPECT_EQ(static_cast<int>(MallocExtension::kOwned),
- static_cast<int>(MallocExtension_kOwned));
- EXPECT_EQ(static_cast<int>(MallocExtension::kNotOwned),
- static_cast<int>(MallocExtension_kNotOwned));
-}
-
TEST(nallocx, SaneBehavior) {
for (size_t size = 0; size < 64 * 1024; ++size) {
size_t alloc_size = nallocx(size, 0);
diff --git a/absl/base/internal/malloc_hook.cc b/absl/base/internal/malloc_hook.cc
index 52531c6..780e8fe 100644
--- a/absl/base/internal/malloc_hook.cc
+++ b/absl/base/internal/malloc_hook.cc
@@ -215,139 +215,101 @@ HookList<MallocHook::MunmapReplacement> munmap_replacement_ = INIT_HOOK_LIST;
#undef INIT_HOOK_LIST_WITH_VALUE
#undef INIT_HOOK_LIST
-} // namespace base_internal
-} // namespace absl
+bool MallocHook::AddNewHook(NewHook hook) { return new_hooks_.Add(hook); }
-// These are available as C bindings as well as C++, hence their
-// definition outside the MallocHook class.
-extern "C"
-int MallocHook_AddNewHook(MallocHook_NewHook hook) {
- return absl::base_internal::new_hooks_.Add(hook);
-}
-
-extern "C"
-int MallocHook_RemoveNewHook(MallocHook_NewHook hook) {
- return absl::base_internal::new_hooks_.Remove(hook);
-}
+bool MallocHook::RemoveNewHook(NewHook hook) { return new_hooks_.Remove(hook); }
-extern "C"
-int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook) {
- return absl::base_internal::delete_hooks_.Add(hook);
+bool MallocHook::AddDeleteHook(DeleteHook hook) {
+ return delete_hooks_.Add(hook);
}
-extern "C"
-int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook hook) {
- return absl::base_internal::delete_hooks_.Remove(hook);
+bool MallocHook::RemoveDeleteHook(DeleteHook hook) {
+ return delete_hooks_.Remove(hook);
}
-extern "C" int MallocHook_AddSampledNewHook(MallocHook_SampledNewHook hook) {
- return absl::base_internal::sampled_new_hooks_.Add(hook);
+bool MallocHook::AddSampledNewHook(SampledNewHook hook) {
+ return sampled_new_hooks_.Add(hook);
}
-extern "C" int MallocHook_RemoveSampledNewHook(MallocHook_SampledNewHook hook) {
- return absl::base_internal::sampled_new_hooks_.Remove(hook);
+bool MallocHook::RemoveSampledNewHook(SampledNewHook hook) {
+ return sampled_new_hooks_.Remove(hook);
}
-extern "C" int MallocHook_AddSampledDeleteHook(
- MallocHook_SampledDeleteHook hook) {
- return absl::base_internal::sampled_delete_hooks_.Add(hook);
+bool MallocHook::AddSampledDeleteHook(SampledDeleteHook hook) {
+ return sampled_delete_hooks_.Add(hook);
}
-extern "C" int MallocHook_RemoveSampledDeleteHook(
- MallocHook_SampledDeleteHook hook) {
- return absl::base_internal::sampled_delete_hooks_.Remove(hook);
+bool MallocHook::RemoveSampledDeleteHook(SampledDeleteHook hook) {
+ return sampled_delete_hooks_.Remove(hook);
}
-extern "C"
-int MallocHook_AddPreMmapHook(MallocHook_PreMmapHook hook) {
- return absl::base_internal::premmap_hooks_.Add(hook);
+bool MallocHook::AddPreMmapHook(PreMmapHook hook) {
+ return premmap_hooks_.Add(hook);
}
-extern "C"
-int MallocHook_RemovePreMmapHook(MallocHook_PreMmapHook hook) {
- return absl::base_internal::premmap_hooks_.Remove(hook);
+bool MallocHook::RemovePreMmapHook(PreMmapHook hook) {
+ return premmap_hooks_.Remove(hook);
}
-extern "C"
-int MallocHook_SetMmapReplacement(MallocHook_MmapReplacement hook) {
+bool MallocHook::SetMmapReplacement(MmapReplacement hook) {
// NOTE this is a best effort CHECK. Concurrent sets could succeed since
// this test is outside of the Add spin lock.
- ABSL_RAW_CHECK(absl::base_internal::mmap_replacement_.empty(),
+ ABSL_RAW_CHECK(mmap_replacement_.empty(),
"Only one MMapReplacement is allowed.");
- return absl::base_internal::mmap_replacement_.Add(hook);
-}
-
-extern "C"
-int MallocHook_RemoveMmapReplacement(MallocHook_MmapReplacement hook) {
- return absl::base_internal::mmap_replacement_.Remove(hook);
-}
-
-extern "C"
-int MallocHook_AddMmapHook(MallocHook_MmapHook hook) {
- return absl::base_internal::mmap_hooks_.Add(hook);
+ return mmap_replacement_.Add(hook);
}
-extern "C"
-int MallocHook_RemoveMmapHook(MallocHook_MmapHook hook) {
- return absl::base_internal::mmap_hooks_.Remove(hook);
+bool MallocHook::RemoveMmapReplacement(MmapReplacement hook) {
+ return mmap_replacement_.Remove(hook);
}
-extern "C"
-int MallocHook_AddMunmapHook(MallocHook_MunmapHook hook) {
- return absl::base_internal::munmap_hooks_.Add(hook);
-}
+bool MallocHook::AddMmapHook(MmapHook hook) { return mmap_hooks_.Add(hook); }
-extern "C"
-int MallocHook_RemoveMunmapHook(MallocHook_MunmapHook hook) {
- return absl::base_internal::munmap_hooks_.Remove(hook);
+bool MallocHook::RemoveMmapHook(MmapHook hook) {
+ return mmap_hooks_.Remove(hook);
}
-extern "C"
-int MallocHook_SetMunmapReplacement(MallocHook_MunmapReplacement hook) {
+bool MallocHook::SetMunmapReplacement(MunmapReplacement hook) {
// NOTE this is a best effort CHECK. Concurrent sets could succeed since
// this test is outside of the Add spin lock.
- ABSL_RAW_CHECK(absl::base_internal::munmap_replacement_.empty(),
+ ABSL_RAW_CHECK(munmap_replacement_.empty(),
"Only one MunmapReplacement is allowed.");
- return absl::base_internal::munmap_replacement_.Add(hook);
+ return munmap_replacement_.Add(hook);
}
-extern "C"
-int MallocHook_RemoveMunmapReplacement(MallocHook_MunmapReplacement hook) {
- return absl::base_internal::munmap_replacement_.Remove(hook);
+bool MallocHook::RemoveMunmapReplacement(MunmapReplacement hook) {
+ return munmap_replacement_.Remove(hook);
}
-extern "C"
-int MallocHook_AddMremapHook(MallocHook_MremapHook hook) {
- return absl::base_internal::mremap_hooks_.Add(hook);
+bool MallocHook::AddMunmapHook(MunmapHook hook) {
+ return munmap_hooks_.Add(hook);
}
-extern "C"
-int MallocHook_RemoveMremapHook(MallocHook_MremapHook hook) {
- return absl::base_internal::mremap_hooks_.Remove(hook);
+bool MallocHook::RemoveMunmapHook(MunmapHook hook) {
+ return munmap_hooks_.Remove(hook);
}
-extern "C"
-int MallocHook_AddPreSbrkHook(MallocHook_PreSbrkHook hook) {
- return absl::base_internal::presbrk_hooks_.Add(hook);
+bool MallocHook::AddMremapHook(MremapHook hook) {
+ return mremap_hooks_.Add(hook);
}
-extern "C"
-int MallocHook_RemovePreSbrkHook(MallocHook_PreSbrkHook hook) {
- return absl::base_internal::presbrk_hooks_.Remove(hook);
+bool MallocHook::RemoveMremapHook(MremapHook hook) {
+ return mremap_hooks_.Remove(hook);
}
-extern "C"
-int MallocHook_AddSbrkHook(MallocHook_SbrkHook hook) {
- return absl::base_internal::sbrk_hooks_.Add(hook);
+bool MallocHook::AddPreSbrkHook(PreSbrkHook hook) {
+ return presbrk_hooks_.Add(hook);
}
-extern "C"
-int MallocHook_RemoveSbrkHook(MallocHook_SbrkHook hook) {
- return absl::base_internal::sbrk_hooks_.Remove(hook);
+bool MallocHook::RemovePreSbrkHook(PreSbrkHook hook) {
+ return presbrk_hooks_.Remove(hook);
}
-namespace absl {
-namespace base_internal {
+bool MallocHook::AddSbrkHook(SbrkHook hook) { return sbrk_hooks_.Add(hook); }
+
+bool MallocHook::RemoveSbrkHook(SbrkHook hook) {
+ return sbrk_hooks_.Remove(hook);
+}
// Note: embedding the function calls inside the traversal of HookList would be
// very confusing, as it is legal for a hook to remove itself and add other
@@ -501,12 +463,11 @@ static void InitializeInHookCaller() {
ABSL_INIT_ATTRIBUTE_SECTION_VARS(blink_malloc);
}
-// We can improve behavior/compactness of this function
-// if we pass a generic test function (with a generic arg)
-// into the implementations for get_stack_trace_fn instead of the skip_count.
-extern "C" int MallocHook_GetCallerStackTrace(
- void** result, int max_depth, int skip_count,
- MallocHook_GetStackTraceFn get_stack_trace_fn) {
+namespace absl {
+namespace base_internal {
+int MallocHook::GetCallerStackTrace(void** result, int max_depth,
+ int skip_count,
+ GetStackTraceFn get_stack_trace_fn) {
if (!ABSL_HAVE_ATTRIBUTE_SECTION) {
// Fall back to get_stack_trace_fn and good old but fragile frame skip
// counts.
@@ -524,11 +485,11 @@ extern "C" int MallocHook_GetCallerStackTrace(
absl::call_once(in_hook_caller_once, InitializeInHookCaller);
// MallocHook caller determination via InHookCaller works, use it:
static const int kMaxSkip = 32 + 6 + 3;
- // Constant tuned to do just one get_stack_trace_fn call below in practice
- // and not get many frames that we don't actually need:
- // currently max passed max_depth is 32,
- // max passed/needed skip_count is 6
- // and 3 is to account for some hook daisy chaining.
+ // Constant tuned to do just one get_stack_trace_fn call below in practice
+ // and not get many frames that we don't actually need:
+ // currently max passed max_depth is 32,
+ // max passed/needed skip_count is 6
+ // and 3 is to account for some hook daisy chaining.
static const int kStackSize = kMaxSkip + 1;
void* stack[kStackSize];
int depth =
@@ -538,11 +499,11 @@ extern "C" int MallocHook_GetCallerStackTrace(
return 0;
for (int i = depth - 1; i >= 0; --i) { // stack[0] is our immediate caller
if (InHookCaller(stack[i])) {
- i += 1; // skip hook caller frame
+ i += 1; // skip hook caller frame
depth -= i; // correct depth
if (depth > max_depth) depth = max_depth;
std::copy(stack + i, stack + i + depth, result);
- if (depth < max_depth && depth + i == kStackSize) {
+ if (depth < max_depth && depth + i == kStackSize) {
// get frames for the missing depth
depth += get_stack_trace_fn(result + depth, max_depth - depth,
1 + kStackSize);
@@ -558,6 +519,8 @@ extern "C" int MallocHook_GetCallerStackTrace(
// all functions in that section must be inside the same library.
return 0;
}
+} // namespace base_internal
+} // namespace absl
// On systems where we know how, we override mmap/munmap/mremap/sbrk
// to provide support for calling the related hooks (in addition,
diff --git a/absl/base/internal/malloc_hook.h b/absl/base/internal/malloc_hook.h
index ed5cf2e..6b006ed 100644
--- a/absl/base/internal/malloc_hook.h
+++ b/absl/base/internal/malloc_hook.h
@@ -74,24 +74,16 @@ class MallocHook {
// Object pointer and size are passed in.
// It may be passed null pointer if the allocator returned null.
typedef MallocHook_NewHook NewHook;
- inline static bool AddNewHook(NewHook hook) {
- return MallocHook_AddNewHook(hook);
- }
- inline static bool RemoveNewHook(NewHook hook) {
- return MallocHook_RemoveNewHook(hook);
- }
+ static bool AddNewHook(NewHook hook);
+ static bool RemoveNewHook(NewHook hook);
inline static void InvokeNewHook(const void* ptr, size_t size);
// The DeleteHook is invoked whenever an object is being deallocated.
// Object pointer is passed in.
// It may be passed null pointer if the caller is trying to delete null.
typedef MallocHook_DeleteHook DeleteHook;
- inline static bool AddDeleteHook(DeleteHook hook) {
- return MallocHook_AddDeleteHook(hook);
- }
- inline static bool RemoveDeleteHook(DeleteHook hook) {
- return MallocHook_RemoveDeleteHook(hook);
- }
+ static bool AddDeleteHook(DeleteHook hook);
+ static bool RemoveDeleteHook(DeleteHook hook);
inline static void InvokeDeleteHook(const void* ptr);
// The SampledNewHook is invoked for some subset of object allocations
@@ -99,20 +91,19 @@ class MallocHook {
// SampledAlloc has the following fields:
// * AllocHandle handle: to be set to an effectively unique value (in this
// process) by allocator.
- // * size_t allocated_size: space actually used by allocator to host
- // the object.
+ // * size_t allocated_size: space actually used by allocator to host the
+ // object. Not necessarily equal to the requested size due to alignment
+ // and other reasons.
+ // * double weight: the expected number of allocations matching this profile
+ // that this sample represents.
// * int stack_depth and const void* stack: invocation stack for
// the allocation.
// The allocator invoking the hook should record the handle value and later
// call InvokeSampledDeleteHook() with that value.
typedef MallocHook_SampledNewHook SampledNewHook;
typedef MallocHook_SampledAlloc SampledAlloc;
- inline static bool AddSampledNewHook(SampledNewHook hook) {
- return MallocHook_AddSampledNewHook(hook);
- }
- inline static bool RemoveSampledNewHook(SampledNewHook hook) {
- return MallocHook_RemoveSampledNewHook(hook);
- }
+ static bool AddSampledNewHook(SampledNewHook hook);
+ static bool RemoveSampledNewHook(SampledNewHook hook);
inline static void InvokeSampledNewHook(const SampledAlloc* sampled_alloc);
// The SampledDeleteHook is invoked whenever an object previously chosen
@@ -121,12 +112,8 @@ class MallocHook {
// InvokeSampledNewHook()-- is passed in.
typedef MallocHook_SampledDeleteHook SampledDeleteHook;
typedef MallocHook_AllocHandle AllocHandle;
- inline static bool AddSampledDeleteHook(SampledDeleteHook hook) {
- return MallocHook_AddSampledDeleteHook(hook);
- }
- inline static bool RemoveSampledDeleteHook(SampledDeleteHook hook) {
- return MallocHook_RemoveSampledDeleteHook(hook);
- }
+ static bool AddSampledDeleteHook(SampledDeleteHook hook);
+ static bool RemoveSampledDeleteHook(SampledDeleteHook hook);
inline static void InvokeSampledDeleteHook(AllocHandle handle);
// The PreMmapHook is invoked with mmap's or mmap64's arguments just
@@ -134,12 +121,8 @@ class MallocHook {
// in memory limited contexts, to catch allocations that will exceed
// a memory limit, and take outside actions to increase that limit.
typedef MallocHook_PreMmapHook PreMmapHook;
- inline static bool AddPreMmapHook(PreMmapHook hook) {
- return MallocHook_AddPreMmapHook(hook);
- }
- inline static bool RemovePreMmapHook(PreMmapHook hook) {
- return MallocHook_RemovePreMmapHook(hook);
- }
+ static bool AddPreMmapHook(PreMmapHook hook);
+ static bool RemovePreMmapHook(PreMmapHook hook);
inline static void InvokePreMmapHook(const void* start,
size_t size,
int protection,
@@ -159,12 +142,8 @@ class MallocHook {
// you must call RemoveMmapReplacement before calling SetMmapReplacement
// again.
typedef MallocHook_MmapReplacement MmapReplacement;
- inline static bool SetMmapReplacement(MmapReplacement hook) {
- return MallocHook_SetMmapReplacement(hook);
- }
- inline static bool RemoveMmapReplacement(MmapReplacement hook) {
- return MallocHook_RemoveMmapReplacement(hook);
- }
+ static bool SetMmapReplacement(MmapReplacement hook);
+ static bool RemoveMmapReplacement(MmapReplacement hook);
inline static bool InvokeMmapReplacement(const void* start,
size_t size,
int protection,
@@ -178,12 +157,8 @@ class MallocHook {
// a region of memory has been just mapped.
// It may be passed MAP_FAILED if the mmap failed.
typedef MallocHook_MmapHook MmapHook;
- inline static bool AddMmapHook(MmapHook hook) {
- return MallocHook_AddMmapHook(hook);
- }
- inline static bool RemoveMmapHook(MmapHook hook) {
- return MallocHook_RemoveMmapHook(hook);
- }
+ static bool AddMmapHook(MmapHook hook);
+ static bool RemoveMmapHook(MmapHook hook);
inline static void InvokeMmapHook(const void* result,
const void* start,
size_t size,
@@ -202,12 +177,8 @@ class MallocHook {
// MunmapReplacement you must call RemoveMunmapReplacement before
// calling SetMunmapReplacement again.
typedef MallocHook_MunmapReplacement MunmapReplacement;
- inline static bool SetMunmapReplacement(MunmapReplacement hook) {
- return MallocHook_SetMunmapReplacement(hook);
- }
- inline static bool RemoveMunmapReplacement(MunmapReplacement hook) {
- return MallocHook_RemoveMunmapReplacement(hook);
- }
+ static bool SetMunmapReplacement(MunmapReplacement hook);
+ static bool RemoveMunmapReplacement(MunmapReplacement hook);
inline static bool InvokeMunmapReplacement(const void* start,
size_t size,
int* result);
@@ -217,23 +188,15 @@ class MallocHook {
// TODO(maxim): Rename this to PreMunmapHook for consistency with PreMmapHook
// and PreSbrkHook.
typedef MallocHook_MunmapHook MunmapHook;
- inline static bool AddMunmapHook(MunmapHook hook) {
- return MallocHook_AddMunmapHook(hook);
- }
- inline static bool RemoveMunmapHook(MunmapHook hook) {
- return MallocHook_RemoveMunmapHook(hook);
- }
+ static bool AddMunmapHook(MunmapHook hook);
+ static bool RemoveMunmapHook(MunmapHook hook);
inline static void InvokeMunmapHook(const void* start, size_t size);
// The MremapHook is invoked with mremap's return value and arguments
// whenever a region of memory has been just remapped.
typedef MallocHook_MremapHook MremapHook;
- inline static bool AddMremapHook(MremapHook hook) {
- return MallocHook_AddMremapHook(hook);
- }
- inline static bool RemoveMremapHook(MremapHook hook) {
- return MallocHook_RemoveMremapHook(hook);
- }
+ static bool AddMremapHook(MremapHook hook);
+ static bool RemoveMremapHook(MremapHook hook);
inline static void InvokeMremapHook(const void* result,
const void* old_addr,
size_t old_size,
@@ -248,12 +211,8 @@ class MallocHook {
// to catch allocations that will exceed the limit and take outside
// actions to increase such a limit.
typedef MallocHook_PreSbrkHook PreSbrkHook;
- inline static bool AddPreSbrkHook(PreSbrkHook hook) {
- return MallocHook_AddPreSbrkHook(hook);
- }
- inline static bool RemovePreSbrkHook(PreSbrkHook hook) {
- return MallocHook_RemovePreSbrkHook(hook);
- }
+ static bool AddPreSbrkHook(PreSbrkHook hook);
+ static bool RemovePreSbrkHook(PreSbrkHook hook);
inline static void InvokePreSbrkHook(ptrdiff_t increment);
// The SbrkHook is invoked with sbrk's result and argument whenever sbrk
@@ -261,12 +220,8 @@ class MallocHook {
// This is because sbrk(0) is often called to get the top of the memory stack,
// and is not actually a memory-allocation call.
typedef MallocHook_SbrkHook SbrkHook;
- inline static bool AddSbrkHook(SbrkHook hook) {
- return MallocHook_AddSbrkHook(hook);
- }
- inline static bool RemoveSbrkHook(SbrkHook hook) {
- return MallocHook_RemoveSbrkHook(hook);
- }
+ static bool AddSbrkHook(SbrkHook hook);
+ static bool RemoveSbrkHook(SbrkHook hook);
inline static void InvokeSbrkHook(const void* result, ptrdiff_t increment);
// Pointer to a absl::GetStackTrace implementation, following the API in
@@ -280,12 +235,8 @@ class MallocHook {
// is not available.
// Stack trace is filled into *result up to the size of max_depth.
// The actual number of stack frames filled is returned.
- inline static int GetCallerStackTrace(void** result, int max_depth,
- int skip_count,
- GetStackTraceFn get_stack_trace_fn) {
- return MallocHook_GetCallerStackTrace(result, max_depth, skip_count,
- get_stack_trace_fn);
- }
+ static int GetCallerStackTrace(void** result, int max_depth, int skip_count,
+ GetStackTraceFn get_stack_trace_fn);
#if ABSL_HAVE_MMAP
// Unhooked versions of mmap() and munmap(). These should be used
diff --git a/absl/base/internal/malloc_hook_c.h b/absl/base/internal/malloc_hook_c.h
index 03b84ca..7255ddc 100644
--- a/absl/base/internal/malloc_hook_c.h
+++ b/absl/base/internal/malloc_hook_c.h
@@ -29,100 +29,38 @@
extern "C" {
#endif /* __cplusplus */
-/* Get the current stack trace. Try to skip all routines up to and
- * including the caller of MallocHook::Invoke*.
- * Use "skip_count" (similarly to absl::GetStackTrace from stacktrace.h)
- * as a hint about how many routines to skip if better information
- * is not available.
- */
typedef int (*MallocHook_GetStackTraceFn)(void**, int, int);
-int MallocHook_GetCallerStackTrace(void** result, int max_depth, int skip_count,
- MallocHook_GetStackTraceFn fn);
-
-/* All the MallocHook_{Add,Remove}*Hook functions below return 1 on success
- * and 0 on failure.
- */
-
typedef void (*MallocHook_NewHook)(const void* ptr, size_t size);
-int MallocHook_AddNewHook(MallocHook_NewHook hook);
-int MallocHook_RemoveNewHook(MallocHook_NewHook hook);
-
typedef void (*MallocHook_DeleteHook)(const void* ptr);
-int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook);
-int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook hook);
-
typedef int64_t MallocHook_AllocHandle;
typedef struct {
/* See malloc_hook.h for documentation for this struct. */
MallocHook_AllocHandle handle;
size_t allocated_size;
+ double weight;
int stack_depth;
const void* stack;
} MallocHook_SampledAlloc;
typedef void (*MallocHook_SampledNewHook)(
const MallocHook_SampledAlloc* sampled_alloc);
-int MallocHook_AddSampledNewHook(MallocHook_SampledNewHook hook);
-int MallocHook_RemoveSampledNewHook(MallocHook_SampledNewHook hook);
-
typedef void (*MallocHook_SampledDeleteHook)(MallocHook_AllocHandle handle);
-int MallocHook_AddSampledDeleteHook(MallocHook_SampledDeleteHook hook);
-int MallocHook_RemoveSampledDeleteHook(MallocHook_SampledDeleteHook hook);
-
-typedef void (*MallocHook_PreMmapHook)(const void *start,
- size_t size,
- int protection,
- int flags,
- int fd,
+typedef void (*MallocHook_PreMmapHook)(const void* start, size_t size,
+ int protection, int flags, int fd,
off_t offset);
-int MallocHook_AddPreMmapHook(MallocHook_PreMmapHook hook);
-int MallocHook_RemovePreMmapHook(MallocHook_PreMmapHook hook);
-
-typedef void (*MallocHook_MmapHook)(const void* result,
- const void* start,
- size_t size,
- int protection,
- int flags,
- int fd,
- off_t offset);
-int MallocHook_AddMmapHook(MallocHook_MmapHook hook);
-int MallocHook_RemoveMmapHook(MallocHook_MmapHook hook);
-
-typedef int (*MallocHook_MmapReplacement)(const void* start,
- size_t size,
- int protection,
- int flags,
- int fd,
- off_t offset,
- void** result);
-int MallocHook_SetMmapReplacement(MallocHook_MmapReplacement hook);
-int MallocHook_RemoveMmapReplacement(MallocHook_MmapReplacement hook);
-
+typedef void (*MallocHook_MmapHook)(const void* result, const void* start,
+ size_t size, int protection, int flags,
+ int fd, off_t offset);
+typedef int (*MallocHook_MmapReplacement)(const void* start, size_t size,
+ int protection, int flags, int fd,
+ off_t offset, void** result);
typedef void (*MallocHook_MunmapHook)(const void* start, size_t size);
-int MallocHook_AddMunmapHook(MallocHook_MunmapHook hook);
-int MallocHook_RemoveMunmapHook(MallocHook_MunmapHook hook);
-
-typedef int (*MallocHook_MunmapReplacement)(const void* start,
- size_t size,
+typedef int (*MallocHook_MunmapReplacement)(const void* start, size_t size,
int* result);
-int MallocHook_SetMunmapReplacement(MallocHook_MunmapReplacement hook);
-int MallocHook_RemoveMunmapReplacement(MallocHook_MunmapReplacement hook);
-
-typedef void (*MallocHook_MremapHook)(const void* result,
- const void* old_addr,
- size_t old_size,
- size_t new_size,
- int flags,
- const void* new_addr);
-int MallocHook_AddMremapHook(MallocHook_MremapHook hook);
-int MallocHook_RemoveMremapHook(MallocHook_MremapHook hook);
-
+typedef void (*MallocHook_MremapHook)(const void* result, const void* old_addr,
+ size_t old_size, size_t new_size,
+ int flags, const void* new_addr);
typedef void (*MallocHook_PreSbrkHook)(ptrdiff_t increment);
-int MallocHook_AddPreSbrkHook(MallocHook_PreSbrkHook hook);
-int MallocHook_RemovePreSbrkHook(MallocHook_PreSbrkHook hook);
-
typedef void (*MallocHook_SbrkHook)(const void* result, ptrdiff_t increment);
-int MallocHook_AddSbrkHook(MallocHook_SbrkHook hook);
-int MallocHook_RemoveSbrkHook(MallocHook_SbrkHook hook);
#ifdef __cplusplus
} /* extern "C" */
diff --git a/absl/base/internal/per_thread_tls.h b/absl/base/internal/per_thread_tls.h
index 7397451..2428bdc 100644
--- a/absl/base/internal/per_thread_tls.h
+++ b/absl/base/internal/per_thread_tls.h
@@ -26,7 +26,7 @@
//
// Microsoft C supports thread-local storage.
// GCC supports it if the appropriate version of glibc is available,
-// which the programme can indicate by defining ABSL_HAVE_TLS
+// which the programmer can indicate by defining ABSL_HAVE_TLS
#include "absl/base/port.h" // For ABSL_HAVE_TLS
diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc
index 301b108..86e34d4 100644
--- a/absl/base/internal/raw_logging.cc
+++ b/absl/base/internal/raw_logging.cc
@@ -22,7 +22,7 @@
#include "absl/base/config.h"
#include "absl/base/internal/atomic_hook.h"
-#include "absl/base/internal/log_severity.h"
+#include "absl/base/log_severity.h"
// We know how to perform low-level writes to stderr in POSIX and Windows. For
// these platforms, we define the token ABSL_LOW_LEVEL_WRITE_SUPPORTED.
diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h
index 568d2af..1b2a44b 100644
--- a/absl/base/internal/raw_logging.h
+++ b/absl/base/internal/raw_logging.h
@@ -20,7 +20,7 @@
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
#include "absl/base/attributes.h"
-#include "absl/base/internal/log_severity.h"
+#include "absl/base/log_severity.h"
#include "absl/base/macros.h"
#include "absl/base/port.h"
diff --git a/absl/base/internal/spinlock.h b/absl/base/internal/spinlock.h
index a9037e3..212abc6 100644
--- a/absl/base/internal/spinlock.h
+++ b/absl/base/internal/spinlock.h
@@ -227,7 +227,7 @@ inline uint32_t SpinLock::TryLockInternal(uint32_t lock_value,
kSpinLockHeld | lock_value | wait_cycles | sched_disabled_bit,
std::memory_order_acquire, std::memory_order_relaxed)) {
} else {
- base_internal::SchedulingGuard::EnableRescheduling(sched_disabled_bit);
+ base_internal::SchedulingGuard::EnableRescheduling(sched_disabled_bit != 0);
}
return lock_value;
diff --git a/absl/base/internal/thread_identity.h b/absl/base/internal/thread_identity.h
index 914d5da..a51722f 100644
--- a/absl/base/internal/thread_identity.h
+++ b/absl/base/internal/thread_identity.h
@@ -61,7 +61,7 @@ struct PerThreadSynch {
PerThreadSynch *next; // Circular waiter queue; initialized to 0.
PerThreadSynch *skip; // If non-zero, all entries in Mutex queue
- // upto and including "skip" have same
+ // up to and including "skip" have same
// condition as this, and will be woken later
bool may_skip; // if false while on mutex queue, a mutex unlocker
// is using this PerThreadSynch as a terminator. Its
diff --git a/absl/base/internal/log_severity.h b/absl/base/log_severity.h
index deaf6a5..deaf6a5 100644
--- a/absl/base/internal/log_severity.h
+++ b/absl/base/log_severity.h
diff --git a/absl/base/macros.h b/absl/base/macros.h
index d414087..5ae0f05 100644
--- a/absl/base/macros.h
+++ b/absl/base/macros.h
@@ -146,7 +146,7 @@ enum LinkerInitialized {
// Every usage of a deprecated entity will trigger a warning when compiled with
// clang's `-Wdeprecated-declarations` option. This option is turned off by
// default, but the warnings will be reported by clang-tidy.
-#if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
+#if defined(__clang__) && __cplusplus >= 201103L
#define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
#endif
diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel
index 7d550cb..8bdf631 100644
--- a/absl/container/BUILD.bazel
+++ b/absl/container/BUILD.bazel
@@ -18,6 +18,7 @@ load(
"//absl:copts.bzl",
"ABSL_DEFAULT_COPTS",
"ABSL_TEST_COPTS",
+ "ABSL_EXCEPTIONS_FLAG",
)
package(default_visibility = ["//visibility:public"])
@@ -33,16 +34,16 @@ cc_library(
"//absl/base:core_headers",
"//absl/base:dynamic_annotations",
"//absl/base:throw_delegate",
+ "//absl/memory",
],
)
cc_test(
name = "fixed_array_test",
srcs = ["fixed_array_test.cc"],
- copts = ABSL_TEST_COPTS + ["-fexceptions"],
+ copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
deps = [
":fixed_array",
- "//absl/base:core_headers",
"//absl/base:exception_testing",
"//absl/memory",
"@com_google_googletest//:gtest_main",
@@ -55,7 +56,6 @@ cc_test(
copts = ABSL_TEST_COPTS,
deps = [
":fixed_array",
- "//absl/base:core_headers",
"//absl/base:exception_testing",
"//absl/memory",
"@com_google_googletest//:gtest_main",
@@ -77,7 +77,7 @@ cc_library(
cc_test(
name = "inlined_vector_test",
srcs = ["inlined_vector_test.cc"],
- copts = ABSL_TEST_COPTS + ["-fexceptions"],
+ copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
deps = [
":inlined_vector",
":test_instance_tracker",
diff --git a/absl/container/fixed_array.h b/absl/container/fixed_array.h
index 58240b4..b92d905 100644
--- a/absl/container/fixed_array.h
+++ b/absl/container/fixed_array.h
@@ -47,6 +47,7 @@
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/base/port.h"
+#include "absl/memory/memory.h"
namespace absl {
@@ -107,6 +108,15 @@ class FixedArray {
? kInlineBytesDefault / sizeof(value_type)
: inlined;
+ FixedArray(const FixedArray& other) : rep_(other.begin(), other.end()) {}
+ FixedArray(FixedArray&& other) noexcept(
+ // clang-format off
+ absl::allocator_is_nothrow<std::allocator<value_type>>::value &&
+ // clang-format on
+ std::is_nothrow_move_constructible<value_type>::value)
+ : rep_(std::make_move_iterator(other.begin()),
+ std::make_move_iterator(other.end())) {}
+
// Creates an array object that can store `n` elements.
// Note that trivially constructible elements will be uninitialized.
explicit FixedArray(size_type n) : rep_(n) {}
@@ -126,11 +136,9 @@ class FixedArray {
~FixedArray() {}
- // Copy and move construction and assignment are deleted because (1) you can't
- // copy or move an array, (2) assignment breaks the invariant that the size of
- // a `FixedArray` never changes, and (3) there's no clear answer as to what
- // should happen to a moved-from `FixedArray`.
- FixedArray(const FixedArray&) = delete;
+ // Assignments are deleted because they break the invariant that the size of a
+ // `FixedArray` never changes.
+ void operator=(FixedArray&&) = delete;
void operator=(const FixedArray&) = delete;
// FixedArray::size()
@@ -167,6 +175,7 @@ class FixedArray {
// fixed array. This pointer can be used to access and modify the contained
// elements.
pointer data() { return AsValue(rep_.begin()); }
+
// FixedArray::operator[]
//
// Returns a reference the ith element of the fixed array.
@@ -449,7 +458,7 @@ class FixedArray {
// Loop optimizes to nothing for trivially destructible T.
for (Holder* p = end(); p != begin();) (--p)->~Holder();
if (IsAllocated(size())) {
- ::operator delete[](begin());
+ std::allocator<Holder>().deallocate(p_, n_);
} else {
this->AnnotateDestruct(size());
}
@@ -461,17 +470,13 @@ class FixedArray {
private:
Holder* MakeHolder(size_type n) {
if (IsAllocated(n)) {
- return Allocate(n);
+ return std::allocator<Holder>().allocate(n);
} else {
this->AnnotateConstruct(n);
return this->data();
}
}
- Holder* Allocate(size_type n) {
- return static_cast<Holder*>(::operator new[](n * sizeof(Holder)));
- }
-
bool IsAllocated(size_type n) const { return n > inline_elements; }
const size_type n_;
diff --git a/absl/container/fixed_array_test.cc b/absl/container/fixed_array_test.cc
index 9e88eab..2142132 100644
--- a/absl/container/fixed_array_test.cc
+++ b/absl/container/fixed_array_test.cc
@@ -27,6 +27,8 @@
#include "absl/base/internal/exception_testing.h"
#include "absl/memory/memory.h"
+using ::testing::ElementsAreArray;
+
namespace {
// Helper routine to determine if a absl::FixedArray used stack allocation.
@@ -89,6 +91,41 @@ class ThreeInts {
int ThreeInts::counter = 0;
+TEST(FixedArrayTest, CopyCtor) {
+ absl::FixedArray<int, 10> on_stack(5);
+ std::iota(on_stack.begin(), on_stack.end(), 0);
+ absl::FixedArray<int, 10> stack_copy = on_stack;
+ EXPECT_THAT(stack_copy, ElementsAreArray(on_stack));
+ EXPECT_TRUE(IsOnStack(stack_copy));
+
+ absl::FixedArray<int, 10> allocated(15);
+ std::iota(allocated.begin(), allocated.end(), 0);
+ absl::FixedArray<int, 10> alloced_copy = allocated;
+ EXPECT_THAT(alloced_copy, ElementsAreArray(allocated));
+ EXPECT_FALSE(IsOnStack(alloced_copy));
+}
+
+TEST(FixedArrayTest, MoveCtor) {
+ absl::FixedArray<std::unique_ptr<int>, 10> on_stack(5);
+ for (int i = 0; i < 5; ++i) {
+ on_stack[i] = absl::make_unique<int>(i);
+ }
+
+ absl::FixedArray<std::unique_ptr<int>, 10> stack_copy = std::move(on_stack);
+ for (int i = 0; i < 5; ++i) EXPECT_EQ(*(stack_copy[i]), i);
+ EXPECT_EQ(stack_copy.size(), on_stack.size());
+
+ absl::FixedArray<std::unique_ptr<int>, 10> allocated(15);
+ for (int i = 0; i < 15; ++i) {
+ allocated[i] = absl::make_unique<int>(i);
+ }
+
+ absl::FixedArray<std::unique_ptr<int>, 10> alloced_copy =
+ std::move(allocated);
+ for (int i = 0; i < 15; ++i) EXPECT_EQ(*(alloced_copy[i]), i);
+ EXPECT_EQ(allocated.size(), alloced_copy.size());
+}
+
TEST(FixedArrayTest, SmallObjects) {
// Small object arrays
{
@@ -467,6 +504,7 @@ struct PickyDelete {
TEST(FixedArrayTest, UsesGlobalAlloc) { absl::FixedArray<PickyDelete, 0> a(5); }
+
TEST(FixedArrayTest, Data) {
static const int kInput[] = { 2, 3, 5, 7, 11, 13, 17 };
absl::FixedArray<int> fa(std::begin(kInput), std::end(kInput));
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h
index 4bf9c18..feba87b 100644
--- a/absl/container/inlined_vector.h
+++ b/absl/container/inlined_vector.h
@@ -573,6 +573,42 @@ class InlinedVector {
}
}
+ // InlinedVector::shrink_to_fit()
+ //
+ // Reduces memory usage by freeing unused memory.
+ // After this call `capacity()` will be equal to `max(N, size())`.
+ //
+ // If `size() <= N` and the elements are currently stored on the heap, they
+ // will be moved to the inlined storage and the heap memory deallocated.
+ // If `size() > N` and `size() < capacity()` the elements will be moved to
+ // a reallocated storage on heap.
+ void shrink_to_fit() {
+ const auto s = size();
+ if (!allocated() || s == capacity()) {
+ // There's nothing to deallocate.
+ return;
+ }
+
+ if (s <= N) {
+ // Move the elements to the inlined storage.
+ // We have to do this using a temporary, because inlined_storage and
+ // allocation_storage are in a union field.
+ auto temp = std::move(*this);
+ assign(std::make_move_iterator(temp.begin()),
+ std::make_move_iterator(temp.end()));
+ return;
+ }
+
+ // 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.
+ Allocation new_allocation(allocator(), s);
+ UninitializedCopy(std::make_move_iterator(allocated_space()),
+ std::make_move_iterator(allocated_space() + s),
+ new_allocation.buffer());
+ ResetAllocation(new_allocation, s);
+ }
+
// InlinedVector::swap()
//
// Swaps the contents of this inlined vector with the contents of `other`.
diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc
index 8527ba1..0e39855 100644
--- a/absl/container/inlined_vector_test.cc
+++ b/absl/container/inlined_vector_test.cc
@@ -407,6 +407,69 @@ TEST(InlinedVectorTest, EmplaceBack) {
EXPECT_EQ(allocated_element.second, 1729);
}
+TEST(InlinedVectorTest, ShrinkToFitGrowingVector) {
+ absl::InlinedVector<std::pair<std::string, int>, 1> v;
+
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 1);
+
+ v.emplace_back("answer", 42);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 1);
+
+ v.emplace_back("taxicab", 1729);
+ EXPECT_GE(v.capacity(), 2);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 2);
+
+ v.reserve(100);
+ EXPECT_GE(v.capacity(), 100);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 2);
+}
+
+TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
+ {
+ absl::InlinedVector<std::pair<std::string, int>, 1> v;
+ v.emplace_back("answer", 42);
+ v.emplace_back("taxicab", 1729);
+ EXPECT_GE(v.capacity(), 2);
+ v.pop_back();
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 1);
+ EXPECT_EQ(v[0].first, "answer");
+ EXPECT_EQ(v[0].second, 42);
+ }
+
+ {
+ absl::InlinedVector<std::string, 2> v(100);
+ v.resize(0);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 2); // inlined capacity
+ }
+
+ {
+ absl::InlinedVector<std::string, 2> v(100);
+ v.resize(1);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 2); // inlined capacity
+ }
+
+ {
+ absl::InlinedVector<std::string, 2> v(100);
+ v.resize(2);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 2);
+ }
+
+ {
+ absl::InlinedVector<std::string, 2> v(100);
+ v.resize(3);
+ v.shrink_to_fit();
+ EXPECT_EQ(v.capacity(), 3);
+ }
+}
+
TEST(IntVec, Insert) {
for (int len = 0; len < 20; len++) {
for (int pos = 0; pos <= len; pos++) {
@@ -1589,6 +1652,20 @@ TEST(AllocatorSupportTest, CountAllocations) {
AllocVec v3(std::move(v), alloc3);
EXPECT_THAT(allocated3, v3.size() * sizeof(int));
}
+ EXPECT_EQ(allocated, 0);
+ {
+ // Test shrink_to_fit deallocations.
+ AllocVec v(8, 2, alloc);
+ EXPECT_EQ(allocated, 8 * sizeof(int));
+ v.resize(5);
+ EXPECT_EQ(allocated, 8 * sizeof(int));
+ v.shrink_to_fit();
+ EXPECT_EQ(allocated, 5 * sizeof(int));
+ v.resize(4);
+ EXPECT_EQ(allocated, 5 * sizeof(int));
+ v.shrink_to_fit();
+ EXPECT_EQ(allocated, 0);
+ }
}
TEST(AllocatorSupportTest, SwapBothAllocated) {
diff --git a/absl/copts.bzl b/absl/copts.bzl
index fa111a0..20c9b61 100644
--- a/absl/copts.bzl
+++ b/absl/copts.bzl
@@ -10,7 +10,6 @@ GCC_FLAGS = [
"-Wcast-qual",
"-Wconversion-null",
"-Wmissing-declarations",
- "-Wno-sign-compare",
"-Woverlength-strings",
"-Wpointer-arith",
"-Wunused-local-typedefs",
@@ -18,6 +17,9 @@ GCC_FLAGS = [
"-Wvarargs",
"-Wvla", # variable-length array
"-Wwrite-strings",
+ # Google style does not use unsigned integers, though STL containers
+ # have unsigned types.
+ "-Wno-sign-compare",
]
GCC_TEST_FLAGS = [
@@ -34,36 +36,43 @@ GCC_TEST_FLAGS = [
# Docs on groups of flags is preceded by ###.
LLVM_FLAGS = [
+ # All warnings are treated as errors by implicit -Werror flag
"-Wall",
"-Wextra",
"-Weverything",
# Abseil does not support C++98
"-Wno-c++98-compat-pedantic",
- "-Wno-comma",
# Turns off all implicit conversion warnings. Most are re-enabled below.
"-Wno-conversion",
"-Wno-covered-switch-default",
"-Wno-deprecated",
"-Wno-disabled-macro-expansion",
"-Wno-double-promotion",
- "-Wno-exit-time-destructors",
+ ###
+ # Turned off as they include valid C++ code.
+ "-Wno-comma",
"-Wno-extra-semi",
+ "-Wno-packed",
+ "-Wno-padded",
+ ###
"-Wno-float-conversion",
"-Wno-float-equal",
"-Wno-format-nonliteral",
- # Too aggressive: warns on Clang extensions enclosed in Clang-only code paths.
+ # Too aggressive: warns on Clang extensions enclosed in Clang-only
+ # compilation paths.
"-Wno-gcc-compat",
+ ###
+ # Some internal globals are necessary. Don't do this at home.
"-Wno-global-constructors",
+ "-Wno-exit-time-destructors",
+ ###
"-Wno-nested-anon-types",
"-Wno-non-modular-include-in-module",
"-Wno-old-style-cast",
- "-Wno-packed",
- "-Wno-padded",
# Warns on preferred usage of non-POD types such as string_view
"-Wno-range-loop-analysis",
"-Wno-reserved-id-macro",
"-Wno-shorten-64-to-32",
- "-Wno-sign-conversion",
"-Wno-switch-enum",
"-Wno-thread-safety-negative",
"-Wno-undef",
@@ -84,6 +93,7 @@ LLVM_FLAGS = [
"-Wnon-literal-null-conversion",
"-Wnull-conversion",
"-Wobjc-literal-conversion",
+ "-Wno-sign-conversion",
"-Wstring-conversion",
###
]
@@ -108,7 +118,7 @@ LLVM_TEST_FLAGS = [
MSVC_FLAGS = [
"/W3",
"/WX",
- "/wd4005", # macro-redifinition
+ "/wd4005", # macro-redefinition
"/wd4068", # unknown pragma
"/wd4244", # conversion from 'type1' to 'type2', possible loss of data
"/wd4267", # conversion from 'size_t' to 'type', possible loss of data
diff --git a/absl/debugging/internal/stacktrace_aarch64-inl.inc b/absl/debugging/internal/stacktrace_aarch64-inl.inc
index c125ea2..45802e7 100644
--- a/absl/debugging/internal/stacktrace_aarch64-inl.inc
+++ b/absl/debugging/internal/stacktrace_aarch64-inl.inc
@@ -178,4 +178,12 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
return n;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return true;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_
diff --git a/absl/debugging/internal/stacktrace_arm-inl.inc b/absl/debugging/internal/stacktrace_arm-inl.inc
index 566b6d3..c840833 100644
--- a/absl/debugging/internal/stacktrace_arm-inl.inc
+++ b/absl/debugging/internal/stacktrace_arm-inl.inc
@@ -112,4 +112,12 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
return n;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return false;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
diff --git a/absl/debugging/internal/stacktrace_generic-inl.inc b/absl/debugging/internal/stacktrace_generic-inl.inc
index de4881e..2c9ca41 100644
--- a/absl/debugging/internal/stacktrace_generic-inl.inc
+++ b/absl/debugging/internal/stacktrace_generic-inl.inc
@@ -48,4 +48,12 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
return result_count;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return true;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_
diff --git a/absl/debugging/internal/stacktrace_powerpc-inl.inc b/absl/debugging/internal/stacktrace_powerpc-inl.inc
index 9193e2d..4b11326 100644
--- a/absl/debugging/internal/stacktrace_powerpc-inl.inc
+++ b/absl/debugging/internal/stacktrace_powerpc-inl.inc
@@ -232,4 +232,12 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
return n;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return true;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_
diff --git a/absl/debugging/internal/stacktrace_unimplemented-inl.inc b/absl/debugging/internal/stacktrace_unimplemented-inl.inc
index a66be77..e256fdd 100644
--- a/absl/debugging/internal/stacktrace_unimplemented-inl.inc
+++ b/absl/debugging/internal/stacktrace_unimplemented-inl.inc
@@ -11,4 +11,12 @@ static int UnwindImpl(void** /* result */, int* /* sizes */,
return 0;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return false;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_UNIMPLEMENTED_INL_H_
diff --git a/absl/debugging/internal/stacktrace_win32-inl.inc b/absl/debugging/internal/stacktrace_win32-inl.inc
index 4c7f855..a8f8a56 100644
--- a/absl/debugging/internal/stacktrace_win32-inl.inc
+++ b/absl/debugging/internal/stacktrace_win32-inl.inc
@@ -72,4 +72,12 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
return n;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return false;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_
diff --git a/absl/debugging/internal/stacktrace_x86-inl.inc b/absl/debugging/internal/stacktrace_x86-inl.inc
index 9bdaa54..7c146ad 100644
--- a/absl/debugging/internal/stacktrace_x86-inl.inc
+++ b/absl/debugging/internal/stacktrace_x86-inl.inc
@@ -326,4 +326,12 @@ static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
return n;
}
+namespace absl {
+namespace debugging_internal {
+bool StackTraceWorksForTest() {
+ return true;
+}
+} // namespace debugging_internal
+} // namespace absl
+
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_
diff --git a/absl/debugging/stacktrace.h b/absl/debugging/stacktrace.h
index 0aab574..82da3f1 100644
--- a/absl/debugging/stacktrace.h
+++ b/absl/debugging/stacktrace.h
@@ -155,6 +155,13 @@ extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth,
int skip_count, const void* uc,
int* min_dropped_frames);
+namespace debugging_internal {
+// Returns true for platforms which are expected to have functioning stack trace
+// implementations. Intended to be used for tests which want to exclude
+// verification of logic known to be broken because stack traces are not
+// working.
+extern bool StackTraceWorksForTest();
+} // namespace debugging_internal
} // namespace absl
#endif // ABSL_DEBUGGING_STACKTRACE_H_
diff --git a/absl/memory/memory.h b/absl/memory/memory.h
index 22d44b9..2220ee4 100644
--- a/absl/memory/memory.h
+++ b/absl/memory/memory.h
@@ -319,13 +319,23 @@ struct RebindPtr<T, U, void_t<typename T::template rebind<U>>> {
using type = typename T::template rebind<U>;
};
-template <typename T, typename U, typename = void>
+template <typename T, typename U>
+constexpr bool HasRebindAlloc(...) {
+ return false;
+}
+
+template <typename T, typename U>
+constexpr bool HasRebindAlloc(typename T::template rebind<U>::other*) {
+ return true;
+}
+
+template <typename T, typename U, bool = HasRebindAlloc<T, U>(nullptr)>
struct RebindAlloc {
using type = typename RebindFirstArg<T, U>::type;
};
template <typename T, typename U>
-struct RebindAlloc<T, U, void_t<typename T::template rebind<U>::other>> {
+struct RebindAlloc<T, U, true> {
using type = typename T::template rebind<U>::other;
};
diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc
index 7d047ca..dee9b48 100644
--- a/absl/memory/memory_test.cc
+++ b/absl/memory/memory_test.cc
@@ -439,6 +439,20 @@ TEST(AllocatorTraits, Typedefs) {
}
template <typename T>
+struct AllocWithPrivateInheritance : private std::allocator<T> {
+ using value_type = T;
+};
+
+TEST(AllocatorTraits, RebindWithPrivateInheritance) {
+ // Regression test for some versions of gcc that do not like the sfinae we
+ // used in combination with private inheritance.
+ EXPECT_TRUE(
+ (std::is_same<AllocWithPrivateInheritance<int>,
+ absl::allocator_traits<AllocWithPrivateInheritance<char>>::
+ rebind_alloc<int>>::value));
+}
+
+template <typename T>
struct Rebound {};
struct AllocWithRebind {
diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h
index 6f7138c..f36a59a 100644
--- a/absl/meta/type_traits.h
+++ b/absl/meta/type_traits.h
@@ -148,11 +148,16 @@ struct negation : std::integral_constant<bool, !T::value> {};
template <typename T>
struct is_trivially_destructible
: std::integral_constant<bool, __has_trivial_destructor(T) &&
- std::is_destructible<T>::value> {
+ std::is_destructible<T>::value> {
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
- static_assert(std::is_trivially_destructible<T>::value ==
- is_trivially_destructible::value,
- "Not compliant with std::is_trivially_destructible");
+ static constexpr bool compliant = std::is_trivially_destructible<T>::value ==
+ is_trivially_destructible::value;
+ static_assert(compliant || std::is_trivially_destructible<T>::value,
+ "Not compliant with std::is_trivially_destructible; "
+ "Standard: false, Implementation: true");
+ static_assert(compliant || !std::is_trivially_destructible<T>::value,
+ "Not compliant with std::is_trivially_destructible; "
+ "Standard: true, Implementation: false");
#endif // ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
};
@@ -186,18 +191,23 @@ struct is_trivially_destructible
// GCC bug 51452: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452
// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116.
//
-// "T obj();" need to be well-formed and not call any non-trivial operation.
+// "T obj();" need to be well-formed and not call any nontrivial operation.
// Nontrivally destructible types will cause the expression to be nontrivial.
template <typename T>
struct is_trivially_default_constructible
- : std::integral_constant<bool,
- __has_trivial_constructor(T) &&
- std::is_default_constructible<T>::value &&
- is_trivially_destructible<T>::value> {
+ : std::integral_constant<bool, __has_trivial_constructor(T) &&
+ std::is_default_constructible<T>::value &&
+ is_trivially_destructible<T>::value> {
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
- static_assert(std::is_trivially_default_constructible<T>::value ==
- is_trivially_default_constructible::value,
- "Not compliant with std::is_trivially_default_constructible");
+ static constexpr bool compliant =
+ std::is_trivially_default_constructible<T>::value ==
+ is_trivially_default_constructible::value;
+ static_assert(compliant || std::is_trivially_default_constructible<T>::value,
+ "Not compliant with std::is_trivially_default_constructible; "
+ "Standard: false, Implementation: true");
+ static_assert(compliant || !std::is_trivially_default_constructible<T>::value,
+ "Not compliant with std::is_trivially_default_constructible; "
+ "Standard: true, Implementation: false");
#endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
};
@@ -217,12 +227,18 @@ struct is_trivially_default_constructible
template <typename T>
struct is_trivially_copy_constructible
: std::integral_constant<bool, __has_trivial_copy(T) &&
- std::is_copy_constructible<T>::value &&
- is_trivially_destructible<T>::value> {
+ std::is_copy_constructible<T>::value &&
+ is_trivially_destructible<T>::value> {
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
- static_assert(std::is_trivially_copy_constructible<T>::value ==
- is_trivially_copy_constructible::value,
- "Not compliant with std::is_trivially_copy_constructible");
+ static constexpr bool compliant =
+ std::is_trivially_copy_constructible<T>::value ==
+ is_trivially_copy_constructible::value;
+ static_assert(compliant || std::is_trivially_copy_constructible<T>::value,
+ "Not compliant with std::is_trivially_copy_constructible; "
+ "Standard: false, Implementation: true");
+ static_assert(compliant || !std::is_trivially_copy_constructible<T>::value,
+ "Not compliant with std::is_trivially_copy_constructible; "
+ "Standard: true, Implementation: false");
#endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
};
@@ -240,15 +256,21 @@ struct is_trivially_copy_constructible
// `declval<T>() = declval<U>()` is well-formed when treated as an unevaluated
// operand. `is_trivially_assignable<T, U>` requires the assignment to call no
// operation that is not trivial. `is_trivially_copy_assignable<T>` is simply
-// `is_trivially_assignable<T, const T&>`.
+// `is_trivially_assignable<T&, const T&>`.
template <typename T>
struct is_trivially_copy_assignable
: std::integral_constant<bool, __has_trivial_assign(T) &&
- std::is_copy_assignable<T>::value> {
+ std::is_copy_assignable<T>::value> {
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
- static_assert(std::is_trivially_copy_assignable<T>::value ==
- is_trivially_copy_assignable::value,
- "Not compliant with std::is_trivially_copy_assignable");
+ static constexpr bool compliant =
+ std::is_trivially_copy_assignable<T>::value ==
+ is_trivially_copy_assignable::value;
+ static_assert(compliant || std::is_trivially_copy_assignable<T>::value,
+ "Not compliant with std::is_trivially_copy_assignable; "
+ "Standard: false, Implementation: true");
+ static_assert(compliant || !std::is_trivially_copy_assignable<T>::value,
+ "Not compliant with std::is_trivially_copy_assignable; "
+ "Standard: true, Implementation: false");
#endif // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
};
diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc
index 67d1c45..c44d1c5 100644
--- a/absl/meta/type_traits_test.cc
+++ b/absl/meta/type_traits_test.cc
@@ -99,6 +99,18 @@ class Trivial {
int n_;
};
+struct TrivialDestructor {
+ ~TrivialDestructor() = default;
+};
+
+struct NontrivialDestructor {
+ ~NontrivialDestructor() {}
+};
+
+struct DeletedDestructor {
+ ~DeletedDestructor() = delete;
+};
+
class TrivialDefaultCtor {
public:
TrivialDefaultCtor() = default;
@@ -108,6 +120,23 @@ class TrivialDefaultCtor {
int n_;
};
+class NontrivialDefaultCtor {
+ public:
+ NontrivialDefaultCtor() : n_(1) {}
+
+ private:
+ int n_;
+};
+
+class DeletedDefaultCtor {
+ public:
+ DeletedDefaultCtor() = delete;
+ explicit DeletedDefaultCtor(int n) : n_(n) {}
+
+ private:
+ int n_;
+};
+
class TrivialCopyCtor {
public:
explicit TrivialCopyCtor(int n) : n_(n) {}
@@ -121,22 +150,57 @@ class TrivialCopyCtor {
int n_;
};
+class NontrivialCopyCtor {
+ public:
+ explicit NontrivialCopyCtor(int n) : n_(n) {}
+ NontrivialCopyCtor(const NontrivialCopyCtor& t) : n_(t.n_) {}
+ NontrivialCopyCtor& operator=(const NontrivialCopyCtor&) = default;
+
+ private:
+ int n_;
+};
+
+class DeletedCopyCtor {
+ public:
+ explicit DeletedCopyCtor(int n) : n_(n) {}
+ DeletedCopyCtor(const DeletedCopyCtor&) = delete;
+ DeletedCopyCtor& operator=(const DeletedCopyCtor&) = default;
+
+ private:
+ int n_;
+};
+
class TrivialCopyAssign {
public:
explicit TrivialCopyAssign(int n) : n_(n) {}
TrivialCopyAssign(const TrivialCopyAssign& t) : n_(t.n_) {}
TrivialCopyAssign& operator=(const TrivialCopyAssign& t) = default;
- ~TrivialCopyAssign() {} // can have non trivial destructor
+ ~TrivialCopyAssign() {} // can have nontrivial destructor
private:
int n_;
};
-struct NonTrivialDestructor {
- ~NonTrivialDestructor() {}
+class NontrivialCopyAssign {
+ public:
+ explicit NontrivialCopyAssign(int n) : n_(n) {}
+ NontrivialCopyAssign(const NontrivialCopyAssign&) = default;
+ NontrivialCopyAssign& operator=(const NontrivialCopyAssign& t) {
+ n_ = t.n_;
+ return *this;
+ }
+
+ private:
+ int n_;
};
-struct TrivialDestructor {
- ~TrivialDestructor() = default;
+class DeletedCopyAssign {
+ public:
+ explicit DeletedCopyAssign(int n) : n_(n) {}
+ DeletedCopyAssign(const DeletedCopyAssign&) = default;
+ DeletedCopyAssign& operator=(const DeletedCopyAssign&) = delete;
+
+ private:
+ int n_;
};
struct NonCopyable {
@@ -152,19 +216,105 @@ class Base {
// In GCC/Clang, std::is_trivially_constructible requires that the destructor is
// trivial. However, MSVC doesn't require that. This results in different
-// behavior when checking is_trivially_constructible on any type with nontrivial
-// destructor. Since absl::is_trivially_default_constructible and
+// behavior when checking is_trivially_constructible on any type with
+// nontrivial destructor. Since absl::is_trivially_default_constructible and
// absl::is_trivially_copy_constructible both follows Clang/GCC's interpretation
// and check is_trivially_destructible, it results in inconsistency with
// std::is_trivially_xxx_constructible on MSVC. This macro is used to work
// around this issue in test. In practice, a trivially constructible type
// should also be trivially destructible.
// GCC bug 51452: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452
-// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116.
-#ifdef _MSC_VER
-#define ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE
+// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116
+#ifndef _MSC_VER
+#define ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE 1
#endif
+// Old versions of libc++, around Clang 3.5 to 3.6, consider deleted destructors
+// as also being trivial. With the resolution of CWG 1928 and CWG 1734, this
+// is no longer considered true and has thus been amended.
+// Compiler Explorer: https://godbolt.org/g/zT59ZL
+// CWG issue 1734: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1734
+// CWG issue 1928: http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1928
+#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 3700
+#define ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL 1
+#endif
+
+// As of the moment, GCC versions >5.1 have a problem compiling for
+// std::is_trivially_default_constructible<NontrivialDestructor[10]>, where
+// NontrivialDestructor is a struct with a custom nontrivial destructor. Note
+// that this problem only occurs for arrays of a known size, so something like
+// std::is_trivially_default_constructible<NontrivialDestructor[]> does not
+// have any problems.
+// Compiler Explorer: https://godbolt.org/g/dXRbdK
+// GCC bug 83689: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83689
+#if defined(__clang__) || defined(_MSC_VER) || \
+ (defined(__GNUC__) && __GNUC__ < 5)
+#define ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL 1
+#endif
+
+TEST(TypeTraitsTest, TestTrivialDestructor) {
+ // Verify that arithmetic types and pointers have trivial destructors.
+ EXPECT_TRUE(absl::is_trivially_destructible<bool>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<char>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<unsigned char>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<signed char>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<wchar_t>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<int>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<unsigned int>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<int16_t>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<uint16_t>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<int64_t>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<uint64_t>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<float>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<double>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<long double>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<std::string*>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<Trivial*>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<const std::string*>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<const Trivial*>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<std::string**>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<Trivial**>::value);
+
+ // classes with destructors
+ EXPECT_TRUE(absl::is_trivially_destructible<Trivial>::value);
+ EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor>::value);
+
+ // Verify that types with a nontrivial or deleted destructor
+ // are marked as such.
+ EXPECT_FALSE(absl::is_trivially_destructible<NontrivialDestructor>::value);
+#ifdef ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL
+ EXPECT_FALSE(absl::is_trivially_destructible<DeletedDestructor>::value);
+#endif
+
+ // simple_pair of such types is trivial
+ EXPECT_TRUE((absl::is_trivially_destructible<simple_pair<int, int>>::value));
+ EXPECT_TRUE((absl::is_trivially_destructible<
+ simple_pair<Trivial, TrivialDestructor>>::value));
+
+ // Verify that types without trivial destructors are correctly marked as such.
+ EXPECT_FALSE(absl::is_trivially_destructible<std::string>::value);
+ EXPECT_FALSE(absl::is_trivially_destructible<std::vector<int>>::value);
+
+ // Verify that simple_pairs of types without trivial destructors
+ // are not marked as trivial.
+ EXPECT_FALSE((absl::is_trivially_destructible<
+ simple_pair<int, std::string>>::value));
+ EXPECT_FALSE((absl::is_trivially_destructible<
+ simple_pair<std::string, int>>::value));
+
+ // array of such types is trivial
+ using int10 = int[10];
+ EXPECT_TRUE(absl::is_trivially_destructible<int10>::value);
+ using Trivial10 = Trivial[10];
+ EXPECT_TRUE(absl::is_trivially_destructible<Trivial10>::value);
+ using TrivialDestructor10 = TrivialDestructor[10];
+ EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor10>::value);
+
+ // Conversely, the opposite also holds.
+ using NontrivialDestructor10 = NontrivialDestructor[10];
+ EXPECT_FALSE(absl::is_trivially_destructible<NontrivialDestructor10>::value);
+}
+
TEST(TypeTraitsTest, TestTrivialDefaultCtor) {
// arithmetic types and pointers have trivial default constructors.
EXPECT_TRUE(absl::is_trivially_default_constructible<bool>::value);
@@ -184,42 +334,67 @@ TEST(TypeTraitsTest, TestTrivialDefaultCtor) {
EXPECT_TRUE(absl::is_trivially_default_constructible<std::string*>::value);
EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial*>::value);
EXPECT_TRUE(
- absl::is_trivially_default_constructible<const TrivialCopyCtor*>::value);
- EXPECT_TRUE(
- absl::is_trivially_default_constructible<TrivialCopyCtor**>::value);
+ absl::is_trivially_default_constructible<const std::string*>::value);
+ EXPECT_TRUE(absl::is_trivially_default_constructible<const Trivial*>::value);
+ EXPECT_TRUE(absl::is_trivially_default_constructible<std::string**>::value);
+ EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial**>::value);
// types with compiler generated default ctors
EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial>::value);
EXPECT_TRUE(
absl::is_trivially_default_constructible<TrivialDefaultCtor>::value);
-#ifndef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE
- // types with non trivial destructor are non trivial
+ // Verify that types without them are not.
EXPECT_FALSE(
- absl::is_trivially_default_constructible<NonTrivialDestructor>::value);
+ absl::is_trivially_default_constructible<NontrivialDefaultCtor>::value);
+ EXPECT_FALSE(
+ absl::is_trivially_default_constructible<DeletedDefaultCtor>::value);
+
+#ifdef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE
+ // types with nontrivial destructor are nontrivial
+ EXPECT_FALSE(
+ absl::is_trivially_default_constructible<NontrivialDestructor>::value);
#endif
// types with vtables
EXPECT_FALSE(absl::is_trivially_default_constructible<Base>::value);
- // Verify that arrays of such types are trivially default constructible
- typedef int int10[10];
- EXPECT_TRUE(absl::is_trivially_default_constructible<int10>::value);
- typedef Trivial Trivial10[10];
- EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial10>::value);
- typedef Trivial TrivialDefaultCtor10[10];
- EXPECT_TRUE(
- absl::is_trivially_default_constructible<TrivialDefaultCtor10>::value);
-
// Verify that simple_pair has trivial constructors where applicable.
EXPECT_TRUE((absl::is_trivially_default_constructible<
simple_pair<int, char*>>::value));
+ EXPECT_TRUE((absl::is_trivially_default_constructible<
+ simple_pair<int, Trivial>>::value));
+ EXPECT_TRUE((absl::is_trivially_default_constructible<
+ simple_pair<int, TrivialDefaultCtor>>::value));
// Verify that types without trivial constructors are
// correctly marked as such.
EXPECT_FALSE(absl::is_trivially_default_constructible<std::string>::value);
EXPECT_FALSE(
absl::is_trivially_default_constructible<std::vector<int>>::value);
+
+ // Verify that simple_pairs of types without trivial constructors
+ // are not marked as trivial.
+ EXPECT_FALSE((absl::is_trivially_default_constructible<
+ simple_pair<int, std::string>>::value));
+ EXPECT_FALSE((absl::is_trivially_default_constructible<
+ simple_pair<std::string, int>>::value));
+
+ // Verify that arrays of such types are trivially default constructible
+ using int10 = int[10];
+ EXPECT_TRUE(absl::is_trivially_default_constructible<int10>::value);
+ using Trivial10 = Trivial[10];
+ EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial10>::value);
+ using TrivialDefaultCtor10 = TrivialDefaultCtor[10];
+ EXPECT_TRUE(
+ absl::is_trivially_default_constructible<TrivialDefaultCtor10>::value);
+
+ // Conversely, the opposite also holds.
+#ifdef ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL
+ using NontrivialDefaultCtor10 = NontrivialDefaultCtor[10];
+ EXPECT_FALSE(
+ absl::is_trivially_default_constructible<NontrivialDefaultCtor10>::value);
+#endif
}
TEST(TypeTraitsTest, TestTrivialCopyCtor) {
@@ -241,18 +416,26 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) {
EXPECT_TRUE(absl::is_trivially_copy_constructible<long double>::value);
EXPECT_TRUE(absl::is_trivially_copy_constructible<std::string*>::value);
EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial*>::value);
- EXPECT_TRUE(
- absl::is_trivially_copy_constructible<const TrivialCopyCtor*>::value);
- EXPECT_TRUE(absl::is_trivially_copy_constructible<TrivialCopyCtor**>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_constructible<const std::string*>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_constructible<const Trivial*>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_constructible<std::string**>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial**>::value);
// types with compiler generated copy ctors
EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial>::value);
EXPECT_TRUE(absl::is_trivially_copy_constructible<TrivialCopyCtor>::value);
-#ifndef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE
- // type with non-trivial destructor are non-trivial copy construbtible
+ // Verify that types without them (i.e. nontrivial or deleted) are not.
+ EXPECT_FALSE(
+ absl::is_trivially_copy_constructible<NontrivialCopyCtor>::value);
+ EXPECT_FALSE(absl::is_trivially_copy_constructible<DeletedCopyCtor>::value);
+ EXPECT_FALSE(
+ absl::is_trivially_copy_constructible<NonCopyable>::value);
+
+#ifdef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE
+ // type with nontrivial destructor are nontrivial copy construbtible
EXPECT_FALSE(
- absl::is_trivially_copy_constructible<NonTrivialDestructor>::value);
+ absl::is_trivially_copy_constructible<NontrivialDestructor>::value);
#endif
// types with vtables
@@ -266,9 +449,10 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) {
EXPECT_TRUE((absl::is_trivially_copy_constructible<
simple_pair<int, TrivialCopyCtor>>::value));
- // Verify that arrays are not
- typedef int int10[10];
- EXPECT_FALSE(absl::is_trivially_copy_constructible<int10>::value);
+ // Verify that types without trivial copy constructors are
+ // correctly marked as such.
+ EXPECT_FALSE(absl::is_trivially_copy_constructible<std::string>::value);
+ EXPECT_FALSE(absl::is_trivially_copy_constructible<std::vector<int>>::value);
// Verify that simple_pairs of types without trivial copy constructors
// are not marked as trivial.
@@ -277,18 +461,14 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) {
EXPECT_FALSE((absl::is_trivially_copy_constructible<
simple_pair<std::string, int>>::value));
- // Verify that types without trivial copy constructors are
- // correctly marked as such.
- EXPECT_FALSE(absl::is_trivially_copy_constructible<std::string>::value);
- EXPECT_FALSE(absl::is_trivially_copy_constructible<std::vector<int>>::value);
-
- // types with deleted copy constructors are not copy constructible
- EXPECT_FALSE(absl::is_trivially_copy_constructible<NonCopyable>::value);
+ // Verify that arrays are not
+ using int10 = int[10];
+ EXPECT_FALSE(absl::is_trivially_copy_constructible<int10>::value);
}
TEST(TypeTraitsTest, TestTrivialCopyAssign) {
// Verify that arithmetic types and pointers have trivial copy
- // constructors.
+ // assignment operators.
EXPECT_TRUE(absl::is_trivially_copy_assignable<bool>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<char>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<unsigned char>::value);
@@ -305,9 +485,10 @@ TEST(TypeTraitsTest, TestTrivialCopyAssign) {
EXPECT_TRUE(absl::is_trivially_copy_assignable<long double>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<std::string*>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial*>::value);
- EXPECT_TRUE(
- absl::is_trivially_copy_assignable<const TrivialCopyCtor*>::value);
- EXPECT_TRUE(absl::is_trivially_copy_assignable<TrivialCopyCtor**>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_assignable<const std::string*>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_assignable<const Trivial*>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_assignable<std::string**>::value);
+ EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial**>::value);
// const qualified types are not assignable
EXPECT_FALSE(absl::is_trivially_copy_assignable<const int>::value);
@@ -316,65 +497,37 @@ TEST(TypeTraitsTest, TestTrivialCopyAssign) {
EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<TrivialCopyAssign>::value);
+ // Verify that types without them (i.e. nontrivial or deleted) are not.
+ EXPECT_FALSE(absl::is_trivially_copy_assignable<NontrivialCopyAssign>::value);
+ EXPECT_FALSE(absl::is_trivially_copy_assignable<DeletedCopyAssign>::value);
+ EXPECT_FALSE(absl::is_trivially_copy_assignable<NonCopyable>::value);
+
// types with vtables
EXPECT_FALSE(absl::is_trivially_copy_assignable<Base>::value);
- // Verify that arrays are not trivially copy assignable
- typedef int int10[10];
- EXPECT_FALSE(absl::is_trivially_copy_assignable<int10>::value);
-
// Verify that simple_pair is trivially assignable
EXPECT_TRUE(
(absl::is_trivially_copy_assignable<simple_pair<int, char*>>::value));
+ EXPECT_TRUE(
+ (absl::is_trivially_copy_assignable<simple_pair<int, Trivial>>::value));
+ EXPECT_TRUE((absl::is_trivially_copy_assignable<
+ simple_pair<int, TrivialCopyAssign>>::value));
- // Verify that types without trivial copy constructors are
+ // Verify that types not trivially copy assignable are
// correctly marked as such.
EXPECT_FALSE(absl::is_trivially_copy_assignable<std::string>::value);
EXPECT_FALSE(absl::is_trivially_copy_assignable<std::vector<int>>::value);
- // types with deleted copy assignment are not copy assignable
- EXPECT_FALSE(absl::is_trivially_copy_assignable<NonCopyable>::value);
-}
-
-TEST(TypeTraitsTest, TestTrivialDestructor) {
- // Verify that arithmetic types and pointers have trivial copy
- // constructors.
- EXPECT_TRUE(absl::is_trivially_destructible<bool>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<char>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<unsigned char>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<signed char>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<wchar_t>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<int>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<unsigned int>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<int16_t>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<uint16_t>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<int64_t>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<uint64_t>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<float>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<double>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<long double>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<std::string*>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<Trivial*>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<const TrivialCopyCtor*>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<TrivialCopyCtor**>::value);
-
- // classes with destructors
- EXPECT_TRUE(absl::is_trivially_destructible<Trivial>::value);
- EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor>::value);
- EXPECT_FALSE(absl::is_trivially_destructible<NonTrivialDestructor>::value);
-
- // simple_pair of such types is trivial
- EXPECT_TRUE((absl::is_trivially_destructible<simple_pair<int, int>>::value));
- EXPECT_TRUE((absl::is_trivially_destructible<
- simple_pair<Trivial, TrivialDestructor>>::value));
+ // Verify that simple_pairs of types not trivially copy assignable
+ // are not marked as trivial.
+ EXPECT_FALSE((absl::is_trivially_copy_assignable<
+ simple_pair<int, std::string>>::value));
+ EXPECT_FALSE((absl::is_trivially_copy_assignable<
+ simple_pair<std::string, int>>::value));
- // array of such types is trivial
- typedef int int10[10];
- EXPECT_TRUE(absl::is_trivially_destructible<int10>::value);
- typedef TrivialDestructor TrivialDestructor10[10];
- EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor10>::value);
- typedef NonTrivialDestructor NonTrivialDestructor10[10];
- EXPECT_FALSE(absl::is_trivially_destructible<NonTrivialDestructor10>::value);
+ // Verify that arrays are not trivially copy assignable
+ using int10 = int[10];
+ EXPECT_FALSE(absl::is_trivially_copy_assignable<int10>::value);
}
#define ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(trait_name, ...) \
diff --git a/absl/numeric/int128.cc b/absl/numeric/int128.cc
index 00bf7f4..de1b997 100644
--- a/absl/numeric/int128.cc
+++ b/absl/numeric/int128.cc
@@ -139,9 +139,9 @@ uint128& uint128::operator%=(uint128 other) {
return *this;
}
-std::ostream& operator<<(std::ostream& o, uint128 b) {
- std::ios_base::fmtflags flags = o.flags();
+namespace {
+std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) {
// Select a divisor which is the largest power of the base < 2^64.
uint128 div;
int div_base_log;
@@ -160,14 +160,14 @@ std::ostream& operator<<(std::ostream& o, uint128 b) {
break;
}
- // Now piece together the uint128 representation from three chunks of
- // the original value, each less than "div" and therefore representable
- // as a uint64_t.
+ // Now piece together the uint128 representation from three chunks of the
+ // original value, each less than "div" and therefore representable as a
+ // uint64_t.
std::ostringstream os;
std::ios_base::fmtflags copy_mask =
std::ios::basefield | std::ios::showbase | std::ios::uppercase;
os.setf(flags & copy_mask, copy_mask);
- uint128 high = b;
+ uint128 high = v;
uint128 low;
DivModImpl(high, div, &high, &low);
uint128 mid;
@@ -182,25 +182,31 @@ std::ostream& operator<<(std::ostream& o, uint128 b) {
os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
}
os << Uint128Low64(low);
- std::string rep = os.str();
+ return os.str();
+}
+
+} // namespace
+
+std::ostream& operator<<(std::ostream& os, uint128 v) {
+ std::ios_base::fmtflags flags = os.flags();
+ std::string rep = Uint128ToFormattedString(v, flags);
// Add the requisite padding.
- std::streamsize width = o.width(0);
+ std::streamsize width = os.width(0);
if (static_cast<size_t>(width) > rep.size()) {
std::ios::fmtflags adjustfield = flags & std::ios::adjustfield;
if (adjustfield == std::ios::left) {
- rep.append(width - rep.size(), o.fill());
+ rep.append(width - rep.size(), os.fill());
} else if (adjustfield == std::ios::internal &&
(flags & std::ios::showbase) &&
- (flags & std::ios::basefield) == std::ios::hex && b != 0) {
- rep.insert(2, width - rep.size(), o.fill());
+ (flags & std::ios::basefield) == std::ios::hex && v != 0) {
+ rep.insert(2, width - rep.size(), os.fill());
} else {
- rep.insert(0, width - rep.size(), o.fill());
+ rep.insert(0, width - rep.size(), os.fill());
}
}
- // Stream the final representation in a single "<<" call.
- return o << rep;
+ return os << rep;
}
} // namespace absl
diff --git a/absl/numeric/int128.h b/absl/numeric/int128.h
index d87cbbd..5e1b3f0 100644
--- a/absl/numeric/int128.h
+++ b/absl/numeric/int128.h
@@ -62,7 +62,7 @@ namespace absl {
// However, a `uint128` differs from intrinsic integral types in the following
// ways:
//
-// * Errors on implicit conversions that does not preserve value (such as
+// * Errors on implicit conversions that do not preserve value (such as
// loss of precision when converting to float values).
// * Requires explicit construction from and conversion to floating point
// types.
@@ -71,8 +71,8 @@ namespace absl {
//
// Example:
//
-// float y = absl::kuint128max; // Error. uint128 cannot be implicitly
-// // converted to float.
+// float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly
+// // converted to float.
//
// absl::uint128 v;
// absl::uint64_t i = v; // Error
@@ -175,10 +175,15 @@ class alignas(16) uint128 {
// Example:
//
// absl::uint128 big = absl::MakeUint128(1, 0);
- friend constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom);
+ friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low);
+
+ // Uint128Max()
+ //
+ // Returns the highest value for a 128-bit unsigned integer.
+ friend constexpr uint128 Uint128Max();
private:
- constexpr uint128(uint64_t top, uint64_t bottom);
+ constexpr uint128(uint64_t high, uint64_t low);
// TODO(strel) Update implementation to use __int128 once all users of
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add
@@ -195,10 +200,13 @@ class alignas(16) uint128 {
#endif // byte order
};
+// Prefer to use the constexpr `Uint128Max()`.
+//
+// TODO(absl-team) deprecate kuint128max once migration tool is released.
extern const uint128 kuint128max;
// allow uint128 to be logged
-extern std::ostream& operator<<(std::ostream& o, uint128 b);
+extern std::ostream& operator<<(std::ostream& os, uint128 v);
// TODO(strel) add operator>>(std::istream&, uint128)
@@ -208,8 +216,13 @@ extern std::ostream& operator<<(std::ostream& o, uint128 b);
// Implementation details follow
// --------------------------------------------------------------------------
-constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom) {
- return uint128(top, bottom);
+constexpr uint128 MakeUint128(uint64_t high, uint64_t low) {
+ return uint128(high, low);
+}
+
+constexpr uint128 Uint128Max() {
+ return uint128(std::numeric_limits<uint64_t>::max(),
+ std::numeric_limits<uint64_t>::max());
}
// Assignment from integer types.
@@ -251,33 +264,19 @@ inline uint128& uint128::operator=(unsigned __int128 v) {
// Shift and arithmetic operators.
-inline uint128 operator<<(uint128 lhs, int amount) {
- return uint128(lhs) <<= amount;
-}
+inline uint128 operator<<(uint128 lhs, int amount) { return lhs <<= amount; }
-inline uint128 operator>>(uint128 lhs, int amount) {
- return uint128(lhs) >>= amount;
-}
+inline uint128 operator>>(uint128 lhs, int amount) { return lhs >>= amount; }
-inline uint128 operator+(uint128 lhs, uint128 rhs) {
- return uint128(lhs) += rhs;
-}
+inline uint128 operator+(uint128 lhs, uint128 rhs) { return lhs += rhs; }
-inline uint128 operator-(uint128 lhs, uint128 rhs) {
- return uint128(lhs) -= rhs;
-}
+inline uint128 operator-(uint128 lhs, uint128 rhs) { return lhs -= rhs; }
-inline uint128 operator*(uint128 lhs, uint128 rhs) {
- return uint128(lhs) *= rhs;
-}
+inline uint128 operator*(uint128 lhs, uint128 rhs) { return lhs *= rhs; }
-inline uint128 operator/(uint128 lhs, uint128 rhs) {
- return uint128(lhs) /= rhs;
-}
+inline uint128 operator/(uint128 lhs, uint128 rhs) { return lhs /= rhs; }
-inline uint128 operator%(uint128 lhs, uint128 rhs) {
- return uint128(lhs) %= rhs;
-}
+inline uint128 operator%(uint128 lhs, uint128 rhs) { return lhs %= rhs; }
constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; }
@@ -287,8 +286,8 @@ constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; }
#if defined(ABSL_IS_LITTLE_ENDIAN)
-constexpr uint128::uint128(uint64_t top, uint64_t bottom)
- : lo_(bottom), hi_(top) {}
+constexpr uint128::uint128(uint64_t high, uint64_t low)
+ : lo_(low), hi_(high) {}
constexpr uint128::uint128(int v)
: lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
@@ -314,8 +313,8 @@ constexpr uint128::uint128(unsigned __int128 v)
#elif defined(ABSL_IS_BIG_ENDIAN)
-constexpr uint128::uint128(uint64_t top, uint64_t bottom)
- : hi_(top), lo_(bottom) {}
+constexpr uint128::uint128(uint64_t high, uint64_t low)
+ : hi_(high), lo_(low) {}
constexpr uint128::uint128(int v)
: hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {}
@@ -460,13 +459,10 @@ inline bool operator>=(uint128 lhs, uint128 rhs) {
// Unary operators.
inline uint128 operator-(uint128 val) {
- const uint64_t hi_flip = ~Uint128High64(val);
- const uint64_t lo_flip = ~Uint128Low64(val);
- const uint64_t lo_add = lo_flip + 1;
- if (lo_add < lo_flip) {
- return MakeUint128(hi_flip + 1, lo_add);
- }
- return MakeUint128(hi_flip, lo_add);
+ uint64_t hi = ~Uint128High64(val);
+ uint64_t lo = ~Uint128Low64(val) + 1;
+ if (lo == 0) ++hi; // carry
+ return MakeUint128(hi, lo);
}
inline bool operator!(uint128 val) {
@@ -515,8 +511,8 @@ inline uint128& uint128::operator^=(uint128 other) {
// Shift and arithmetic assign operators.
inline uint128& uint128::operator<<=(int amount) {
- // Shifts of >= 128 are undefined.
- assert(amount < 128);
+ assert(amount >= 0); // Negative shifts are undefined.
+ assert(amount < 128); // Shifts of >= 128 are undefined.
// uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing.
@@ -533,8 +529,8 @@ inline uint128& uint128::operator<<=(int amount) {
}
inline uint128& uint128::operator>>=(int amount) {
- // Shifts of >= 128 are undefined.
- assert(amount < 128);
+ assert(amount >= 0); // Negative shifts are undefined.
+ assert(amount < 128); // Shifts of >= 128 are undefined.
// uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing.
@@ -574,25 +570,14 @@ inline uint128& uint128::operator*=(uint128 other) {
static_cast<unsigned __int128>(other);
return *this;
#else // ABSL_HAVE_INTRINSIC128
- uint64_t a96 = hi_ >> 32;
- uint64_t a64 = hi_ & 0xffffffff;
uint64_t a32 = lo_ >> 32;
uint64_t a00 = lo_ & 0xffffffff;
- uint64_t b96 = other.hi_ >> 32;
- uint64_t b64 = other.hi_ & 0xffffffff;
uint64_t b32 = other.lo_ >> 32;
uint64_t b00 = other.lo_ & 0xffffffff;
- // multiply [a96 .. a00] x [b96 .. b00]
- // terms higher than c96 disappear off the high side
- // terms c96 and c64 are safe to ignore carry bit
- uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
- uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
- this->hi_ = (c96 << 32) + c64;
- this->lo_ = 0;
- // add terms after this one at a time to capture carry
+ hi_ = hi_ * other.lo_ + lo_ * other.hi_ + a32 * b32;
+ lo_ = a00 * b00;
*this += uint128(a32 * b00) << 32;
*this += uint128(a00 * b32) << 32;
- *this += a00 * b00;
return *this;
#endif // ABSL_HAVE_INTRINSIC128
}
diff --git a/absl/numeric/int128_have_intrinsic.inc b/absl/numeric/int128_have_intrinsic.inc
index 49bde07..ee2a093 100644
--- a/absl/numeric/int128_have_intrinsic.inc
+++ b/absl/numeric/int128_have_intrinsic.inc
@@ -1,3 +1,18 @@
-// This file will contain :int128 implementation details that depend on internal
-// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file will be
+//
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// This file contains :int128 implementation details that depend on internal
+// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is
// included by int128.h.
diff --git a/absl/numeric/int128_no_intrinsic.inc b/absl/numeric/int128_no_intrinsic.inc
index 2dbff2b..0d0b3cf 100644
--- a/absl/numeric/int128_no_intrinsic.inc
+++ b/absl/numeric/int128_no_intrinsic.inc
@@ -1,3 +1,18 @@
-// This file will contain :int128 implementation details that depend on internal
+//
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// This file contains :int128 implementation details that depend on internal
// representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file
-// will be included by int128.h.
+// is included by int128.h.
diff --git a/absl/numeric/int128_test.cc b/absl/numeric/int128_test.cc
index d674cb1..79bcca9 100644
--- a/absl/numeric/int128_test.cc
+++ b/absl/numeric/int128_test.cc
@@ -110,7 +110,7 @@ TEST(Uint128, AllTests) {
absl::uint128 big = absl::MakeUint128(2000, 2);
absl::uint128 big_minus_one = absl::MakeUint128(2000, 1);
absl::uint128 bigger = absl::MakeUint128(2001, 1);
- absl::uint128 biggest = absl::kuint128max;
+ absl::uint128 biggest = absl::Uint128Max();
absl::uint128 high_low = absl::MakeUint128(1, 0);
absl::uint128 low_high =
absl::MakeUint128(0, std::numeric_limits<uint64_t>::max());
@@ -227,8 +227,10 @@ TEST(Uint128, AllTests) {
EXPECT_EQ(big, -(-big));
EXPECT_EQ(two, -((-one) - 1));
- EXPECT_EQ(absl::kuint128max, -one);
+ EXPECT_EQ(absl::Uint128Max(), -one);
EXPECT_EQ(zero, -zero);
+
+ EXPECT_EQ(absl::Uint128Max(), absl::kuint128max);
}
TEST(Uint128, ConversionTests) {
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel
index f627258..13badb7 100644
--- a/absl/strings/BUILD.bazel
+++ b/absl/strings/BUILD.bazel
@@ -39,6 +39,7 @@ cc_library(
"escaping.cc",
"internal/memutil.cc",
"internal/memutil.h",
+ "internal/stl_type_traits.h",
"internal/str_join_internal.h",
"internal/str_split_internal.h",
"match.cc",
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index f75b55a..83cb934 100644
--- a/absl/strings/CMakeLists.txt
+++ b/absl/strings/CMakeLists.txt
@@ -35,6 +35,7 @@ list(APPEND STRINGS_INTERNAL_HEADERS
"internal/memutil.h"
"internal/ostringstream.h"
"internal/resize_uninitialized.h"
+ "internal/stl_type_traits.h"
"internal/str_join_internal.h"
"internal/str_split_internal.h"
"internal/utf8.h"
diff --git a/absl/strings/escaping.h b/absl/strings/escaping.h
index 86f63aa..1af0afa 100644
--- a/absl/strings/escaping.h
+++ b/absl/strings/escaping.h
@@ -152,7 +152,7 @@ std::string HexStringToBytes(absl::string_view from);
// BytesToHexString()
//
-// Converts binary data into an ASCII text std::string, returing a std::string of size
+// Converts binary data into an ASCII text std::string, returning a std::string of size
// `2*from.size()`.
std::string BytesToHexString(absl::string_view from);
diff --git a/absl/strings/internal/stl_type_traits.h b/absl/strings/internal/stl_type_traits.h
new file mode 100644
index 0000000..04c4a53
--- /dev/null
+++ b/absl/strings/internal/stl_type_traits.h
@@ -0,0 +1,246 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Thie file provides the IsStrictlyBaseOfAndConvertibleToSTLContainer type
+// trait metafunction to assist in working with the _GLIBCXX_DEBUG debug
+// wrappers of STL containers.
+//
+// DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
+// absl/strings/str_split.h.
+//
+// IWYU pragma: private, include "absl/strings/str_split.h"
+
+#ifndef ABSL_STRINGS_INTERNAL_STL_TYPE_TRAITS_H_
+#define ABSL_STRINGS_INTERNAL_STL_TYPE_TRAITS_H_
+
+#include <array>
+#include <bitset>
+#include <deque>
+#include <forward_list>
+#include <list>
+#include <map>
+#include <set>
+#include <type_traits>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "absl/meta/type_traits.h"
+
+namespace absl {
+namespace strings_internal {
+
+template <typename C, template <typename...> class T>
+struct IsSpecializationImpl : std::false_type {};
+template <template <typename...> class T, typename... Args>
+struct IsSpecializationImpl<T<Args...>, T> : std::true_type {};
+template <typename C, template <typename...> class T>
+using IsSpecialization = IsSpecializationImpl<absl::decay_t<C>, T>;
+
+template <typename C>
+struct IsArrayImpl : std::false_type {};
+template <template <typename, size_t> class A, typename T, size_t N>
+struct IsArrayImpl<A<T, N>> : std::is_same<A<T, N>, std::array<T, N>> {};
+template <typename C>
+using IsArray = IsArrayImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsBitsetImpl : std::false_type {};
+template <template <size_t> class B, size_t N>
+struct IsBitsetImpl<B<N>> : std::is_same<B<N>, std::bitset<N>> {};
+template <typename C>
+using IsBitset = IsBitsetImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsSTLContainer
+ : absl::disjunction<
+ IsArray<C>, IsBitset<C>, IsSpecialization<C, std::deque>,
+ IsSpecialization<C, std::forward_list>,
+ IsSpecialization<C, std::list>, IsSpecialization<C, std::map>,
+ IsSpecialization<C, std::multimap>, IsSpecialization<C, std::set>,
+ IsSpecialization<C, std::multiset>,
+ IsSpecialization<C, std::unordered_map>,
+ IsSpecialization<C, std::unordered_multimap>,
+ IsSpecialization<C, std::unordered_set>,
+ IsSpecialization<C, std::unordered_multiset>,
+ IsSpecialization<C, std::vector>> {};
+
+template <typename C, template <typename...> class T, typename = void>
+struct IsBaseOfSpecializationImpl : std::false_type {};
+// IsBaseOfSpecializationImpl needs multiple partial specializations to SFINAE
+// on the existence of container dependent types and plug them into the STL
+// template.
+template <typename C, template <typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T, absl::void_t<typename C::value_type, typename C::allocator_type>>
+ : std::is_base_of<C,
+ T<typename C::value_type, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>>
+ : std::is_base_of<C,
+ T<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>> {
+};
+template <typename C, template <typename, typename, typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>> {
+};
+template <typename C,
+ template <typename, typename, typename, typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
+template <typename C, template <typename...> class T>
+using IsBaseOfSpecialization = IsBaseOfSpecializationImpl<absl::decay_t<C>, T>;
+
+template <typename C>
+struct IsBaseOfArrayImpl : std::false_type {};
+template <template <typename, size_t> class A, typename T, size_t N>
+struct IsBaseOfArrayImpl<A<T, N>> : std::is_base_of<A<T, N>, std::array<T, N>> {
+};
+template <typename C>
+using IsBaseOfArray = IsBaseOfArrayImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsBaseOfBitsetImpl : std::false_type {};
+template <template <size_t> class B, size_t N>
+struct IsBaseOfBitsetImpl<B<N>> : std::is_base_of<B<N>, std::bitset<N>> {};
+template <typename C>
+using IsBaseOfBitset = IsBaseOfBitsetImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsBaseOfSTLContainer
+ : absl::disjunction<IsBaseOfArray<C>, IsBaseOfBitset<C>,
+ IsBaseOfSpecialization<C, std::deque>,
+ IsBaseOfSpecialization<C, std::forward_list>,
+ IsBaseOfSpecialization<C, std::list>,
+ IsBaseOfSpecialization<C, std::map>,
+ IsBaseOfSpecialization<C, std::multimap>,
+ IsBaseOfSpecialization<C, std::set>,
+ IsBaseOfSpecialization<C, std::multiset>,
+ IsBaseOfSpecialization<C, std::unordered_map>,
+ IsBaseOfSpecialization<C, std::unordered_multimap>,
+ IsBaseOfSpecialization<C, std::unordered_set>,
+ IsBaseOfSpecialization<C, std::unordered_multiset>,
+ IsBaseOfSpecialization<C, std::vector>> {};
+
+template <typename C, template <typename...> class T, typename = void>
+struct IsConvertibleToSpecializationImpl : std::false_type {};
+// IsConvertibleToSpecializationImpl needs multiple partial specializations to
+// SFINAE on the existence of container dependent types and plug them into the
+// STL template.
+template <typename C, template <typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T, absl::void_t<typename C::value_type, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::value_type, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>>
+ : std::is_convertible<C, T<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::key_type, typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
+template <typename C,
+ template <typename, typename, typename, typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>>
+ : std::is_convertible<C, T<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
+template <typename C, template <typename...> class T>
+using IsConvertibleToSpecialization =
+ IsConvertibleToSpecializationImpl<absl::decay_t<C>, T>;
+
+template <typename C>
+struct IsConvertibleToArrayImpl : std::false_type {};
+template <template <typename, size_t> class A, typename T, size_t N>
+struct IsConvertibleToArrayImpl<A<T, N>>
+ : std::is_convertible<A<T, N>, std::array<T, N>> {};
+template <typename C>
+using IsConvertibleToArray = IsConvertibleToArrayImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsConvertibleToBitsetImpl : std::false_type {};
+template <template <size_t> class B, size_t N>
+struct IsConvertibleToBitsetImpl<B<N>>
+ : std::is_convertible<B<N>, std::bitset<N>> {};
+template <typename C>
+using IsConvertibleToBitset = IsConvertibleToBitsetImpl<absl::decay_t<C>>;
+
+template <typename C>
+struct IsConvertibleToSTLContainer
+ : absl::disjunction<
+ IsConvertibleToArray<C>, IsConvertibleToBitset<C>,
+ IsConvertibleToSpecialization<C, std::deque>,
+ IsConvertibleToSpecialization<C, std::forward_list>,
+ IsConvertibleToSpecialization<C, std::list>,
+ IsConvertibleToSpecialization<C, std::map>,
+ IsConvertibleToSpecialization<C, std::multimap>,
+ IsConvertibleToSpecialization<C, std::set>,
+ IsConvertibleToSpecialization<C, std::multiset>,
+ IsConvertibleToSpecialization<C, std::unordered_map>,
+ IsConvertibleToSpecialization<C, std::unordered_multimap>,
+ IsConvertibleToSpecialization<C, std::unordered_set>,
+ IsConvertibleToSpecialization<C, std::unordered_multiset>,
+ IsConvertibleToSpecialization<C, std::vector>> {};
+
+template <typename C>
+struct IsStrictlyBaseOfAndConvertibleToSTLContainer
+ : absl::conjunction<absl::negation<IsSTLContainer<C>>,
+ IsBaseOfSTLContainer<C>,
+ IsConvertibleToSTLContainer<C>> {};
+
+} // namespace strings_internal
+} // namespace absl
+#endif // ABSL_STRINGS_INTERNAL_STL_TYPE_TRAITS_H_
diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h
index dc31a8e..a1b10f3 100644
--- a/absl/strings/internal/str_split_internal.h
+++ b/absl/strings/internal/str_split_internal.h
@@ -29,10 +29,6 @@
#ifndef ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_
#define ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_
-#ifdef _GLIBCXX_DEBUG
-#include <glibcxx_debug_traits.h>
-#endif // _GLIBCXX_DEBUG
-
#include <array>
#include <initializer_list>
#include <iterator>
@@ -46,15 +42,13 @@
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
-namespace absl {
-namespace strings_internal {
-
#ifdef _GLIBCXX_DEBUG
-using ::glibcxx_debug_traits::IsStrictlyDebugWrapperBase;
-#else // _GLIBCXX_DEBUG
-template <typename T> struct IsStrictlyDebugWrapperBase : std::false_type {};
+#include "absl/strings/internal/stl_type_traits.h"
#endif // _GLIBCXX_DEBUG
+namespace absl {
+namespace strings_internal {
+
// This class is implicitly constructible from everything that absl::string_view
// is implicitly constructible from. If it's constructed from a temporary
// std::string, the data is moved into a data member so its lifetime matches that of
@@ -237,10 +231,12 @@ struct IsInitializerList
template <typename C>
struct SplitterIsConvertibleTo
: std::enable_if<
- !IsStrictlyDebugWrapperBase<C>::value &&
- !IsInitializerList<C>::value &&
- HasValueType<C>::value &&
- HasConstIterator<C>::value> {};
+#ifdef _GLIBCXX_DEBUG
+ !IsStrictlyBaseOfAndConvertibleToSTLContainer<C>::value &&
+#endif // _GLIBCXX_DEBUG
+ !IsInitializerList<C>::value && HasValueType<C>::value &&
+ HasConstIterator<C>::value> {
+};
// This class implements the range that is returned by absl::StrSplit(). This
// class has templated conversion operators that allow it to be implicitly
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index 5b4c9ba..1cf7b11 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -46,8 +46,7 @@
// You can convert to hexadecimal output rather than decimal output using the
// `Hex` type contained here. To do so, pass `Hex(my_int)` as a parameter to
// `StrCat()` or `StrAppend()`. You may specify a minimum hex field width using
-// a `PadSpec` enum, so the equivalent of `StringPrintf("%04x", my_int)` is
-// `absl::StrCat(absl::Hex(my_int, absl::kZeroPad4))`.
+// a `PadSpec` enum.
//
// -----------------------------------------------------------------------------
diff --git a/absl/strings/str_split_test.cc b/absl/strings/str_split_test.cc
index 9c79d7d..500f3cb 100644
--- a/absl/strings/str_split_test.cc
+++ b/absl/strings/str_split_test.cc
@@ -166,6 +166,18 @@ TEST(Split, APIExamples) {
}
{
+ // Different forms of initialization / conversion.
+ std::vector<std::string> v1 = absl::StrSplit("a,b,c", ',');
+ EXPECT_THAT(v1, ElementsAre("a", "b", "c"));
+ std::vector<std::string> v2(absl::StrSplit("a,b,c", ','));
+ EXPECT_THAT(v2, ElementsAre("a", "b", "c"));
+ auto v3 = std::vector<std::string>(absl::StrSplit("a,b,c", ','));
+ EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
+ v3 = absl::StrSplit("a,b,c", ',');
+ EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
+ }
+
+ {
// Results stored in a std::map.
std::map<std::string, std::string> m = absl::StrSplit("a,1,b,2,a,3", ',');
EXPECT_EQ(2, m.size());
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index c3acd72..ddc8934 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -419,7 +419,7 @@ class string_view {
size_type rfind(string_view s, size_type pos = npos) const
noexcept;
- // Overload of `string_view::rfind()` for finding the given character `c`
+ // Overload of `string_view::rfind()` for finding the last given character `c`
// within the `string_view`.
size_type rfind(char c, size_type pos = npos) const noexcept;
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index 13fc214..df307ac 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -796,11 +796,25 @@ TEST(StringViewTest, FrontBackSingleChar) {
EXPECT_EQ(&c, &csp.back());
}
+// `std::string_view::string_view(const char*)` calls
+// `std::char_traits<char>::length(const char*)` to get the std::string length. In
+// libc++, it doesn't allow `nullptr` in the constexpr context, with the error
+// "read of dereferenced null pointer is not allowed in a constant expression".
+// At run time, the behavior of `std::char_traits::length()` on `nullptr` is
+// undefined by the standard and usually results in crash with libc++. This
+// conforms to the standard, but `absl::string_view` implements a different
+// behavior for historical reasons. We work around tests that construct
+// `string_view` from `nullptr` when using libc++.
+#if !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
+#define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
+#endif // !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
+
TEST(StringViewTest, NULLInput) {
absl::string_view s;
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
s = absl::string_view(nullptr);
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
@@ -808,6 +822,7 @@ TEST(StringViewTest, NULLInput) {
// .ToString() on a absl::string_view with nullptr should produce the empty
// std::string.
EXPECT_EQ("", std::string(s));
+#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
}
TEST(StringViewTest, Comparisons2) {
@@ -879,7 +894,9 @@ TEST(StringViewTest, NullSafeStringView) {
TEST(StringViewTest, ConstexprCompiles) {
constexpr absl::string_view sp;
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
constexpr absl::string_view cstr(nullptr);
+#endif
constexpr absl::string_view cstr_len("cstr", 4);
#if defined(ABSL_HAVE_STD_STRING_VIEW)
@@ -923,10 +940,12 @@ TEST(StringViewTest, ConstexprCompiles) {
constexpr absl::string_view::iterator const_end_empty = sp.end();
EXPECT_EQ(const_begin_empty, const_end_empty);
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
constexpr absl::string_view::iterator const_begin_nullptr = cstr.begin();
constexpr absl::string_view::iterator const_end_nullptr = cstr.end();
EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
-#endif
+#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
+#endif // !defined(__clang__) || ...
constexpr absl::string_view::iterator const_begin = cstr_len.begin();
constexpr absl::string_view::iterator const_end = cstr_len.end();
@@ -1042,11 +1061,11 @@ TEST(HugeStringView, TwoPointTwoGB) {
}
#endif // THREAD_SANITIZER
-#ifndef NDEBUG
+#if !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
TEST(NonNegativeLenTest, NonNegativeLen) {
EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1), "len <= kMaxSize");
}
-#endif // NDEBUG
+#endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
class StringViewStreamTest : public ::testing::Test {
public:
diff --git a/absl/strings/strip.h b/absl/strings/strip.h
index 370f9e8..2f8d21f 100644
--- a/absl/strings/strip.h
+++ b/absl/strings/strip.h
@@ -67,8 +67,8 @@ inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
// Returns a view into the input std::string 'str' with the given 'prefix' removed,
// but leaving the original std::string intact. If the prefix does not match at the
// start of the std::string, returns the original std::string instead.
-inline absl::string_view StripPrefix(absl::string_view str,
- absl::string_view prefix) {
+ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
+ absl::string_view str, absl::string_view prefix) {
if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
return str;
}
@@ -78,8 +78,8 @@ inline absl::string_view StripPrefix(absl::string_view str,
// Returns a view into the input std::string 'str' with the given 'suffix' removed,
// but leaving the original std::string intact. If the suffix does not match at the
// end of the std::string, returns the original std::string instead.
-inline absl::string_view StripSuffix(absl::string_view str,
- absl::string_view suffix) {
+ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
+ absl::string_view str, absl::string_view suffix) {
if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
return str;
}
diff --git a/absl/strings/strip_test.cc b/absl/strings/strip_test.cc
index ff0e7f1..205c160 100644
--- a/absl/strings/strip_test.cc
+++ b/absl/strings/strip_test.cc
@@ -178,4 +178,24 @@ TEST(String, StripLeadingAsciiWhitespace) {
EXPECT_EQ(absl::string_view(), absl::StripLeadingAsciiWhitespace(orig));
}
+TEST(Strip, StripAsciiWhitespace) {
+ std::string test2 = "\t \f\r\n\vfoo \t\f\r\v\n";
+ absl::StripAsciiWhitespace(&test2);
+ EXPECT_EQ(test2, "foo");
+ std::string test3 = "bar";
+ absl::StripAsciiWhitespace(&test3);
+ EXPECT_EQ(test3, "bar");
+ std::string test4 = "\t \f\r\n\vfoo";
+ absl::StripAsciiWhitespace(&test4);
+ EXPECT_EQ(test4, "foo");
+ std::string test5 = "foo \t\f\r\v\n";
+ absl::StripAsciiWhitespace(&test5);
+ EXPECT_EQ(test5, "foo");
+ absl::string_view test6("\t \f\r\n\vfoo \t\f\r\v\n");
+ test6 = absl::StripAsciiWhitespace(test6);
+ EXPECT_EQ(test6, "foo");
+ test6 = absl::StripAsciiWhitespace(test6);
+ EXPECT_EQ(test6, "foo"); // already stripped
+}
+
} // namespace
diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h
index 3fc4ac4..5596a5d 100644
--- a/absl/strings/substitute.h
+++ b/absl/strings/substitute.h
@@ -45,17 +45,6 @@
// SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
// EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
//
-// Differences from `StringPrintf()`:
-// * The format std::string does not identify the types of arguments. Instead, the
-// arguments are implicitly converted to strings. See below for a list of
-// accepted types.
-// * Substitutions in the format std::string are identified by a '$' followed by a
-// single digit. You can use arguments out-of-order and use the same
-// argument multiple times.
-// * A '$$' sequence in the format std::string means output a literal '$'
-// character.
-// * `Substitute()` is significantly faster than `StringPrintf()`. For very
-// large strings, it may be orders of magnitude faster.
//
// Supported types:
// * absl::string_view, std::string, const char* (null is equivalent to "")
@@ -109,7 +98,7 @@ class Arg {
//
// Explicitly overload `const char*` so the compiler doesn't cast to `bool`.
Arg(const char* value) // NOLINT(runtime/explicit)
- : piece_(value) {}
+ : piece_(absl::NullSafeStringView(value)) {}
Arg(const std::string& value) // NOLINT(runtime/explicit)
: piece_(value) {}
Arg(absl::string_view value) // NOLINT(runtime/explicit)
@@ -157,8 +146,7 @@ class Arg {
Arg(bool value) // NOLINT(runtime/explicit)
: piece_(value ? "true" : "false") {}
// `void*` values, with the exception of `char*`, are printed as
- // `StringPrintf()` with format "%p": e.g. ("0x<hex value>").
- // However, in the case of `nullptr`, "NULL" is printed.
+ // "0x<hex value>". However, in the case of `nullptr`, "NULL" is printed.
Arg(const void* value); // NOLINT(runtime/explicit)
Arg(const Arg&) = delete;
diff --git a/absl/synchronization/lifetime_test.cc b/absl/synchronization/lifetime_test.cc
index a3e2701..90c9009 100644
--- a/absl/synchronization/lifetime_test.cc
+++ b/absl/synchronization/lifetime_test.cc
@@ -18,6 +18,7 @@
#include "absl/base/attributes.h"
#include "absl/base/internal/raw_logging.h"
+#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
@@ -106,24 +107,19 @@ void TestLocals() {
// definitions. We can use this to arrange for tests to be run on these objects
// before they are created, and after they are destroyed.
-class ConstructorTestRunner {
+using Function = void (*)();
+
+class OnConstruction {
public:
- ConstructorTestRunner(absl::Mutex* mutex, absl::CondVar* condvar,
- absl::Notification* notification) {
- RunTests(mutex, condvar, notification);
- }
+ explicit OnConstruction(Function fn) { fn(); }
};
-class DestructorTestRunner {
+class OnDestruction {
public:
- DestructorTestRunner(absl::Mutex* mutex, absl::CondVar* condvar,
- absl::Notification* notification)
- : mutex_(mutex), condvar_(condvar), notification_(notification) {}
- ~DestructorTestRunner() { RunTests(mutex_, condvar_, notification_); }
+ explicit OnDestruction(Function fn) : fn_(fn) {}
+ ~OnDestruction() { fn_(); }
private:
- absl::Mutex* mutex_;
- absl::CondVar* condvar_;
- absl::Notification* notification_;
+ Function fn_;
};
} // namespace
diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc
index 06a058c..33f92d8 100644
--- a/absl/synchronization/mutex.cc
+++ b/absl/synchronization/mutex.cc
@@ -48,7 +48,6 @@
#include "absl/base/internal/spinlock.h"
#include "absl/base/internal/sysinfo.h"
#include "absl/base/internal/thread_identity.h"
-#include "absl/base/internal/tsan_mutex_interface.h"
#include "absl/base/port.h"
#include "absl/debugging/stacktrace.h"
#include "absl/synchronization/internal/graphcycles.h"
@@ -687,10 +686,6 @@ static unsigned TsanFlags(Mutex::MuHow how) {
}
#endif
-Mutex::Mutex() : mu_(0) {
- ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
-}
-
static bool DebugOnlyIsExiting() {
return false;
}
diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h
index 26ac7f6..374cf57 100644
--- a/absl/synchronization/mutex.h
+++ b/absl/synchronization/mutex.h
@@ -64,6 +64,7 @@
#include "absl/base/internal/identity.h"
#include "absl/base/internal/low_level_alloc.h"
#include "absl/base/internal/thread_identity.h"
+#include "absl/base/internal/tsan_mutex_interface.h"
#include "absl/base/port.h"
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/internal/kernel_timeout.h"
@@ -713,7 +714,7 @@ class Condition {
// The implementation may deliver signals to any condition variable at
// any time, even when no call to `Signal()` or `SignalAll()` is made; as a
// result, upon being awoken, you must check the logical condition you have
-// been waiting upon. The implementation wakes waiters in the FIFO order.
+// been waiting upon.
//
// Examples:
//
@@ -742,29 +743,19 @@ class CondVar {
// CondVar::Wait()
//
- // Atomically releases a `Mutex` and blocks on this condition variable. After
- // blocking, the thread will unblock, reacquire the `Mutex`, and return if
- // either:
- // - this condition variable is signalled with `SignalAll()`, or
- // - this condition variable is signalled in any manner and this thread
- // was the most recently blocked thread that has not yet woken.
+ // Atomically releases a `Mutex` and blocks on this condition variable.
+ // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
+ // spurious wakeup), then reacquires the `Mutex` and returns.
+ //
// Requires and ensures that the current thread holds the `Mutex`.
void Wait(Mutex *mu);
// CondVar::WaitWithTimeout()
//
- // Atomically releases a `Mutex`, blocks on this condition variable, and
- // attempts to reacquire the mutex upon being signalled, or upon reaching the
- // timeout.
- //
- // After blocking, the thread will unblock, reacquire the `Mutex`, and return
- // for any of the following:
- // - this condition variable is signalled with `SignalAll()`
- // - the timeout has expired
- // - this condition variable is signalled in any manner and this thread
- // was the most recently blocked thread that has not yet woken.
- //
- // Negative timeouts are equivalent to a zero timeout.
+ // Atomically releases a `Mutex` and blocks on this condition variable.
+ // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
+ // spurious wakeup), or until the timeout has expired, then reacquires
+ // the `Mutex` and returns.
//
// Returns true if the timeout has expired without this `CondVar`
// being signalled in any manner. If both the timeout has expired
@@ -776,15 +767,10 @@ class CondVar {
// CondVar::WaitWithDeadline()
//
- // Atomically releases a `Mutex`, blocks on this condition variable, and
- // attempts to reacquire the mutex within the provided deadline.
- //
- // After blocking, the thread will unblock, reacquire the `Mutex`, and return
- // for any of the following:
- // - this condition variable is signalled with `SignalAll()`
- // - the deadline has passed
- // - this condition variable is signalled in any manner and this thread
- // was the most recently blocked thread that has not yet woken.
+ // Atomically releases a `Mutex` and blocks on this condition variable.
+ // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
+ // spurious wakeup), or until the deadline has passed, then reacquires
+ // the `Mutex` and returns.
//
// Deadlines in the past are equivalent to an immediate deadline.
//
@@ -875,6 +861,9 @@ class SCOPED_LOCKABLE ReleasableMutexLock {
#ifdef ABSL_INTERNAL_USE_NONPROD_MUTEX
#else
+inline Mutex::Mutex() : mu_(0) {
+ ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
+}
inline CondVar::CondVar() : cv_(0) {}
#endif
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc
index 5a5874d..53b9378 100644
--- a/absl/synchronization/mutex_test.cc
+++ b/absl/synchronization/mutex_test.cc
@@ -1234,10 +1234,12 @@ static void CheckResults(bool exp_result, bool act_result,
absl::Duration act_duration) {
ABSL_RAW_CHECK(exp_result == act_result, "CheckResults failed");
// Allow for some worse-case scheduling delay and clock skew.
- ABSL_RAW_CHECK(exp_duration - absl::Milliseconds(40) <= act_duration,
- "CheckResults failed");
- ABSL_RAW_CHECK(exp_duration + absl::Milliseconds(150) >= act_duration,
- "CheckResults failed");
+ if ((exp_duration - absl::Milliseconds(40) > act_duration) ||
+ (exp_duration + absl::Milliseconds(150) < act_duration)) {
+ ABSL_RAW_LOG(FATAL, "CheckResults failed: operation took %s, expected %s",
+ absl::FormatDuration(act_duration).c_str(),
+ absl::FormatDuration(exp_duration).c_str());
+ }
}
static void TestAwaitTimeout(Cond *cp, absl::Duration timeout, bool exp_result,
diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel
index f98d9af..c2f0338 100644
--- a/absl/types/BUILD.bazel
+++ b/absl/types/BUILD.bazel
@@ -28,7 +28,7 @@ licenses(["notice"]) # Apache 2.0
cc_library(
name = "any",
hdrs = ["any.h"],
- copts = ABSL_DEFAULT_COPTS,
+ copts = ABSL_DEFAULT_COPTS + ABSL_EXCEPTIONS_FLAG,
deps = [
":bad_any_cast",
"//absl/base:config",
@@ -83,6 +83,17 @@ cc_test(
],
)
+cc_test(
+ name = "any_exception_safety_test",
+ srcs = ["any_exception_safety_test.cc"],
+ copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+ deps = [
+ ":any",
+ "//absl/base:exception_safety_testing",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
+
cc_library(
name = "span",
hdrs = ["span.h"],
@@ -99,7 +110,7 @@ cc_test(
name = "span_test",
size = "small",
srcs = ["span_test.cc"],
- copts = ABSL_TEST_COPTS + ["-fexceptions"],
+ copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
deps = [
":span",
"//absl/base:config",
diff --git a/absl/types/CMakeLists.txt b/absl/types/CMakeLists.txt
index 3e46449..b2d95b4 100644
--- a/absl/types/CMakeLists.txt
+++ b/absl/types/CMakeLists.txt
@@ -29,6 +29,8 @@ absl_header_library(
absl_any
PUBLIC_LIBRARIES
absl::utility
+ PRIVATE_COMPILE_FLAGS
+ ${ABSL_EXCEPTIONS_FLAG}
EXPORT_NAME
any
)
@@ -126,6 +128,21 @@ absl_test(
${ANY_TEST_PUBLIC_LIBRARIES}
)
+# test any_exception_safety_test
+set(ANY_EXCEPTION_SAFETY_TEST_SRC "any_exception_safety_test.cc")
+set(ANY_EXCEPTION_SAFETY_TEST_PUBLIC_LIBRARIES absl::any absl::base)
+
+absl_test(
+ TARGET
+ any_exception_safety_test
+ SOURCES
+ ${ANY_EXCEPTION_SAFETY_TEST_SRC}
+ PUBLIC_LIBRARIES
+ ${ANY_EXCEPTION_SAFETY_TEST_PUBLIC_LIBRARIES}
+ PRIVATE_COMPILE_FLAGS
+ ${ABSL_EXCEPTIONS_FLAG}
+)
+
# test span_test
set(SPAN_TEST_SRC "span_test.cc")
diff --git a/absl/types/any.h b/absl/types/any.h
index 2e7bf21..cee9cd3 100644
--- a/absl/types/any.h
+++ b/absl/types/any.h
@@ -373,6 +373,7 @@ class any {
return typeid(void);
}
#endif // ABSL_ANY_DETAIL_HAS_RTTI
+
private:
// Tagged type-erased abstraction for holding a cloneable object.
class ObjInterface {
diff --git a/absl/types/any_exception_safety_test.cc b/absl/types/any_exception_safety_test.cc
new file mode 100644
index 0000000..eadba30
--- /dev/null
+++ b/absl/types/any_exception_safety_test.cc
@@ -0,0 +1,201 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/types/any.h"
+
+#include <typeinfo>
+#include <vector>
+
+#include "gtest/gtest.h"
+#include "absl/base/internal/exception_safety_testing.h"
+
+using Thrower = absl::ThrowingValue<>;
+using ThrowerList = std::initializer_list<Thrower>;
+using ThrowerVec = std::vector<Thrower>;
+using ThrowingAlloc = absl::ThrowingAllocator<Thrower>;
+using ThrowingThrowerVec = std::vector<Thrower, ThrowingAlloc>;
+
+namespace absl {
+
+testing::AssertionResult AbslCheckInvariants(absl::any* a,
+ InternalAbslNamespaceFinder) {
+ using testing::AssertionFailure;
+ using testing::AssertionSuccess;
+
+ if (a->has_value()) {
+ if (a->type() == typeid(void)) {
+ return AssertionFailure()
+ << "A non-empty any should not have type `void`";
+ }
+ } else {
+ if (a->type() != typeid(void)) {
+ return AssertionFailure()
+ << "An empty any should have type void, but has type "
+ << a->type().name();
+ }
+ }
+
+ // Make sure that reset() changes any to a valid state.
+ a->reset();
+ if (a->has_value()) {
+ return AssertionFailure() << "A reset `any` should be valueless";
+ }
+ if (a->type() != typeid(void)) {
+ return AssertionFailure() << "A reset `any` should have type() of `void`, "
+ "but instead has type "
+ << a->type().name();
+ }
+ try {
+ auto unused = absl::any_cast<Thrower>(*a);
+ static_cast<void>(unused);
+ return AssertionFailure()
+ << "A reset `any` should not be able to be any_cast";
+ } catch (absl::bad_any_cast) {
+ } catch (...) {
+ return AssertionFailure()
+ << "Unexpected exception thrown from absl::any_cast";
+ }
+ return AssertionSuccess();
+}
+
+} // namespace absl
+
+namespace {
+
+class AnyExceptionSafety : public ::testing::Test {
+ private:
+ absl::AllocInspector inspector_;
+};
+
+testing::AssertionResult AnyIsEmpty(absl::any* a) {
+ if (!a->has_value()) return testing::AssertionSuccess();
+ return testing::AssertionFailure()
+ << "a should be empty, but instead has value "
+ << absl::any_cast<Thrower>(*a).Get();
+}
+
+TEST_F(AnyExceptionSafety, Ctors) {
+ Thrower val(1);
+ auto with_val = absl::TestThrowingCtor<absl::any>(val);
+ auto copy = absl::TestThrowingCtor<absl::any>(with_val);
+ auto in_place =
+ absl::TestThrowingCtor<absl::any>(absl::in_place_type_t<Thrower>(), 1);
+ auto in_place_list = absl::TestThrowingCtor<absl::any>(
+ absl::in_place_type_t<ThrowerVec>(), ThrowerList{val});
+ auto in_place_list_again =
+ absl::TestThrowingCtor<absl::any,
+ absl::in_place_type_t<ThrowingThrowerVec>,
+ ThrowerList, ThrowingAlloc>(
+ absl::in_place_type_t<ThrowingThrowerVec>(), {val}, ThrowingAlloc());
+}
+
+struct OneFactory {
+ std::unique_ptr<absl::any> operator()() const {
+ return absl::make_unique<absl::any>(absl::in_place_type_t<Thrower>(), 1,
+ absl::no_throw_ctor);
+ }
+};
+
+struct EmptyFactory {
+ std::unique_ptr<absl::any> operator()() const {
+ return absl::make_unique<absl::any>();
+ }
+};
+
+TEST_F(AnyExceptionSafety, Assignment) {
+ auto thrower_comp = [](const absl::any& l, const absl::any& r) {
+ return absl::any_cast<Thrower>(l) == absl::any_cast<Thrower>(r);
+ };
+
+ OneFactory one_factory;
+
+ absl::ThrowingValue<absl::NoThrow::kMoveCtor | absl::NoThrow::kMoveAssign>
+ moveable_val(2);
+ Thrower val(2);
+ absl::any any_val(val);
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory, [&any_val](absl::any* ap) { *ap = any_val; },
+ absl::StrongGuarantee(one_factory, thrower_comp)));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory, [&val](absl::any* ap) { *ap = val; },
+ absl::StrongGuarantee(one_factory, thrower_comp)));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory, [&val](absl::any* ap) { *ap = std::move(val); },
+ absl::StrongGuarantee(one_factory, thrower_comp)));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory,
+ [&moveable_val](absl::any* ap) { *ap = std::move(moveable_val); },
+ absl::StrongGuarantee(one_factory, thrower_comp)));
+
+ EmptyFactory empty_factory;
+ auto empty_comp = [](const absl::any& l, const absl::any& r) {
+ return !(l.has_value() || r.has_value());
+ };
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ empty_factory, [&any_val](absl::any* ap) { *ap = any_val; },
+ absl::StrongGuarantee(empty_factory, empty_comp)));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ empty_factory, [&val](absl::any* ap) { *ap = val; },
+ absl::StrongGuarantee(empty_factory, empty_comp)));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ empty_factory, [&val](absl::any* ap) { *ap = std::move(val); },
+ absl::StrongGuarantee(empty_factory, empty_comp)));
+}
+// libstdc++ std::any fails this test
+#if !defined(ABSL_HAVE_STD_ANY)
+TEST_F(AnyExceptionSafety, Emplace) {
+ OneFactory one_factory;
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory, [](absl::any* ap) { ap->emplace<Thrower>(2); }, AnyIsEmpty));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ one_factory,
+ [](absl::any* ap) {
+ ap->emplace<absl::ThrowingValue<absl::NoThrow::kMoveCtor |
+ absl::NoThrow::kMoveAssign>>(2);
+ },
+ AnyIsEmpty));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(one_factory,
+ [](absl::any* ap) {
+ std::initializer_list<Thrower> il{
+ Thrower(2, absl::no_throw_ctor)};
+ ap->emplace<ThrowerVec>(il);
+ },
+ AnyIsEmpty));
+
+ EmptyFactory empty_factory;
+ EXPECT_TRUE(absl::TestExceptionSafety(
+ empty_factory, [](absl::any* ap) { ap->emplace<Thrower>(2); },
+ AnyIsEmpty));
+
+ EXPECT_TRUE(absl::TestExceptionSafety(empty_factory,
+ [](absl::any* ap) {
+ std::initializer_list<Thrower> il{
+ Thrower(2, absl::no_throw_ctor)};
+ ap->emplace<ThrowerVec>(il);
+ },
+ AnyIsEmpty));
+}
+#endif // ABSL_HAVE_STD_ANY
+
+} // namespace
diff --git a/absl/types/optional.h b/absl/types/optional.h
index 353e618..9858a97 100644
--- a/absl/types/optional.h
+++ b/absl/types/optional.h
@@ -496,7 +496,7 @@ class optional : private optional_internal::optional_data<T>,
// default constructed `T`.
constexpr optional() noexcept {}
- // Construct an` optional` initialized with `nullopt` to hold an empty value.
+ // Construct an `optional` initialized with `nullopt` to hold an empty value.
constexpr optional(nullopt_t) noexcept {} // NOLINT(runtime/explicit)
// Copy constructor, standard semantics
@@ -515,7 +515,7 @@ class optional : private optional_internal::optional_data<T>,
constexpr explicit optional(in_place_t, Args&&... args)
: data_base(in_place_t(), absl::forward<Args>(args)...) {}
- // Constructs a non-empty `optional' direct-initialized value of type `T` from
+ // Constructs a non-empty `optional` direct-initialized value of type `T` from
// the arguments of an initializer_list and `std::forward<Args>(args)...`.
// (The `in_place_t` is a tag used to indicate that the contained object
// should be constructed in-place.)
@@ -849,12 +849,20 @@ class optional : private optional_internal::optional_data<T>,
// is empty.
template <typename U>
constexpr T value_or(U&& v) const& {
+ static_assert(std::is_copy_constructible<value_type>::value,
+ "optional<T>::value_or: T must by copy constructible");
+ static_assert(std::is_convertible<U&&, value_type>::value,
+ "optional<T>::value_or: U must be convertible to T");
return static_cast<bool>(*this)
? **this
: static_cast<T>(absl::forward<U>(v));
}
template <typename U>
T value_or(U&& v) && { // NOLINT(build/c++11)
+ static_assert(std::is_move_constructible<value_type>::value,
+ "optional<T>::value_or: T must by copy constructible");
+ static_assert(std::is_convertible<U&&, value_type>::value,
+ "optional<T>::value_or: U must be convertible to T");
return static_cast<bool>(*this) ? std::move(**this)
: static_cast<T>(std::forward<U>(v));
}
diff --git a/absl/types/span.h b/absl/types/span.h
index e2abe78..d1ef97a 100644
--- a/absl/types/span.h
+++ b/absl/types/span.h
@@ -152,11 +152,11 @@ bool LessThanImpl(Span<T> a, Span<T> b) {
template <typename From, typename To>
struct IsConvertibleHelper {
private:
- static std::true_type test(To);
- static std::false_type test(...);
+ static std::true_type testval(To);
+ static std::false_type testval(...);
public:
- using type = decltype(test(std::declval<From>()));
+ using type = decltype(testval(std::declval<From>()));
};
template <typename From, typename To>
@@ -660,7 +660,7 @@ bool operator>=(Span<T> a, const U& b) {
// MyRoutine(my_vector); // error, type mismatch
//
// // Explicitly constructing the Span is verbose
-// MyRoutine(absl::Span<MyComplicatedType>(my_vector);
+// MyRoutine(absl::Span<MyComplicatedType>(my_vector));
//
// // Use MakeSpan() to make an absl::Span<T>
// MyRoutine(absl::MakeSpan(my_vector));
diff --git a/absl/utility/BUILD.bazel b/absl/utility/BUILD.bazel
index 396b05f..c01b49b 100644
--- a/absl/utility/BUILD.bazel
+++ b/absl/utility/BUILD.bazel
@@ -10,10 +10,10 @@ licenses(["notice"]) # Apache 2.0
cc_library(
name = "utility",
- srcs = ["utility.cc"],
hdrs = ["utility.h"],
copts = ABSL_DEFAULT_COPTS,
deps = [
+ "//absl/base:base_internal",
"//absl/base:config",
"//absl/meta:type_traits",
],
@@ -26,6 +26,8 @@ cc_test(
deps = [
":utility",
"//absl/base:core_headers",
+ "//absl/memory",
+ "//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/utility/CMakeLists.txt b/absl/utility/CMakeLists.txt
index daf3417..fe8b32b 100644
--- a/absl/utility/CMakeLists.txt
+++ b/absl/utility/CMakeLists.txt
@@ -19,26 +19,14 @@ list(APPEND UTILITY_PUBLIC_HEADERS
"utility.h"
)
-
-
-list(APPEND UTILITY_SRC
- "utility.cc"
- ${UTILITY_PUBLIC_HEADERS}
-)
-
-absl_library(
+absl_header_library(
TARGET
absl_utility
- SOURCES
- ${UTILITY_SRC}
- PUBLIC_LIBRARIES
- ${UTILITY_PUBLIC_LIBRARIES}
EXPORT_NAME
utility
)
-
#
## TESTS
#
diff --git a/absl/utility/utility.h b/absl/utility/utility.h
index 732cd4c..1943c4a 100644
--- a/absl/utility/utility.h
+++ b/absl/utility/utility.h
@@ -23,6 +23,7 @@
// * make_integer_sequence<T, N> == std::make_integer_sequence<T, N>
// * make_index_sequence<N> == std::make_index_sequence<N>
// * index_sequence_for<Ts...> == std::index_sequence_for<Ts...>
+// * apply<Functor, Tuple> == std::apply<Functor, Tuple>
//
// This header file also provides the tag types `in_place_t`, `in_place_type_t`,
// and `in_place_index_t`, as well as the constant `in_place`, and
@@ -31,34 +32,21 @@
// References:
//
// http://en.cppreference.com/w/cpp/utility/integer_sequence
+// http://en.cppreference.com/w/cpp/utility/apply
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
//
-//
-// Example:
-// // Unpack a tuple for use as a function call's argument list.
-//
-// template <typename F, typename Tup, size_t... Is>
-// auto Impl(F f, const Tup& tup, absl::index_sequence<Is...>)
-// -> decltype(f(std::get<Is>(tup) ...)) {
-// return f(std::get<Is>(tup) ...);
-// }
-//
-// template <typename Tup>
-// using TupIdxSeq = absl::make_index_sequence<std::tuple_size<Tup>::value>;
-//
-// template <typename F, typename Tup>
-// auto ApplyFromTuple(F f, const Tup& tup)
-// -> decltype(Impl(f, tup, TupIdxSeq<Tup>{})) {
-// return Impl(f, tup, TupIdxSeq<Tup>{});
-// }
+
#ifndef ABSL_UTILITY_UTILITY_H_
#define ABSL_UTILITY_UTILITY_H_
#include <cstddef>
#include <cstdlib>
+#include <tuple>
#include <utility>
#include "absl/base/config.h"
+#include "absl/base/internal/inline_variable.h"
+#include "absl/base/internal/invoke.h"
#include "absl/meta/type_traits.h"
namespace absl {
@@ -168,7 +156,8 @@ using std::in_place;
// `absl::optional`, designed to be a drop-in replacement for C++17's
// `std::in_place_t`.
struct in_place_t {};
-extern const in_place_t in_place;
+
+ABSL_INTERNAL_INLINE_CONSTEXPR(in_place_t, in_place, {});
#endif // ABSL_HAVE_STD_OPTIONAL
@@ -214,6 +203,62 @@ constexpr T&& forward(
return static_cast<T&&>(t);
}
+namespace utility_internal {
+// Helper method for expanding tuple into a called method.
+template <typename Functor, typename Tuple, std::size_t... Indexes>
+auto apply_helper(Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
+ -> decltype(absl::base_internal::Invoke(
+ absl::forward<Functor>(functor),
+ std::get<Indexes>(absl::forward<Tuple>(t))...)) {
+ return absl::base_internal::Invoke(
+ absl::forward<Functor>(functor),
+ std::get<Indexes>(absl::forward<Tuple>(t))...);
+}
+
+} // namespace utility_internal
+
+// apply
+//
+// Invokes a Callable using elements of a tuple as its arguments.
+// Each element of the tuple corresponds to an argument of the call (in order).
+// Both the Callable argument and the tuple argument are perfect-forwarded.
+// For member-function Callables, the first tuple element acts as the `this`
+// pointer. `absl::apply` is designed to be a drop-in replacement for C++17's
+// `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
+//
+// Example:
+//
+// class Foo{void Bar(int);};
+// void user_function(int, std::string);
+// void user_function(std::unique_ptr<Foo>);
+//
+// int main()
+// {
+// std::tuple<int, std::string> tuple1(42, "bar");
+// // Invokes the user function overload on int, std::string.
+// absl::apply(&user_function, tuple1);
+//
+// auto foo = absl::make_unique<Foo>();
+// std::tuple<Foo*, int> tuple2(foo.get(), 42);
+// // Invokes the method Bar on foo with one argument 42.
+// absl::apply(&Foo::Bar, foo.get(), 42);
+//
+// std::tuple<std::unique_ptr<Foo>> tuple3(absl::make_unique<Foo>());
+// // Invokes the user function that takes ownership of the unique
+// // pointer.
+// absl::apply(&user_function, std::move(tuple));
+// }
+template <typename Functor, typename Tuple>
+auto apply(Functor&& functor, Tuple&& t)
+ -> decltype(utility_internal::apply_helper(
+ absl::forward<Functor>(functor), absl::forward<Tuple>(t),
+ absl::make_index_sequence<std::tuple_size<
+ typename std::remove_reference<Tuple>::type>::value>{})) {
+ return utility_internal::apply_helper(
+ absl::forward<Functor>(functor), absl::forward<Tuple>(t),
+ absl::make_index_sequence<std::tuple_size<
+ typename std::remove_reference<Tuple>::type>::value>{});
+}
} // namespace absl
#endif // ABSL_UTILITY_UTILITY_H_
diff --git a/absl/utility/utility_test.cc b/absl/utility/utility_test.cc
index 62cb46f..342165e 100644
--- a/absl/utility/utility_test.cc
+++ b/absl/utility/utility_test.cc
@@ -23,6 +23,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
+#include "absl/memory/memory.h"
+#include "absl/strings/str_cat.h"
namespace {
@@ -38,8 +40,9 @@ namespace {
#pragma warning( disable : 4101 ) // unreferenced local variable
#endif // _MSC_VER
-using testing::StaticAssertTypeEq;
-using testing::ElementsAre;
+using ::testing::ElementsAre;
+using ::testing::Pointee;
+using ::testing::StaticAssertTypeEq;
TEST(IntegerSequenceTest, ValueType) {
StaticAssertTypeEq<int, absl::integer_sequence<int>::value_type>();
@@ -159,5 +162,176 @@ TEST(IndexSequenceForTest, Example) {
ElementsAre("12", "abc", "3.14"));
}
+int Function(int a, int b) { return a - b; }
+
+int Sink(std::unique_ptr<int> p) { return *p; }
+
+std::unique_ptr<int> Factory(int n) { return absl::make_unique<int>(n); }
+
+void NoOp() {}
+
+struct ConstFunctor {
+ int operator()(int a, int b) const { return a - b; }
+};
+
+struct MutableFunctor {
+ int operator()(int a, int b) { return a - b; }
+};
+
+struct EphemeralFunctor {
+ EphemeralFunctor() {}
+ EphemeralFunctor(const EphemeralFunctor&) {}
+ EphemeralFunctor(EphemeralFunctor&&) {}
+ int operator()(int a, int b) && { return a - b; }
+};
+
+struct OverloadedFunctor {
+ OverloadedFunctor() {}
+ OverloadedFunctor(const OverloadedFunctor&) {}
+ OverloadedFunctor(OverloadedFunctor&&) {}
+ template <typename... Args>
+ std::string operator()(const Args&... args) & {
+ return absl::StrCat("&", args...);
+ }
+ template <typename... Args>
+ std::string operator()(const Args&... args) const& {
+ return absl::StrCat("const&", args...);
+ }
+ template <typename... Args>
+ std::string operator()(const Args&... args) && {
+ return absl::StrCat("&&", args...);
+ }
+};
+
+struct Class {
+ int Method(int a, int b) { return a - b; }
+ int ConstMethod(int a, int b) const { return a - b; }
+
+ int member;
+};
+
+struct FlipFlop {
+ int ConstMethod() const { return member; }
+ FlipFlop operator*() const { return {-member}; }
+
+ int member;
+};
+
+TEST(ApplyTest, Function) {
+ EXPECT_EQ(1, absl::apply(Function, std::make_tuple(3, 2)));
+ EXPECT_EQ(1, absl::apply(&Function, std::make_tuple(3, 2)));
+}
+
+TEST(ApplyTest, NonCopyableArgument) {
+ EXPECT_EQ(42, absl::apply(Sink, std::make_tuple(absl::make_unique<int>(42))));
+}
+
+TEST(ApplyTest, NonCopyableResult) {
+ EXPECT_THAT(absl::apply(Factory, std::make_tuple(42)),
+ ::testing::Pointee(42));
+}
+
+TEST(ApplyTest, VoidResult) { absl::apply(NoOp, std::tuple<>()); }
+
+TEST(ApplyTest, ConstFunctor) {
+ EXPECT_EQ(1, absl::apply(ConstFunctor(), std::make_tuple(3, 2)));
+}
+
+TEST(ApplyTest, MutableFunctor) {
+ MutableFunctor f;
+ EXPECT_EQ(1, absl::apply(f, std::make_tuple(3, 2)));
+ EXPECT_EQ(1, absl::apply(MutableFunctor(), std::make_tuple(3, 2)));
+}
+TEST(ApplyTest, EphemeralFunctor) {
+ EphemeralFunctor f;
+ EXPECT_EQ(1, absl::apply(std::move(f), std::make_tuple(3, 2)));
+ EXPECT_EQ(1, absl::apply(EphemeralFunctor(), std::make_tuple(3, 2)));
+}
+TEST(ApplyTest, OverloadedFunctor) {
+ OverloadedFunctor f;
+ const OverloadedFunctor& cf = f;
+
+ EXPECT_EQ("&", absl::apply(f, std::tuple<>{}));
+ EXPECT_EQ("& 42", absl::apply(f, std::make_tuple(" 42")));
+
+ EXPECT_EQ("const&", absl::apply(cf, std::tuple<>{}));
+ EXPECT_EQ("const& 42", absl::apply(cf, std::make_tuple(" 42")));
+
+ EXPECT_EQ("&&", absl::apply(std::move(f), std::tuple<>{}));
+ OverloadedFunctor f2;
+ EXPECT_EQ("&& 42", absl::apply(std::move(f2), std::make_tuple(" 42")));
+}
+
+TEST(ApplyTest, ReferenceWrapper) {
+ ConstFunctor cf;
+ MutableFunctor mf;
+ EXPECT_EQ(1, absl::apply(std::cref(cf), std::make_tuple(3, 2)));
+ EXPECT_EQ(1, absl::apply(std::ref(cf), std::make_tuple(3, 2)));
+ EXPECT_EQ(1, absl::apply(std::ref(mf), std::make_tuple(3, 2)));
+}
+
+TEST(ApplyTest, MemberFunction) {
+ std::unique_ptr<Class> p(new Class);
+ std::unique_ptr<const Class> cp(new Class);
+ EXPECT_EQ(
+ 1, absl::apply(&Class::Method,
+ std::tuple<std::unique_ptr<Class>&, int, int>(p, 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::Method,
+ std::tuple<Class*, int, int>(p.get(), 3, 2)));
+ EXPECT_EQ(
+ 1, absl::apply(&Class::Method, std::tuple<Class&, int, int>(*p, 3, 2)));
+
+ EXPECT_EQ(
+ 1, absl::apply(&Class::ConstMethod,
+ std::tuple<std::unique_ptr<Class>&, int, int>(p, 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::tuple<Class*, int, int>(p.get(), 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::tuple<Class&, int, int>(*p, 3, 2)));
+
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::tuple<std::unique_ptr<const Class>&, int, int>(
+ cp, 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::tuple<const Class*, int, int>(cp.get(), 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::tuple<const Class&, int, int>(*cp, 3, 2)));
+
+ EXPECT_EQ(1, absl::apply(&Class::Method,
+ std::make_tuple(absl::make_unique<Class>(), 3, 2)));
+ EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
+ std::make_tuple(absl::make_unique<Class>(), 3, 2)));
+ EXPECT_EQ(
+ 1, absl::apply(&Class::ConstMethod,
+ std::make_tuple(absl::make_unique<const Class>(), 3, 2)));
+}
+
+TEST(ApplyTest, DataMember) {
+ std::unique_ptr<Class> p(new Class{42});
+ std::unique_ptr<const Class> cp(new Class{42});
+ EXPECT_EQ(
+ 42, absl::apply(&Class::member, std::tuple<std::unique_ptr<Class>&>(p)));
+ EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<Class&>(*p)));
+ EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<Class*>(p.get())));
+
+ absl::apply(&Class::member, std::tuple<std::unique_ptr<Class>&>(p)) = 42;
+ absl::apply(&Class::member, std::tuple<Class*>(p.get())) = 42;
+ absl::apply(&Class::member, std::tuple<Class&>(*p)) = 42;
+
+ EXPECT_EQ(42, absl::apply(&Class::member,
+ std::tuple<std::unique_ptr<const Class>&>(cp)));
+ EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<const Class&>(*cp)));
+ EXPECT_EQ(42,
+ absl::apply(&Class::member, std::tuple<const Class*>(cp.get())));
+}
+
+TEST(ApplyTest, FlipFlop) {
+ FlipFlop obj = {42};
+ // This call could resolve to (obj.*&FlipFlop::ConstMethod)() or
+ // ((*obj).*&FlipFlop::ConstMethod)(). We verify that it's the former.
+ EXPECT_EQ(42, absl::apply(&FlipFlop::ConstMethod, std::make_tuple(obj)));
+ EXPECT_EQ(42, absl::apply(&FlipFlop::member, std::make_tuple(obj)));
+}
+
} // namespace