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