Export of internal Abseil changes

--
167cd2647144583746311129b0cc98a89a9897e1 by Andy Soffer <asoffer@google.com>:

Internal Changes

PiperOrigin-RevId: 268000987

--
ab44fea7dfdaf763c69609a08ddfac22480ce520 by Derek Mauro <dmauro@google.com>:

Fix flags Cmake tests on Windows

PiperOrigin-RevId: 267859442

--
d7ec9f7611370b01bc9a5aed7e18460df2a15429 by Abseil Team <absl-team@google.com>:

Fix undefined behavior in symbolize_elf.inc

PiperOrigin-RevId: 267684904

--
e7c5f8e472a91ed99bdf4876942f2bda5751aa6c by Abseil Team <absl-team@google.com>:

Turn off ABSL_HAS_ALARM on Fuchsia.

PiperOrigin-RevId: 267666090

--
b7d25d5c71a49e697a8bf6440ed30f2e3f036420 by Chris Kennelly <ckennelly@google.com>:

Mark once initialization function as no inline.

We expect this code to run infrequently.

PiperOrigin-RevId: 267649713

--
dc2a5e5f1e39a03fff837d34a319033fde55d9ba by Derek Mauro <dmauro@google.com>:

Fix the MSVC CMake random build

PiperOrigin-RevId: 267624074

--
ba2751b67fa17d1b6c53e5ba79f81a5371e4a03a by Abseil Team <absl-team@google.com>:

Move "internal/flag.*" files to the "internal" build target.

PiperOrigin-RevId: 267588996

--
05b985a33eec4f8acff1809ad9218a1e22220f34 by Abseil Team <absl-team@google.com>:

Move "internal/flag.*" files to the "internal" build target.

PiperOrigin-RevId: 267580412
GitOrigin-RevId: 167cd2647144583746311129b0cc98a89a9897e1
Change-Id: Ibd334f46a5671c7c1d3fcf5354029e2fbb7ba91f
This commit is contained in:
Abseil Team 2019-09-09 08:20:10 -07:00 committed by Shaindel Schwartz
parent 325fd7b042
commit 97c1664b4b
14 changed files with 154 additions and 62 deletions

View file

@ -142,6 +142,7 @@ enum {
};
template <typename Callable, typename... Args>
ABSL_ATTRIBUTE_NOINLINE
void CallOnceImpl(std::atomic<uint32_t>* control,
base_internal::SchedulingMode scheduling_mode, Callable&& fn,
Args&&... args) {

View file

@ -336,6 +336,8 @@
// feature tests for Microsoft's library
#elif defined(__EMSCRIPTEN__)
// emscripten doesn't support signals
#elif defined(__Fuchsia__)
// Signals don't exist on fuchsia.
#elif defined(__native_client__)
#else
// other standard libraries

View file

@ -624,6 +624,13 @@ static bool InSection(const void *address, const ElfW(Shdr) * section) {
return start <= address && address < (start + size);
}
static const char *ComputeOffset(const char *base, ptrdiff_t offset) {
// Note: cast to uintptr_t to avoid undefined behavior when base evaluates to
// zero and offset is non-zero.
return reinterpret_cast<const char *>(
reinterpret_cast<uintptr_t>(base) + offset);
}
// Read a symbol table and look for the symbol containing the
// pc. Iterate over symbols in a symbol table and look for the symbol
// containing "pc". If the symbol is found, and its name fits in
@ -676,7 +683,8 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// We keep the original address for opd redirection below.
const char *const original_start_address =
reinterpret_cast<const char *>(symbol.st_value);
const char *start_address = original_start_address + relocation;
const char *start_address =
ComputeOffset(original_start_address, relocation);
if (deref_function_descriptor_pointer &&
InSection(original_start_address, opd)) {
@ -688,8 +696,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// If pc is inside the .opd section, it points to a function descriptor.
const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size;
const void *const end_address =
reinterpret_cast<const char *>(start_address) + size;
const void *const end_address = ComputeOffset(start_address, size);
if (symbol.st_value != 0 && // Skip null value symbols.
symbol.st_shndx != 0 && // Skip undefined symbols.
#ifdef STT_TLS

View file

@ -231,6 +231,7 @@ cc_test(
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":flag",
":handle",
":registry",
@ -264,6 +265,7 @@ cc_test(
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":flag",
"//absl/strings",
"@com_google_googletest//:gtest_main",

View file

@ -213,6 +213,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::flags_config
absl::flags_handle
absl::flags_registry
absl::memory
@ -242,6 +243,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::flags_config
absl::strings
gtest_main
)

View file

@ -15,7 +15,11 @@
#include "absl/flags/flag.h"
#include <algorithm>
#include <string>
#include "gtest/gtest.h"
#include "absl/flags/usage_config.h"
#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
@ -63,7 +67,27 @@ struct UDT {
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
std::string AbslUnparseFlag(const UDT&) { return ""; }
TEST(FlagTest, TestConstruction) {
class FlagTest : public testing::Test {
protected:
static void SetUpTestSuite() {
// Install a function to normalize filenames before this test is run.
absl::FlagsUsageConfig default_config;
default_config.normalize_filename = &FlagTest::NormalizeFileName;
absl::SetFlagsUsageConfig(default_config);
}
private:
static std::string NormalizeFileName(absl::string_view fname) {
#ifdef _WIN32
std::string normalized(fname);
std::replace(normalized.begin(), normalized.end(), '\\', '/');
fname = normalized;
#endif
return std::string(fname);
}
};
TEST_F(FlagTest, TestConstruction) {
TestConstructionFor<bool>();
TestConstructionFor<int16_t>();
TestConstructionFor<uint16_t>();
@ -98,7 +122,7 @@ namespace {
#if !ABSL_FLAGS_STRIP_NAMES
TEST(FlagTest, TestFlagDeclaration) {
TEST_F(FlagTest, TestFlagDeclaration) {
// test that we can access flag objects.
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
@ -133,69 +157,69 @@ ABSL_FLAG(std::string, test_flag_11, "", "test flag 11");
namespace {
#if !ABSL_FLAGS_STRIP_NAMES
TEST(FlagTest, TestFlagDefinition) {
TEST_F(FlagTest, TestFlagDefinition) {
absl::string_view expected_file_name = "absl/flags/flag_test.cc";
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
EXPECT_EQ(FLAGS_test_flag_01.Help(), "test flag 01");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name))
<< FLAGS_test_flag_01.Filename();
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
EXPECT_EQ(FLAGS_test_flag_02.Help(), "test flag 02");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name))
<< FLAGS_test_flag_02.Filename();
EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03");
EXPECT_EQ(FLAGS_test_flag_03.Help(), "test flag 03");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name))
<< FLAGS_test_flag_03.Filename();
EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04");
EXPECT_EQ(FLAGS_test_flag_04.Help(), "test flag 04");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name))
<< FLAGS_test_flag_04.Filename();
EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05");
EXPECT_EQ(FLAGS_test_flag_05.Help(), "test flag 05");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name))
<< FLAGS_test_flag_05.Filename();
EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06");
EXPECT_EQ(FLAGS_test_flag_06.Help(), "test flag 06");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name))
<< FLAGS_test_flag_06.Filename();
EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07");
EXPECT_EQ(FLAGS_test_flag_07.Help(), "test flag 07");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name))
<< FLAGS_test_flag_07.Filename();
EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08");
EXPECT_EQ(FLAGS_test_flag_08.Help(), "test flag 08");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name))
<< FLAGS_test_flag_08.Filename();
EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09");
EXPECT_EQ(FLAGS_test_flag_09.Help(), "test flag 09");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name))
<< FLAGS_test_flag_09.Filename();
EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10");
EXPECT_EQ(FLAGS_test_flag_10.Help(), "test flag 10");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name))
<< FLAGS_test_flag_10.Filename();
EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11");
EXPECT_EQ(FLAGS_test_flag_11.Help(), "test flag 11");
EXPECT_TRUE(
absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name));
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name))
<< FLAGS_test_flag_11.Filename();
}
#endif // !ABSL_FLAGS_STRIP_NAMES
// --------------------------------------------------------------------
TEST(FlagTest, TestDefault) {
TEST_F(FlagTest, TestDefault) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
@ -211,7 +235,7 @@ TEST(FlagTest, TestDefault) {
// --------------------------------------------------------------------
TEST(FlagTest, TestGetSet) {
TEST_F(FlagTest, TestGetSet) {
absl::SetFlag(&FLAGS_test_flag_01, false);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), false);
@ -258,7 +282,7 @@ ABSL_FLAG(std::string, test_flag_13, absl::StrCat("AAA", "BBB"),
namespace {
TEST(FlagTest, TestNonConstexprDefault) {
TEST_F(FlagTest, TestNonConstexprDefault) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), 1);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), "AAABBB");
}
@ -272,7 +296,7 @@ ABSL_FLAG(bool, test_flag_14, true, absl::StrCat("test ", "flag ", "14"));
namespace {
#if !ABSL_FLAGS_STRIP_HELP
TEST(FlagTest, TestNonConstexprHelp) {
TEST_F(FlagTest, TestNonConstexprHelp) {
EXPECT_EQ(FLAGS_test_flag_14.Help(), "test flag 14");
}
#endif //! ABSL_FLAGS_STRIP_HELP
@ -296,7 +320,7 @@ namespace {
void TestFlagCB() { cb_test_value = absl::GetFlag(FLAGS_test_flag_with_cb); }
// Tests side-effects of callback invocation.
TEST(FlagTest, CallbackInvocation) {
TEST_F(FlagTest, CallbackInvocation) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_with_cb), 100);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_with_lambda_cb), 200);
EXPECT_EQ(cb_test_value, 300);
@ -343,7 +367,7 @@ ABSL_FLAG(CustomUDT, test_flag_15, CustomUDT(), "test flag 15");
namespace {
TEST(FlagTest, TestCustomUDT) {
TEST_F(FlagTest, TestCustomUDT) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(1, 1));
absl::SetFlag(&FLAGS_test_flag_15, CustomUDT(2, 3));
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(2, 3));
@ -409,7 +433,7 @@ ABSL_FLAG(ConversionTestVal, test_flag_16,
namespace {
TEST(FlagTest, CanSetViaImplicitConversion) {
TEST_F(FlagTest, CanSetViaImplicitConversion) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_16).a, 10);
absl::SetFlag(&FLAGS_test_flag_16,
ConversionTestVal::ViaImplicitConv::kEleven);
@ -447,7 +471,7 @@ ABSL_FLAG(NonDfltConstructible, ndc_flag2, 0,
namespace {
TEST(FlagTest, TestNonDefaultConstructibleType) {
TEST_F(FlagTest, TestNonDefaultConstructibleType) {
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag1).value, '1' + 100);
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag2).value, 0);
@ -468,7 +492,7 @@ ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr"));
namespace {
TEST(FlagTest, TestRetiredFlagRegistration) {
TEST_F(FlagTest, TestRetiredFlagRegistration) {
bool is_bool = false;
EXPECT_TRUE(flags::IsRetiredFlag("old_bool_flag", &is_bool));
EXPECT_TRUE(is_bool);

View file

@ -15,9 +15,13 @@
#include "absl/flags/internal/commandlineflag.h"
#include <algorithm>
#include <string>
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/registry.h"
#include "absl/flags/usage_config.h"
#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
@ -33,10 +37,26 @@ namespace flags = absl::flags_internal;
class CommandLineFlagTest : public testing::Test {
protected:
static void SetUpTestSuite() {
// Install a function to normalize filenames before this test is run.
absl::FlagsUsageConfig default_config;
default_config.normalize_filename = &CommandLineFlagTest::NormalizeFileName;
absl::SetFlagsUsageConfig(default_config);
}
void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
void TearDown() override { flag_saver_.reset(); }
private:
static std::string NormalizeFileName(absl::string_view fname) {
#ifdef _WIN32
std::string normalized(fname);
std::replace(normalized.begin(), normalized.end(), '\\', '/');
fname = normalized;
#endif
return std::string(fname);
}
std::unique_ptr<flags::FlagSaver> flag_saver_;
};
@ -49,9 +69,10 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
EXPECT_EQ(flag_01->Typename(), "");
EXPECT_TRUE(!flag_01->IsRetired());
EXPECT_TRUE(flag_01->IsOfType<int>());
EXPECT_TRUE(absl::EndsWith(
flag_01->Filename(),
"absl/flags/internal/commandlineflag_test.cc"));
EXPECT_TRUE(
absl::EndsWith(flag_01->Filename(),
"absl/flags/internal/commandlineflag_test.cc"))
<< flag_01->Filename();
auto* flag_02 = flags::FindCommandLineFlag("string_flag");
@ -61,9 +82,10 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
EXPECT_EQ(flag_02->Typename(), "");
EXPECT_TRUE(!flag_02->IsRetired());
EXPECT_TRUE(flag_02->IsOfType<std::string>());
EXPECT_TRUE(absl::EndsWith(
flag_02->Filename(),
"absl/flags/internal/commandlineflag_test.cc"));
EXPECT_TRUE(
absl::EndsWith(flag_02->Filename(),
"absl/flags/internal/commandlineflag_test.cc"))
<< flag_02->Filename();
auto* flag_03 = flags::FindRetiredFlag("bool_retired_flag");

View file

@ -67,9 +67,9 @@ static std::string NormalizeFileName(absl::string_view fname) {
fname = normalized;
#endif
auto absl_pos = fname.find("/absl/");
auto absl_pos = fname.rfind("absl/");
if (absl_pos != absl::string_view::npos) {
fname = fname.substr(absl_pos + 1);
fname = fname.substr(absl_pos);
}
return std::string(fname);
}

View file

@ -1025,6 +1025,8 @@ absl_cc_test(
random_internal_iostream_state_saver_test
SRCS
"internal/iostream_state_saver_test.cc"
COPTS
${ABSL_TEST_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS

View file

@ -129,7 +129,7 @@ class beta_distribution {
private:
friend class beta_distribution;
#ifdef COMPILER_MSVC
#ifdef _MSC_VER
// MSVC does not have constexpr implementations for std::log and std::exp
// so they are computed at runtime.
#define ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR

View file

@ -36,6 +36,12 @@
#include "absl/types/span.h"
namespace absl {
struct IntervalClosedClosedTag;
struct IntervalClosedOpenTag;
struct IntervalOpenClosedTag;
struct IntervalOpenOpenTag;
namespace random_internal {
// ScalarTypeName defines a preferred hierarchy of preferred type names for
@ -243,6 +249,39 @@ struct DistributionFormatTraits<absl::log_uniform_int_distribution<R>> {
}
};
template <typename TagType, typename NumType>
struct UniformDistributionWrapper;
template <typename TagType, typename NumType>
struct DistributionFormatTraits<UniformDistributionWrapper<TagType, NumType>> {
using distribution_t = UniformDistributionWrapper<TagType, NumType>;
using result_t = NumType;
static constexpr const char* Name() { return "Uniform"; }
static std::string FunctionName() {
return absl::StrCat(Name(), "<", ScalarTypeName<NumType>(), ">");
}
static std::string FormatArgs(const distribution_t& d) {
absl::string_view tag;
if (std::is_same<TagType, IntervalClosedClosedTag>::value) {
tag = "IntervalClosedClosed";
} else if (std::is_same<TagType, IntervalClosedOpenTag>::value) {
tag = "IntervalClosedOpen";
} else if (std::is_same<TagType, IntervalOpenClosedTag>::value) {
tag = "IntervalOpenClosed";
} else if (std::is_same<TagType, IntervalOpenOpenTag>::value) {
tag = "IntervalOpenOpen";
} else {
tag = "[[unknown tag type]]";
}
return absl::StrCat(tag, ", ", (d.min)(), ", ", (d.max)());
}
static std::string FormatResults(absl::Span<const result_t> results) {
return absl::StrJoin(results, ", ");
}
};
} // namespace random_internal
} // namespace absl

View file

@ -60,13 +60,11 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":distribution_caller",
":fast_uniform_bits",
":fastmath",
":traits",
":uniform_helper",
"//absl/base",
"//absl/meta:type_traits",
"//absl/strings",
"//absl/types:span",
],
)
@ -618,11 +616,6 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":distribution_impl",
":fast_uniform_bits",
":iostream_state_saver",
":traits",
"//absl/base:core_headers",
"//absl/meta:type_traits",
],
)

View file

@ -39,22 +39,20 @@ NumType UniformImpl(TagType tag,
"absl::Uniform<T>() must use an integer or real parameter type.");
using distribution_t =
typename std::conditional<std::is_integral<NumType>::value,
absl::uniform_int_distribution<NumType>,
absl::uniform_real_distribution<NumType>>::type;
UniformDistributionWrapper<absl::decay_t<TagType>, NumType>;
using format_t = random_internal::DistributionFormatTraits<distribution_t>;
auto a = uniform_lower_bound(tag, lo, hi);
auto b = uniform_upper_bound(tag, lo, hi);
auto a = random_internal::uniform_lower_bound<NumType>(tag, lo, hi);
auto b = random_internal::uniform_upper_bound<NumType>(tag, lo, hi);
// TODO(lar): it doesn't make a lot of sense to ask for a random number in an
// empty range. Right now we just return a boundary--even though that
// boundary is not an acceptable value! Is there something better we can do
// here?
if (a > b) return a;
using gen_t = absl::decay_t<URBG>;
if (a > b) return a;
return DistributionCaller<gen_t>::template Call<distribution_t, format_t>(
&urbg, a, b);
&urbg, tag, lo, hi);
}
// In the absence of an explicitly provided return-type, the template

View file

@ -156,7 +156,7 @@ using UniformDistribution =
template <typename TagType, typename NumType>
struct UniformDistributionWrapper : public UniformDistribution<NumType> {
explicit UniformDistributionWrapper(NumType lo, NumType hi)
explicit UniformDistributionWrapper(TagType, NumType lo, NumType hi)
: UniformDistribution<NumType>(
uniform_lower_bound<NumType>(TagType{}, lo, hi),
uniform_upper_bound<NumType>(TagType{}, lo, hi)) {}