Commit 52a711fc by Gennadiy Rozental Committed by Copybara-Service

Avoid static initializers in case of ABSL_FLAGS_STRIP_NAMES=1

PiperOrigin-RevId: 603784442
Change-Id: I3d57e5f438b276c984f5d5416889b19e7ddb501a
parent 146169f9
......@@ -236,10 +236,10 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":commandlineflag",
":config",
":flag_internal",
":reflection",
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
"//absl/strings",
......@@ -343,7 +343,6 @@ cc_test(
],
deps = [
":commandlineflag",
":commandlineflag_internal",
":config",
":flag",
":private_handle_accessor",
......@@ -394,9 +393,11 @@ cc_test(
":reflection",
"//absl/base:core_headers",
"//absl/base:malloc_internal",
"//absl/base:raw_logging_internal",
"//absl/numeric:int128",
"//absl/strings",
"//absl/time",
"//absl/types:optional",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
......@@ -459,6 +460,7 @@ cc_test(
"no_test_wasm",
],
deps = [
":config",
":flag",
":parse",
":reflection",
......@@ -520,11 +522,9 @@ cc_test(
"no_test_wasm",
],
deps = [
":commandlineflag_internal",
":config",
":flag",
":marshalling",
":reflection",
":usage_internal",
"//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest",
......
......@@ -214,7 +214,6 @@ absl_cc_library(
absl::flags_config
absl::flags_internal
absl::flags_reflection
absl::base
absl::core_headers
absl::strings
)
......@@ -307,7 +306,6 @@ absl_cc_test(
DEPS
absl::flags
absl::flags_commandlineflag
absl::flags_commandlineflag_internal
absl::flags_config
absl::flags_private_handle_accessor
absl::flags_reflection
......@@ -344,6 +342,8 @@ absl_cc_test(
absl::flags_marshalling
absl::flags_reflection
absl::int128
absl::optional
absl::raw_logging_internal
absl::strings
absl::time
GTest::gtest_main
......@@ -370,6 +370,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::flags_config
absl::flags_parse
absl::flags_reflection
absl::flags_usage_internal
......@@ -413,8 +414,8 @@ absl_cc_test(
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_commandlineflag_internal
absl::flags
absl::flags_config
absl::flags_reflection
absl::flags_usage
absl::memory
......
......@@ -19,8 +19,8 @@
#include <string>
#include "gtest/gtest.h"
#include "absl/flags/config.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/private_handle_accessor.h"
#include "absl/flags/reflection.h"
#include "absl/flags/usage_config.h"
......@@ -51,7 +51,12 @@ class CommandLineFlagTest : public testing::Test {
absl::SetFlagsUsageConfig(default_config);
}
void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
void SetUp() override {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
flag_saver_ = absl::make_unique<absl::FlagSaver>();
}
void TearDown() override { flag_saver_.reset(); }
private:
......
......@@ -29,12 +29,14 @@
#ifndef ABSL_FLAGS_FLAG_H_
#define ABSL_FLAGS_FLAG_H_
#include <cstdint>
#include <string>
#include <type_traits>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/optimization.h"
#include "absl/flags/commandlineflag.h"
#include "absl/flags/config.h"
#include "absl/flags/internal/flag.h"
#include "absl/flags/internal/registry.h"
......
......@@ -19,14 +19,13 @@
#include <stdint.h>
#include <atomic>
#include <cmath>
#include <new>
#include <string>
#include <thread> // NOLINT
#include <vector>
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/flags/config.h"
#include "absl/flags/declare.h"
......@@ -40,7 +39,9 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
ABSL_DECLARE_FLAG(int64_t, mistyped_int_flag);
ABSL_DECLARE_FLAG(std::vector<std::string>, mistyped_string_flag);
......@@ -226,9 +227,10 @@ ABSL_DECLARE_FLAG(absl::uint128, test_flag_14);
namespace {
#if !ABSL_FLAGS_STRIP_NAMES
TEST_F(FlagTest, TestFlagDeclaration) {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
// test that we can access flag objects.
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
"test_flag_01");
......@@ -259,12 +261,27 @@ TEST_F(FlagTest, TestFlagDeclaration) {
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_14).Name(),
"test_flag_14");
}
#endif // !ABSL_FLAGS_STRIP_NAMES
// --------------------------------------------------------------------
} // namespace
#if ABSL_FLAGS_STRIP_NAMES
// The intent of this helper struct and an expression below is to make sure that
// in the configuration where ABSL_FLAGS_STRIP_NAMES=1 registrar construction
// (in cases of of no Tail calls like OnUpdate) is constexpr and thus can and
// should be completely optimized away, thus avoiding the cost/overhead of
// static initializers.
struct VerifyConsteval {
friend consteval flags::FlagRegistrarEmpty operator+(
flags::FlagRegistrarEmpty, VerifyConsteval) {
return {};
}
};
ABSL_FLAG(int, test_registrar_const_init, 0, "") + VerifyConsteval();
#endif
// --------------------------------------------------------------------
ABSL_FLAG(bool, test_flag_01, true, "test flag 01");
ABSL_FLAG(int, test_flag_02, 1234, "test flag 02");
ABSL_FLAG(int16_t, test_flag_03, -34, "test flag 03");
......@@ -283,8 +300,10 @@ ABSL_FLAG(absl::uint128, test_flag_14, absl::MakeUint128(0, 0xFFFAAABBBCCCDDD),
namespace {
#if !ABSL_FLAGS_STRIP_NAMES
TEST_F(FlagTest, TestFlagDefinition) {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
absl::string_view expected_file_name = "absl/flags/flag_test.cc";
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
......@@ -413,7 +432,6 @@ TEST_F(FlagTest, TestFlagDefinition) {
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_14).Filename();
}
#endif // !ABSL_FLAGS_STRIP_NAMES
// --------------------------------------------------------------------
......@@ -604,6 +622,9 @@ TEST_F(FlagTest, TestGetSet) {
// --------------------------------------------------------------------
TEST_F(FlagTest, TestGetViaReflection) {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
auto* handle = absl::FindCommandLineFlag("test_flag_01");
EXPECT_EQ(*handle->TryGet<bool>(), true);
handle = absl::FindCommandLineFlag("test_flag_02");
......@@ -638,6 +659,9 @@ TEST_F(FlagTest, TestGetViaReflection) {
// --------------------------------------------------------------------
TEST_F(FlagTest, ConcurrentSetAndGet) {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
static constexpr int kNumThreads = 8;
// Two arbitrary durations. One thread will concurrently flip the flag
// between these two values, while the other threads read it and verify
......@@ -785,10 +809,12 @@ TEST_F(FlagTest, TestCustomUDT) {
// MSVC produces link error on the type mismatch.
// Linux does not have build errors and validations work as expected.
#if !defined(_WIN32) && GTEST_HAS_DEATH_TEST
using FlagDeathTest = FlagTest;
TEST_F(FlagDeathTest, TestTypeMismatchValidations) {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
#if !defined(NDEBUG)
EXPECT_DEATH_IF_SUPPORTED(
static_cast<void>(absl::GetFlag(FLAGS_mistyped_int_flag)),
......
......@@ -22,7 +22,6 @@
#include <atomic>
#include <cstring>
#include <memory>
#include <new>
#include <string>
#include <type_traits>
#include <typeinfo>
......@@ -770,7 +769,8 @@ struct FlagRegistrarEmpty {};
template <typename T, bool do_register>
class FlagRegistrar {
public:
explicit FlagRegistrar(Flag<T>& flag, const char* filename) : flag_(flag) {
constexpr explicit FlagRegistrar(Flag<T>& flag, const char* filename)
: flag_(flag) {
if (do_register)
flags_internal::RegisterCommandLineFlag(flag_.impl_, filename);
}
......@@ -783,7 +783,7 @@ class FlagRegistrar {
// Make the registrar "die" gracefully as an empty struct on a line where
// registration happens. Registrar objects are intended to live only as
// temporary.
operator FlagRegistrarEmpty() const { return {}; } // NOLINT
constexpr operator FlagRegistrarEmpty() const { return {}; } // NOLINT
private:
Flag<T>& flag_; // Flag being registered (not owned).
......
......@@ -22,6 +22,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/flags/config.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/parse.h"
#include "absl/flags/internal/program_name.h"
......@@ -97,6 +98,11 @@ class UsageReportingTest : public testing::Test {
flags::SetFlagsHelpMatchSubstr("");
flags::SetFlagsHelpFormat(flags::HelpFormat::kHumanReadable);
}
void SetUp() override {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
}
private:
absl::FlagSaver flag_saver_;
......
......@@ -25,6 +25,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/internal/scoped_set_env.h"
#include "absl/flags/config.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/parse.h"
#include "absl/flags/internal/usage.h"
......@@ -243,6 +244,12 @@ class ParseTest : public testing::Test {
public:
~ParseTest() override { flags::SetFlagsHelpMode(flags::HelpMode::kNone); }
void SetUp() override {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
}
private:
absl::FlagSaver flag_saver_;
};
......
......@@ -20,10 +20,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/flags/declare.h"
#include "absl/flags/config.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/marshalling.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
......@@ -36,7 +34,12 @@ namespace {
class ReflectionTest : public testing::Test {
protected:
void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
void SetUp() override {
#if ABSL_FLAGS_STRIP_NAMES
GTEST_SKIP() << "This test requires flag names to be present";
#endif
flag_saver_ = absl::make_unique<absl::FlagSaver>();
}
void TearDown() override { flag_saver_.reset(); }
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment