Skip to content

Commit 1dd160f

Browse files
Abseil Teamdinord
Abseil Team
authored andcommitted
Export of internal Abseil changes
-- f0b7d230a90c82c6fee7adcb46a213d2582b6a7b by Martijn Vels <[email protected]>: Optimize substring logic now that CONCAT is removed This CL adds a static Substring() method to CordRepSubstring, and implements substring logic in cord.cc in terms of the new function. This cleans up various helper functions and logic remaining from previous complex CONCAT logic that is no longer needed. PiperOrigin-RevId: 431756805 Change-Id: I39c875b5af119916780e68598c7fc619fb2e8476 -- fa7d1bedf0e1244303844869a332c2a5dbd9ac0f by Derek Mauro <[email protected]>: Allow macro expansion within ABSL_FLAG and ABSL_DECLARE_FLAG args PiperOrigin-RevId: 431721184 Change-Id: I6e19713fb541205d796f940998db5ee25178d55e -- 1b328badd92304ed1c634f23e1c191be57b7bb15 by Laramie Leavitt <[email protected]>: Add #include for std:: types PiperOrigin-RevId: 431546757 Change-Id: I75efbcd3c77e6f53e4db66494101d30d670d988e -- e25323b299d4d3840218702860f537cdd2a3926f by Thomas Köppe <[email protected]>: Add hashing support for pointers to member. Also add tests for function pointers to the existing "pointer" test. PiperOrigin-RevId: 431067588 Change-Id: I3686010635d9fee34c47a418b72402e10737cdbc -- ab27b012a61cf10109fd51932b3b0b05ee78f32f by Laramie Leavitt <[email protected]>: Avoid use of std::pow in ChiSquare test. PiperOrigin-RevId: 431015830 Change-Id: Idd767ff2f51009ee171de48757207b38330ffea3 -- 28c359135d89061177958580fe4a7493802499cb by Laramie Leavitt <[email protected]>: Add #include <type_traits> for std::false_type PiperOrigin-RevId: 431005757 Change-Id: I85a6a918778601e19512aaea744424cf39018521 -- a920730f23669479d92e3a696d65d0bc3a5b1de1 by Laramie Leavitt <[email protected]>: #include <utility> for std::declval PiperOrigin-RevId: 431004934 Change-Id: I295237b2d44e9a15e4083698ea121b68ce0a1bb7 GitOrigin-RevId: f0b7d230a90c82c6fee7adcb46a213d2582b6a7b
1 parent 5e4ea1c commit 1dd160f

File tree

11 files changed

+188
-139
lines changed

11 files changed

+188
-139
lines changed

absl/flags/declare.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ ABSL_NAMESPACE_END
6060
// The ABSL_DECLARE_FLAG(type, name) macro expands to:
6161
//
6262
// extern absl::Flag<type> FLAGS_name;
63-
#define ABSL_DECLARE_FLAG(type, name) \
63+
#define ABSL_DECLARE_FLAG(type, name) ABSL_DECLARE_FLAG_INTERNAL(type, name)
64+
65+
// Internal implementation of ABSL_DECLARE_FLAG to allow macro expansion of its
66+
// arguments. Clients must use ABSL_DECLARE_FLAG instead.
67+
#define ABSL_DECLARE_FLAG_INTERNAL(type, name) \
6468
extern absl::Flag<type> FLAGS_##name; \
6569
namespace absl /* block flags in namespaces */ {} \
6670
/* second redeclaration is to allow applying attributes */ \

absl/flags/flag.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ ABSL_NAMESPACE_END
163163
// Note: do not construct objects of type `absl::Flag<T>` directly. Only use the
164164
// `ABSL_FLAG()` macro for such construction.
165165
#define ABSL_FLAG(Type, name, default_value, help) \
166-
extern ::absl::Flag<Type> FLAGS_##name; \
167166
ABSL_FLAG_IMPL(Type, name, default_value, help)
168167

169168
// ABSL_FLAG().OnUpdate()
@@ -266,6 +265,7 @@ ABSL_NAMESPACE_END
266265
// global name for FLAGS_no<flag_name> symbol, thus preventing the possibility
267266
// of defining two flags with names foo and nofoo.
268267
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
268+
extern ::absl::Flag<Type> FLAGS_##name; \
269269
namespace absl /* block flags in namespaces */ {} \
270270
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
271271
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \

absl/flags/flag_test.cc

+13
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,16 @@ TEST_F(FlagTest, TesTypeWrappingEnum) {
977977
value = absl::GetFlag(FLAGS_test_enum_wrapper_flag);
978978
EXPECT_EQ(value.e, B);
979979
}
980+
981+
// This is a compile test to ensure macros are expanded within ABSL_FLAG and
982+
// ABSL_DECLARE_FLAG.
983+
#define FLAG_NAME_MACRO(name) prefix_ ## name
984+
ABSL_DECLARE_FLAG(int, FLAG_NAME_MACRO(test_macro_named_flag));
985+
ABSL_FLAG(int, FLAG_NAME_MACRO(test_macro_named_flag), 0,
986+
"Testing macro expansion within ABSL_FLAG");
987+
988+
TEST_F(FlagTest, MacroWithinAbslFlag) {
989+
EXPECT_EQ(absl::GetFlag(FLAGS_prefix_test_macro_named_flag), 0);
990+
absl::SetFlag(&FLAGS_prefix_test_macro_named_flag, 1);
991+
EXPECT_EQ(absl::GetFlag(FLAGS_prefix_test_macro_named_flag), 1);
992+
}

absl/hash/hash_test.cc

+81
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ TEST(HashValueTest, FloatingPoint) {
185185

186186
TEST(HashValueTest, Pointer) {
187187
EXPECT_TRUE((is_hashable<int*>::value));
188+
EXPECT_TRUE((is_hashable<int(*)(char, float)>::value));
189+
EXPECT_TRUE((is_hashable<void(*)(int, int, ...)>::value));
188190

189191
int i;
190192
int* ptr = &i;
@@ -224,6 +226,85 @@ TEST(HashValueTest, PointerAlignment) {
224226
}
225227
}
226228

229+
TEST(HashValueTest, PointerToMember) {
230+
struct Bass {
231+
void q() {}
232+
};
233+
234+
struct A : Bass {
235+
virtual ~A() = default;
236+
virtual void vfa() {}
237+
238+
static auto pq() -> void (A::*)() { return &A::q; }
239+
};
240+
241+
struct B : Bass {
242+
virtual ~B() = default;
243+
virtual void vfb() {}
244+
245+
static auto pq() -> void (B::*)() { return &B::q; }
246+
};
247+
248+
struct Foo : A, B {
249+
void f1() {}
250+
void f2() const {}
251+
252+
int g1() & { return 0; }
253+
int g2() const & { return 0; }
254+
int g3() && { return 0; }
255+
int g4() const && { return 0; }
256+
257+
int h1() & { return 0; }
258+
int h2() const & { return 0; }
259+
int h3() && { return 0; }
260+
int h4() const && { return 0; }
261+
262+
int a;
263+
int b;
264+
265+
const int c = 11;
266+
const int d = 22;
267+
};
268+
269+
EXPECT_TRUE((is_hashable<float Foo::*>::value));
270+
EXPECT_TRUE((is_hashable<double (Foo::*)(int, int)&&>::value));
271+
272+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
273+
std::make_tuple(&Foo::a, &Foo::b, static_cast<int Foo::*>(nullptr))));
274+
275+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
276+
std::make_tuple(&Foo::c, &Foo::d, static_cast<const int Foo::*>(nullptr),
277+
&Foo::a, &Foo::b)));
278+
279+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
280+
&Foo::f1, static_cast<void (Foo::*)()>(nullptr))));
281+
282+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
283+
&Foo::f2, static_cast<void (Foo::*)() const>(nullptr))));
284+
285+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
286+
&Foo::g1, &Foo::h1, static_cast<int (Foo::*)() &>(nullptr))));
287+
288+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
289+
&Foo::g2, &Foo::h2, static_cast<int (Foo::*)() const &>(nullptr))));
290+
291+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
292+
&Foo::g3, &Foo::h3, static_cast<int (Foo::*)() &&>(nullptr))));
293+
294+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
295+
&Foo::g4, &Foo::h4, static_cast<int (Foo::*)() const &&>(nullptr))));
296+
297+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
298+
std::make_tuple(static_cast<void (Foo::*)()>(&Foo::vfa),
299+
static_cast<void (Foo::*)()>(&Foo::vfb),
300+
static_cast<void (Foo::*)()>(nullptr))));
301+
302+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
303+
std::make_tuple(static_cast<void (Foo::*)()>(Foo::A::pq()),
304+
static_cast<void (Foo::*)()>(Foo::B::pq()),
305+
static_cast<void (Foo::*)()>(nullptr))));
306+
}
307+
227308
TEST(HashValueTest, PairAndTuple) {
228309
EXPECT_TRUE((is_hashable<std::pair<int, int>>::value));
229310
EXPECT_TRUE((is_hashable<std::pair<const int&, const int&>>::value));

absl/hash/internal/hash.h

+33
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,39 @@ H AbslHashValue(H hash_state, std::nullptr_t) {
421421
return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
422422
}
423423

424+
// AbslHashValue() for hashing pointers-to-member
425+
template <typename H, typename T, typename C>
426+
H AbslHashValue(H hash_state, T C::* ptr) {
427+
auto salient_ptm_size = [](std::size_t n) -> std::size_t {
428+
#if defined(_MSC_VER)
429+
// Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
430+
// or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
431+
// padding (namely when they have 1 or 3 ints). The value below is a lower
432+
// bound on the number of salient, non-padding bytes that we use for
433+
// hashing.
434+
if (alignof(T C::*) == alignof(int)) {
435+
// No padding when all subobjects have the same size as the total
436+
// alignment. This happens in 32-bit mode.
437+
return n;
438+
} else {
439+
// Padding for 1 int (size 16) or 3 ints (size 24).
440+
// With 2 ints, the size is 16 with no padding, which we pessimize.
441+
return n == 24 ? 20 : n == 16 ? 12 : n;
442+
}
443+
#else
444+
// On other platforms, we assume that pointers-to-members do not have
445+
// padding.
446+
#ifdef __cpp_lib_has_unique_object_representations
447+
static_assert(std::has_unique_object_representations_v<T C::*>);
448+
#endif // __cpp_lib_has_unique_object_representations
449+
return n;
450+
#endif
451+
};
452+
return H::combine_contiguous(std::move(hash_state),
453+
reinterpret_cast<unsigned char*>(&ptr),
454+
salient_ptm_size(sizeof ptr));
455+
}
456+
424457
// -----------------------------------------------------------------------------
425458
// AbslHashValue for Composite Types
426459
// -----------------------------------------------------------------------------

absl/random/bit_gen_ref.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#ifndef ABSL_RANDOM_BIT_GEN_REF_H_
2525
#define ABSL_RANDOM_BIT_GEN_REF_H_
2626

27+
#include <limits>
28+
#include <type_traits>
29+
#include <utility>
30+
2731
#include "absl/base/internal/fast_type_id.h"
2832
#include "absl/base/macros.h"
2933
#include "absl/meta/type_traits.h"

absl/random/internal/chi_square.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ double ChiSquareValue(int dof, double p) {
125125
const double variance = 2.0 / (9 * dof);
126126
// Cannot use this method if the variance is 0.
127127
if (variance != 0) {
128-
return std::pow(z * std::sqrt(variance) + mean, 3.0) * dof;
128+
double term = z * std::sqrt(variance) + mean;
129+
return dof * (term * term * term);
129130
}
130131
}
131132

absl/random/internal/distribution_caller.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
1919

2020
#include <utility>
21+
#include <type_traits>
2122

2223
#include "absl/base/config.h"
2324
#include "absl/base/internal/fast_type_id.h"

absl/random/internal/mock_helpers.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <tuple>
2020
#include <type_traits>
21+
#include <utility>
2122

2223
#include "absl/base/internal/fast_type_id.h"
2324
#include "absl/types/optional.h"

0 commit comments

Comments
 (0)