Commit ca9856ca by Abseil Team Committed by Gennadiy Rozental

Export of internal Abseil changes

--
53550735f5a943dfb99225e7c53f211c2d6e7951 by Gennadiy Rozental <rogeeff@google.com>:

Import of CCTZ from GitHub.

PiperOrigin-RevId: 309333648

--
847bbf8a1d9cd322ec058c6f932d1f687fd3d331 by Gennadiy Rozental <rogeeff@google.com>:

Make Validation interfaces private in CommandLineFlag.

Calls are rewired via private interface access struct.

PiperOrigin-RevId: 309323013

--
a600fc5051e0a0af50a7850450fd3ed1aef3f316 by Matthew Brown <matthewbr@google.com>:

Internal Change.

PiperOrigin-RevId: 309292207

--
937d00ce3cf62c5f23f59b5377471fd01d6bfbc7 by Gennadiy Rozental <rogeeff@google.com>:

Make TypeId interface private in CommandLineFlag.

We also rewire the SaveState via the new PrivateHandleInterface trampoline class. This class will be the only way to access private methods of class CommandLineFlag.

PiperOrigin-RevId: 309282547

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

Cleanup uses of kLinkerInitialized

PiperOrigin-RevId: 309274734

--
c831446c52d9ef4bdcb1ea369840904620abc4b9 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate the interface IsModified of CommndLineFlag.

PiperOrigin-RevId: 309256248

--
a1db59d7f7aa39cb0a37dbf80f8c04e371da8465 by Gennadiy Rozental <rogeeff@google.com>:

Avoid default value generator if default value expression is constexpr.

If possible, we detect constexpr-ness of default value expression and avoid storing default value generator in side of flag and instead set the flag's value to the value of that expression at const initialization time of flag objects.

At the moment we only do this for flags of (all) integral, float and double value types

PiperOrigin-RevId: 309110630

--
ae3b4a139aacd8fc165c9acd2a3cbae1f9e26af4 by Gennadiy Rozental <rogeeff@google.com>:

Make SaveState a private method of the CommandLineFlag and make it only accessible from FlagSaverImpl. There is no other call sites for this call.

PiperOrigin-RevId: 309073989

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

Eliminate the interface IsModified of CommndLineFlag.

PiperOrigin-RevId: 309064639

--
08e79645a89d71785c5381cea9c413357db9824a by Gennadiy Rozental <rogeeff@google.com>:

Eliminate the interface IsModified of CommndLineFlag.

PiperOrigin-RevId: 309054430

--
4a6c70233c60dc8c39b7fa9beb5fa687c215261f by Gennadiy Rozental <rogeeff@google.com>:

Internal change

PiperOrigin-RevId: 308900784

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

Provide definitions of static member variables -- improved C++11 support.

PiperOrigin-RevId: 308900290

--
0343b8228657b9b313afdfe88c4a7b2137d56db4 by Gennadiy Rozental <rogeeff@google.com>:

Rename method Get<T> to TryGet<T> per approved spec before making interface public.

PiperOrigin-RevId: 308889113

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

Remove node_hash_* methods that were deprecated on release

PiperOrigin-RevId: 308837933

--
599d44ee72c02b6bb6e1c1a1db72873841441416 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate CommandLineFlag::Typename interface per approved spec before making CommandLineFlag public.

PiperOrigin-RevId: 308814376
GitOrigin-RevId: 53550735f5a943dfb99225e7c53f211c2d6e7951
Change-Id: Iae52c65b7322152c7e58f222d60eb5a21699a2cb
parent 6e18c711
...@@ -66,6 +66,13 @@ void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock, ...@@ -66,6 +66,13 @@ void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock,
submit_profile_data.Store(fn); submit_profile_data.Store(fn);
} }
// Static member variable definitions.
constexpr uint32_t SpinLock::kSpinLockHeld;
constexpr uint32_t SpinLock::kSpinLockCooperative;
constexpr uint32_t SpinLock::kSpinLockDisabledScheduling;
constexpr uint32_t SpinLock::kSpinLockSleeper;
constexpr uint32_t SpinLock::kWaitTimeMask;
// Uncommon constructors. // Uncommon constructors.
SpinLock::SpinLock(base_internal::SchedulingMode mode) SpinLock::SpinLock(base_internal::SchedulingMode mode)
: lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) { : lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <atomic> #include <atomic>
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/const_init.h"
#include "absl/base/dynamic_annotations.h" #include "absl/base/dynamic_annotations.h"
#include "absl/base/internal/low_level_scheduling.h" #include "absl/base/internal/low_level_scheduling.h"
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
...@@ -77,6 +78,10 @@ class ABSL_LOCKABLE SpinLock { ...@@ -77,6 +78,10 @@ class ABSL_LOCKABLE SpinLock {
SpinLock(base_internal::LinkerInitialized, SpinLock(base_internal::LinkerInitialized,
base_internal::SchedulingMode mode); base_internal::SchedulingMode mode);
// Constructor for global SpinLock instances. See absl/base/const_init.h.
constexpr SpinLock(absl::ConstInitType, base_internal::SchedulingMode mode)
: lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {}
~SpinLock() { ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static); } ~SpinLock() { ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static); }
// Acquire this SpinLock. // Acquire this SpinLock.
......
...@@ -344,15 +344,16 @@ pid_t GetTID() { ...@@ -344,15 +344,16 @@ pid_t GetTID() {
#else #else
// Fallback implementation of GetTID using pthread_getspecific. // Fallback implementation of GetTID using pthread_getspecific.
static once_flag tid_once; ABSL_CONST_INIT static once_flag tid_once;
static pthread_key_t tid_key; ABSL_CONST_INIT static pthread_key_t tid_key;
static absl::base_internal::SpinLock tid_lock( ABSL_CONST_INIT static absl::base_internal::SpinLock tid_lock(
absl::base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
// We set a bit per thread in this array to indicate that an ID is in // We set a bit per thread in this array to indicate that an ID is in
// use. ID 0 is unused because it is the default value returned by // use. ID 0 is unused because it is the default value returned by
// pthread_getspecific(). // pthread_getspecific().
static std::vector<uint32_t>* tid_array ABSL_GUARDED_BY(tid_lock) = nullptr; ABSL_CONST_INIT static std::vector<uint32_t> *tid_array
ABSL_GUARDED_BY(tid_lock) = nullptr;
static constexpr int kBitsPerWord = 32; // tid_array is uint32_t. static constexpr int kBitsPerWord = 32; // tid_array is uint32_t.
// Returns the TID to tid_array. // Returns the TID to tid_array.
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/internal/spinlock.h" #include "absl/base/internal/spinlock.h"
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/internal/per_thread_sem.h" #include "absl/synchronization/internal/per_thread_sem.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
...@@ -29,10 +30,9 @@ ABSL_NAMESPACE_BEGIN ...@@ -29,10 +30,9 @@ ABSL_NAMESPACE_BEGIN
namespace base_internal { namespace base_internal {
namespace { namespace {
// protects num_identities_reused ABSL_CONST_INIT static absl::base_internal::SpinLock map_lock(
static absl::base_internal::SpinLock map_lock( absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
absl::base_internal::kLinkerInitialized); ABSL_CONST_INIT static int num_identities_reused ABSL_GUARDED_BY(map_lock);
static int num_identities_reused;
static const void* const kCheckNoIdentity = reinterpret_cast<void*>(1); static const void* const kCheckNoIdentity = reinterpret_cast<void*>(1);
...@@ -90,6 +90,7 @@ TEST(ThreadIdentityTest, BasicIdentityWorksThreaded) { ...@@ -90,6 +90,7 @@ TEST(ThreadIdentityTest, BasicIdentityWorksThreaded) {
// We should have recycled ThreadIdentity objects above; while (external) // We should have recycled ThreadIdentity objects above; while (external)
// library threads allocating their own identities may preclude some // library threads allocating their own identities may preclude some
// reuse, we should have sufficient repetitions to exclude this. // reuse, we should have sufficient repetitions to exclude this.
absl::base_internal::SpinLockHolder l(&map_lock);
EXPECT_LT(kNumThreads, num_identities_reused); EXPECT_LT(kNumThreads, num_identities_reused);
} }
......
...@@ -56,12 +56,10 @@ namespace { ...@@ -56,12 +56,10 @@ namespace {
static constexpr int kArrayLength = 10; static constexpr int kArrayLength = 10;
static uint32_t values[kArrayLength]; static uint32_t values[kArrayLength];
static SpinLock static_spinlock(base_internal::kLinkerInitialized); ABSL_CONST_INIT static SpinLock static_cooperative_spinlock(
static SpinLock static_cooperative_spinlock( absl::kConstInit, base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL);
base_internal::kLinkerInitialized, ABSL_CONST_INIT static SpinLock static_noncooperative_spinlock(
base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
static SpinLock static_noncooperative_spinlock(
base_internal::kLinkerInitialized, base_internal::SCHEDULE_KERNEL_ONLY);
// Simple integer hash function based on the public domain lookup2 hash. // Simple integer hash function based on the public domain lookup2 hash.
// http://burtleburtle.net/bob/c/lookup2.c // http://burtleburtle.net/bob/c/lookup2.c
...@@ -191,10 +189,6 @@ TEST(SpinLock, WaitCyclesEncoding) { ...@@ -191,10 +189,6 @@ TEST(SpinLock, WaitCyclesEncoding) {
EXPECT_GT(expected_max_value_decoded, before_max_value_decoded); EXPECT_GT(expected_max_value_decoded, before_max_value_decoded);
} }
TEST(SpinLockWithThreads, StaticSpinLock) {
ThreadedTest(&static_spinlock);
}
TEST(SpinLockWithThreads, StackSpinLock) { TEST(SpinLockWithThreads, StackSpinLock) {
SpinLock spinlock; SpinLock spinlock;
ThreadedTest(&spinlock); ThreadedTest(&spinlock);
......
...@@ -514,12 +514,6 @@ class node_hash_map ...@@ -514,12 +514,6 @@ class node_hash_map
// //
// Returns the function used for comparing keys equality. // Returns the function used for comparing keys equality.
using Base::key_eq; using Base::key_eq;
ABSL_DEPRECATED("Call `hash_function()` instead.")
typename Base::hasher hash_funct() { return this->hash_function(); }
ABSL_DEPRECATED("Call `rehash()` instead.")
void resize(typename Base::size_type hint) { this->rehash(hint); }
}; };
// erase_if(node_hash_map<>, Pred) // erase_if(node_hash_map<>, Pred)
......
...@@ -427,12 +427,6 @@ class node_hash_set ...@@ -427,12 +427,6 @@ class node_hash_set
// //
// Returns the function used for comparing keys equality. // Returns the function used for comparing keys equality.
using Base::key_eq; using Base::key_eq;
ABSL_DEPRECATED("Call `hash_function()` instead.")
typename Base::hasher hash_funct() { return this->hash_function(); }
ABSL_DEPRECATED("Call `rehash()` instead.")
void resize(typename Base::size_type hint) { this->rehash(hint); }
}; };
// erase_if(node_hash_set<>, Pred) // erase_if(node_hash_set<>, Pred)
......
...@@ -118,11 +118,12 @@ class Flag { ...@@ -118,11 +118,12 @@ class Flag {
return impl_; return impl_;
} }
impl_ = impl_ = new flags_internal::Flag<T>(
new flags_internal::Flag<T>(name_, filename_, name_, filename_,
{flags_internal::FlagHelpMsg(help_gen_), {flags_internal::FlagHelpMsg(help_gen_),
flags_internal::FlagHelpKind::kGenFunc}, flags_internal::FlagHelpKind::kGenFunc},
default_value_gen_); {flags_internal::FlagDefaultSrc(default_value_gen_),
flags_internal::FlagDefaultKind::kGenFunc});
inited_.store(true, std::memory_order_release); inited_.store(true, std::memory_order_release);
} }
...@@ -132,14 +133,12 @@ class Flag { ...@@ -132,14 +133,12 @@ class Flag {
// Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API. // Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
// See https://abseil.io/docs/cpp/guides/flags // See https://abseil.io/docs/cpp/guides/flags
bool IsRetired() const { return GetImpl()->IsRetired(); } bool IsRetired() const { return GetImpl()->IsRetired(); }
bool IsAbseilFlag() const { return GetImpl()->IsAbseilFlag(); }
absl::string_view Name() const { return GetImpl()->Name(); } absl::string_view Name() const { return GetImpl()->Name(); }
std::string Help() const { return GetImpl()->Help(); } std::string Help() const { return GetImpl()->Help(); }
bool IsModified() const { return GetImpl()->IsModified(); } bool IsModified() const { return GetImpl()->IsModified(); }
bool IsSpecifiedOnCommandLine() const { bool IsSpecifiedOnCommandLine() const {
return GetImpl()->IsSpecifiedOnCommandLine(); return GetImpl()->IsSpecifiedOnCommandLine();
} }
absl::string_view Typename() const { return GetImpl()->Typename(); }
std::string Filename() const { return GetImpl()->Filename(); } std::string Filename() const { return GetImpl()->Filename(); }
std::string DefaultValue() const { return GetImpl()->DefaultValue(); } std::string DefaultValue() const { return GetImpl()->DefaultValue(); }
std::string CurrentValue() const { return GetImpl()->CurrentValue(); } std::string CurrentValue() const { return GetImpl()->CurrentValue(); }
...@@ -311,9 +310,12 @@ ABSL_NAMESPACE_END ...@@ -311,9 +310,12 @@ ABSL_NAMESPACE_END
static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \ static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \
} }
#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ #define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
static void AbslFlagsInitFlag##name(void* dst) { \ struct AbslFlagDefaultGenFor##name { \
absl::flags_internal::MakeFromDefaultValue<Type>(dst, default_value); \ Type value = absl::flags_internal::InitDefaultValue<Type>(default_value); \
static void Gen(void* p) { \
new (p) Type(AbslFlagDefaultGenFor##name{}.value); \
} \
} }
// ABSL_FLAG_IMPL // ABSL_FLAG_IMPL
...@@ -322,29 +324,30 @@ ABSL_NAMESPACE_END ...@@ -322,29 +324,30 @@ ABSL_NAMESPACE_END
// global name for FLAGS_no<flag_name> symbol, thus preventing the possibility // global name for FLAGS_no<flag_name> symbol, thus preventing the possibility
// of defining two flags with names foo and nofoo. // of defining two flags with names foo and nofoo.
#if !defined(_MSC_VER) || defined(__clang__) #if !defined(_MSC_VER) || defined(__clang__)
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \ #define ABSL_FLAG_IMPL(Type, name, default_value, help) \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value); \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \ ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \ ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
&AbslFlagsInitFlag##name}; \ absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \
extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ absl::flags_internal::DefaultArg<Type, AbslFlagDefaultGenFor##name>(0)}; \
absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \
absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \
ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#else #else
// MSVC version uses aggregate initialization. We also do not try to // MSVC version uses aggregate initialization. We also do not try to
// optimize away help wrapper. // optimize away help wrapper.
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ #define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \ namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value); \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \ ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
&AbslFlagHelpGenFor##name::NonConst, &AbslFlagsInitFlag##name}; \ &AbslFlagHelpGenFor##name::NonConst, &AbslFlagDefaultGenFor##name::Gen}; \
extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \
absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \
ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#endif #endif
......
...@@ -128,26 +128,27 @@ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"), ...@@ -128,26 +128,27 @@ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"),
using String = std::string; using String = std::string;
#define DEFINE_CONSTRUCTED_FLAG(T) \ #define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
constexpr flags::Flag<T> f1##T("f1", "file", help_arg, &TestMakeDflt<T>); \ constexpr flags::FlagDefaultArg f1default##T{ \
ABSL_CONST_INIT flags::Flag<T> f2##T( \ flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
"f2", "file", \ constexpr flags::Flag<T> f1##T("f1", "file", help_arg, f1default##T); \
{flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \ ABSL_CONST_INIT flags::Flag<T> f2##T( \
&TestMakeDflt<T>) "f2", "file", \
{flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \
#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, &f2##T); flags::FlagDefaultArg{flags::FlagDefaultSrc(&TestMakeDflt<T>), \
flags::FlagDefaultKind::kGenFunc})
DEFINE_CONSTRUCTED_FLAG(bool);
DEFINE_CONSTRUCTED_FLAG(int16_t); DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord);
DEFINE_CONSTRUCTED_FLAG(uint16_t); DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord);
DEFINE_CONSTRUCTED_FLAG(int32_t); DEFINE_CONSTRUCTED_FLAG(uint16_t, 2, kOneWord);
DEFINE_CONSTRUCTED_FLAG(uint32_t); DEFINE_CONSTRUCTED_FLAG(int32_t, 3, kOneWord);
DEFINE_CONSTRUCTED_FLAG(int64_t); DEFINE_CONSTRUCTED_FLAG(uint32_t, 4, kOneWord);
DEFINE_CONSTRUCTED_FLAG(uint64_t); DEFINE_CONSTRUCTED_FLAG(int64_t, 5, kOneWord);
DEFINE_CONSTRUCTED_FLAG(float); DEFINE_CONSTRUCTED_FLAG(uint64_t, 6, kOneWord);
DEFINE_CONSTRUCTED_FLAG(double); DEFINE_CONSTRUCTED_FLAG(float, 7.8, kFloat);
DEFINE_CONSTRUCTED_FLAG(String); DEFINE_CONSTRUCTED_FLAG(double, 9.10, kDouble);
DEFINE_CONSTRUCTED_FLAG(UDT); DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt<String>, kGenFunc);
DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt<UDT>, kGenFunc);
template <typename T> template <typename T>
bool TestConstructionFor(const flags::Flag<T>& f1, flags::Flag<T>* f2) { bool TestConstructionFor(const flags::Flag<T>& f1, flags::Flag<T>* f2) {
...@@ -164,6 +165,8 @@ bool TestConstructionFor(const flags::Flag<T>& f1, flags::Flag<T>* f2) { ...@@ -164,6 +165,8 @@ bool TestConstructionFor(const flags::Flag<T>& f1, flags::Flag<T>* f2) {
return true; return true;
} }
#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, &f2##T);
TEST_F(FlagTest, TestConstruction) { TEST_F(FlagTest, TestConstruction) {
TEST_CONSTRUCTED_FLAG(bool); TEST_CONSTRUCTED_FLAG(bool);
TEST_CONSTRUCTED_FLAG(int16_t); TEST_CONSTRUCTED_FLAG(int16_t);
...@@ -443,29 +446,29 @@ TEST_F(FlagTest, TestGetSet) { ...@@ -443,29 +446,29 @@ TEST_F(FlagTest, TestGetSet) {
TEST_F(FlagTest, TestGetViaReflection) { TEST_F(FlagTest, TestGetViaReflection) {
auto* handle = flags::FindCommandLineFlag("test_flag_01"); auto* handle = flags::FindCommandLineFlag("test_flag_01");
EXPECT_EQ(*handle->Get<bool>(), true); EXPECT_EQ(*handle->TryGet<bool>(), true);
handle = flags::FindCommandLineFlag("test_flag_02"); handle = flags::FindCommandLineFlag("test_flag_02");
EXPECT_EQ(*handle->Get<int>(), 1234); EXPECT_EQ(*handle->TryGet<int>(), 1234);
handle = flags::FindCommandLineFlag("test_flag_03"); handle = flags::FindCommandLineFlag("test_flag_03");
EXPECT_EQ(*handle->Get<int16_t>(), -34); EXPECT_EQ(*handle->TryGet<int16_t>(), -34);
handle = flags::FindCommandLineFlag("test_flag_04"); handle = flags::FindCommandLineFlag("test_flag_04");
EXPECT_EQ(*handle->Get<uint16_t>(), 189); EXPECT_EQ(*handle->TryGet<uint16_t>(), 189);
handle = flags::FindCommandLineFlag("test_flag_05"); handle = flags::FindCommandLineFlag("test_flag_05");
EXPECT_EQ(*handle->Get<int32_t>(), 10765); EXPECT_EQ(*handle->TryGet<int32_t>(), 10765);
handle = flags::FindCommandLineFlag("test_flag_06"); handle = flags::FindCommandLineFlag("test_flag_06");
EXPECT_EQ(*handle->Get<uint32_t>(), 40000); EXPECT_EQ(*handle->TryGet<uint32_t>(), 40000);
handle = flags::FindCommandLineFlag("test_flag_07"); handle = flags::FindCommandLineFlag("test_flag_07");
EXPECT_EQ(*handle->Get<int64_t>(), -1234567); EXPECT_EQ(*handle->TryGet<int64_t>(), -1234567);
handle = flags::FindCommandLineFlag("test_flag_08"); handle = flags::FindCommandLineFlag("test_flag_08");
EXPECT_EQ(*handle->Get<uint64_t>(), 9876543); EXPECT_EQ(*handle->TryGet<uint64_t>(), 9876543);
handle = flags::FindCommandLineFlag("test_flag_09"); handle = flags::FindCommandLineFlag("test_flag_09");
EXPECT_NEAR(*handle->Get<double>(), -9.876e-50, 1e-55); EXPECT_NEAR(*handle->TryGet<double>(), -9.876e-50, 1e-55);
handle = flags::FindCommandLineFlag("test_flag_10"); handle = flags::FindCommandLineFlag("test_flag_10");
EXPECT_NEAR(*handle->Get<float>(), 1.234e12f, 1e5f); EXPECT_NEAR(*handle->TryGet<float>(), 1.234e12f, 1e5f);
handle = flags::FindCommandLineFlag("test_flag_11"); handle = flags::FindCommandLineFlag("test_flag_11");
EXPECT_EQ(*handle->Get<std::string>(), ""); EXPECT_EQ(*handle->TryGet<std::string>(), "");
handle = flags::FindCommandLineFlag("test_flag_12"); handle = flags::FindCommandLineFlag("test_flag_12");
EXPECT_EQ(*handle->Get<absl::Duration>(), absl::Minutes(10)); EXPECT_EQ(*handle->TryGet<absl::Duration>(), absl::Minutes(10));
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
...@@ -22,7 +22,20 @@ namespace flags_internal { ...@@ -22,7 +22,20 @@ namespace flags_internal {
FlagStateInterface::~FlagStateInterface() {} FlagStateInterface::~FlagStateInterface() {}
bool CommandLineFlag::IsRetired() const { return false; } bool CommandLineFlag::IsRetired() const { return false; }
bool CommandLineFlag::IsAbseilFlag() const { return true; }
FlagFastTypeId PrivateHandleInterface::TypeId(const CommandLineFlag& flag) {
return flag.TypeId();
}
std::unique_ptr<FlagStateInterface> PrivateHandleInterface::SaveState(
CommandLineFlag* flag) {
return flag->SaveState();
}
bool PrivateHandleInterface::ValidateInputValue(const CommandLineFlag& flag,
absl::string_view value) {
return flag.ValidateInputValue(value);
}
} // namespace flags_internal } // namespace flags_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
......
...@@ -93,7 +93,7 @@ class CommandLineFlag { ...@@ -93,7 +93,7 @@ class CommandLineFlag {
// Attempts to retrieve the flag value. Returns value on success, // Attempts to retrieve the flag value. Returns value on success,
// absl::nullopt otherwise. // absl::nullopt otherwise.
template <typename T> template <typename T>
absl::optional<T> Get() const { absl::optional<T> TryGet() const {
if (IsRetired() || !IsOfType<T>()) { if (IsRetired() || !IsOfType<T>()) {
return absl::nullopt; return absl::nullopt;
} }
...@@ -130,29 +130,14 @@ class CommandLineFlag { ...@@ -130,29 +130,14 @@ class CommandLineFlag {
virtual absl::string_view Name() const = 0; virtual absl::string_view Name() const = 0;
// Returns name of the file where this flag is defined. // Returns name of the file where this flag is defined.
virtual std::string Filename() const = 0; virtual std::string Filename() const = 0;
// Returns name of the flag's value type for some built-in types or empty
// string.
virtual absl::string_view Typename() const = 0;
// Returns help message associated with this flag. // Returns help message associated with this flag.
virtual std::string Help() const = 0; virtual std::string Help() const = 0;
// Returns true iff this object corresponds to retired flag. // Returns true iff this object corresponds to retired flag.
virtual bool IsRetired() const; virtual bool IsRetired() const;
// Returns true iff this is a handle to an Abseil Flag.
virtual bool IsAbseilFlag() const;
// Returns id of the flag's value type.
virtual FlagFastTypeId TypeId() const = 0;
virtual bool IsModified() const = 0;
virtual bool IsSpecifiedOnCommandLine() const = 0; virtual bool IsSpecifiedOnCommandLine() const = 0;
virtual std::string DefaultValue() const = 0; virtual std::string DefaultValue() const = 0;
virtual std::string CurrentValue() const = 0; virtual std::string CurrentValue() const = 0;
// Interfaces to operate on validators.
virtual bool ValidateInputValue(absl::string_view value) const = 0;
// Interface to save flag to some persistent state. Returns current flag state
// or nullptr if flag does not support saving and restoring a state.
virtual std::unique_ptr<FlagStateInterface> SaveState() = 0;
// Sets the value of the flag based on specified string `value`. If the flag // Sets the value of the flag based on specified string `value`. If the flag
// was successfully set to new value, it returns true. Otherwise, sets `error` // was successfully set to new value, it returns true. Otherwise, sets `error`
// to indicate the error, leaves the flag unchanged, and returns false. There // to indicate the error, leaves the flag unchanged, and returns false. There
...@@ -174,9 +159,38 @@ class CommandLineFlag { ...@@ -174,9 +159,38 @@ class CommandLineFlag {
~CommandLineFlag() = default; ~CommandLineFlag() = default;
private: private:
friend class PrivateHandleInterface;
// Returns id of the flag's value type.
virtual FlagFastTypeId TypeId() const = 0;
// Interface to save flag to some persistent state. Returns current flag state
// or nullptr if flag does not support saving and restoring a state.
virtual std::unique_ptr<FlagStateInterface> SaveState() = 0;
// Copy-construct a new value of the flag's type in a memory referenced by // Copy-construct a new value of the flag's type in a memory referenced by
// the dst based on the current flag's value. // the dst based on the current flag's value.
virtual void Read(void* dst) const = 0; virtual void Read(void* dst) const = 0;
// Interfaces to operate on validators.
// Validates supplied value usign validator or parseflag routine
virtual bool ValidateInputValue(absl::string_view value) const = 0;
};
// This class serves as a trampoline to access private methods of
// CommandLineFlag. This class is intended for use exclusively internally inside
// of the Abseil Flags implementation
class PrivateHandleInterface {
public:
// Access to CommandLineFlag::TypeId.
static FlagFastTypeId TypeId(const CommandLineFlag& flag);
// Access to CommandLineFlag::SaveState.
static std::unique_ptr<FlagStateInterface> SaveState(CommandLineFlag* flag);
// Access to CommandLineFlag::ValidateInputValue.
static bool ValidateInputValue(const CommandLineFlag& flag,
absl::string_view value);
}; };
// This macro is the "source of truth" for the list of supported flag built-in // This macro is the "source of truth" for the list of supported flag built-in
......
...@@ -67,7 +67,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ...@@ -67,7 +67,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
ASSERT_TRUE(flag_01); ASSERT_TRUE(flag_01);
EXPECT_EQ(flag_01->Name(), "int_flag"); EXPECT_EQ(flag_01->Name(), "int_flag");
EXPECT_EQ(flag_01->Help(), "int_flag help"); EXPECT_EQ(flag_01->Help(), "int_flag help");
EXPECT_EQ(flag_01->Typename(), "");
EXPECT_TRUE(!flag_01->IsRetired()); EXPECT_TRUE(!flag_01->IsRetired());
EXPECT_TRUE(flag_01->IsOfType<int>()); EXPECT_TRUE(flag_01->IsOfType<int>());
EXPECT_TRUE( EXPECT_TRUE(
...@@ -80,7 +79,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ...@@ -80,7 +79,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
ASSERT_TRUE(flag_02); ASSERT_TRUE(flag_02);
EXPECT_EQ(flag_02->Name(), "string_flag"); EXPECT_EQ(flag_02->Name(), "string_flag");
EXPECT_EQ(flag_02->Help(), "string_flag help"); EXPECT_EQ(flag_02->Help(), "string_flag help");
EXPECT_EQ(flag_02->Typename(), "");
EXPECT_TRUE(!flag_02->IsRetired()); EXPECT_TRUE(!flag_02->IsRetired());
EXPECT_TRUE(flag_02->IsOfType<std::string>()); EXPECT_TRUE(flag_02->IsOfType<std::string>());
EXPECT_TRUE( EXPECT_TRUE(
...@@ -93,7 +91,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ...@@ -93,7 +91,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
ASSERT_TRUE(flag_03); ASSERT_TRUE(flag_03);
EXPECT_EQ(flag_03->Name(), "bool_retired_flag"); EXPECT_EQ(flag_03->Name(), "bool_retired_flag");
EXPECT_EQ(flag_03->Help(), ""); EXPECT_EQ(flag_03->Help(), "");
EXPECT_EQ(flag_03->Typename(), "");
EXPECT_TRUE(flag_03->IsRetired()); EXPECT_TRUE(flag_03->IsRetired());
EXPECT_TRUE(flag_03->IsOfType<bool>()); EXPECT_TRUE(flag_03->IsOfType<bool>());
EXPECT_EQ(flag_03->Filename(), "RETIRED"); EXPECT_EQ(flag_03->Filename(), "RETIRED");
......
...@@ -139,19 +139,43 @@ void DynValueDeleter::operator()(void* ptr) const { ...@@ -139,19 +139,43 @@ void DynValueDeleter::operator()(void* ptr) const {
void FlagImpl::Init() { void FlagImpl::Init() {
new (&data_guard_) absl::Mutex; new (&data_guard_) absl::Mutex;
// At this point the default_value_ always points to gen_func. auto def_kind = static_cast<FlagDefaultKind>(def_kind_);
switch (ValueStorageKind()) { switch (ValueStorageKind()) {
case FlagValueStorageKind::kAlignedBuffer: case FlagValueStorageKind::kAlignedBuffer:
// For this storage kind the default_value_ always points to gen_func
// during initialization.
assert(def_kind == FlagDefaultKind::kGenFunc);
(*default_value_.gen_func)(AlignedBufferValue()); (*default_value_.gen_func)(AlignedBufferValue());
break; break;
case FlagValueStorageKind::kOneWordAtomic: { case FlagValueStorageKind::kOneWordAtomic: {
alignas(int64_t) std::array<char, sizeof(int64_t)> buf{}; alignas(int64_t) std::array<char, sizeof(int64_t)> buf{};
(*default_value_.gen_func)(buf.data()); switch (def_kind) {
auto value = absl::bit_cast<int64_t>(buf); case FlagDefaultKind::kOneWord:
OneWordValue().store(value, std::memory_order_release); std::memcpy(buf.data(), &default_value_.one_word,
sizeof(default_value_.one_word));
break;
case FlagDefaultKind::kFloat:
std::memcpy(buf.data(), &default_value_.float_value,
sizeof(default_value_.float_value));
break;
case FlagDefaultKind::kDouble:
std::memcpy(buf.data(), &default_value_.double_value,
sizeof(default_value_.double_value));
break;
default:
assert(def_kind == FlagDefaultKind::kGenFunc);
(*default_value_.gen_func)(buf.data());
break;
}
OneWordValue().store(absl::bit_cast<int64_t>(buf),
std::memory_order_release);
break; break;
} }
case FlagValueStorageKind::kTwoWordsAtomic: { case FlagValueStorageKind::kTwoWordsAtomic: {
// For this storage kind the default_value_ always points to gen_func
// during initialization.
assert(def_kind == FlagDefaultKind::kGenFunc);
alignas(AlignedTwoWords) std::array<char, sizeof(AlignedTwoWords)> buf{}; alignas(AlignedTwoWords) std::array<char, sizeof(AlignedTwoWords)> buf{};
(*default_value_.gen_func)(buf.data()); (*default_value_.gen_func)(buf.data());
auto atomic_value = absl::bit_cast<AlignedTwoWords>(buf); auto atomic_value = absl::bit_cast<AlignedTwoWords>(buf);
...@@ -196,11 +220,23 @@ void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id, ...@@ -196,11 +220,23 @@ void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id,
std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const { std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
void* res = nullptr; void* res = nullptr;
if (DefaultKind() == FlagDefaultKind::kDynamicValue) { switch (DefaultKind()) {
res = flags_internal::Clone(op_, default_value_.dynamic_value); case FlagDefaultKind::kDynamicValue:
} else { res = flags_internal::Clone(op_, default_value_.dynamic_value);
res = flags_internal::Alloc(op_); break;
(*default_value_.gen_func)(res); case FlagDefaultKind::kGenFunc:
res = flags_internal::Alloc(op_);
(*default_value_.gen_func)(res);
break;
case FlagDefaultKind::kOneWord:
res = flags_internal::Clone(op_, &default_value_.one_word);
break;
case FlagDefaultKind::kFloat:
res = flags_internal::Clone(op_, &default_value_.float_value);
break;
case FlagDefaultKind::kDouble:
res = flags_internal::Clone(op_, &default_value_.double_value);
break;
} }
return {res, DynValueDeleter{op_}}; return {res, DynValueDeleter{op_}};
} }
...@@ -235,8 +271,6 @@ std::string FlagImpl::Filename() const { ...@@ -235,8 +271,6 @@ std::string FlagImpl::Filename() const {
return flags_internal::GetUsageConfig().normalize_filename(filename_); return flags_internal::GetUsageConfig().normalize_filename(filename_);
} }
absl::string_view FlagImpl::Typename() const { return ""; }
std::string FlagImpl::Help() const { std::string FlagImpl::Help() const {
return HelpSourceKind() == FlagHelpKind::kLiteral ? help_.literal return HelpSourceKind() == FlagHelpKind::kLiteral ? help_.literal
: help_.gen_func(); : help_.gen_func();
...@@ -246,11 +280,6 @@ FlagFastTypeId FlagImpl::TypeId() const { ...@@ -246,11 +280,6 @@ FlagFastTypeId FlagImpl::TypeId() const {
return flags_internal::FastTypeId(op_); return flags_internal::FastTypeId(op_);
} }
bool FlagImpl::IsModified() const {
absl::MutexLock l(DataGuard());
return modified_;
}
bool FlagImpl::IsSpecifiedOnCommandLine() const { bool FlagImpl::IsSpecifiedOnCommandLine() const {
absl::MutexLock l(DataGuard()); absl::MutexLock l(DataGuard());
return on_command_line_; return on_command_line_;
......
...@@ -206,12 +206,72 @@ using FlagDfltGenFunc = void (*)(void*); ...@@ -206,12 +206,72 @@ using FlagDfltGenFunc = void (*)(void*);
union FlagDefaultSrc { union FlagDefaultSrc {
constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg) constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)
: gen_func(gen_func_arg) {} : gen_func(gen_func_arg) {}
template <typename T>
constexpr explicit FlagDefaultSrc(T one_word_value)
: one_word(static_cast<int64_t>(one_word_value)) {}
constexpr explicit FlagDefaultSrc(float f) : float_value(f) {}
constexpr explicit FlagDefaultSrc(double d) : double_value(d) {}
void* dynamic_value; void* dynamic_value;
FlagDfltGenFunc gen_func; FlagDfltGenFunc gen_func;
int64_t one_word;
float float_value;
double double_value;
};
enum class FlagDefaultKind : uint8_t {
kDynamicValue = 0,
kGenFunc = 1,
kOneWord = 2,
kFloat = 3,
kDouble = 4
};
struct FlagDefaultArg {
FlagDefaultSrc source;
FlagDefaultKind kind;
}; };
enum class FlagDefaultKind : uint8_t { kDynamicValue = 0, kGenFunc = 1 }; // This struct and corresponding overload to InitDefaultValue are used to
// facilitate usage of {} as default value in ABSL_FLAG macro.
// TODO(rogeeff): Fix handling types with explicit constructors.
struct EmptyBraces {};
template <typename T>
constexpr T InitDefaultValue(T t) {
return t;
}
template <typename T>
constexpr T InitDefaultValue(EmptyBraces) {
return T{};
}
template <typename ValueT, typename GenT,
typename std::enable_if<std::is_integral<ValueT>::value, int>::type =
(GenT{}, 0)>
constexpr FlagDefaultArg DefaultArg(int) {
return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord};
}
template <typename ValueT, typename GenT,
typename std::enable_if<std::is_same<ValueT, float>::value,
int>::type = (GenT{}, 0)>
constexpr FlagDefaultArg DefaultArg(int) {
return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kFloat};
}
template <typename ValueT, typename GenT,
typename std::enable_if<std::is_same<ValueT, double>::value,
int>::type = (GenT{}, 0)>
constexpr FlagDefaultArg DefaultArg(int) {
return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kDouble};
}
template <typename ValueT, typename GenT>
constexpr FlagDefaultArg DefaultArg(char) {
return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc};
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Flag current value auxiliary structs. // Flag current value auxiliary structs.
...@@ -356,19 +416,19 @@ class FlagImpl final : public flags_internal::CommandLineFlag { ...@@ -356,19 +416,19 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
public: public:
constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op, constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op,
FlagHelpArg help, FlagValueStorageKind value_kind, FlagHelpArg help, FlagValueStorageKind value_kind,
FlagDfltGenFunc default_value_gen) FlagDefaultArg default_arg)
: name_(name), : name_(name),
filename_(filename), filename_(filename),
op_(op), op_(op),
help_(help.source), help_(help.source),
help_source_kind_(static_cast<uint8_t>(help.kind)), help_source_kind_(static_cast<uint8_t>(help.kind)),
value_storage_kind_(static_cast<uint8_t>(value_kind)), value_storage_kind_(static_cast<uint8_t>(value_kind)),
def_kind_(static_cast<uint8_t>(FlagDefaultKind::kGenFunc)), def_kind_(static_cast<uint8_t>(default_arg.kind)),
modified_(false), modified_(false),
on_command_line_(false), on_command_line_(false),
counter_(0), counter_(0),
callback_(nullptr), callback_(nullptr),
default_value_(default_value_gen), default_value_(default_arg.source),
data_guard_{} {} data_guard_{} {}
// Constant access methods // Constant access methods
...@@ -444,10 +504,8 @@ class FlagImpl final : public flags_internal::CommandLineFlag { ...@@ -444,10 +504,8 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
// CommandLineFlag interface implementation // CommandLineFlag interface implementation
absl::string_view Name() const override; absl::string_view Name() const override;
std::string Filename() const override; std::string Filename() const override;
absl::string_view Typename() const override;
std::string Help() const override; std::string Help() const override;
FlagFastTypeId TypeId() const override; FlagFastTypeId TypeId() const override;
bool IsModified() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
bool IsSpecifiedOnCommandLine() const override bool IsSpecifiedOnCommandLine() const override
ABSL_LOCKS_EXCLUDED(*DataGuard()); ABSL_LOCKS_EXCLUDED(*DataGuard());
std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard()); std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
...@@ -492,9 +550,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag { ...@@ -492,9 +550,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
// Mutable flag's state (guarded by `data_guard_`). // Mutable flag's state (guarded by `data_guard_`).
// If def_kind_ == kDynamicValue, default_value_ holds a dynamically allocated // def_kind_ is not guard by DataGuard() since it is accessed in Init without
// value. // locks. If necessary we can decrease number of bits used to 2 by folding
uint8_t def_kind_ : 1 ABSL_GUARDED_BY(*DataGuard()); // one_word storage cases.
uint8_t def_kind_ : 3;
// Has this flag's value been modified? // Has this flag's value been modified?
bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard()); bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard());
// Has this flag been specified on command line. // Has this flag been specified on command line.
...@@ -530,10 +589,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag { ...@@ -530,10 +589,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
template <typename T> template <typename T>
class Flag { class Flag {
public: public:
constexpr Flag(const char* name, const char* filename, const FlagHelpArg help, constexpr Flag(const char* name, const char* filename, FlagHelpArg help,
const FlagDfltGenFunc default_value_gen) const FlagDefaultArg default_arg)
: impl_(name, filename, &FlagOps<T>, help, : impl_(name, filename, &FlagOps<T>, help,
flags_internal::StorageKind<T>(), default_value_gen), flags_internal::StorageKind<T>(), default_arg),
value_() {} value_() {}
T Get() const { T Get() const {
...@@ -560,9 +619,7 @@ class Flag { ...@@ -560,9 +619,7 @@ class Flag {
// CommandLineFlag interface // CommandLineFlag interface
absl::string_view Name() const { return impl_.Name(); } absl::string_view Name() const { return impl_.Name(); }
std::string Filename() const { return impl_.Filename(); } std::string Filename() const { return impl_.Filename(); }
absl::string_view Typename() const { return ""; }
std::string Help() const { return impl_.Help(); } std::string Help() const { return impl_.Help(); }
bool IsModified() const { return impl_.IsModified(); }
bool IsSpecifiedOnCommandLine() const { bool IsSpecifiedOnCommandLine() const {
return impl_.IsSpecifiedOnCommandLine(); return impl_.IsSpecifiedOnCommandLine();
} }
...@@ -662,20 +719,6 @@ class FlagRegistrar { ...@@ -662,20 +719,6 @@ class FlagRegistrar {
Flag<T>* flag_; // Flag being registered (not owned). Flag<T>* flag_; // Flag being registered (not owned).
}; };
// This struct and corresponding overload to MakeDefaultValue are used to
// facilitate usage of {} as default value in ABSL_FLAG macro.
struct EmptyBraces {};
template <typename T>
void MakeFromDefaultValue(void* dst, T t) {
new (dst) T(std::move(t));
}
template <typename T>
void MakeFromDefaultValue(void* dst, EmptyBraces) {
new (dst) T{};
}
} // namespace flags_internal } // namespace flags_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
......
...@@ -127,14 +127,13 @@ void FlagRegistry::RegisterFlag(CommandLineFlag* flag) { ...@@ -127,14 +127,13 @@ void FlagRegistry::RegisterFlag(CommandLineFlag* flag) {
(flag->IsRetired() ? old_flag->Filename() : flag->Filename()), (flag->IsRetired() ? old_flag->Filename() : flag->Filename()),
"'."), "'."),
true); true);
} else if (flag->TypeId() != old_flag->TypeId()) { } else if (flags_internal::PrivateHandleInterface::TypeId(*flag) !=
flags_internal::PrivateHandleInterface::TypeId(*old_flag)) {
flags_internal::ReportUsageError( flags_internal::ReportUsageError(
absl::StrCat("Flag '", flag->Name(), absl::StrCat("Flag '", flag->Name(),
"' was defined more than once but with " "' was defined more than once but with "
"differing types. Defined in files '", "differing types. Defined in files '",
old_flag->Filename(), "' and '", flag->Filename(), old_flag->Filename(), "' and '", flag->Filename(), "'."),
"' with types '", old_flag->Typename(), "' and '",
flag->Typename(), "', respectively."),
true); true);
} else if (old_flag->IsRetired()) { } else if (old_flag->IsRetired()) {
// Retired flag can just be deleted. // Retired flag can just be deleted.
...@@ -206,7 +205,8 @@ class FlagSaverImpl { ...@@ -206,7 +205,8 @@ class FlagSaverImpl {
void SaveFromRegistry() { void SaveFromRegistry() {
assert(backup_registry_.empty()); // call only once! assert(backup_registry_.empty()); // call only once!
flags_internal::ForEachFlag([&](flags_internal::CommandLineFlag* flag) { flags_internal::ForEachFlag([&](flags_internal::CommandLineFlag* flag) {
if (auto flag_state = flag->SaveState()) { if (auto flag_state =
flags_internal::PrivateHandleInterface::SaveState(flag)) {
backup_registry_.emplace_back(std::move(flag_state)); backup_registry_.emplace_back(std::move(flag_state));
} }
}); });
...@@ -290,11 +290,9 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag { ...@@ -290,11 +290,9 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
private: private:
absl::string_view Name() const override { return name_; } absl::string_view Name() const override { return name_; }
std::string Filename() const override { return "RETIRED"; } std::string Filename() const override { return "RETIRED"; }
absl::string_view Typename() const override { return ""; }
FlagFastTypeId TypeId() const override { return type_id_; } FlagFastTypeId TypeId() const override { return type_id_; }
std::string Help() const override { return ""; } std::string Help() const override { return ""; }
bool IsRetired() const override { return true; } bool IsRetired() const override { return true; }
bool IsModified() const override { return false; }
bool IsSpecifiedOnCommandLine() const override { return false; } bool IsSpecifiedOnCommandLine() const override { return false; }
std::string DefaultValue() const override { return ""; } std::string DefaultValue() const override { return ""; }
std::string CurrentValue() const override { return ""; } std::string CurrentValue() const override { return ""; }
......
...@@ -72,7 +72,9 @@ bool IsValidFlagValue(absl::string_view name, absl::string_view value) { ...@@ -72,7 +72,9 @@ bool IsValidFlagValue(absl::string_view name, absl::string_view value) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
return flag != nullptr && return flag != nullptr &&
(flag->IsRetired() || flag->ValidateInputValue(value)); (flag->IsRetired() ||
flags_internal::PrivateHandleInterface::ValidateInputValue(*flag,
value));
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
...@@ -75,7 +75,7 @@ inline bool GetByName(absl::string_view name, T* dst) { ...@@ -75,7 +75,7 @@ inline bool GetByName(absl::string_view name, T* dst) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (!flag) return false; if (!flag) return false;
if (auto val = flag->Get<T>()) { if (auto val = flag->TryGet<T>()) {
*dst = *val; *dst = *val;
return true; return true;
} }
......
...@@ -54,27 +54,6 @@ ABSL_NAMESPACE_BEGIN ...@@ -54,27 +54,6 @@ ABSL_NAMESPACE_BEGIN
namespace flags_internal { namespace flags_internal {
namespace { namespace {
absl::string_view TypenameForHelp(const flags_internal::CommandLineFlag& flag) {
// Only report names of v1 built-in types
#define HANDLE_V1_BUILTIN_TYPE(t) \
if (flag.IsOfType<t>()) { \
return #t; \
}
HANDLE_V1_BUILTIN_TYPE(bool);
HANDLE_V1_BUILTIN_TYPE(int32_t);
HANDLE_V1_BUILTIN_TYPE(int64_t);
HANDLE_V1_BUILTIN_TYPE(uint64_t);
HANDLE_V1_BUILTIN_TYPE(double);
#undef HANDLE_V1_BUILTIN_TYPE
if (flag.IsOfType<std::string>()) {
return "string";
}
return "";
}
// This class is used to emit an XML element with `tag` and `text`. // This class is used to emit an XML element with `tag` and `text`.
// It adds opening and closing tags and escapes special characters in the text. // It adds opening and closing tags and escapes special characters in the text.
// For example: // For example:
...@@ -212,23 +191,20 @@ void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag, ...@@ -212,23 +191,20 @@ void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag,
// Flag help. // Flag help.
printer.Write(absl::StrCat("(", flag.Help(), ");"), /*wrap_line=*/true); printer.Write(absl::StrCat("(", flag.Help(), ");"), /*wrap_line=*/true);
// Flag data type (for V1 flags only).
if (!flag.IsAbseilFlag() && !flag.IsRetired()) {
printer.Write(absl::StrCat("type: ", TypenameForHelp(flag), ";"));
}
// The listed default value will be the actual default from the flag // The listed default value will be the actual default from the flag
// definition in the originating source file, unless the value has // definition in the originating source file, unless the value has
// subsequently been modified using SetCommandLineOption() with mode // subsequently been modified using SetCommandLineOption() with mode
// SET_FLAGS_DEFAULT. // SET_FLAGS_DEFAULT.
std::string dflt_val = flag.DefaultValue(); std::string dflt_val = flag.DefaultValue();
std::string curr_val = flag.CurrentValue();
bool is_modified = curr_val != dflt_val;
if (flag.IsOfType<std::string>()) { if (flag.IsOfType<std::string>()) {
dflt_val = absl::StrCat("\"", dflt_val, "\""); dflt_val = absl::StrCat("\"", dflt_val, "\"");
} }
printer.Write(absl::StrCat("default: ", dflt_val, ";")); printer.Write(absl::StrCat("default: ", dflt_val, ";"));
if (flag.IsModified()) { if (is_modified) {
std::string curr_val = flag.CurrentValue();
if (flag.IsOfType<std::string>()) { if (flag.IsOfType<std::string>()) {
curr_val = absl::StrCat("\"", curr_val, "\""); curr_val = absl::StrCat("\"", curr_val, "\"");
} }
......
...@@ -315,7 +315,7 @@ bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { ...@@ -315,7 +315,7 @@ bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) {
inline bool ConvertStringArg(string_view v, const ConversionSpec conv, inline bool ConvertStringArg(string_view v, const ConversionSpec conv,
FormatSinkImpl *sink) { FormatSinkImpl *sink) {
if (conv.conversion_char() != ConversionChar::s) return false; if (conv.conversion_char() != FormatConversionCharInternal::s) return false;
if (conv.is_basic()) { if (conv.is_basic()) {
sink->Append(v); sink->Append(v);
return true; return true;
...@@ -327,22 +327,22 @@ inline bool ConvertStringArg(string_view v, const ConversionSpec conv, ...@@ -327,22 +327,22 @@ inline bool ConvertStringArg(string_view v, const ConversionSpec conv,
} // namespace } // namespace
// ==================== Strings ==================== // ==================== Strings ====================
ConvertResult<Conv::s> FormatConvertImpl(const std::string &v, StringConvertResult FormatConvertImpl(const std::string &v,
const ConversionSpec conv, const ConversionSpec conv,
FormatSinkImpl *sink) { FormatSinkImpl *sink) {
return {ConvertStringArg(v, conv, sink)}; return {ConvertStringArg(v, conv, sink)};
} }
ConvertResult<Conv::s> FormatConvertImpl(string_view v, StringConvertResult FormatConvertImpl(string_view v, const ConversionSpec conv,
const ConversionSpec conv, FormatSinkImpl *sink) {
FormatSinkImpl *sink) {
return {ConvertStringArg(v, conv, sink)}; return {ConvertStringArg(v, conv, sink)};
} }
ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char *v, ArgConvertResult<FormatConversionCharSetUnion(
const ConversionSpec conv, FormatConversionCharSetInternal::s, FormatConversionCharSetInternal::p)>
FormatSinkImpl *sink) { FormatConvertImpl(const char *v, const ConversionSpec conv,
if (conv.conversion_char() == ConversionChar::p) FormatSinkImpl *sink) {
if (conv.conversion_char() == FormatConversionCharInternal::p)
return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; return {FormatConvertImpl(VoidPtr(v), conv, sink).value};
size_t len; size_t len;
if (v == nullptr) { if (v == nullptr) {
...@@ -357,9 +357,9 @@ ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char *v, ...@@ -357,9 +357,9 @@ ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char *v,
} }
// ==================== Raw pointers ==================== // ==================== Raw pointers ====================
ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, const ConversionSpec conv, ArgConvertResult<FormatConversionCharSetInternal::p> FormatConvertImpl(
FormatSinkImpl *sink) { VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) {
if (conv.conversion_char() != ConversionChar::p) return {false}; if (conv.conversion_char() != FormatConversionCharInternal::p) return {false};
if (!v.value) { if (!v.value) {
sink->Append("(nil)"); sink->Append("(nil)");
return {true}; return {true};
......
...@@ -31,10 +31,11 @@ template <typename T, typename = void> ...@@ -31,10 +31,11 @@ template <typename T, typename = void>
struct HasUserDefinedConvert : std::false_type {}; struct HasUserDefinedConvert : std::false_type {};
template <typename T> template <typename T>
struct HasUserDefinedConvert< struct HasUserDefinedConvert<T, void_t<decltype(AbslFormatConvert(
T, void_t<decltype(AbslFormatConvert( std::declval<const T&>(),
std::declval<const T&>(), std::declval<ConversionSpec>(), std::declval<const FormatConversionSpec&>(),
std::declval<FormatSink*>()))>> : std::true_type {}; std::declval<FormatSink*>()))>>
: std::true_type {};
template <typename T> template <typename T>
class StreamedWrapper; class StreamedWrapper;
...@@ -52,25 +53,36 @@ struct VoidPtr { ...@@ -52,25 +53,36 @@ struct VoidPtr {
: value(ptr ? reinterpret_cast<uintptr_t>(ptr) : 0) {} : value(ptr ? reinterpret_cast<uintptr_t>(ptr) : 0) {}
uintptr_t value; uintptr_t value;
}; };
ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, ConversionSpec conv,
FormatSinkImpl* sink); template <FormatConversionCharSet C>
struct ArgConvertResult {
bool value;
};
template <FormatConversionCharSet C>
constexpr FormatConversionCharSet ExtractCharSet(ArgConvertResult<C>) {
return C;
}
using StringConvertResult =
ArgConvertResult<FormatConversionCharSetInternal::s>;
ArgConvertResult<FormatConversionCharSetInternal::p> FormatConvertImpl(
VoidPtr v, ConversionSpec conv, FormatSinkImpl* sink);
// Strings. // Strings.
ConvertResult<Conv::s> FormatConvertImpl(const std::string& v, StringConvertResult FormatConvertImpl(const std::string& v, ConversionSpec conv,
ConversionSpec conv, FormatSinkImpl* sink);
FormatSinkImpl* sink); StringConvertResult FormatConvertImpl(string_view v, ConversionSpec conv,
ConvertResult<Conv::s> FormatConvertImpl(string_view v, ConversionSpec conv, FormatSinkImpl* sink);
FormatSinkImpl* sink); ArgConvertResult<FormatConversionCharSetUnion(
ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char* v, FormatConversionCharSetInternal::s, FormatConversionCharSetInternal::p)>
ConversionSpec conv, FormatConvertImpl(const char* v, ConversionSpec conv, FormatSinkImpl* sink);
FormatSinkImpl* sink); template <class AbslCord, typename std::enable_if<std::is_same<
template <class AbslCord, AbslCord, absl::Cord>::value>::type* = nullptr>
typename std::enable_if< StringConvertResult FormatConvertImpl(const AbslCord& value,
std::is_same<AbslCord, absl::Cord>::value>::type* = nullptr> ConversionSpec conv,
ConvertResult<Conv::s> FormatConvertImpl(const AbslCord& value, FormatSinkImpl* sink) {
ConversionSpec conv, if (conv.conversion_char() != FormatConversionCharInternal::s) {
FormatSinkImpl* sink) {
if (conv.conversion_char() != ConversionChar::s) {
return {false}; return {false};
} }
...@@ -107,9 +119,12 @@ ConvertResult<Conv::s> FormatConvertImpl(const AbslCord& value, ...@@ -107,9 +119,12 @@ ConvertResult<Conv::s> FormatConvertImpl(const AbslCord& value,
return {true}; return {true};
} }
using IntegralConvertResult = using IntegralConvertResult = ArgConvertResult<FormatConversionCharSetUnion(
ConvertResult<Conv::c | Conv::kNumeric | Conv::kStar>; FormatConversionCharSetInternal::c,
using FloatingConvertResult = ConvertResult<Conv::kFloating>; FormatConversionCharSetInternal::kNumeric,
FormatConversionCharSetInternal::kStar)>;
using FloatingConvertResult =
ArgConvertResult<FormatConversionCharSetInternal::kFloating>;
// Floats. // Floats.
FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv, FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv,
...@@ -169,9 +184,9 @@ typename std::enable_if<std::is_enum<T>::value && ...@@ -169,9 +184,9 @@ typename std::enable_if<std::is_enum<T>::value &&
FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink); FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink);
template <typename T> template <typename T>
ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<T>& v, StringConvertResult FormatConvertImpl(const StreamedWrapper<T>& v,
ConversionSpec conv, ConversionSpec conv,
FormatSinkImpl* out) { FormatSinkImpl* out) {
std::ostringstream oss; std::ostringstream oss;
oss << v.v_; oss << v.v_;
if (!oss) return {false}; if (!oss) return {false};
...@@ -182,12 +197,12 @@ ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<T>& v, ...@@ -182,12 +197,12 @@ ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<T>& v,
// until after FormatCountCapture is fully defined. // until after FormatCountCapture is fully defined.
struct FormatCountCaptureHelper { struct FormatCountCaptureHelper {
template <class T = int> template <class T = int>
static ConvertResult<Conv::n> ConvertHelper(const FormatCountCapture& v, static ArgConvertResult<FormatConversionCharSetInternal::n> ConvertHelper(
ConversionSpec conv, const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) {
FormatSinkImpl* sink) {
const absl::enable_if_t<sizeof(T) != 0, FormatCountCapture>& v2 = v; const absl::enable_if_t<sizeof(T) != 0, FormatCountCapture>& v2 = v;
if (conv.conversion_char() != str_format_internal::ConversionChar::n) { if (conv.conversion_char() !=
str_format_internal::FormatConversionCharInternal::n) {
return {false}; return {false};
} }
*v2.p_ = static_cast<int>(sink->size()); *v2.p_ = static_cast<int>(sink->size());
...@@ -196,9 +211,8 @@ struct FormatCountCaptureHelper { ...@@ -196,9 +211,8 @@ struct FormatCountCaptureHelper {
}; };
template <class T = int> template <class T = int>
ConvertResult<Conv::n> FormatConvertImpl(const FormatCountCapture& v, ArgConvertResult<FormatConversionCharSetInternal::n> FormatConvertImpl(
ConversionSpec conv, const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) {
FormatSinkImpl* sink) {
return FormatCountCaptureHelper::ConvertHelper(v, conv, sink); return FormatCountCaptureHelper::ConvertHelper(v, conv, sink);
} }
...@@ -381,7 +395,8 @@ class FormatArgImpl { ...@@ -381,7 +395,8 @@ class FormatArgImpl {
template <typename T> template <typename T>
static bool Dispatch(Data arg, ConversionSpec spec, void* out) { static bool Dispatch(Data arg, ConversionSpec spec, void* out) {
// A `none` conv indicates that we want the `int` conversion. // A `none` conv indicates that we want the `int` conversion.
if (ABSL_PREDICT_FALSE(spec.conversion_char() == ConversionChar::kNone)) { if (ABSL_PREDICT_FALSE(spec.conversion_char() ==
FormatConversionCharInternal::kNone)) {
return ToInt<T>(arg, static_cast<int*>(out), std::is_integral<T>(), return ToInt<T>(arg, static_cast<int*>(out), std::is_integral<T>(),
std::is_enum<T>()); std::is_enum<T>());
} }
......
...@@ -189,9 +189,8 @@ class StreamedWrapper { ...@@ -189,9 +189,8 @@ class StreamedWrapper {
private: private:
template <typename S> template <typename S>
friend ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<S>& v, friend ArgConvertResult<FormatConversionCharSetInternal::s> FormatConvertImpl(
ConversionSpec conv, const StreamedWrapper<S>& v, ConversionSpec conv, FormatSinkImpl* out);
FormatSinkImpl* out);
const T& v_; const T& v_;
}; };
......
...@@ -25,10 +25,12 @@ constexpr bool AllOf(bool b, T... t) { ...@@ -25,10 +25,12 @@ constexpr bool AllOf(bool b, T... t) {
} }
template <typename Arg> template <typename Arg>
constexpr Conv ArgumentToConv() { constexpr FormatConversionCharSet ArgumentToConv() {
return decltype(str_format_internal::FormatConvertImpl( return absl::str_format_internal::ExtractCharSet(
std::declval<const Arg&>(), std::declval<const ConversionSpec&>(), decltype(str_format_internal::FormatConvertImpl(
std::declval<FormatSinkImpl*>()))::kConv; std::declval<const Arg&>(),
std::declval<const FormatConversionSpecImpl&>(),
std::declval<FormatSinkImpl*>())){});
} }
#ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
...@@ -39,14 +41,14 @@ constexpr bool ContainsChar(const char* chars, char c) { ...@@ -39,14 +41,14 @@ constexpr bool ContainsChar(const char* chars, char c) {
// A constexpr compatible list of Convs. // A constexpr compatible list of Convs.
struct ConvList { struct ConvList {
const Conv* array; const FormatConversionCharSet* array;
int count; int count;
// We do the bound check here to avoid having to do it on the callers. // We do the bound check here to avoid having to do it on the callers.
// Returning an empty Conv has the same effect as short circuiting because it // Returning an empty FormatConversionCharSet has the same effect as
// will never match any conversion. // short circuiting because it will never match any conversion.
constexpr Conv operator[](int i) const { constexpr FormatConversionCharSet operator[](int i) const {
return i < count ? array[i] : Conv{}; return i < count ? array[i] : FormatConversionCharSet{};
} }
constexpr ConvList without_front() const { constexpr ConvList without_front() const {
...@@ -57,7 +59,7 @@ struct ConvList { ...@@ -57,7 +59,7 @@ struct ConvList {
template <size_t count> template <size_t count>
struct ConvListT { struct ConvListT {
// Make sure the array has size > 0. // Make sure the array has size > 0.
Conv list[count ? count : 1]; FormatConversionCharSet list[count ? count : 1];
}; };
constexpr char GetChar(string_view str, size_t index) { constexpr char GetChar(string_view str, size_t index) {
...@@ -310,7 +312,7 @@ class FormatParser { ...@@ -310,7 +312,7 @@ class FormatParser {
ConvList args_; ConvList args_;
}; };
template <Conv... C> template <FormatConversionCharSet... C>
constexpr bool ValidFormatImpl(string_view format) { constexpr bool ValidFormatImpl(string_view format) {
return FormatParser(format, return FormatParser(format,
{ConvListT<sizeof...(C)>{{C...}}.list, sizeof...(C)}) {ConvListT<sizeof...(C)>{{C...}}.list, sizeof...(C)})
......
...@@ -80,13 +80,4 @@ TEST(FormatExtensionTest, SinkAppendChars) { ...@@ -80,13 +80,4 @@ TEST(FormatExtensionTest, SinkAppendChars) {
EXPECT_EQ(actual, expected); EXPECT_EQ(actual, expected);
} }
} }
TEST(FormatExtensionTest, CustomSink) {
my_namespace::UserDefinedType sink;
absl::Format(&sink, "There were %04d little %s.", 3, "pigs");
EXPECT_EQ("There were 0003 little pigs.", sink.Value());
absl::Format(&sink, "And %-3llx bad wolf!", 1);
EXPECT_EQ("There were 0003 little pigs.And 1 bad wolf!", sink.Value());
}
} // namespace } // namespace
...@@ -403,70 +403,62 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, ...@@ -403,70 +403,62 @@ bool FloatToSink(const Float v, const ConversionSpec &conv,
Buffer buffer; Buffer buffer;
switch (conv.conversion_char()) { FormatConversionChar c = conv.conversion_char();
case ConversionChar::f:
case ConversionChar::F:
if (!FloatToBuffer<FormatStyle::Fixed>(decomposed, precision, &buffer,
nullptr)) {
return FallbackToSnprintf(v, conv, sink);
}
if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back();
break;
case ConversionChar::e: if (c == FormatConversionCharInternal::f ||
case ConversionChar::E: c == FormatConversionCharInternal::F) {
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer, if (!FloatToBuffer<FormatStyle::Fixed>(decomposed, precision, &buffer,
&exp)) { nullptr)) {
return FallbackToSnprintf(v, conv, sink); return FallbackToSnprintf(v, conv, sink);
}
if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back();
} else if (c == FormatConversionCharInternal::e ||
c == FormatConversionCharInternal::E) {
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
&exp)) {
return FallbackToSnprintf(v, conv, sink);
}
if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back();
PrintExponent(
exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
&buffer);
} else if (c == FormatConversionCharInternal::g ||
c == FormatConversionCharInternal::G) {
precision = std::max(0, precision - 1);
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
&exp)) {
return FallbackToSnprintf(v, conv, sink);
}
if (precision + 1 > exp && exp >= -4) {
if (exp < 0) {
// Have 1.23456, needs 0.00123456
// Move the first digit
buffer.begin[1] = *buffer.begin;
// Add some zeros
for (; exp < -1; ++exp) *buffer.begin-- = '0';
*buffer.begin-- = '.';
*buffer.begin = '0';
} else if (exp > 0) {
// Have 1.23456, needs 1234.56
// Move the '.' exp positions to the right.
std::rotate(buffer.begin + 1, buffer.begin + 2, buffer.begin + exp + 2);
} }
if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); exp = 0;
}
if (!conv.has_alt_flag()) {
while (buffer.back() == '0') buffer.pop_back();
if (buffer.back() == '.') buffer.pop_back();
}
if (exp) {
PrintExponent( PrintExponent(
exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
&buffer); &buffer);
break; }
} else if (c == FormatConversionCharInternal::a ||
case ConversionChar::g: c == FormatConversionCharInternal::A) {
case ConversionChar::G: return FallbackToSnprintf(v, conv, sink);
precision = std::max(0, precision - 1); } else {
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer, return false;
&exp)) {
return FallbackToSnprintf(v, conv, sink);
}
if (precision + 1 > exp && exp >= -4) {
if (exp < 0) {
// Have 1.23456, needs 0.00123456
// Move the first digit
buffer.begin[1] = *buffer.begin;
// Add some zeros
for (; exp < -1; ++exp) *buffer.begin-- = '0';
*buffer.begin-- = '.';
*buffer.begin = '0';
} else if (exp > 0) {
// Have 1.23456, needs 1234.56
// Move the '.' exp positions to the right.
std::rotate(buffer.begin + 1, buffer.begin + 2,
buffer.begin + exp + 2);
}
exp = 0;
}
if (!conv.has_alt_flag()) {
while (buffer.back() == '0') buffer.pop_back();
if (buffer.back() == '.') buffer.pop_back();
}
if (exp) {
PrintExponent(
exp,
FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
&buffer);
}
break;
case ConversionChar::a:
case ConversionChar::A:
return FallbackToSnprintf(v, conv, sink);
default:
return false;
} }
WriteBufferToSink(sign_char, WriteBufferToSink(sign_char,
......
...@@ -17,7 +17,7 @@ namespace absl { ...@@ -17,7 +17,7 @@ namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace str_format_internal { namespace str_format_internal {
using CC = ConversionChar; using CC = FormatConversionCharInternal;
using LM = LengthMod; using LM = LengthMod;
ABSL_CONST_INIT const ConvTag kTags[256] = { ABSL_CONST_INIT const ConvTag kTags[256] = {
...@@ -296,15 +296,17 @@ struct ParsedFormatBase::ParsedFormatConsumer { ...@@ -296,15 +296,17 @@ struct ParsedFormatBase::ParsedFormatConsumer {
char* data_pos; char* data_pos;
}; };
ParsedFormatBase::ParsedFormatBase(string_view format, bool allow_ignored, ParsedFormatBase::ParsedFormatBase(
std::initializer_list<Conv> convs) string_view format, bool allow_ignored,
std::initializer_list<FormatConversionCharSet> convs)
: data_(format.empty() ? nullptr : new char[format.size()]) { : data_(format.empty() ? nullptr : new char[format.size()]) {
has_error_ = !ParseFormatString(format, ParsedFormatConsumer(this)) || has_error_ = !ParseFormatString(format, ParsedFormatConsumer(this)) ||
!MatchesConversions(allow_ignored, convs); !MatchesConversions(allow_ignored, convs);
} }
bool ParsedFormatBase::MatchesConversions( bool ParsedFormatBase::MatchesConversions(
bool allow_ignored, std::initializer_list<Conv> convs) const { bool allow_ignored,
std::initializer_list<FormatConversionCharSet> convs) const {
std::unordered_set<int> used; std::unordered_set<int> used;
auto add_if_valid_conv = [&](int pos, char c) { auto add_if_valid_conv = [&](int pos, char c) {
if (static_cast<size_t>(pos) > convs.size() || if (static_cast<size_t>(pos) > convs.size() ||
......
...@@ -67,7 +67,7 @@ struct UnboundConversion { ...@@ -67,7 +67,7 @@ struct UnboundConversion {
Flags flags; Flags flags;
LengthMod length_mod = LengthMod::none; LengthMod length_mod = LengthMod::none;
ConversionChar conv = FormatConversionChar::kNone; FormatConversionChar conv = FormatConversionCharInternal::kNone;
}; };
// Consume conversion spec prefix (not including '%') of [p, end) if valid. // Consume conversion spec prefix (not including '%') of [p, end) if valid.
...@@ -186,8 +186,9 @@ constexpr bool EnsureConstexpr(string_view s) { ...@@ -186,8 +186,9 @@ constexpr bool EnsureConstexpr(string_view s) {
class ParsedFormatBase { class ParsedFormatBase {
public: public:
explicit ParsedFormatBase(string_view format, bool allow_ignored, explicit ParsedFormatBase(
std::initializer_list<Conv> convs); string_view format, bool allow_ignored,
std::initializer_list<FormatConversionCharSet> convs);
ParsedFormatBase(const ParsedFormatBase& other) { *this = other; } ParsedFormatBase(const ParsedFormatBase& other) { *this = other; }
...@@ -234,8 +235,9 @@ class ParsedFormatBase { ...@@ -234,8 +235,9 @@ class ParsedFormatBase {
private: private:
// Returns whether the conversions match and if !allow_ignored it verifies // Returns whether the conversions match and if !allow_ignored it verifies
// that all conversions are used by the format. // that all conversions are used by the format.
bool MatchesConversions(bool allow_ignored, bool MatchesConversions(
std::initializer_list<Conv> convs) const; bool allow_ignored,
std::initializer_list<FormatConversionCharSet> convs) const;
struct ParsedFormatConsumer; struct ParsedFormatConsumer;
......
...@@ -46,13 +46,13 @@ TEST(ConversionCharTest, Names) { ...@@ -46,13 +46,13 @@ TEST(ConversionCharTest, Names) {
}; };
// clang-format off // clang-format off
const Expectation kExpect[] = { const Expectation kExpect[] = {
#define X(c) {ConversionChar::c, #c[0]} #define X(c) {FormatConversionCharInternal::c, #c[0]}
X(c), X(C), X(s), X(S), // text X(c), X(C), X(s), X(S), // text
X(d), X(i), X(o), X(u), X(x), X(X), // int X(d), X(i), X(o), X(u), X(x), X(X), // int
X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float
X(n), X(p), // misc X(n), X(p), // misc
#undef X #undef X
{ConversionChar::kNone, '\0'}, {FormatConversionCharInternal::kNone, '\0'},
}; };
// clang-format on // clang-format on
for (auto e : kExpect) { for (auto e : kExpect) {
...@@ -349,7 +349,8 @@ TEST_F(ParsedFormatTest, ValueSemantics) { ...@@ -349,7 +349,8 @@ TEST_F(ParsedFormatTest, ValueSemantics) {
ParsedFormatBase p2 = p1; // copy construct (empty) ParsedFormatBase p2 = p1; // copy construct (empty)
EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2)); EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2));
p1 = ParsedFormatBase("hello%s", true, {Conv::s}); // move assign p1 = ParsedFormatBase("hello%s", true,
{FormatConversionCharSetInternal::s}); // move assign
EXPECT_EQ("[hello]{s:1$s}", SummarizeParsedFormat(p1)); EXPECT_EQ("[hello]{s:1$s}", SummarizeParsedFormat(p1));
ParsedFormatBase p3 = p1; // copy construct (nonempty) ParsedFormatBase p3 = p1; // copy construct (nonempty)
...@@ -377,9 +378,9 @@ TEST_F(ParsedFormatTest, Parsing) { ...@@ -377,9 +378,9 @@ TEST_F(ParsedFormatTest, Parsing) {
const ExpectParse kExpect[] = { const ExpectParse kExpect[] = {
{"", {}, ""}, {"", {}, ""},
{"ab", {}, "[ab]"}, {"ab", {}, "[ab]"},
{"a%d", {Conv::d}, "[a]{d:1$d}"}, {"a%d", {FormatConversionCharSetInternal::d}, "[a]{d:1$d}"},
{"a%+d", {Conv::d}, "[a]{+d:1$d}"}, {"a%+d", {FormatConversionCharSetInternal::d}, "[a]{+d:1$d}"},
{"a% d", {Conv::d}, "[a]{ d:1$d}"}, {"a% d", {FormatConversionCharSetInternal::d}, "[a]{ d:1$d}"},
{"a%b %d", {}, "[a]!"}, // stop after error {"a%b %d", {}, "[a]!"}, // stop after error
}; };
for (const auto& e : kExpect) { for (const auto& e : kExpect) {
...@@ -391,13 +392,13 @@ TEST_F(ParsedFormatTest, Parsing) { ...@@ -391,13 +392,13 @@ TEST_F(ParsedFormatTest, Parsing) {
TEST_F(ParsedFormatTest, ParsingFlagOrder) { TEST_F(ParsedFormatTest, ParsingFlagOrder) {
const ExpectParse kExpect[] = { const ExpectParse kExpect[] = {
{"a%+ 0d", {Conv::d}, "[a]{+ 0d:1$d}"}, {"a%+ 0d", {FormatConversionCharSetInternal::d}, "[a]{+ 0d:1$d}"},
{"a%+0 d", {Conv::d}, "[a]{+0 d:1$d}"}, {"a%+0 d", {FormatConversionCharSetInternal::d}, "[a]{+0 d:1$d}"},
{"a%0+ d", {Conv::d}, "[a]{0+ d:1$d}"}, {"a%0+ d", {FormatConversionCharSetInternal::d}, "[a]{0+ d:1$d}"},
{"a% +0d", {Conv::d}, "[a]{ +0d:1$d}"}, {"a% +0d", {FormatConversionCharSetInternal::d}, "[a]{ +0d:1$d}"},
{"a%0 +d", {Conv::d}, "[a]{0 +d:1$d}"}, {"a%0 +d", {FormatConversionCharSetInternal::d}, "[a]{0 +d:1$d}"},
{"a% 0+d", {Conv::d}, "[a]{ 0+d:1$d}"}, {"a% 0+d", {FormatConversionCharSetInternal::d}, "[a]{ 0+d:1$d}"},
{"a%+ 0+d", {Conv::d}, "[a]{+ 0+d:1$d}"}, {"a%+ 0+d", {FormatConversionCharSetInternal::d}, "[a]{+ 0+d:1$d}"},
}; };
for (const auto& e : kExpect) { for (const auto& e : kExpect) {
SCOPED_TRACE(e.in); SCOPED_TRACE(e.in);
......
...@@ -450,7 +450,7 @@ struct SummarizeConsumer { ...@@ -450,7 +450,7 @@ struct SummarizeConsumer {
if (conv.precision.is_from_arg()) { if (conv.precision.is_from_arg()) {
*out += "." + std::to_string(conv.precision.get_from_arg()) + "$*"; *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*";
} }
*out += FormatConversionCharToChar(conv.conv); *out += str_format_internal::FormatConversionCharToChar(conv.conv);
*out += "}"; *out += "}";
return true; return true;
} }
......
...@@ -32,9 +32,9 @@ namespace synchronization_internal { ...@@ -32,9 +32,9 @@ namespace synchronization_internal {
// ThreadIdentity storage is persistent, we maintain a free-list of previously // ThreadIdentity storage is persistent, we maintain a free-list of previously
// released ThreadIdentity objects. // released ThreadIdentity objects.
static base_internal::SpinLock freelist_lock( ABSL_CONST_INIT static base_internal::SpinLock freelist_lock(
base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
static base_internal::ThreadIdentity* thread_identity_freelist; ABSL_CONST_INIT static base_internal::ThreadIdentity* thread_identity_freelist;
// A per-thread destructor for reclaiming associated ThreadIdentity objects. // A per-thread destructor for reclaiming associated ThreadIdentity objects.
// Since we must preserve their storage we cache them for re-use. // Since we must preserve their storage we cache them for re-use.
......
...@@ -51,9 +51,9 @@ namespace { ...@@ -51,9 +51,9 @@ namespace {
// Avoid LowLevelAlloc's default arena since it calls malloc hooks in // Avoid LowLevelAlloc's default arena since it calls malloc hooks in
// which people are doing things like acquiring Mutexes. // which people are doing things like acquiring Mutexes.
static absl::base_internal::SpinLock arena_mu( ABSL_CONST_INIT static absl::base_internal::SpinLock arena_mu(
absl::base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
static base_internal::LowLevelAlloc::Arena* arena; ABSL_CONST_INIT static base_internal::LowLevelAlloc::Arena* arena;
static void InitArenaIfNecessary() { static void InitArenaIfNecessary() {
arena_mu.Lock(); arena_mu.Lock();
......
...@@ -207,12 +207,12 @@ static void AtomicClearBits(std::atomic<intptr_t>* pv, intptr_t bits, ...@@ -207,12 +207,12 @@ static void AtomicClearBits(std::atomic<intptr_t>* pv, intptr_t bits,
//------------------------------------------------------------------ //------------------------------------------------------------------
// Data for doing deadlock detection. // Data for doing deadlock detection.
static absl::base_internal::SpinLock deadlock_graph_mu( ABSL_CONST_INIT static absl::base_internal::SpinLock deadlock_graph_mu(
absl::base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
// graph used to detect deadlocks. // Graph used to detect deadlocks.
static GraphCycles *deadlock_graph ABSL_GUARDED_BY(deadlock_graph_mu) ABSL_CONST_INIT static GraphCycles *deadlock_graph
ABSL_PT_GUARDED_BY(deadlock_graph_mu); ABSL_GUARDED_BY(deadlock_graph_mu) ABSL_PT_GUARDED_BY(deadlock_graph_mu);
//------------------------------------------------------------------ //------------------------------------------------------------------
// An event mechanism for debugging mutex use. // An event mechanism for debugging mutex use.
...@@ -273,13 +273,12 @@ static const struct { ...@@ -273,13 +273,12 @@ static const struct {
{0, "SignalAll on "}, {0, "SignalAll on "},
}; };
static absl::base_internal::SpinLock synch_event_mu( ABSL_CONST_INIT static absl::base_internal::SpinLock synch_event_mu(
absl::base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
// protects synch_event
// Hash table size; should be prime > 2. // Hash table size; should be prime > 2.
// Can't be too small, as it's used for deadlock detection information. // Can't be too small, as it's used for deadlock detection information.
static const uint32_t kNSynchEvent = 1031; static constexpr uint32_t kNSynchEvent = 1031;
static struct SynchEvent { // this is a trivial hash table for the events static struct SynchEvent { // this is a trivial hash table for the events
// struct is freed when refcount reaches 0 // struct is freed when refcount reaches 0
......
...@@ -226,9 +226,9 @@ static_assert(((kMinNSBetweenSamples << (kScale + 1)) >> (kScale + 1)) == ...@@ -226,9 +226,9 @@ static_assert(((kMinNSBetweenSamples << (kScale + 1)) >> (kScale + 1)) ==
// A reader-writer lock protecting the static locations below. // A reader-writer lock protecting the static locations below.
// See SeqAcquire() and SeqRelease() above. // See SeqAcquire() and SeqRelease() above.
static absl::base_internal::SpinLock lock( ABSL_CONST_INIT static absl::base_internal::SpinLock lock(
absl::base_internal::kLinkerInitialized); absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
static std::atomic<uint64_t> seq(0); ABSL_CONST_INIT static std::atomic<uint64_t> seq(0);
// data from a sample of the kernel's time value // data from a sample of the kernel's time value
struct TimeSampleAtomic { struct TimeSampleAtomic {
......
...@@ -280,6 +280,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan", ...@@ -280,6 +280,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan",
"America/North_Dakota/Beulah", "America/North_Dakota/Beulah",
"America/North_Dakota/Center", "America/North_Dakota/Center",
"America/North_Dakota/New_Salem", "America/North_Dakota/New_Salem",
"America/Nuuk",
"America/Ojinaga", "America/Ojinaga",
"America/Panama", "America/Panama",
"America/Pangnirtung", "America/Pangnirtung",
......
...@@ -211,6 +211,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan", ...@@ -211,6 +211,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan",
"America/North_Dakota/Beulah", "America/North_Dakota/Beulah",
"America/North_Dakota/Center", "America/North_Dakota/Center",
"America/North_Dakota/New_Salem", "America/North_Dakota/New_Salem",
"America/Nuuk",
"America/Ojinaga", "America/Ojinaga",
"America/Panama", "America/Panama",
"America/Pangnirtung", "America/Pangnirtung",
......
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