aboutsummaryrefslogtreecommitdiff
path: root/third_party/abseil-cpp/absl/flags
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/abseil-cpp/absl/flags')
-rw-r--r--third_party/abseil-cpp/absl/flags/BUILD.bazel222
-rw-r--r--third_party/abseil-cpp/absl/flags/CMakeLists.txt193
-rw-r--r--third_party/abseil-cpp/absl/flags/commandlineflag.cc34
-rw-r--r--third_party/abseil-cpp/absl/flags/commandlineflag.h200
-rw-r--r--third_party/abseil-cpp/absl/flags/commandlineflag_test.cc231
-rw-r--r--third_party/abseil-cpp/absl/flags/config.h31
-rw-r--r--third_party/abseil-cpp/absl/flags/declare.h1
-rw-r--r--third_party/abseil-cpp/absl/flags/flag.cc2
-rw-r--r--third_party/abseil-cpp/absl/flags/flag.h242
-rw-r--r--third_party/abseil-cpp/absl/flags/flag_benchmark.cc157
-rw-r--r--third_party/abseil-cpp/absl/flags/flag_benchmark.lds13
-rw-r--r--third_party/abseil-cpp/absl/flags/flag_test.cc737
-rw-r--r--third_party/abseil-cpp/absl/flags/flag_test_defs.cc4
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/commandlineflag.cc26
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/commandlineflag.h157
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/commandlineflag_test.cc219
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/flag.cc380
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/flag.h825
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/flag_msvc.inc116
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/parse.h8
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/path_util.h1
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.cc65
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.h61
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/program_name_test.cc4
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/registry.cc351
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/registry.h55
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/sequence_lock.h187
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/sequence_lock_test.cc169
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/type_erased.cc90
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/type_erased.h90
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/type_erased_test.cc157
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/usage.cc351
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/usage.h47
-rw-r--r--third_party/abseil-cpp/absl/flags/internal/usage_test.cc138
-rw-r--r--third_party/abseil-cpp/absl/flags/marshalling.cc23
-rw-r--r--third_party/abseil-cpp/absl/flags/marshalling.h4
-rw-r--r--third_party/abseil-cpp/absl/flags/parse.cc117
-rw-r--r--third_party/abseil-cpp/absl/flags/parse.h1
-rw-r--r--third_party/abseil-cpp/absl/flags/parse_test.cc129
-rw-r--r--third_party/abseil-cpp/absl/flags/reflection.cc354
-rw-r--r--third_party/abseil-cpp/absl/flags/reflection.h90
-rw-r--r--third_party/abseil-cpp/absl/flags/reflection_test.cc265
-rw-r--r--third_party/abseil-cpp/absl/flags/usage_config.cc17
-rw-r--r--third_party/abseil-cpp/absl/flags/usage_config.h5
-rw-r--r--third_party/abseil-cpp/absl/flags/usage_config_test.cc4
45 files changed, 4337 insertions, 2236 deletions
diff --git a/third_party/abseil-cpp/absl/flags/BUILD.bazel b/third_party/abseil-cpp/absl/flags/BUILD.bazel
index cdb4e7e8fe..d20deab464 100644
--- a/third_party/abseil-cpp/absl/flags/BUILD.bazel
+++ b/third_party/abseil-cpp/absl/flags/BUILD.bazel
@@ -14,7 +14,6 @@
# limitations under the License.
#
-load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load(
"//absl:copts/configure_copts.bzl",
"ABSL_DEFAULT_COPTS",
@@ -24,29 +23,21 @@ load(
package(default_visibility = ["//visibility:public"])
-licenses(["notice"]) # Apache 2.0
+licenses(["notice"])
cc_library(
- name = "flag_internal",
- srcs = [
- "internal/flag.cc",
- ],
+ name = "path_util",
hdrs = [
- "internal/flag.h",
+ "internal/path_util.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
- visibility = ["//absl/base:__subpackages__"],
+ visibility = [
+ "//absl/flags:__pkg__",
+ ],
deps = [
- ":config",
- ":handle",
- ":registry",
- "//absl/base",
"//absl/base:config",
- "//absl/base:core_headers",
- "//absl/memory",
"//absl/strings",
- "//absl/synchronization",
],
)
@@ -73,22 +64,6 @@ cc_library(
)
cc_library(
- name = "path_util",
- hdrs = [
- "internal/path_util.h",
- ],
- copts = ABSL_DEFAULT_COPTS,
- linkopts = ABSL_DEFAULT_LINKOPTS,
- visibility = [
- "//absl/flags:__pkg__",
- ],
- deps = [
- "//absl/base:config",
- "//absl/strings",
- ],
-)
-
-cc_library(
name = "config",
srcs = [
"usage_config.cc",
@@ -129,34 +104,47 @@ cc_library(
)
cc_library(
- name = "handle",
+ name = "commandlineflag_internal",
+ srcs = [
+ "internal/commandlineflag.cc",
+ ],
hdrs = [
"internal/commandlineflag.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
- visibility = [
- "//absl/flags:__pkg__",
+ deps = [
+ "//absl/base:config",
+ "//absl/base:fast_type_id",
+ ],
+)
+
+cc_library(
+ name = "commandlineflag",
+ srcs = [
+ "commandlineflag.cc",
],
+ hdrs = [
+ "commandlineflag.h",
+ ],
+ copts = ABSL_DEFAULT_COPTS,
+ linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
- ":config",
- ":marshalling",
+ ":commandlineflag_internal",
"//absl/base:config",
- "//absl/base:core_headers",
+ "//absl/base:fast_type_id",
"//absl/strings",
"//absl/types:optional",
],
)
cc_library(
- name = "registry",
+ name = "private_handle_accessor",
srcs = [
- "internal/registry.cc",
- "internal/type_erased.cc",
+ "internal/private_handle_accessor.cc",
],
hdrs = [
- "internal/registry.h",
- "internal/type_erased.h",
+ "internal/private_handle_accessor.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
@@ -164,20 +152,71 @@ cc_library(
"//absl/flags:__pkg__",
],
deps = [
+ ":commandlineflag",
+ ":commandlineflag_internal",
+ "//absl/base:config",
+ "//absl/strings",
+ ],
+)
+
+cc_library(
+ name = "reflection",
+ srcs = [
+ "reflection.cc",
+ ],
+ hdrs = [
+ "internal/registry.h",
+ "reflection.h",
+ ],
+ copts = ABSL_DEFAULT_COPTS,
+ linkopts = ABSL_DEFAULT_LINKOPTS,
+ deps = [
+ ":commandlineflag",
+ ":commandlineflag_internal",
":config",
- ":handle",
+ ":private_handle_accessor",
"//absl/base:config",
"//absl/base:core_headers",
- "//absl/base:raw_logging_internal",
+ "//absl/container:flat_hash_map",
"//absl/strings",
"//absl/synchronization",
],
)
cc_library(
+ name = "flag_internal",
+ srcs = [
+ "internal/flag.cc",
+ ],
+ hdrs = [
+ "internal/flag.h",
+ "internal/sequence_lock.h",
+ ],
+ copts = ABSL_DEFAULT_COPTS,
+ linkopts = ABSL_DEFAULT_LINKOPTS,
+ visibility = ["//absl/base:__subpackages__"],
+ deps = [
+ ":commandlineflag",
+ ":commandlineflag_internal",
+ ":config",
+ ":marshalling",
+ ":reflection",
+ "//absl/base",
+ "//absl/base:config",
+ "//absl/base:core_headers",
+ "//absl/memory",
+ "//absl/meta:type_traits",
+ "//absl/strings",
+ "//absl/synchronization",
+ "//absl/utility",
+ ],
+)
+
+cc_library(
name = "flag",
srcs = [
"flag.cc",
+ "internal/flag_msvc.inc",
],
hdrs = [
"declare.h",
@@ -188,9 +227,7 @@ cc_library(
deps = [
":config",
":flag_internal",
- ":handle",
- ":marshalling",
- ":registry",
+ ":reflection",
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
@@ -212,15 +249,17 @@ cc_library(
"//absl/flags:__pkg__",
],
deps = [
+ ":commandlineflag",
":config",
":flag",
":flag_internal",
- ":handle",
":path_util",
+ ":private_handle_accessor",
":program_name",
- ":registry",
+ ":reflection",
"//absl/base:config",
"//absl/base:core_headers",
+ "//absl/container:flat_hash_map",
"//absl/strings",
],
)
@@ -254,12 +293,14 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
+ ":commandlineflag",
+ ":commandlineflag_internal",
":config",
":flag",
":flag_internal",
- ":handle",
+ ":private_handle_accessor",
":program_name",
- ":registry",
+ ":reflection",
":usage",
":usage_internal",
"//absl/base:config",
@@ -276,15 +317,17 @@ cc_test(
name = "commandlineflag_test",
size = "small",
srcs = [
- "internal/commandlineflag_test.cc",
+ "commandlineflag_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
+ ":commandlineflag",
+ ":commandlineflag_internal",
":config",
":flag",
- ":handle",
- ":registry",
+ ":private_handle_accessor",
+ ":reflection",
"//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest_main",
@@ -318,10 +361,12 @@ cc_test(
":config",
":flag",
":flag_internal",
- ":handle",
- ":registry",
+ ":marshalling",
+ ":reflection",
"//absl/base:core_headers",
+ "//absl/base:malloc_internal",
"//absl/strings",
+ "//absl/time",
"@com_google_googletest//:gtest_main",
],
)
@@ -333,10 +378,18 @@ cc_binary(
"flag_benchmark.cc",
],
copts = ABSL_TEST_COPTS,
+ linkopts = select({
+ "//conditions:default": [],
+ }) + ABSL_DEFAULT_LINKOPTS,
tags = ["benchmark"],
visibility = ["//visibility:private"],
deps = [
+ "flag_benchmark.lds",
":flag",
+ ":marshalling",
+ ":parse",
+ ":reflection",
+ "//absl/strings",
"//absl/time",
"//absl/types:optional",
"@com_github_google_benchmark//:benchmark_main",
@@ -358,35 +411,36 @@ cc_test(
)
cc_test(
- name = "path_util_test",
+ name = "parse_test",
size = "small",
srcs = [
- "internal/path_util_test.cc",
+ "parse_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
- ":path_util",
+ ":flag",
+ ":parse",
+ ":reflection",
+ ":usage_internal",
+ "//absl/base:raw_logging_internal",
+ "//absl/base:scoped_set_env",
+ "//absl/strings",
+ "//absl/types:span",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
- name = "parse_test",
+ name = "path_util_test",
size = "small",
srcs = [
- "parse_test.cc",
+ "internal/path_util_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
- ":flag",
- ":parse",
- ":registry",
- "//absl/base:raw_logging_internal",
- "//absl/base:scoped_set_env",
- "//absl/strings",
- "//absl/types:span",
+ ":path_util",
"@com_google_googletest//:gtest_main",
],
)
@@ -407,19 +461,40 @@ cc_test(
)
cc_test(
- name = "type_erased_test",
+ name = "reflection_test",
size = "small",
srcs = [
- "internal/type_erased_test.cc",
+ "reflection_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
+ ":commandlineflag_internal",
":flag",
- ":handle",
":marshalling",
- ":registry",
+ ":reflection",
+ ":usage_internal",
"//absl/memory",
+ "//absl/strings",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
+
+cc_test(
+ name = "sequence_lock_test",
+ size = "small",
+ timeout = "moderate",
+ srcs = [
+ "internal/sequence_lock_test.cc",
+ ],
+ copts = ABSL_TEST_COPTS,
+ linkopts = ABSL_DEFAULT_LINKOPTS,
+ shard_count = 31,
+ deps = [
+ ":flag_internal",
+ "//absl/base",
+ "//absl/container:fixed_array",
+ "//absl/time",
"@com_google_googletest//:gtest_main",
],
)
@@ -455,10 +530,9 @@ cc_test(
":parse",
":path_util",
":program_name",
- ":registry",
+ ":reflection",
":usage",
":usage_internal",
- "//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest",
],
diff --git a/third_party/abseil-cpp/absl/flags/CMakeLists.txt b/third_party/abseil-cpp/absl/flags/CMakeLists.txt
index 1d25f0ded8..7f3298e9dd 100644
--- a/third_party/abseil-cpp/absl/flags/CMakeLists.txt
+++ b/third_party/abseil-cpp/absl/flags/CMakeLists.txt
@@ -17,22 +17,16 @@
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
- flags_internal
- SRCS
- "internal/flag.cc"
+ flags_path_util
HDRS
- "internal/flag.h"
+ "internal/path_util.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
- absl::base
absl::config
- absl::flags_config
- absl::flags_handle
- absl::flags_registry
- absl::synchronization
+ absl::strings
PUBLIC
)
@@ -57,22 +51,6 @@ absl_cc_library(
PUBLIC
)
-# Internal-only target, do not depend on directly.
-absl_cc_library(
- NAME
- flags_path_util
- HDRS
- "internal/path_util.h"
- COPTS
- ${ABSL_DEFAULT_COPTS}
- LINKOPTS
- ${ABSL_DEFAULT_LINKOPTS}
- DEPS
- absl::config
- absl::strings
- PUBLIC
-)
-
absl_cc_library(
NAME
flags_config
@@ -116,7 +94,9 @@ absl_cc_library(
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
- flags_handle
+ flags_commandlineflag_internal
+ SRCS
+ "internal/commandlineflag.cc"
HDRS
"internal/commandlineflag.h"
COPTS
@@ -125,37 +105,93 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
- absl::flags_config
- absl::flags_marshalling
- absl::core_headers
+ absl::fast_type_id
+)
+
+absl_cc_library(
+ NAME
+ flags_commandlineflag
+ SRCS
+ "commandlineflag.cc"
+ HDRS
+ "commandlineflag.h"
+ COPTS
+ ${ABSL_DEFAULT_COPTS}
+ LINKOPTS
+ ${ABSL_DEFAULT_LINKOPTS}
+ DEPS
+ absl::config
+ absl::fast_type_id
+ absl::flags_commandlineflag_internal
absl::optional
- absl::raw_logging_internal
absl::strings
- absl::synchronization
)
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
- flags_registry
+ flags_private_handle_accessor
+ SRCS
+ "internal/private_handle_accessor.cc"
+ HDRS
+ "internal/private_handle_accessor.h"
+ COPTS
+ ${ABSL_DEFAULT_COPTS}
+ LINKOPTS
+ ${ABSL_DEFAULT_LINKOPTS}
+ DEPS
+ absl::config
+ absl::flags_commandlineflag
+ absl::flags_commandlineflag_internal
+ absl::strings
+)
+
+absl_cc_library(
+ NAME
+ flags_reflection
SRCS
- "internal/registry.cc"
- "internal/type_erased.cc"
+ "reflection.cc"
HDRS
+ "reflection.h"
"internal/registry.h"
- "internal/type_erased.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
+ absl::flags_commandlineflag
+ absl::flags_private_handle_accessor
absl::flags_config
- absl::flags_handle
- absl::core_headers
- absl::raw_logging_internal
absl::strings
absl::synchronization
+ absl::flat_hash_map
+)
+
+# Internal-only target, do not depend on directly.
+absl_cc_library(
+ NAME
+ flags_internal
+ SRCS
+ "internal/flag.cc"
+ HDRS
+ "internal/flag.h"
+ "internal/sequence_lock.h"
+ COPTS
+ ${ABSL_DEFAULT_COPTS}
+ LINKOPTS
+ ${ABSL_DEFAULT_LINKOPTS}
+ DEPS
+ absl::base
+ absl::config
+ absl::flags_commandlineflag
+ absl::flags_commandlineflag_internal
+ absl::flags_config
+ absl::flags_marshalling
+ absl::synchronization
+ absl::meta
+ absl::utility
+ PUBLIC
)
absl_cc_library(
@@ -166,17 +202,17 @@ absl_cc_library(
HDRS
"declare.h"
"flag.h"
+ "internal/flag_msvc.inc"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
+ absl::flags_commandlineflag
absl::flags_config
- absl::flags_handle
absl::flags_internal
- absl::flags_marshalling
- absl::flags_registry
+ absl::flags_reflection
absl::base
absl::core_headers
absl::strings
@@ -198,11 +234,13 @@ absl_cc_library(
absl::config
absl::flags_config
absl::flags
- absl::flags_handle
+ absl::flags_commandlineflag
absl::flags_internal
absl::flags_path_util
+ absl::flags_private_handle_accessor
absl::flags_program_name
- absl::flags_registry
+ absl::flags_reflection
+ absl::flat_hash_map
absl::strings
absl::synchronization
)
@@ -243,10 +281,12 @@ absl_cc_library(
absl::core_headers
absl::flags_config
absl::flags
- absl::flags_handle
+ absl::flags_commandlineflag
+ absl::flags_commandlineflag_internal
absl::flags_internal
+ absl::flags_private_handle_accessor
absl::flags_program_name
- absl::flags_registry
+ absl::flags_reflection
absl::flags_usage
absl::strings
absl::synchronization
@@ -259,17 +299,19 @@ absl_cc_test(
NAME
flags_commandlineflag_test
SRCS
- "internal/commandlineflag_test.cc"
+ "commandlineflag_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags
+ absl::flags_commandlineflag
+ absl::flags_commandlineflag_internal
absl::flags_config
- absl::flags_handle
- absl::flags_registry
+ absl::flags_private_handle_accessor
+ absl::flags_reflection
absl::memory
absl::strings
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
@@ -281,7 +323,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags_config
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
@@ -296,11 +338,12 @@ absl_cc_test(
absl::core_headers
absl::flags
absl::flags_config
- absl::flags_handle
absl::flags_internal
- absl::flags_registry
+ absl::flags_marshalling
+ absl::flags_reflection
absl::strings
- gtest_main
+ absl::time
+ GTest::gtest_main
)
absl_cc_test(
@@ -312,7 +355,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags_marshalling
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
@@ -325,12 +368,13 @@ absl_cc_test(
DEPS
absl::flags
absl::flags_parse
- absl::flags_registry
+ absl::flags_reflection
+ absl::flags_usage_internal
absl::raw_logging_internal
absl::scoped_set_env
absl::span
absl::strings
- gmock_main
+ GTest::gmock_main
)
absl_cc_test(
@@ -342,7 +386,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::flags_path_util
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
@@ -355,24 +399,38 @@ absl_cc_test(
DEPS
absl::flags_program_name
absl::strings
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
NAME
- flags_type_erased_test
+ flags_reflection_test
SRCS
- "internal/type_erased_test.cc"
+ "reflection_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
+ absl::flags_commandlineflag_internal
absl::flags
- absl::flags_handle
- absl::flags_marshalling
- absl::flags_registry
+ absl::flags_reflection
+ absl::flags_usage
absl::memory
absl::strings
- gtest_main
+ GTest::gmock_main
+)
+
+absl_cc_test(
+ NAME
+ flags_sequence_lock_test
+ SRCS
+ "internal/sequence_lock_test.cc"
+ COPTS
+ ${ABSL_TEST_COPTS}
+ DEPS
+ absl::base
+ absl::flags_internal
+ absl::time
+ GTest::gmock_main
)
absl_cc_test(
@@ -387,7 +445,7 @@ absl_cc_test(
absl::flags_path_util
absl::flags_program_name
absl::strings
- gtest_main
+ GTest::gtest_main
)
absl_cc_test(
@@ -403,9 +461,8 @@ absl_cc_test(
absl::flags_path_util
absl::flags_program_name
absl::flags_parse
- absl::flags_registry
+ absl::flags_reflection
absl::flags_usage
- absl::memory
absl::strings
- gtest
+ GTest::gtest
)
diff --git a/third_party/abseil-cpp/absl/flags/commandlineflag.cc b/third_party/abseil-cpp/absl/flags/commandlineflag.cc
new file mode 100644
index 0000000000..9f3b4a5a28
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/commandlineflag.cc
@@ -0,0 +1,34 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/commandlineflag.h"
+
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+
+bool CommandLineFlag::IsRetired() const { return false; }
+bool CommandLineFlag::ParseFrom(absl::string_view value, std::string* error) {
+ return ParseFrom(value, flags_internal::SET_FLAGS_VALUE,
+ flags_internal::kProgrammaticChange, *error);
+}
+
+ABSL_NAMESPACE_END
+} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/commandlineflag.h b/third_party/abseil-cpp/absl/flags/commandlineflag.h
new file mode 100644
index 0000000000..f2fa08977f
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/commandlineflag.h
@@ -0,0 +1,200 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: commandlineflag.h
+// -----------------------------------------------------------------------------
+//
+// This header file defines the `CommandLineFlag`, which acts as a type-erased
+// handle for accessing metadata about the Abseil Flag in question.
+//
+// Because an actual Abseil flag is of an unspecified type, you should not
+// manipulate or interact directly with objects of that type. Instead, use the
+// CommandLineFlag type as an intermediary.
+#ifndef ABSL_FLAGS_COMMANDLINEFLAG_H_
+#define ABSL_FLAGS_COMMANDLINEFLAG_H_
+
+#include <memory>
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/base/internal/fast_type_id.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/strings/string_view.h"
+#include "absl/types/optional.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+class PrivateHandleAccessor;
+} // namespace flags_internal
+
+// CommandLineFlag
+//
+// This type acts as a type-erased handle for an instance of an Abseil Flag and
+// holds reflection information pertaining to that flag. Use CommandLineFlag to
+// access a flag's name, location, help string etc.
+//
+// To obtain an absl::CommandLineFlag, invoke `absl::FindCommandLineFlag()`
+// passing it the flag name string.
+//
+// Example:
+//
+// // Obtain reflection handle for a flag named "flagname".
+// const absl::CommandLineFlag* my_flag_data =
+// absl::FindCommandLineFlag("flagname");
+//
+// // Now you can get flag info from that reflection handle.
+// std::string flag_location = my_flag_data->Filename();
+// ...
+class CommandLineFlag {
+ public:
+ constexpr CommandLineFlag() = default;
+
+ // Not copyable/assignable.
+ CommandLineFlag(const CommandLineFlag&) = delete;
+ CommandLineFlag& operator=(const CommandLineFlag&) = delete;
+
+ // absl::CommandLineFlag::IsOfType()
+ //
+ // Return true iff flag has type T.
+ template <typename T>
+ inline bool IsOfType() const {
+ return TypeId() == base_internal::FastTypeId<T>();
+ }
+
+ // absl::CommandLineFlag::TryGet()
+ //
+ // Attempts to retrieve the flag value. Returns value on success,
+ // absl::nullopt otherwise.
+ template <typename T>
+ absl::optional<T> TryGet() const {
+ if (IsRetired() || !IsOfType<T>()) {
+ return absl::nullopt;
+ }
+
+ // Implementation notes:
+ //
+ // We are wrapping a union around the value of `T` to serve three purposes:
+ //
+ // 1. `U.value` has correct size and alignment for a value of type `T`
+ // 2. The `U.value` constructor is not invoked since U's constructor does
+ // not do it explicitly.
+ // 3. The `U.value` destructor is invoked since U's destructor does it
+ // explicitly. This makes `U` a kind of RAII wrapper around non default
+ // constructible value of T, which is destructed when we leave the
+ // scope. We do need to destroy U.value, which is constructed by
+ // CommandLineFlag::Read even though we left it in a moved-from state
+ // after std::move.
+ //
+ // All of this serves to avoid requiring `T` being default constructible.
+ union U {
+ T value;
+ U() {}
+ ~U() { value.~T(); }
+ };
+ U u;
+
+ Read(&u.value);
+ // allow retired flags to be "read", so we can report invalid access.
+ if (IsRetired()) {
+ return absl::nullopt;
+ }
+ return std::move(u.value);
+ }
+
+ // absl::CommandLineFlag::Name()
+ //
+ // Returns name of this flag.
+ virtual absl::string_view Name() const = 0;
+
+ // absl::CommandLineFlag::Filename()
+ //
+ // Returns name of the file where this flag is defined.
+ virtual std::string Filename() const = 0;
+
+ // absl::CommandLineFlag::Help()
+ //
+ // Returns help message associated with this flag.
+ virtual std::string Help() const = 0;
+
+ // absl::CommandLineFlag::IsRetired()
+ //
+ // Returns true iff this object corresponds to retired flag.
+ virtual bool IsRetired() const;
+
+ // absl::CommandLineFlag::DefaultValue()
+ //
+ // Returns the default value for this flag.
+ virtual std::string DefaultValue() const = 0;
+
+ // absl::CommandLineFlag::CurrentValue()
+ //
+ // Returns the current value for this flag.
+ virtual std::string CurrentValue() const = 0;
+
+ // absl::CommandLineFlag::ParseFrom()
+ //
+ // 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`
+ // to indicate the error, leaves the flag unchanged, and returns false.
+ bool ParseFrom(absl::string_view value, std::string* error);
+
+ protected:
+ ~CommandLineFlag() = default;
+
+ private:
+ friend class flags_internal::PrivateHandleAccessor;
+
+ // 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`
+ // to indicate the error, leaves the flag unchanged, and returns false. There
+ // are three ways to set the flag's value:
+ // * Update the current flag value
+ // * Update the flag's default value
+ // * Update the current flag value if it was never set before
+ // The mode is selected based on `set_mode` parameter.
+ virtual bool ParseFrom(absl::string_view value,
+ flags_internal::FlagSettingMode set_mode,
+ flags_internal::ValueSource source,
+ std::string& error) = 0;
+
+ // Returns id of the flag's value type.
+ virtual flags_internal::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<flags_internal::FlagStateInterface> SaveState() = 0;
+
+ // Copy-construct a new value of the flag's type in a memory referenced by
+ // the dst based on the current flag's value.
+ virtual void Read(void* dst) const = 0;
+
+ // To be deleted. Used to return true if flag's current value originated from
+ // command line.
+ virtual bool IsSpecifiedOnCommandLine() const = 0;
+
+ // Validates supplied value usign validator or parseflag routine
+ virtual bool ValidateInputValue(absl::string_view value) const = 0;
+
+ // Checks that flags default value can be converted to string and back to the
+ // flag's value type.
+ virtual void CheckDefaultValueParsingRoundtrip() const = 0;
+};
+
+ABSL_NAMESPACE_END
+} // namespace absl
+
+#endif // ABSL_FLAGS_COMMANDLINEFLAG_H_
diff --git a/third_party/abseil-cpp/absl/flags/commandlineflag_test.cc b/third_party/abseil-cpp/absl/flags/commandlineflag_test.cc
new file mode 100644
index 0000000000..585db4ba78
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/commandlineflag_test.cc
@@ -0,0 +1,231 @@
+//
+// Copyright 2019 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/commandlineflag.h"
+
+#include <memory>
+#include <string>
+
+#include "gtest/gtest.h"
+#include "absl/flags/flag.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/flags/internal/private_handle_accessor.h"
+#include "absl/flags/reflection.h"
+#include "absl/flags/usage_config.h"
+#include "absl/memory/memory.h"
+#include "absl/strings/match.h"
+#include "absl/strings/str_cat.h"
+#include "absl/strings/string_view.h"
+
+ABSL_FLAG(int, int_flag, 201, "int_flag help");
+ABSL_FLAG(std::string, string_flag, "dflt",
+ absl::StrCat("string_flag", " help"));
+ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
+
+// These are only used to test default values.
+ABSL_FLAG(int, int_flag2, 201, "");
+ABSL_FLAG(std::string, string_flag2, "dflt", "");
+
+namespace {
+
+namespace flags = absl::flags_internal;
+
+class CommandLineFlagTest : public testing::Test {
+ protected:
+ static void SetUpTestSuite() {
+ // Install a function to normalize filenames before this test is run.
+ absl::FlagsUsageConfig default_config;
+ default_config.normalize_filename = &CommandLineFlagTest::NormalizeFileName;
+ absl::SetFlagsUsageConfig(default_config);
+ }
+
+ void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
+ void TearDown() override { flag_saver_.reset(); }
+
+ private:
+ static std::string NormalizeFileName(absl::string_view fname) {
+#ifdef _WIN32
+ std::string normalized(fname);
+ std::replace(normalized.begin(), normalized.end(), '\\', '/');
+ fname = normalized;
+#endif
+ return std::string(fname);
+ }
+
+ std::unique_ptr<absl::FlagSaver> flag_saver_;
+};
+
+TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
+ auto* flag_01 = absl::FindCommandLineFlag("int_flag");
+
+ ASSERT_TRUE(flag_01);
+ EXPECT_EQ(flag_01->Name(), "int_flag");
+ EXPECT_EQ(flag_01->Help(), "int_flag help");
+ EXPECT_TRUE(!flag_01->IsRetired());
+ EXPECT_TRUE(flag_01->IsOfType<int>());
+ EXPECT_TRUE(!flag_01->IsOfType<bool>());
+ EXPECT_TRUE(!flag_01->IsOfType<std::string>());
+ EXPECT_TRUE(absl::EndsWith(flag_01->Filename(),
+ "absl/flags/commandlineflag_test.cc"))
+ << flag_01->Filename();
+
+ auto* flag_02 = absl::FindCommandLineFlag("string_flag");
+
+ ASSERT_TRUE(flag_02);
+ EXPECT_EQ(flag_02->Name(), "string_flag");
+ EXPECT_EQ(flag_02->Help(), "string_flag help");
+ EXPECT_TRUE(!flag_02->IsRetired());
+ EXPECT_TRUE(flag_02->IsOfType<std::string>());
+ EXPECT_TRUE(!flag_02->IsOfType<bool>());
+ EXPECT_TRUE(!flag_02->IsOfType<int>());
+ EXPECT_TRUE(absl::EndsWith(flag_02->Filename(),
+ "absl/flags/commandlineflag_test.cc"))
+ << flag_02->Filename();
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
+ absl::SetFlag(&FLAGS_int_flag2, 301);
+ auto* flag_01 = absl::FindCommandLineFlag("int_flag2");
+
+ ASSERT_TRUE(flag_01);
+ EXPECT_EQ(flag_01->CurrentValue(), "301");
+ EXPECT_EQ(flag_01->DefaultValue(), "201");
+
+ absl::SetFlag(&FLAGS_string_flag2, "new_str_value");
+ auto* flag_02 = absl::FindCommandLineFlag("string_flag2");
+
+ ASSERT_TRUE(flag_02);
+ EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
+ EXPECT_EQ(flag_02->DefaultValue(), "dflt");
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) {
+ std::string err;
+
+ auto* flag_01 = absl::FindCommandLineFlag("int_flag");
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
+ EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
+ EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
+ EXPECT_FALSE(
+ flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
+ EXPECT_TRUE(flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
+
+ EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
+ EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
+
+ auto* flag_02 = absl::FindCommandLineFlag("string_flag");
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) {
+ std::string err;
+
+ auto* flag_01 = absl::FindCommandLineFlag("int_flag");
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(flag_01->DefaultValue(), "111");
+
+ auto* flag_02 = absl::FindCommandLineFlag("string_flag");
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(flag_02->DefaultValue(), "abc");
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(CommandLineFlagTest, TestParseFromIfDefault) {
+ std::string err;
+
+ auto* flag_01 = absl::FindCommandLineFlag("int_flag");
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+ err))
+ << err;
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
+ // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
+
+ // Reset back to default value
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
+ err));
+
+ EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
+ *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
+ err));
+ EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
+ // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
+}
+
+} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/config.h b/third_party/abseil-cpp/absl/flags/config.h
index 001f8feaf6..5ab1f311dc 100644
--- a/third_party/abseil-cpp/absl/flags/config.h
+++ b/third_party/abseil-cpp/absl/flags/config.h
@@ -45,17 +45,6 @@
#define ABSL_FLAGS_STRIP_HELP ABSL_FLAGS_STRIP_NAMES
#endif
-// ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD macro is used for using atomics with
-// double words, e.g. absl::Duration.
-// For reasons in bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80878, modern
-// versions of GCC do not support cmpxchg16b instruction in standard atomics.
-#ifdef ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD
-#error "ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD should not be defined."
-#elif defined(__clang__) && defined(__x86_64__) && \
- defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16)
-#define ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD 1
-#endif
-
// ABSL_FLAGS_INTERNAL_HAS_RTTI macro is used for selecting if we can use RTTI
// for flag type identification.
#ifdef ABSL_FLAGS_INTERNAL_HAS_RTTI
@@ -64,4 +53,24 @@
#define ABSL_FLAGS_INTERNAL_HAS_RTTI 1
#endif // !defined(__GNUC__) || defined(__GXX_RTTI)
+// These macros represent the "source of truth" for the list of supported
+// built-in types.
+#define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \
+ A(bool, bool) \
+ A(short, short) \
+ A(unsigned short, unsigned_short) \
+ A(int, int) \
+ A(unsigned int, unsigned_int) \
+ A(long, long) \
+ A(unsigned long, unsigned_long) \
+ A(long long, long_long) \
+ A(unsigned long long, unsigned_long_long) \
+ A(double, double) \
+ A(float, float)
+
+#define ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(A) \
+ ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \
+ A(std::string, std_string) \
+ A(std::vector<std::string>, std_vector_of_string)
+
#endif // ABSL_FLAGS_CONFIG_H_
diff --git a/third_party/abseil-cpp/absl/flags/declare.h b/third_party/abseil-cpp/absl/flags/declare.h
index 0f8cc6a599..b9794d8b85 100644
--- a/third_party/abseil-cpp/absl/flags/declare.h
+++ b/third_party/abseil-cpp/absl/flags/declare.h
@@ -26,7 +26,6 @@
#define ABSL_FLAGS_DECLARE_H_
#include "absl/base/config.h"
-#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
diff --git a/third_party/abseil-cpp/absl/flags/flag.cc b/third_party/abseil-cpp/absl/flags/flag.cc
index f7a457bf0c..531df1287a 100644
--- a/third_party/abseil-cpp/absl/flags/flag.cc
+++ b/third_party/abseil-cpp/absl/flags/flag.cc
@@ -16,8 +16,6 @@
#include "absl/flags/flag.h"
#include "absl/base/config.h"
-#include "absl/flags/internal/commandlineflag.h"
-#include "absl/flags/internal/flag.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
diff --git a/third_party/abseil-cpp/absl/flags/flag.h b/third_party/abseil-cpp/absl/flags/flag.h
index cff02c1fcb..a724ccc97d 100644
--- a/third_party/abseil-cpp/absl/flags/flag.h
+++ b/third_party/abseil-cpp/absl/flags/flag.h
@@ -33,14 +33,12 @@
#include <type_traits>
#include "absl/base/attributes.h"
-#include "absl/base/casts.h"
#include "absl/base/config.h"
+#include "absl/base/optimization.h"
#include "absl/flags/config.h"
-#include "absl/flags/declare.h"
-#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/flag.h"
#include "absl/flags/internal/registry.h"
-#include "absl/flags/marshalling.h"
+#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
@@ -73,98 +71,7 @@ ABSL_NAMESPACE_BEGIN
template <typename T>
using Flag = flags_internal::Flag<T>;
#else
-// MSVC debug builds do not implement initialization with constexpr constructors
-// correctly. To work around this we add a level of indirection, so that the
-// class `absl::Flag` contains an `internal::Flag*` (instead of being an alias
-// to that class) and dynamically allocates an instance when necessary. We also
-// forward all calls to internal::Flag methods via trampoline methods. In this
-// setup the `absl::Flag` class does not have constructor and virtual methods,
-// all the data members are public and thus MSVC is able to initialize it at
-// link time. To deal with multiple threads accessing the flag for the first
-// time concurrently we use an atomic boolean indicating if flag object is
-// initialized. We also employ the double-checked locking pattern where the
-// second level of protection is a global Mutex, so if two threads attempt to
-// construct the flag concurrently only one wins.
-// This solution is based on a recomendation here:
-// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html?childToView=648454#comment-648454
-
-namespace flags_internal {
-absl::Mutex* GetGlobalConstructionGuard();
-} // namespace flags_internal
-
-template <typename T>
-class Flag {
- public:
- // No constructor and destructor to ensure this is an aggregate type.
- // Visual Studio 2015 still requires the constructor for class to be
- // constexpr initializable.
-#if _MSC_VER <= 1900
- constexpr Flag(const char* name, const char* filename,
- const flags_internal::HelpGenFunc help_gen,
- const flags_internal::FlagDfltGenFunc default_value_gen)
- : name_(name),
- filename_(filename),
- help_gen_(help_gen),
- default_value_gen_(default_value_gen),
- inited_(false),
- impl_(nullptr) {}
-#endif
-
- flags_internal::Flag<T>* GetImpl() const {
- if (!inited_.load(std::memory_order_acquire)) {
- absl::MutexLock l(flags_internal::GetGlobalConstructionGuard());
-
- if (inited_.load(std::memory_order_acquire)) {
- return impl_;
- }
-
- impl_ =
- new flags_internal::Flag<T>(name_, filename_,
- {flags_internal::FlagHelpMsg(help_gen_),
- flags_internal::FlagHelpKind::kGenFunc},
- default_value_gen_);
- inited_.store(true, std::memory_order_release);
- }
-
- return impl_;
- }
-
- // Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
- // See https://abseil.io/docs/cpp/guides/flags
- bool IsRetired() const { return GetImpl()->IsRetired(); }
- bool IsAbseilFlag() const { return GetImpl()->IsAbseilFlag(); }
- absl::string_view Name() const { return GetImpl()->Name(); }
- std::string Help() const { return GetImpl()->Help(); }
- bool IsModified() const { return GetImpl()->IsModified(); }
- bool IsSpecifiedOnCommandLine() const {
- return GetImpl()->IsSpecifiedOnCommandLine();
- }
- absl::string_view Typename() const { return GetImpl()->Typename(); }
- std::string Filename() const { return GetImpl()->Filename(); }
- std::string DefaultValue() const { return GetImpl()->DefaultValue(); }
- std::string CurrentValue() const { return GetImpl()->CurrentValue(); }
- template <typename U>
- inline bool IsOfType() const {
- return GetImpl()->template IsOfType<U>();
- }
- T Get() const { return GetImpl()->Get(); }
- bool AtomicGet(T* v) const { return GetImpl()->AtomicGet(v); }
- void Set(const T& v) { GetImpl()->Set(v); }
- void SetCallback(const flags_internal::FlagCallbackFunc mutation_callback) {
- GetImpl()->SetCallback(mutation_callback);
- }
- void InvokeCallback() { GetImpl()->InvokeCallback(); }
-
- // The data members are logically private, but they need to be public for
- // this to be an aggregate type.
- const char* name_;
- const char* filename_;
- const flags_internal::HelpGenFunc help_gen_;
- const flags_internal::FlagDfltGenFunc default_value_gen_;
-
- mutable std::atomic<bool> inited_;
- mutable flags_internal::Flag<T>* impl_;
-};
+#include "absl/flags/internal/flag_msvc.inc"
#endif
// GetFlag()
@@ -185,7 +92,7 @@ class Flag {
// std::string first_name = absl::GetFlag(FLAGS_firstname);
template <typename T>
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
- return flag.Get();
+ return flags_internal::FlagImplPeer::InvokeGet<T>(flag);
}
// SetFlag()
@@ -197,7 +104,7 @@ ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
// but especially within performance-critical code.
template <typename T>
void SetFlag(absl::Flag<T>* flag, const T& v) {
- flag->Set(v);
+ flags_internal::FlagImplPeer::InvokeSet(*flag, v);
}
// Overload of `SetFlag()` to allow callers to pass in a value that is
@@ -206,7 +113,22 @@ void SetFlag(absl::Flag<T>* flag, const T& v) {
template <typename T, typename V>
void SetFlag(absl::Flag<T>* flag, const V& v) {
T value(v);
- flag->Set(value);
+ flags_internal::FlagImplPeer::InvokeSet(*flag, value);
+}
+
+// GetFlagReflectionHandle()
+//
+// Returns the reflection handle corresponding to specified Abseil Flag
+// instance. Use this handle to access flag's reflection information, like name,
+// location, default value etc.
+//
+// Example:
+//
+// std::string = absl::GetFlagReflectionHandle(FLAGS_count).DefaultValue();
+
+template <typename T>
+const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<T>& f) {
+ return flags_internal::FlagImplPeer::InvokeReflect(f);
}
ABSL_NAMESPACE_END
@@ -249,6 +171,8 @@ ABSL_NAMESPACE_END
//
// ABSL_FLAG(T, name, default_value, help).OnUpdate(callback);
//
+// `callback` should be convertible to `void (*)()`.
+//
// After any setting of the flag value, the callback will be called at least
// once. A rapid sequence of changes may be merged together into the same
// callback. No concurrent calls to the callback will be made for the same
@@ -263,33 +187,36 @@ ABSL_NAMESPACE_END
// Note: ABSL_FLAG.OnUpdate() does not have a public definition. Hence, this
// comment serves as its API documentation.
-
// -----------------------------------------------------------------------------
// Implementation details below this section
// -----------------------------------------------------------------------------
// ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES
+#if !defined(_MSC_VER) || defined(__clang__)
+#define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag
+#define ABSL_FLAG_IMPL_HELP_ARG(name) \
+ absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>( \
+ FLAGS_help_storage_##name)
+#define ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name) \
+ absl::flags_internal::DefaultArg<Type, AbslFlagDefaultGenFor##name>(0)
+#else
+#define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag.GetImpl()
+#define ABSL_FLAG_IMPL_HELP_ARG(name) &AbslFlagHelpGenFor##name::NonConst
+#define ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name) &AbslFlagDefaultGenFor##name::Gen
+#endif
#if ABSL_FLAGS_STRIP_NAMES
#define ABSL_FLAG_IMPL_FLAGNAME(txt) ""
#define ABSL_FLAG_IMPL_FILENAME() ""
-#if !defined(_MSC_VER) || defined(__clang__)
-#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
- absl::flags_internal::FlagRegistrar<T, false>(&flag)
-#else
-#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
- absl::flags_internal::FlagRegistrar<T, false>(flag.GetImpl())
-#endif
+#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
+ absl::flags_internal::FlagRegistrar<T, false>(ABSL_FLAG_IMPL_FLAG_PTR(flag), \
+ nullptr)
#else
#define ABSL_FLAG_IMPL_FLAGNAME(txt) txt
#define ABSL_FLAG_IMPL_FILENAME() __FILE__
-#if !defined(_MSC_VER) || defined(__clang__)
-#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
- absl::flags_internal::FlagRegistrar<T, true>(&flag)
-#else
-#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
- absl::flags_internal::FlagRegistrar<T, true>(flag.GetImpl())
-#endif
+#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
+ absl::flags_internal::FlagRegistrar<T, true>(ABSL_FLAG_IMPL_FLAG_PTR(flag), \
+ __FILE__)
#endif
// ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_HELP
@@ -305,50 +232,48 @@ ABSL_NAMESPACE_END
// between the two via the call to HelpArg in absl::Flag instantiation below.
// If help message expression is constexpr evaluable compiler will optimize
// away this whole struct.
-#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
- struct AbslFlagHelpGenFor##name { \
- template <typename T = void> \
- static constexpr const char* Const() { \
- return absl::flags_internal::HelpConstexprWrap( \
- 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) \
- static void* AbslFlagsInitFlag##name() { \
- return absl::flags_internal::MakeFromDefaultValue<Type>(default_value); \
- }
+// TODO(rogeeff): place these generated structs into local namespace and apply
+// ABSL_INTERNAL_UNIQUE_SHORT_NAME.
+// TODO(rogeeff): Apply __attribute__((nodebug)) to FLAGS_help_storage_##name
+#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
+ struct AbslFlagHelpGenFor##name { \
+ /* The expression is run in the caller as part of the */ \
+ /* default value argument. That keeps temporaries alive */ \
+ /* long enough for NonConst to work correctly. */ \
+ static constexpr absl::string_view Value( \
+ absl::string_view absl_flag_help = ABSL_FLAG_IMPL_FLAGHELP(txt)) { \
+ return absl_flag_help; \
+ } \
+ static std::string NonConst() { return std::string(Value()); } \
+ }; \
+ constexpr auto FLAGS_help_storage_##name ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
+ ABSL_ATTRIBUTE_SECTION_VARIABLE(flags_help_cold) = \
+ absl::flags_internal::HelpStringAsArray<AbslFlagHelpGenFor##name>( \
+ 0);
+
+#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
+ struct AbslFlagDefaultGenFor##name { \
+ Type value = absl::flags_internal::InitDefaultValue<Type>(default_value); \
+ static void Gen(void* absl_flag_default_loc) { \
+ new (absl_flag_default_loc) Type(AbslFlagDefaultGenFor##name{}.value); \
+ } \
+ };
// ABSL_FLAG_IMPL
//
// Note: Name of registrar object is not arbitrary. It is used to "grab"
// global name for FLAGS_no<flag_name> symbol, thus preventing the possibility
// of defining two flags with names foo and nofoo.
-#if !defined(_MSC_VER) || defined(__clang__)
-#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
- namespace absl /* block flags in namespaces */ {} \
- ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
- ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
- ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
- ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
- absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \
- &AbslFlagsInitFlag##name}; \
- extern bool FLAGS_no##name; \
- bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
-#else
-// MSVC version uses aggregate initialization. We also do not try to
-// optimize away help wrapper.
-#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
- namespace absl /* block flags in namespaces */ {} \
- ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
- ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
- ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
- ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
- &AbslFlagHelpGenFor##name::NonConst, &AbslFlagsInitFlag##name}; \
- extern bool FLAGS_no##name; \
- bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
-#endif
+#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
+ namespace absl /* block flags in namespaces */ {} \
+ ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
+ ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \
+ ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
+ ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
+ ABSL_FLAG_IMPL_HELP_ARG(name), ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name)}; \
+ extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \
+ absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \
+ ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
// ABSL_RETIRED_FLAG
//
@@ -369,11 +294,12 @@ ABSL_NAMESPACE_END
//
// `default_value` is only used as a double check on the type. `explanation` is
// unused.
-// TODO(rogeeff): Return an anonymous struct instead of bool, and place it into
-// the unnamed namespace.
-#define ABSL_RETIRED_FLAG(type, flagname, default_value, explanation) \
- ABSL_ATTRIBUTE_UNUSED static const bool ignored_##flagname = \
- ([] { return type(default_value); }, \
- absl::flags_internal::RetiredFlag<type>(#flagname))
+// TODO(rogeeff): replace RETIRED_FLAGS with FLAGS once forward declarations of
+// retired flags are cleaned up.
+#define ABSL_RETIRED_FLAG(type, name, default_value, explanation) \
+ static absl::flags_internal::RetiredFlag<type> RETIRED_FLAGS_##name; \
+ ABSL_ATTRIBUTE_UNUSED static const auto RETIRED_FLAGS_REG_##name = \
+ (RETIRED_FLAGS_##name.Retire(#name), \
+ ::absl::flags_internal::FlagRegistrarEmpty{})
#endif // ABSL_FLAGS_FLAG_H_
diff --git a/third_party/abseil-cpp/absl/flags/flag_benchmark.cc b/third_party/abseil-cpp/absl/flags/flag_benchmark.cc
index 87f731704c..fc572d9ce3 100644
--- a/third_party/abseil-cpp/absl/flags/flag_benchmark.cc
+++ b/third_party/abseil-cpp/absl/flags/flag_benchmark.cc
@@ -13,7 +13,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+#include <stdint.h>
+
+#include <string>
+#include <vector>
+
#include "absl/flags/flag.h"
+#include "absl/flags/marshalling.h"
+#include "absl/flags/parse.h"
+#include "absl/flags/reflection.h"
+#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "benchmark/benchmark.h"
@@ -92,20 +101,150 @@ std::string AbslUnparseFlag(const UDT&) { return ""; }
A(AbslDuration) \
A(UDT)
-#define FLAG_DEF(T) ABSL_FLAG(T, T##_flag, {}, "");
+#define REPLICATE_0(A, T, name, index) A(T, name, index)
+#define REPLICATE_1(A, T, name, index) \
+ REPLICATE_0(A, T, name, index##0) REPLICATE_0(A, T, name, index##1)
+#define REPLICATE_2(A, T, name, index) \
+ REPLICATE_1(A, T, name, index##0) REPLICATE_1(A, T, name, index##1)
+#define REPLICATE_3(A, T, name, index) \
+ REPLICATE_2(A, T, name, index##0) REPLICATE_2(A, T, name, index##1)
+#define REPLICATE_4(A, T, name, index) \
+ REPLICATE_3(A, T, name, index##0) REPLICATE_3(A, T, name, index##1)
+#define REPLICATE_5(A, T, name, index) \
+ REPLICATE_4(A, T, name, index##0) REPLICATE_4(A, T, name, index##1)
+#define REPLICATE_6(A, T, name, index) \
+ REPLICATE_5(A, T, name, index##0) REPLICATE_5(A, T, name, index##1)
+#define REPLICATE_7(A, T, name, index) \
+ REPLICATE_6(A, T, name, index##0) REPLICATE_6(A, T, name, index##1)
+#define REPLICATE_8(A, T, name, index) \
+ REPLICATE_7(A, T, name, index##0) REPLICATE_7(A, T, name, index##1)
+#define REPLICATE_9(A, T, name, index) \
+ REPLICATE_8(A, T, name, index##0) REPLICATE_8(A, T, name, index##1)
+#if defined(_MSC_VER)
+#define REPLICATE(A, T, name) \
+ REPLICATE_7(A, T, name, 0) REPLICATE_7(A, T, name, 1)
+#define SINGLE_FLAG(T) FLAGS_##T##_flag_00000000
+#else
+#define REPLICATE(A, T, name) \
+ REPLICATE_9(A, T, name, 0) REPLICATE_9(A, T, name, 1)
+#define SINGLE_FLAG(T) FLAGS_##T##_flag_0000000000
+#endif
+#define REPLICATE_ALL(A, T, name) \
+ REPLICATE_9(A, T, name, 0) REPLICATE_9(A, T, name, 1)
+
+#define COUNT(T, name, index) +1
+constexpr size_t kNumFlags = 0 REPLICATE(COUNT, _, _);
+#if defined(__clang__) && defined(__linux__)
+// Force the flags used for benchmarks into a separate ELF section.
+// This ensures that, even when other parts of the code might change size,
+// the layout of the flags across cachelines is kept constant. This makes
+// benchmark results more reproducible across unrelated code changes.
+#pragma clang section data = ".benchmark_flags"
+#endif
+#define DEFINE_FLAG(T, name, index) ABSL_FLAG(T, name##_##index, {}, "");
+#define FLAG_DEF(T) REPLICATE(DEFINE_FLAG, T, T##_flag);
BENCHMARKED_TYPES(FLAG_DEF)
+#if defined(__clang__) && defined(__linux__)
+#pragma clang section data = ""
+#endif
+// Register thousands of flags to bloat up the size of the registry.
+// This mimics real life production binaries.
+#define BLOAT_FLAG(_unused1, _unused2, index) \
+ ABSL_FLAG(int, bloat_flag_##index, 0, "");
+REPLICATE_ALL(BLOAT_FLAG, _, _)
namespace {
-#define BM_GetFlag(T) \
- void BM_GetFlag_##T(benchmark::State& state) { \
- for (auto _ : state) { \
- benchmark::DoNotOptimize(absl::GetFlag(FLAGS_##T##_flag)); \
- } \
- } \
- BENCHMARK(BM_GetFlag_##T);
+#define FLAG_PTR(T, name, index) &FLAGS_##name##_##index,
+#define FLAG_PTR_ARR(T) \
+ static constexpr absl::Flag<T>* FlagPtrs_##T[] = { \
+ REPLICATE(FLAG_PTR, T, T##_flag)};
+BENCHMARKED_TYPES(FLAG_PTR_ARR)
+
+#define BM_SingleGetFlag(T) \
+ void BM_SingleGetFlag_##T(benchmark::State& state) { \
+ for (auto _ : state) { \
+ benchmark::DoNotOptimize(absl::GetFlag(SINGLE_FLAG(T))); \
+ } \
+ } \
+ BENCHMARK(BM_SingleGetFlag_##T)->ThreadRange(1, 16);
+
+BENCHMARKED_TYPES(BM_SingleGetFlag)
+
+template <typename T>
+struct Accumulator {
+ using type = T;
+};
+template <>
+struct Accumulator<String> {
+ using type = size_t;
+};
+template <>
+struct Accumulator<VectorOfStrings> {
+ using type = size_t;
+};
+template <>
+struct Accumulator<OptionalInt> {
+ using type = bool;
+};
+template <>
+struct Accumulator<OptionalString> {
+ using type = bool;
+};
+template <>
+struct Accumulator<UDT> {
+ using type = bool;
+};
+
+template <typename T>
+void Accumulate(typename Accumulator<T>::type& a, const T& f) {
+ a += f;
+}
+void Accumulate(bool& a, bool f) { a = a || f; }
+void Accumulate(size_t& a, const std::string& f) { a += f.size(); }
+void Accumulate(size_t& a, const std::vector<std::string>& f) { a += f.size(); }
+void Accumulate(bool& a, const OptionalInt& f) { a |= f.has_value(); }
+void Accumulate(bool& a, const OptionalString& f) { a |= f.has_value(); }
+void Accumulate(bool& a, const UDT& f) {
+ a |= reinterpret_cast<int64_t>(&f) & 0x1;
+}
+
+#define BM_ManyGetFlag(T) \
+ void BM_ManyGetFlag_##T(benchmark::State& state) { \
+ Accumulator<T>::type res = {}; \
+ while (state.KeepRunningBatch(kNumFlags)) { \
+ for (auto* flag_ptr : FlagPtrs_##T) { \
+ Accumulate(res, absl::GetFlag(*flag_ptr)); \
+ } \
+ } \
+ benchmark::DoNotOptimize(res); \
+ } \
+ BENCHMARK(BM_ManyGetFlag_##T)->ThreadRange(1, 8);
+
+BENCHMARKED_TYPES(BM_ManyGetFlag)
-BENCHMARKED_TYPES(BM_GetFlag)
+void BM_ThreadedFindCommandLineFlag(benchmark::State& state) {
+ char dummy[] = "dummy";
+ char* argv[] = {dummy};
+ // We need to ensure that flags have been parsed. That is where the registry
+ // is finalized.
+ absl::ParseCommandLine(1, argv);
+
+ while (state.KeepRunningBatch(kNumFlags)) {
+ for (auto* flag_ptr : FlagPtrs_bool) {
+ benchmark::DoNotOptimize(absl::FindCommandLineFlag(flag_ptr->Name()));
+ }
+ }
+}
+BENCHMARK(BM_ThreadedFindCommandLineFlag)->ThreadRange(1, 16);
} // namespace
+
+#define InvokeGetFlag(T) \
+ T AbslInvokeGetFlag##T() { return absl::GetFlag(SINGLE_FLAG(T)); } \
+ int odr##T = (benchmark::DoNotOptimize(AbslInvokeGetFlag##T), 1);
+
+BENCHMARKED_TYPES(InvokeGetFlag)
+
+// To veiw disassembly use: gdb ${BINARY} -batch -ex "disassemble /s $FUNC"
diff --git a/third_party/abseil-cpp/absl/flags/flag_benchmark.lds b/third_party/abseil-cpp/absl/flags/flag_benchmark.lds
new file mode 100644
index 0000000000..af115dfc00
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/flag_benchmark.lds
@@ -0,0 +1,13 @@
+/* This linker script forces the flags used by flags_benchmark
+ * into a separate page-aligned section. This isn't necessary for
+ * correctness but ensures that the benchmark results are more
+ * reproducible across unrelated code changes.
+ */
+SECTIONS {
+ .benchmark_flags : {
+ . = ALIGN(0x1000);
+ * (.benchmark_flags);
+ }
+}
+
+INSERT AFTER .data
diff --git a/third_party/abseil-cpp/absl/flags/flag_test.cc b/third_party/abseil-cpp/absl/flags/flag_test.cc
index 4984d28403..6e974a5b5e 100644
--- a/third_party/abseil-cpp/absl/flags/flag_test.cc
+++ b/third_party/abseil-cpp/absl/flags/flag_test.cc
@@ -15,25 +15,31 @@
#include "absl/flags/flag.h"
+#include <stddef.h>
#include <stdint.h>
+#include <atomic>
#include <cmath>
+#include <new>
#include <string>
+#include <thread> // NOLINT
#include <vector>
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
+#include "absl/base/macros.h"
#include "absl/flags/config.h"
#include "absl/flags/declare.h"
-#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/flag.h"
-#include "absl/flags/internal/registry.h"
+#include "absl/flags/marshalling.h"
+#include "absl/flags/reflection.h"
#include "absl/flags/usage_config.h"
#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
+#include "absl/time/time.h"
ABSL_DECLARE_FLAG(int64_t, mistyped_int_flag);
ABSL_DECLARE_FLAG(std::vector<std::string>, mistyped_string_flag);
@@ -43,37 +49,19 @@ namespace {
namespace flags = absl::flags_internal;
std::string TestHelpMsg() { return "dynamic help"; }
+#if defined(_MSC_VER) && !defined(__clang__)
+std::string TestLiteralHelpMsg() { return "literal help"; }
+#endif
template <typename T>
-void* TestMakeDflt() {
- return new T{};
+void TestMakeDflt(void* dst) {
+ new (dst) T{};
}
void TestCallback() {}
-template <typename T>
-bool TestConstructionFor() {
- constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"),
- flags::FlagHelpKind::kLiteral};
- constexpr flags::Flag<T> f1("f1", "file", help_arg, &TestMakeDflt<T>);
- EXPECT_EQ(f1.Name(), "f1");
- EXPECT_EQ(f1.Help(), "literal help");
- EXPECT_EQ(f1.Filename(), "file");
-
- ABSL_CONST_INIT static flags::Flag<T> f2(
- "f2", "file",
- {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc},
- &TestMakeDflt<T>);
- flags::FlagRegistrar<T, false>(&f2).OnUpdate(TestCallback);
-
- EXPECT_EQ(f2.Name(), "f2");
- EXPECT_EQ(f2.Help(), "dynamic help");
- EXPECT_EQ(f2.Filename(), "file");
-
- return true;
-}
-
struct UDT {
UDT() = default;
UDT(const UDT&) = default;
+ UDT& operator=(const UDT&) = default;
};
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
std::string AbslUnparseFlag(const UDT&) { return ""; }
@@ -96,21 +84,124 @@ class FlagTest : public testing::Test {
#endif
return std::string(fname);
}
+ absl::FlagSaver flag_saver_;
};
-TEST_F(FlagTest, TestConstruction) {
- TestConstructionFor<bool>();
- TestConstructionFor<int16_t>();
- TestConstructionFor<uint16_t>();
- TestConstructionFor<int32_t>();
- TestConstructionFor<uint32_t>();
- TestConstructionFor<int64_t>();
- TestConstructionFor<uint64_t>();
- TestConstructionFor<double>();
- TestConstructionFor<float>();
- TestConstructionFor<std::string>();
+struct S1 {
+ S1() = default;
+ S1(const S1&) = default;
+ int32_t f1;
+ int64_t f2;
+};
+
+struct S2 {
+ S2() = default;
+ S2(const S2&) = default;
+ int64_t f1;
+ double f2;
+};
+
+TEST_F(FlagTest, Traits) {
+ EXPECT_EQ(flags::StorageKind<int>(),
+ flags::FlagValueStorageKind::kValueAndInitBit);
+ EXPECT_EQ(flags::StorageKind<bool>(),
+ flags::FlagValueStorageKind::kValueAndInitBit);
+ EXPECT_EQ(flags::StorageKind<double>(),
+ flags::FlagValueStorageKind::kOneWordAtomic);
+ EXPECT_EQ(flags::StorageKind<int64_t>(),
+ flags::FlagValueStorageKind::kOneWordAtomic);
+
+ EXPECT_EQ(flags::StorageKind<S1>(),
+ flags::FlagValueStorageKind::kSequenceLocked);
+ EXPECT_EQ(flags::StorageKind<S2>(),
+ flags::FlagValueStorageKind::kSequenceLocked);
+// Make sure absl::Duration uses the sequence-locked code path. MSVC 2015
+// doesn't consider absl::Duration to be trivially-copyable so we just
+// restrict this to clang as it seems to be a well-behaved compiler.
+#ifdef __clang__
+ EXPECT_EQ(flags::StorageKind<absl::Duration>(),
+ flags::FlagValueStorageKind::kSequenceLocked);
+#endif
+
+ EXPECT_EQ(flags::StorageKind<std::string>(),
+ flags::FlagValueStorageKind::kAlignedBuffer);
+ EXPECT_EQ(flags::StorageKind<std::vector<std::string>>(),
+ flags::FlagValueStorageKind::kAlignedBuffer);
+}
+
+// --------------------------------------------------------------------
+
+constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"),
+ flags::FlagHelpKind::kLiteral};
+
+using String = std::string;
+
+#if !defined(_MSC_VER) || defined(__clang__)
+#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
+ constexpr flags::FlagDefaultArg f1default##T{ \
+ flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
+ constexpr absl::Flag<T> f1##T{"f1", "file", help_arg, f1default##T}; \
+ ABSL_CONST_INIT absl::Flag<T> f2##T { \
+ "f2", "file", \
+ {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \
+ flags::FlagDefaultArg { \
+ flags::FlagDefaultSrc(&TestMakeDflt<T>), \
+ flags::FlagDefaultKind::kGenFunc \
+ } \
+ }
+#else
+#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
+ constexpr flags::FlagDefaultArg f1default##T{ \
+ flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
+ constexpr absl::Flag<T> f1##T{"f1", "file", &TestLiteralHelpMsg, \
+ &TestMakeDflt<T>}; \
+ ABSL_CONST_INIT absl::Flag<T> f2##T { \
+ "f2", "file", &TestHelpMsg, &TestMakeDflt<T> \
+ }
+#endif
+
+DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(uint16_t, 2, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(int32_t, 3, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(uint32_t, 4, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(int64_t, 5, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(uint64_t, 6, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(float, 7.8, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(double, 9.10, kOneWord);
+DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt<String>, kGenFunc);
+DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt<UDT>, kGenFunc);
+
+template <typename T>
+bool TestConstructionFor(const absl::Flag<T>& f1, absl::Flag<T>& f2) {
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Name(), "f1");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Help(), "literal help");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Filename(), "file");
+
+ flags::FlagRegistrar<T, false>(ABSL_FLAG_IMPL_FLAG_PTR(f2), nullptr)
+ .OnUpdate(TestCallback);
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Name(), "f2");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Help(), "dynamic help");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Filename(), "file");
+
+ return true;
+}
+
+#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, f2##T);
- TestConstructionFor<UDT>();
+TEST_F(FlagTest, TestConstruction) {
+ TEST_CONSTRUCTED_FLAG(bool);
+ TEST_CONSTRUCTED_FLAG(int16_t);
+ TEST_CONSTRUCTED_FLAG(uint16_t);
+ TEST_CONSTRUCTED_FLAG(int32_t);
+ TEST_CONSTRUCTED_FLAG(uint32_t);
+ TEST_CONSTRUCTED_FLAG(int64_t);
+ TEST_CONSTRUCTED_FLAG(uint64_t);
+ TEST_CONSTRUCTED_FLAG(float);
+ TEST_CONSTRUCTED_FLAG(double);
+ TEST_CONSTRUCTED_FLAG(String);
+ TEST_CONSTRUCTED_FLAG(UDT);
}
// --------------------------------------------------------------------
@@ -128,6 +219,7 @@ ABSL_DECLARE_FLAG(uint64_t, test_flag_08);
ABSL_DECLARE_FLAG(double, test_flag_09);
ABSL_DECLARE_FLAG(float, test_flag_10);
ABSL_DECLARE_FLAG(std::string, test_flag_11);
+ABSL_DECLARE_FLAG(absl::Duration, test_flag_12);
namespace {
@@ -135,17 +227,30 @@ namespace {
TEST_F(FlagTest, TestFlagDeclaration) {
// test that we can access flag objects.
- EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
- EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
- EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03");
- EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04");
- EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05");
- EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06");
- EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07");
- EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08");
- EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09");
- EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10");
- EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
+ "test_flag_01");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Name(),
+ "test_flag_02");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Name(),
+ "test_flag_03");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Name(),
+ "test_flag_04");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Name(),
+ "test_flag_05");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Name(),
+ "test_flag_06");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Name(),
+ "test_flag_07");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Name(),
+ "test_flag_08");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Name(),
+ "test_flag_09");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Name(),
+ "test_flag_10");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Name(),
+ "test_flag_11");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Name(),
+ "test_flag_12");
}
#endif // !ABSL_FLAGS_STRIP_NAMES
@@ -164,6 +269,7 @@ ABSL_FLAG(uint64_t, test_flag_08, 9876543, "test flag 08");
ABSL_FLAG(double, test_flag_09, -9.876e-50, "test flag 09");
ABSL_FLAG(float, test_flag_10, 1.234e12f, "test flag 10");
ABSL_FLAG(std::string, test_flag_11, "", "test flag 11");
+ABSL_FLAG(absl::Duration, test_flag_12, absl::Minutes(10), "test flag 12");
namespace {
@@ -171,66 +277,169 @@ namespace {
TEST_F(FlagTest, TestFlagDefinition) {
absl::string_view expected_file_name = "absl/flags/flag_test.cc";
- EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
- EXPECT_EQ(FLAGS_test_flag_01.Help(), "test flag 01");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name))
- << FLAGS_test_flag_01.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
- EXPECT_EQ(FLAGS_test_flag_02.Help(), "test flag 02");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name))
- << FLAGS_test_flag_02.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03");
- EXPECT_EQ(FLAGS_test_flag_03.Help(), "test flag 03");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name))
- << FLAGS_test_flag_03.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04");
- EXPECT_EQ(FLAGS_test_flag_04.Help(), "test flag 04");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name))
- << FLAGS_test_flag_04.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05");
- EXPECT_EQ(FLAGS_test_flag_05.Help(), "test flag 05");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name))
- << FLAGS_test_flag_05.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06");
- EXPECT_EQ(FLAGS_test_flag_06.Help(), "test flag 06");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name))
- << FLAGS_test_flag_06.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07");
- EXPECT_EQ(FLAGS_test_flag_07.Help(), "test flag 07");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name))
- << FLAGS_test_flag_07.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08");
- EXPECT_EQ(FLAGS_test_flag_08.Help(), "test flag 08");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name))
- << FLAGS_test_flag_08.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09");
- EXPECT_EQ(FLAGS_test_flag_09.Help(), "test flag 09");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name))
- << FLAGS_test_flag_09.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10");
- EXPECT_EQ(FLAGS_test_flag_10.Help(), "test flag 10");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name))
- << FLAGS_test_flag_10.Filename();
-
- EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11");
- EXPECT_EQ(FLAGS_test_flag_11.Help(), "test flag 11");
- EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name))
- << FLAGS_test_flag_11.Filename();
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
+ "test_flag_01");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Help(),
+ "test flag 01");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Name(),
+ "test_flag_02");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Help(),
+ "test flag 02");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Name(),
+ "test_flag_03");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Help(),
+ "test flag 03");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Name(),
+ "test_flag_04");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Help(),
+ "test flag 04");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Name(),
+ "test_flag_05");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Help(),
+ "test flag 05");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Name(),
+ "test_flag_06");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Help(),
+ "test flag 06");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Name(),
+ "test_flag_07");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Help(),
+ "test flag 07");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Name(),
+ "test_flag_08");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Help(),
+ "test flag 08");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Name(),
+ "test_flag_09");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Help(),
+ "test flag 09");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Name(),
+ "test_flag_10");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Help(),
+ "test flag 10");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Name(),
+ "test_flag_11");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Help(),
+ "test flag 11");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Filename();
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Name(),
+ "test_flag_12");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Help(),
+ "test flag 12");
+ EXPECT_TRUE(absl::EndsWith(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Filename(),
+ expected_file_name))
+ << absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Filename();
}
#endif // !ABSL_FLAGS_STRIP_NAMES
// --------------------------------------------------------------------
TEST_F(FlagTest, TestDefault) {
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).DefaultValue(),
+ "true");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).DefaultValue(),
+ "1234");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).DefaultValue(),
+ "-34");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).DefaultValue(),
+ "189");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).DefaultValue(),
+ "10765");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).DefaultValue(),
+ "40000");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).DefaultValue(),
+ "-1234567");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).DefaultValue(),
+ "9876543");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).DefaultValue(),
+ "-9.876e-50");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).DefaultValue(),
+ "1.234e+12");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).DefaultValue(),
+ "");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).DefaultValue(),
+ "10m");
+
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).CurrentValue(),
+ "true");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).CurrentValue(),
+ "1234");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).CurrentValue(),
+ "-34");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).CurrentValue(),
+ "189");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).CurrentValue(),
+ "10765");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).CurrentValue(),
+ "40000");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).CurrentValue(),
+ "-1234567");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).CurrentValue(),
+ "9876543");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).CurrentValue(),
+ "-9.876e-50");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).CurrentValue(),
+ "1.234e+12");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).CurrentValue(),
+ "");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).CurrentValue(),
+ "10m");
+
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
@@ -242,6 +451,68 @@ TEST_F(FlagTest, TestDefault) {
EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Minutes(10));
+}
+
+// --------------------------------------------------------------------
+
+struct NonTriviallyCopyableAggregate {
+ NonTriviallyCopyableAggregate() = default;
+ NonTriviallyCopyableAggregate(const NonTriviallyCopyableAggregate& rhs)
+ : value(rhs.value) {}
+ NonTriviallyCopyableAggregate& operator=(
+ const NonTriviallyCopyableAggregate& rhs) {
+ value = rhs.value;
+ return *this;
+ }
+
+ int value;
+};
+bool AbslParseFlag(absl::string_view src, NonTriviallyCopyableAggregate* f,
+ std::string* e) {
+ return absl::ParseFlag(src, &f->value, e);
+}
+std::string AbslUnparseFlag(const NonTriviallyCopyableAggregate& ntc) {
+ return absl::StrCat(ntc.value);
+}
+
+bool operator==(const NonTriviallyCopyableAggregate& ntc1,
+ const NonTriviallyCopyableAggregate& ntc2) {
+ return ntc1.value == ntc2.value;
+}
+
+} // namespace
+
+ABSL_FLAG(bool, test_flag_eb_01, {}, "");
+ABSL_FLAG(int32_t, test_flag_eb_02, {}, "");
+ABSL_FLAG(int64_t, test_flag_eb_03, {}, "");
+ABSL_FLAG(double, test_flag_eb_04, {}, "");
+ABSL_FLAG(std::string, test_flag_eb_05, {}, "");
+ABSL_FLAG(NonTriviallyCopyableAggregate, test_flag_eb_06, {}, "");
+
+namespace {
+
+TEST_F(FlagTest, TestEmptyBracesDefault) {
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_01).DefaultValue(),
+ "false");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_02).DefaultValue(),
+ "0");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_03).DefaultValue(),
+ "0");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_04).DefaultValue(),
+ "0");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_05).DefaultValue(),
+ "");
+ EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_06).DefaultValue(),
+ "0");
+
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_01), false);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_02), 0);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_03), 0);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_04), 0.0);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_05), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_06),
+ NonTriviallyCopyableAggregate{});
}
// --------------------------------------------------------------------
@@ -279,6 +550,75 @@ TEST_F(FlagTest, TestGetSet) {
absl::SetFlag(&FLAGS_test_flag_11, "asdf");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "asdf");
+
+ absl::SetFlag(&FLAGS_test_flag_12, absl::Seconds(110));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Seconds(110));
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, TestGetViaReflection) {
+ auto* handle = absl::FindCommandLineFlag("test_flag_01");
+ EXPECT_EQ(*handle->TryGet<bool>(), true);
+ handle = absl::FindCommandLineFlag("test_flag_02");
+ EXPECT_EQ(*handle->TryGet<int>(), 1234);
+ handle = absl::FindCommandLineFlag("test_flag_03");
+ EXPECT_EQ(*handle->TryGet<int16_t>(), -34);
+ handle = absl::FindCommandLineFlag("test_flag_04");
+ EXPECT_EQ(*handle->TryGet<uint16_t>(), 189);
+ handle = absl::FindCommandLineFlag("test_flag_05");
+ EXPECT_EQ(*handle->TryGet<int32_t>(), 10765);
+ handle = absl::FindCommandLineFlag("test_flag_06");
+ EXPECT_EQ(*handle->TryGet<uint32_t>(), 40000);
+ handle = absl::FindCommandLineFlag("test_flag_07");
+ EXPECT_EQ(*handle->TryGet<int64_t>(), -1234567);
+ handle = absl::FindCommandLineFlag("test_flag_08");
+ EXPECT_EQ(*handle->TryGet<uint64_t>(), 9876543);
+ handle = absl::FindCommandLineFlag("test_flag_09");
+ EXPECT_NEAR(*handle->TryGet<double>(), -9.876e-50, 1e-55);
+ handle = absl::FindCommandLineFlag("test_flag_10");
+ EXPECT_NEAR(*handle->TryGet<float>(), 1.234e12f, 1e5f);
+ handle = absl::FindCommandLineFlag("test_flag_11");
+ EXPECT_EQ(*handle->TryGet<std::string>(), "");
+ handle = absl::FindCommandLineFlag("test_flag_12");
+ EXPECT_EQ(*handle->TryGet<absl::Duration>(), absl::Minutes(10));
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, ConcurrentSetAndGet) {
+ static constexpr int kNumThreads = 8;
+ // Two arbitrary durations. One thread will concurrently flip the flag
+ // between these two values, while the other threads read it and verify
+ // that no other value is seen.
+ static const absl::Duration kValidDurations[] = {
+ absl::Seconds(int64_t{0x6cebf47a9b68c802}) + absl::Nanoseconds(229702057),
+ absl::Seconds(int64_t{0x23fec0307e4e9d3}) + absl::Nanoseconds(44555374)};
+ absl::SetFlag(&FLAGS_test_flag_12, kValidDurations[0]);
+
+ std::atomic<bool> stop{false};
+ std::vector<std::thread> threads;
+ auto* handle = absl::FindCommandLineFlag("test_flag_12");
+ for (int i = 0; i < kNumThreads; i++) {
+ threads.emplace_back([&]() {
+ while (!stop.load(std::memory_order_relaxed)) {
+ // Try loading the flag both directly and via a reflection
+ // handle.
+ absl::Duration v = absl::GetFlag(FLAGS_test_flag_12);
+ EXPECT_TRUE(v == kValidDurations[0] || v == kValidDurations[1]);
+ v = *handle->TryGet<absl::Duration>();
+ EXPECT_TRUE(v == kValidDurations[0] || v == kValidDurations[1]);
+ }
+ });
+ }
+ absl::Time end_time = absl::Now() + absl::Seconds(1);
+ int i = 0;
+ while (absl::Now() < end_time) {
+ absl::SetFlag(&FLAGS_test_flag_12,
+ kValidDurations[i++ % ABSL_ARRAYSIZE(kValidDurations)]);
+ }
+ stop.store(true, std::memory_order_relaxed);
+ for (auto& t : threads) t.join();
}
// --------------------------------------------------------------------
@@ -287,28 +627,33 @@ int GetDflt1() { return 1; }
} // namespace
-ABSL_FLAG(int, test_flag_12, GetDflt1(), "test flag 12");
-ABSL_FLAG(std::string, test_flag_13, absl::StrCat("AAA", "BBB"),
- "test flag 13");
+ABSL_FLAG(int, test_int_flag_with_non_const_default, GetDflt1(),
+ "test int flag non const default");
+ABSL_FLAG(std::string, test_string_flag_with_non_const_default,
+ absl::StrCat("AAA", "BBB"), "test string flag non const default");
namespace {
TEST_F(FlagTest, TestNonConstexprDefault) {
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), 1);
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), "AAABBB");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_int_flag_with_non_const_default), 1);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_string_flag_with_non_const_default),
+ "AAABBB");
}
// --------------------------------------------------------------------
} // namespace
-ABSL_FLAG(bool, test_flag_14, true, absl::StrCat("test ", "flag ", "14"));
+ABSL_FLAG(bool, test_flag_with_non_const_help, true,
+ absl::StrCat("test ", "flag ", "non const help"));
namespace {
#if !ABSL_FLAGS_STRIP_HELP
TEST_F(FlagTest, TestNonConstexprHelp) {
- EXPECT_EQ(FLAGS_test_flag_14.Help(), "test flag 14");
+ EXPECT_EQ(
+ absl::GetFlagReflectionHandle(FLAGS_test_flag_with_non_const_help).Help(),
+ "test flag non const help");
}
#endif //! ABSL_FLAGS_STRIP_HELP
@@ -374,14 +719,16 @@ std::string AbslUnparseFlag(const CustomUDT& f) {
} // namespace
-ABSL_FLAG(CustomUDT, test_flag_15, CustomUDT(), "test flag 15");
+ABSL_FLAG(CustomUDT, test_flag_custom_udt, CustomUDT(), "test flag custom UDT");
namespace {
TEST_F(FlagTest, TestCustomUDT) {
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(1, 1));
- absl::SetFlag(&FLAGS_test_flag_15, CustomUDT(2, 3));
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(2, 3));
+ EXPECT_EQ(flags::StorageKind<CustomUDT>(),
+ flags::FlagValueStorageKind::kOneWordAtomic);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_custom_udt), CustomUDT(1, 1));
+ absl::SetFlag(&FLAGS_test_flag_custom_udt, CustomUDT(2, 3));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_custom_udt), CustomUDT(2, 3));
}
// MSVC produces link error on the type mismatch.
@@ -391,18 +738,22 @@ TEST_F(FlagTest, TestCustomUDT) {
using FlagDeathTest = FlagTest;
TEST_F(FlagDeathTest, TestTypeMismatchValidations) {
- EXPECT_DEBUG_DEATH(
+#if !defined(NDEBUG)
+ EXPECT_DEATH_IF_SUPPORTED(
static_cast<void>(absl::GetFlag(FLAGS_mistyped_int_flag)),
"Flag 'mistyped_int_flag' is defined as one type and declared "
"as another");
- EXPECT_DEATH(absl::SetFlag(&FLAGS_mistyped_int_flag, 1),
- "Flag 'mistyped_int_flag' is defined as one type and declared "
- "as another");
-
- EXPECT_DEATH(static_cast<void>(absl::GetFlag(FLAGS_mistyped_string_flag)),
- "Flag 'mistyped_string_flag' is defined as one type and "
- "declared as another");
- EXPECT_DEATH(
+ EXPECT_DEATH_IF_SUPPORTED(
+ static_cast<void>(absl::GetFlag(FLAGS_mistyped_string_flag)),
+ "Flag 'mistyped_string_flag' is defined as one type and "
+ "declared as another");
+#endif
+
+ EXPECT_DEATH_IF_SUPPORTED(
+ absl::SetFlag(&FLAGS_mistyped_int_flag, 1),
+ "Flag 'mistyped_int_flag' is defined as one type and declared "
+ "as another");
+ EXPECT_DEATH_IF_SUPPORTED(
absl::SetFlag(&FLAGS_mistyped_string_flag, std::vector<std::string>{}),
"Flag 'mistyped_string_flag' is defined as one type and declared as "
"another");
@@ -440,16 +791,17 @@ std::string AbslUnparseFlag(const ConversionTestVal& val) {
// Flag default values can be specified with a value that converts to the flag
// value type implicitly.
-ABSL_FLAG(ConversionTestVal, test_flag_16,
- ConversionTestVal::ViaImplicitConv::kTen, "test flag 16");
+ABSL_FLAG(ConversionTestVal, test_flag_implicit_conv,
+ ConversionTestVal::ViaImplicitConv::kTen,
+ "test flag init via implicit conversion");
namespace {
TEST_F(FlagTest, CanSetViaImplicitConversion) {
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_16).a, 10);
- absl::SetFlag(&FLAGS_test_flag_16,
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_implicit_conv).a, 10);
+ absl::SetFlag(&FLAGS_test_flag_implicit_conv,
ConversionTestVal::ViaImplicitConv::kEleven);
- EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_16).a, 11);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_implicit_conv).a, 11);
}
// --------------------------------------------------------------------
@@ -494,25 +846,134 @@ TEST_F(FlagTest, TestNonDefaultConstructibleType) {
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag2).value, 25);
}
-// --------------------------------------------------------------------
-
} // namespace
+// --------------------------------------------------------------------
+
ABSL_RETIRED_FLAG(bool, old_bool_flag, true, "old descr");
ABSL_RETIRED_FLAG(int, old_int_flag, (int)std::sqrt(10), "old descr");
ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr"));
+bool initializaion_order_fiasco_test = [] {
+ // Iterate over all the flags during static initialization.
+ // This should not trigger ASan's initialization-order-fiasco.
+ auto* handle1 = absl::FindCommandLineFlag("flag_on_separate_file");
+ auto* handle2 = absl::FindCommandLineFlag("retired_flag_on_separate_file");
+ if (handle1 != nullptr && handle2 != nullptr) {
+ return handle1->Name() == handle2->Name();
+ }
+ return true;
+}();
+
namespace {
TEST_F(FlagTest, TestRetiredFlagRegistration) {
- bool is_bool = false;
- EXPECT_TRUE(flags::IsRetiredFlag("old_bool_flag", &is_bool));
- EXPECT_TRUE(is_bool);
- EXPECT_TRUE(flags::IsRetiredFlag("old_int_flag", &is_bool));
- EXPECT_FALSE(is_bool);
- EXPECT_TRUE(flags::IsRetiredFlag("old_str_flag", &is_bool));
- EXPECT_FALSE(is_bool);
- EXPECT_FALSE(flags::IsRetiredFlag("some_other_flag", &is_bool));
+ auto* handle = absl::FindCommandLineFlag("old_bool_flag");
+ EXPECT_TRUE(handle->IsOfType<bool>());
+ EXPECT_TRUE(handle->IsRetired());
+ handle = absl::FindCommandLineFlag("old_int_flag");
+ EXPECT_TRUE(handle->IsOfType<int>());
+ EXPECT_TRUE(handle->IsRetired());
+ handle = absl::FindCommandLineFlag("old_str_flag");
+ EXPECT_TRUE(handle->IsOfType<std::string>());
+ EXPECT_TRUE(handle->IsRetired());
}
} // namespace
+
+// --------------------------------------------------------------------
+
+namespace {
+
+// User-defined type with small alignment, but size exceeding 16.
+struct SmallAlignUDT {
+ SmallAlignUDT() : c('A'), s(12) {}
+ char c;
+ int16_t s;
+ char bytes[14];
+};
+
+bool AbslParseFlag(absl::string_view, SmallAlignUDT*, std::string*) {
+ return true;
+}
+std::string AbslUnparseFlag(const SmallAlignUDT&) { return ""; }
+
+// User-defined type with small size, but not trivially copyable.
+struct NonTriviallyCopyableUDT {
+ NonTriviallyCopyableUDT() : c('A') {}
+ NonTriviallyCopyableUDT(const NonTriviallyCopyableUDT& rhs) : c(rhs.c) {}
+ NonTriviallyCopyableUDT& operator=(const NonTriviallyCopyableUDT& rhs) {
+ c = rhs.c;
+ return *this;
+ }
+
+ char c;
+};
+
+bool AbslParseFlag(absl::string_view, NonTriviallyCopyableUDT*, std::string*) {
+ return true;
+}
+std::string AbslUnparseFlag(const NonTriviallyCopyableUDT&) { return ""; }
+
+} // namespace
+
+ABSL_FLAG(SmallAlignUDT, test_flag_sa_udt, {}, "help");
+ABSL_FLAG(NonTriviallyCopyableUDT, test_flag_ntc_udt, {}, "help");
+
+namespace {
+
+TEST_F(FlagTest, TestSmallAlignUDT) {
+ SmallAlignUDT value = absl::GetFlag(FLAGS_test_flag_sa_udt);
+ EXPECT_EQ(value.c, 'A');
+ EXPECT_EQ(value.s, 12);
+
+ value.c = 'B';
+ value.s = 45;
+ absl::SetFlag(&FLAGS_test_flag_sa_udt, value);
+ value = absl::GetFlag(FLAGS_test_flag_sa_udt);
+ EXPECT_EQ(value.c, 'B');
+ EXPECT_EQ(value.s, 45);
+}
+
+TEST_F(FlagTest, TestNonTriviallyCopyableUDT) {
+ NonTriviallyCopyableUDT value = absl::GetFlag(FLAGS_test_flag_ntc_udt);
+ EXPECT_EQ(value.c, 'A');
+
+ value.c = 'B';
+ absl::SetFlag(&FLAGS_test_flag_ntc_udt, value);
+ value = absl::GetFlag(FLAGS_test_flag_ntc_udt);
+ EXPECT_EQ(value.c, 'B');
+}
+
+} // namespace
+
+// --------------------------------------------------------------------
+
+namespace {
+
+enum TestE { A = 1, B = 2, C = 3 };
+
+struct EnumWrapper {
+ EnumWrapper() : e(A) {}
+
+ TestE e;
+};
+
+bool AbslParseFlag(absl::string_view, EnumWrapper*, std::string*) {
+ return true;
+}
+std::string AbslUnparseFlag(const EnumWrapper&) { return ""; }
+
+} // namespace
+
+ABSL_FLAG(EnumWrapper, test_enum_wrapper_flag, {}, "help");
+
+TEST_F(FlagTest, TesTypeWrappingEnum) {
+ EnumWrapper value = absl::GetFlag(FLAGS_test_enum_wrapper_flag);
+ EXPECT_EQ(value.e, A);
+
+ value.e = B;
+ absl::SetFlag(&FLAGS_test_enum_wrapper_flag, value);
+ value = absl::GetFlag(FLAGS_test_enum_wrapper_flag);
+ EXPECT_EQ(value.e, B);
+}
diff --git a/third_party/abseil-cpp/absl/flags/flag_test_defs.cc b/third_party/abseil-cpp/absl/flags/flag_test_defs.cc
index 49f91dee39..4e1693cdb9 100644
--- a/third_party/abseil-cpp/absl/flags/flag_test_defs.cc
+++ b/third_party/abseil-cpp/absl/flags/flag_test_defs.cc
@@ -20,5 +20,5 @@
ABSL_FLAG(int, mistyped_int_flag, 0, "");
ABSL_FLAG(std::string, mistyped_string_flag, "", "");
-ABSL_RETIRED_FLAG(bool, old_bool_flag, true,
- "repetition of retired flag definition");
+ABSL_FLAG(bool, flag_on_separate_file, false, "");
+ABSL_RETIRED_FLAG(bool, retired_flag_on_separate_file, false, "");
diff --git a/third_party/abseil-cpp/absl/flags/internal/commandlineflag.cc b/third_party/abseil-cpp/absl/flags/internal/commandlineflag.cc
new file mode 100644
index 0000000000..4482955c4c
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/commandlineflag.cc
@@ -0,0 +1,26 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/internal/commandlineflag.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+
+FlagStateInterface::~FlagStateInterface() {}
+
+} // namespace flags_internal
+ABSL_NAMESPACE_END
+} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/commandlineflag.h b/third_party/abseil-cpp/absl/flags/internal/commandlineflag.h
index 6363c6615b..ebfe81ba1e 100644
--- a/third_party/abseil-cpp/absl/flags/internal/commandlineflag.h
+++ b/third_party/abseil-cpp/absl/flags/internal/commandlineflag.h
@@ -16,41 +16,19 @@
#ifndef ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_
#define ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_
-#include <stddef.h>
-#include <stdint.h>
-
-#include <memory>
-#include <string>
-#include <typeinfo>
-
#include "absl/base/config.h"
-#include "absl/base/macros.h"
-#include "absl/flags/config.h"
-#include "absl/flags/marshalling.h"
-#include "absl/strings/string_view.h"
-#include "absl/types/optional.h"
+#include "absl/base/internal/fast_type_id.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace flags_internal {
-// An alias for flag static type id. Values of type identify the flag value type
-// simialarly to typeid(T), but without relying on RTTI being available. In most
+// An alias for flag fast type id. This value identifies the flag value type
+// similarly to typeid(T), without relying on RTTI being available. In most
// cases this id is enough to uniquely identify the flag's value type. In a few
// cases we'll have to resort to using actual RTTI implementation if it is
// available.
-using FlagStaticTypeId = void* (*)();
-
-// Address of this function template is used in current implementation as a flag
-// static type id.
-template <typename T>
-void* FlagStaticTypeIdGen() {
-#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
- return const_cast<std::type_info*>(&typeid(T));
-#else
- return nullptr;
-#endif
-}
+using FlagFastTypeId = absl::base_internal::FastTypeIdType;
// Options that control SetCommandLineOptionWithMode.
enum FlagSettingMode {
@@ -65,7 +43,7 @@ enum FlagSettingMode {
SET_FLAGS_DEFAULT
};
-// Options that control SetFromString: Source of a value.
+// Options that control ParseFrom: Source of a value.
enum ValueSource {
// Flag is being set by value specified on a command line.
kCommandLine,
@@ -77,135 +55,12 @@ enum ValueSource {
// of a flag produced this flag state from method CommandLineFlag::SaveState().
class FlagStateInterface {
public:
- virtual ~FlagStateInterface() {}
+ virtual ~FlagStateInterface();
// Restores the flag originated this object to the saved state.
virtual void Restore() const = 0;
};
-// Holds all information for a flag.
-class CommandLineFlag {
- public:
- constexpr CommandLineFlag() = default;
-
- // Not copyable/assignable.
- CommandLineFlag(const CommandLineFlag&) = delete;
- CommandLineFlag& operator=(const CommandLineFlag&) = delete;
-
- // Non-polymorphic access methods.
-
- // Return true iff flag has type T.
- template <typename T>
- inline bool IsOfType() const {
- return TypeId() == &flags_internal::FlagStaticTypeIdGen<T>;
- }
-
- // Attempts to retrieve the flag value. Returns value on success,
- // absl::nullopt otherwise.
- template <typename T>
- absl::optional<T> Get() const {
- if (IsRetired() || !IsOfType<T>()) {
- return absl::nullopt;
- }
-
- // Implementation notes:
- //
- // We are wrapping a union around the value of `T` to serve three purposes:
- //
- // 1. `U.value` has correct size and alignment for a value of type `T`
- // 2. The `U.value` constructor is not invoked since U's constructor does
- // not do it explicitly.
- // 3. The `U.value` destructor is invoked since U's destructor does it
- // explicitly. This makes `U` a kind of RAII wrapper around non default
- // constructible value of T, which is destructed when we leave the
- // scope. We do need to destroy U.value, which is constructed by
- // CommandLineFlag::Read even though we left it in a moved-from state
- // after std::move.
- //
- // All of this serves to avoid requiring `T` being default constructible.
- union U {
- T value;
- U() {}
- ~U() { value.~T(); }
- };
- U u;
-
- Read(&u.value);
- return std::move(u.value);
- }
-
- // Polymorphic access methods
-
- // Returns name of this flag.
- virtual absl::string_view Name() const = 0;
- // Returns name of the file where this flag is defined.
- virtual std::string Filename() const = 0;
- // Returns name of the flag's value type for some built-in types or empty
- // std::string.
- virtual absl::string_view Typename() const = 0;
- // Returns help message associated with this flag.
- virtual std::string Help() const = 0;
- // Returns true iff this object corresponds to retired flag.
- virtual bool IsRetired() const { return false; }
- // Returns true iff this is a handle to an Abseil Flag.
- virtual bool IsAbseilFlag() const { return true; }
- // Returns id of the flag's value type.
- virtual FlagStaticTypeId TypeId() const = 0;
- virtual bool IsModified() const = 0;
- virtual bool IsSpecifiedOnCommandLine() const = 0;
- virtual std::string DefaultValue() 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 std::string `value`. If the flag
- // was successfully set to new value, it returns true. Otherwise, sets `error`
- // to indicate the error, leaves the flag unchanged, and returns false. There
- // are three ways to set the flag's value:
- // * Update the current flag value
- // * Update the flag's default value
- // * Update the current flag value if it was never set before
- // The mode is selected based on `set_mode` parameter.
- virtual bool SetFromString(absl::string_view value,
- flags_internal::FlagSettingMode set_mode,
- flags_internal::ValueSource source,
- std::string* error) = 0;
-
- // Checks that flags default value can be converted to std::string and back to the
- // flag's value type.
- virtual void CheckDefaultValueParsingRoundtrip() const = 0;
-
- protected:
- ~CommandLineFlag() = default;
-
- private:
- // Copy-construct a new value of the flag's type in a memory referenced by
- // the dst based on the current flag's value.
- virtual void Read(void* dst) const = 0;
-};
-
-// This macro is the "source of truth" for the list of supported flag built-in
-// types.
-#define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \
- A(bool) \
- A(short) \
- A(unsigned short) \
- A(int) \
- A(unsigned int) \
- A(long) \
- A(unsigned long) \
- A(long long) \
- A(unsigned long long) \
- A(double) \
- A(float) \
- A(std::string) \
- A(std::vector<std::string>)
-
} // namespace flags_internal
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/commandlineflag_test.cc b/third_party/abseil-cpp/absl/flags/internal/commandlineflag_test.cc
deleted file mode 100644
index 0e8bc3133b..0000000000
--- a/third_party/abseil-cpp/absl/flags/internal/commandlineflag_test.cc
+++ /dev/null
@@ -1,219 +0,0 @@
-//
-// Copyright 2019 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "absl/flags/internal/commandlineflag.h"
-
-#include <memory>
-#include <string>
-
-#include "gtest/gtest.h"
-#include "absl/flags/flag.h"
-#include "absl/flags/internal/registry.h"
-#include "absl/flags/usage_config.h"
-#include "absl/memory/memory.h"
-#include "absl/strings/match.h"
-#include "absl/strings/str_cat.h"
-#include "absl/strings/string_view.h"
-
-ABSL_FLAG(int, int_flag, 201, "int_flag help");
-ABSL_FLAG(std::string, string_flag, "dflt",
- absl::StrCat("string_flag", " help"));
-ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
-
-namespace {
-
-namespace flags = absl::flags_internal;
-
-class CommandLineFlagTest : public testing::Test {
- protected:
- static void SetUpTestSuite() {
- // Install a function to normalize filenames before this test is run.
- absl::FlagsUsageConfig default_config;
- default_config.normalize_filename = &CommandLineFlagTest::NormalizeFileName;
- absl::SetFlagsUsageConfig(default_config);
- }
-
- void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
- void TearDown() override { flag_saver_.reset(); }
-
- private:
- static std::string NormalizeFileName(absl::string_view fname) {
-#ifdef _WIN32
- std::string normalized(fname);
- std::replace(normalized.begin(), normalized.end(), '\\', '/');
- fname = normalized;
-#endif
- return std::string(fname);
- }
-
- std::unique_ptr<flags::FlagSaver> flag_saver_;
-};
-
-TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
- auto* flag_01 = flags::FindCommandLineFlag("int_flag");
-
- ASSERT_TRUE(flag_01);
- EXPECT_EQ(flag_01->Name(), "int_flag");
- EXPECT_EQ(flag_01->Help(), "int_flag help");
- EXPECT_EQ(flag_01->Typename(), "");
- EXPECT_TRUE(!flag_01->IsRetired());
- EXPECT_TRUE(flag_01->IsOfType<int>());
- EXPECT_TRUE(
- absl::EndsWith(flag_01->Filename(),
- "absl/flags/internal/commandlineflag_test.cc"))
- << flag_01->Filename();
-
- auto* flag_02 = flags::FindCommandLineFlag("string_flag");
-
- ASSERT_TRUE(flag_02);
- EXPECT_EQ(flag_02->Name(), "string_flag");
- EXPECT_EQ(flag_02->Help(), "string_flag help");
- EXPECT_EQ(flag_02->Typename(), "");
- EXPECT_TRUE(!flag_02->IsRetired());
- EXPECT_TRUE(flag_02->IsOfType<std::string>());
- EXPECT_TRUE(
- absl::EndsWith(flag_02->Filename(),
- "absl/flags/internal/commandlineflag_test.cc"))
- << flag_02->Filename();
-
- auto* flag_03 = flags::FindRetiredFlag("bool_retired_flag");
-
- ASSERT_TRUE(flag_03);
- EXPECT_EQ(flag_03->Name(), "bool_retired_flag");
- EXPECT_EQ(flag_03->Help(), "");
- EXPECT_EQ(flag_03->Typename(), "");
- EXPECT_TRUE(flag_03->IsRetired());
- EXPECT_TRUE(flag_03->IsOfType<bool>());
- EXPECT_EQ(flag_03->Filename(), "RETIRED");
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
- absl::SetFlag(&FLAGS_int_flag, 301);
- auto* flag_01 = flags::FindCommandLineFlag("int_flag");
-
- ASSERT_TRUE(flag_01);
- EXPECT_EQ(flag_01->CurrentValue(), "301");
- EXPECT_EQ(flag_01->DefaultValue(), "201");
-
- absl::SetFlag(&FLAGS_string_flag, "new_str_value");
- auto* flag_02 = flags::FindCommandLineFlag("string_flag");
-
- ASSERT_TRUE(flag_02);
- EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
- EXPECT_EQ(flag_02->DefaultValue(), "dflt");
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(CommandLineFlagTest, TestSetFromStringCurrentValue) {
- std::string err;
-
- auto* flag_01 = flags::FindCommandLineFlag("int_flag");
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(flag_01->SetFromString("11", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(flag_01->SetFromString("-123", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(!flag_01->SetFromString("xyz", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
- EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(!flag_01->SetFromString("A1", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
- EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(flag_01->SetFromString("0x10", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
- EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(flag_01->SetFromString("011", flags::SET_FLAGS_VALUE,
- flags::kCommandLine, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
- EXPECT_TRUE(flag_01->IsSpecifiedOnCommandLine());
-
- EXPECT_TRUE(!flag_01->SetFromString("", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
-
- auto* flag_02 = flags::FindCommandLineFlag("string_flag");
- EXPECT_TRUE(flag_02->SetFromString("xyz", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
-
- EXPECT_TRUE(flag_02->SetFromString("", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(CommandLineFlagTest, TestSetFromStringDefaultValue) {
- std::string err;
-
- auto* flag_01 = flags::FindCommandLineFlag("int_flag");
-
- EXPECT_TRUE(flag_01->SetFromString("111", flags::SET_FLAGS_DEFAULT,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(flag_01->DefaultValue(), "111");
-
- auto* flag_02 = flags::FindCommandLineFlag("string_flag");
-
- EXPECT_TRUE(flag_02->SetFromString("abc", flags::SET_FLAGS_DEFAULT,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(flag_02->DefaultValue(), "abc");
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(CommandLineFlagTest, TestSetFromStringIfDefault) {
- std::string err;
-
- auto* flag_01 = flags::FindCommandLineFlag("int_flag");
-
- EXPECT_TRUE(flag_01->SetFromString("22", flags::SET_FLAG_IF_DEFAULT,
- flags::kProgrammaticChange, &err))
- << err;
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
-
- EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
- // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
-
- // Reset back to default value
- EXPECT_TRUE(flag_01->SetFromString("201", flags::SET_FLAGS_VALUE,
- flags::kProgrammaticChange, &err));
-
- EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
- flags::kProgrammaticChange, &err));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
- // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
-}
-
-} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/internal/flag.cc b/third_party/abseil-cpp/absl/flags/internal/flag.cc
index 5a921e28d7..1515022d11 100644
--- a/third_party/abseil-cpp/absl/flags/internal/flag.cc
+++ b/third_party/abseil-cpp/absl/flags/internal/flag.cc
@@ -15,21 +15,26 @@
#include "absl/flags/internal/flag.h"
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
+#include <array>
#include <atomic>
#include <memory>
+#include <new>
#include <string>
-#include <vector>
+#include <typeinfo>
-#include "absl/base/attributes.h"
+#include "absl/base/call_once.h"
+#include "absl/base/casts.h"
#include "absl/base/config.h"
-#include "absl/base/const_init.h"
#include "absl/base/optimization.h"
+#include "absl/flags/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/usage_config.h"
+#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
@@ -47,10 +52,10 @@ const char kStrippedFlagHelp[] = "\001\002\003\004 (unknown) \004\003\002\001";
namespace {
// Currently we only validate flag values for user-defined flag types.
-bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
-#define DONT_VALIDATE(T) \
- if (flag_type_id == &FlagStaticTypeIdGen<T>) return false;
- ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE)
+bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) {
+#define DONT_VALIDATE(T, _) \
+ if (flag_type_id == base_internal::FastTypeId<T>()) return false;
+ ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(DONT_VALIDATE)
#undef DONT_VALIDATE
return true;
@@ -62,55 +67,146 @@ bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
// need to acquire these locks themselves.
class MutexRelock {
public:
- explicit MutexRelock(absl::Mutex* mu) : mu_(mu) { mu_->Unlock(); }
- ~MutexRelock() { mu_->Lock(); }
+ explicit MutexRelock(absl::Mutex& mu) : mu_(mu) { mu_.Unlock(); }
+ ~MutexRelock() { mu_.Lock(); }
MutexRelock(const MutexRelock&) = delete;
MutexRelock& operator=(const MutexRelock&) = delete;
private:
- absl::Mutex* mu_;
+ absl::Mutex& mu_;
};
} // namespace
+///////////////////////////////////////////////////////////////////////////////
+// Persistent state of the flag data.
+
+class FlagImpl;
+
+class FlagState : public flags_internal::FlagStateInterface {
+ public:
+ template <typename V>
+ FlagState(FlagImpl& flag_impl, const V& v, bool modified,
+ bool on_command_line, int64_t counter)
+ : flag_impl_(flag_impl),
+ value_(v),
+ modified_(modified),
+ on_command_line_(on_command_line),
+ counter_(counter) {}
+
+ ~FlagState() override {
+ if (flag_impl_.ValueStorageKind() != FlagValueStorageKind::kAlignedBuffer &&
+ flag_impl_.ValueStorageKind() != FlagValueStorageKind::kSequenceLocked)
+ return;
+ flags_internal::Delete(flag_impl_.op_, value_.heap_allocated);
+ }
+
+ private:
+ friend class FlagImpl;
+
+ // Restores the flag to the saved state.
+ void Restore() const override {
+ if (!flag_impl_.RestoreState(*this)) return;
+
+ ABSL_INTERNAL_LOG(INFO,
+ absl::StrCat("Restore saved value of ", flag_impl_.Name(),
+ " to: ", flag_impl_.CurrentValue()));
+ }
+
+ // Flag and saved flag data.
+ FlagImpl& flag_impl_;
+ union SavedValue {
+ explicit SavedValue(void* v) : heap_allocated(v) {}
+ explicit SavedValue(int64_t v) : one_word(v) {}
+
+ void* heap_allocated;
+ int64_t one_word;
+ } value_;
+ bool modified_;
+ bool on_command_line_;
+ int64_t counter_;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// Flag implementation, which does not depend on flag value type.
+
+DynValueDeleter::DynValueDeleter(FlagOpFn op_arg) : op(op_arg) {}
+
+void DynValueDeleter::operator()(void* ptr) const {
+ if (op == nullptr) return;
+
+ Delete(op, ptr);
+}
+
void FlagImpl::Init() {
new (&data_guard_) absl::Mutex;
- absl::MutexLock lock(reinterpret_cast<absl::Mutex*>(&data_guard_));
+ auto def_kind = static_cast<FlagDefaultKind>(def_kind_);
- value_.dynamic = MakeInitValue().release();
- StoreAtomic();
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic: {
+ alignas(int64_t) std::array<char, sizeof(int64_t)> buf{};
+ if (def_kind == FlagDefaultKind::kGenFunc) {
+ (*default_value_.gen_func)(buf.data());
+ } else {
+ assert(def_kind != FlagDefaultKind::kDynamicValue);
+ std::memcpy(buf.data(), &default_value_, Sizeof(op_));
+ }
+ if (ValueStorageKind() == FlagValueStorageKind::kValueAndInitBit) {
+ // We presume here the memory layout of FlagValueAndInitBit struct.
+ uint8_t initialized = 1;
+ std::memcpy(buf.data() + Sizeof(op_), &initialized,
+ sizeof(initialized));
+ }
+ OneWordValue().store(absl::bit_cast<int64_t>(buf),
+ std::memory_order_release);
+ break;
+ }
+ case FlagValueStorageKind::kSequenceLocked: {
+ // For this storage kind the default_value_ always points to gen_func
+ // during initialization.
+ assert(def_kind == FlagDefaultKind::kGenFunc);
+ (*default_value_.gen_func)(AtomicBufferValue());
+ break;
+ }
+ 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());
+ break;
+ }
+ seq_lock_.MarkInitialized();
}
-// Ensures that the lazily initialized data is initialized,
-// and returns pointer to the mutex guarding flags data.
absl::Mutex* FlagImpl::DataGuard() const {
absl::call_once(const_cast<FlagImpl*>(this)->init_control_, &FlagImpl::Init,
const_cast<FlagImpl*>(this));
- // data_guard_ is initialized.
+ // data_guard_ is initialized inside Init.
return reinterpret_cast<absl::Mutex*>(&data_guard_);
}
-void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
- FlagStaticTypeId this_type_id = flags_internal::StaticTypeId(op_);
+void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id,
+ const std::type_info* (*gen_rtti)()) const {
+ FlagFastTypeId lhs_type_id = flags_internal::FastTypeId(op_);
- // `type_id` is the type id corresponding to the declaration visibile at the
- // call site. `this_type_id` is the type id corresponding to the type stored
- // during flag definition. They must match for this operation to be
- // well-defined.
- if (ABSL_PREDICT_TRUE(type_id == this_type_id)) return;
+ // `rhs_type_id` is the fast type id corresponding to the declaration
+ // visibile at the call site. `lhs_type_id` is the fast type id
+ // corresponding to the type specified in flag definition. They must match
+ // for this operation to be well-defined.
+ if (ABSL_PREDICT_TRUE(lhs_type_id == rhs_type_id)) return;
- void* lhs_runtime_type_id = type_id();
- void* rhs_runtime_type_id = this_type_id();
+ const std::type_info* lhs_runtime_type_id =
+ flags_internal::RuntimeTypeId(op_);
+ const std::type_info* rhs_runtime_type_id = (*gen_rtti)();
if (lhs_runtime_type_id == rhs_runtime_type_id) return;
#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
- if (*reinterpret_cast<std::type_info*>(lhs_runtime_type_id) ==
- *reinterpret_cast<std::type_info*>(rhs_runtime_type_id))
- return;
+ if (*lhs_runtime_type_id == *rhs_runtime_type_id) return;
#endif
ABSL_INTERNAL_LOG(
@@ -120,19 +216,42 @@ void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
void* res = nullptr;
- if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
- res = flags_internal::Clone(op_, default_value_.dynamic_value);
- } else {
- res = (*default_value_.gen_func)();
+ switch (DefaultKind()) {
+ case FlagDefaultKind::kDynamicValue:
+ res = flags_internal::Clone(op_, default_value_.dynamic_value);
+ break;
+ case FlagDefaultKind::kGenFunc:
+ res = flags_internal::Alloc(op_);
+ (*default_value_.gen_func)(res);
+ break;
+ default:
+ res = flags_internal::Clone(op_, &default_value_);
+ break;
}
return {res, DynValueDeleter{op_}};
}
void FlagImpl::StoreValue(const void* src) {
- flags_internal::Copy(op_, src, value_.dynamic);
- StoreAtomic();
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic: {
+ // Load the current value to avoid setting 'init' bit manualy.
+ int64_t one_word_val = OneWordValue().load(std::memory_order_acquire);
+ std::memcpy(&one_word_val, src, Sizeof(op_));
+ OneWordValue().store(one_word_val, std::memory_order_release);
+ seq_lock_.IncrementModificationCount();
+ break;
+ }
+ case FlagValueStorageKind::kSequenceLocked: {
+ seq_lock_.Write(AtomicBufferValue(), src, Sizeof(op_));
+ break;
+ }
+ case FlagValueStorageKind::kAlignedBuffer:
+ Copy(op_, src, AlignedBufferValue());
+ seq_lock_.IncrementModificationCount();
+ break;
+ }
modified_ = true;
- ++counter_;
InvokeCallback();
}
@@ -147,9 +266,12 @@ std::string FlagImpl::Help() const {
: help_.gen_func();
}
-bool FlagImpl::IsModified() const {
- absl::MutexLock l(DataGuard());
- return modified_;
+FlagFastTypeId FlagImpl::TypeId() const {
+ return flags_internal::FastTypeId(op_);
+}
+
+int64_t FlagImpl::ModificationCount() const {
+ return seq_lock_.ModificationCount();
}
bool FlagImpl::IsSpecifiedOnCommandLine() const {
@@ -165,9 +287,28 @@ std::string FlagImpl::DefaultValue() const {
}
std::string FlagImpl::CurrentValue() const {
- absl::MutexLock l(DataGuard());
+ auto* guard = DataGuard(); // Make sure flag initialized
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic: {
+ const auto one_word_val =
+ absl::bit_cast<std::array<char, sizeof(int64_t)>>(
+ OneWordValue().load(std::memory_order_acquire));
+ return flags_internal::Unparse(op_, one_word_val.data());
+ }
+ case FlagValueStorageKind::kSequenceLocked: {
+ std::unique_ptr<void, DynValueDeleter> cloned(flags_internal::Alloc(op_),
+ DynValueDeleter{op_});
+ ReadSequenceLockedData(cloned.get());
+ return flags_internal::Unparse(op_, cloned.get());
+ }
+ case FlagValueStorageKind::kAlignedBuffer: {
+ absl::MutexLock l(guard);
+ return flags_internal::Unparse(op_, AlignedBufferValue());
+ }
+ }
- return flags_internal::Unparse(op_, value_.dynamic);
+ return "";
}
void FlagImpl::SetCallback(const FlagCallbackFunc mutation_callback) {
@@ -199,44 +340,103 @@ void FlagImpl::InvokeCallback() const {
// and it also can be different by the time the callback invocation is
// completed. Requires that *primary_lock be held in exclusive mode; it may be
// released and reacquired by the implementation.
- MutexRelock relock(DataGuard());
+ MutexRelock relock(*DataGuard());
absl::MutexLock lock(&callback_->guard);
cb();
}
-bool FlagImpl::RestoreState(const void* value, bool modified,
- bool on_command_line, int64_t counter) {
- {
- absl::MutexLock l(DataGuard());
+std::unique_ptr<FlagStateInterface> FlagImpl::SaveState() {
+ absl::MutexLock l(DataGuard());
- if (counter_ == counter) return false;
+ bool modified = modified_;
+ bool on_command_line = on_command_line_;
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic: {
+ return absl::make_unique<FlagState>(
+ *this, OneWordValue().load(std::memory_order_acquire), modified,
+ on_command_line, ModificationCount());
+ }
+ case FlagValueStorageKind::kSequenceLocked: {
+ void* cloned = flags_internal::Alloc(op_);
+ // Read is guaranteed to be successful because we hold the lock.
+ bool success =
+ seq_lock_.TryRead(cloned, AtomicBufferValue(), Sizeof(op_));
+ assert(success);
+ static_cast<void>(success);
+ return absl::make_unique<FlagState>(*this, cloned, modified,
+ on_command_line, ModificationCount());
+ }
+ case FlagValueStorageKind::kAlignedBuffer: {
+ return absl::make_unique<FlagState>(
+ *this, flags_internal::Clone(op_, AlignedBufferValue()), modified,
+ on_command_line, ModificationCount());
+ }
}
+ return nullptr;
+}
- Write(value);
-
- {
- absl::MutexLock l(DataGuard());
+bool FlagImpl::RestoreState(const FlagState& flag_state) {
+ absl::MutexLock l(DataGuard());
+ if (flag_state.counter_ == ModificationCount()) {
+ return false;
+ }
- modified_ = modified;
- on_command_line_ = on_command_line;
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic:
+ StoreValue(&flag_state.value_.one_word);
+ break;
+ case FlagValueStorageKind::kSequenceLocked:
+ case FlagValueStorageKind::kAlignedBuffer:
+ StoreValue(flag_state.value_.heap_allocated);
+ break;
}
+ modified_ = flag_state.modified_;
+ on_command_line_ = flag_state.on_command_line_;
+
return true;
}
+template <typename StorageT>
+StorageT* FlagImpl::OffsetValue() const {
+ char* p = reinterpret_cast<char*>(const_cast<FlagImpl*>(this));
+ // The offset is deduced via Flag value type specific op_.
+ size_t offset = flags_internal::ValueOffset(op_);
+
+ return reinterpret_cast<StorageT*>(p + offset);
+}
+
+void* FlagImpl::AlignedBufferValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kAlignedBuffer);
+ return OffsetValue<void>();
+}
+
+std::atomic<uint64_t>* FlagImpl::AtomicBufferValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kSequenceLocked);
+ return OffsetValue<std::atomic<uint64_t>>();
+}
+
+std::atomic<int64_t>& FlagImpl::OneWordValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kOneWordAtomic ||
+ ValueStorageKind() == FlagValueStorageKind::kValueAndInitBit);
+ return OffsetValue<FlagOneWordValue>()->value;
+}
+
// Attempts to parse supplied `value` string using parsing routine in the `flag`
// argument. If parsing successful, this function replaces the dst with newly
// parsed value. In case if any error is encountered in either step, the error
// message is stored in 'err'
std::unique_ptr<void, DynValueDeleter> FlagImpl::TryParse(
- absl::string_view value, std::string* err) const {
+ absl::string_view value, std::string& err) const {
std::unique_ptr<void, DynValueDeleter> tentative_value = MakeInitValue();
std::string parse_err;
if (!flags_internal::Parse(op_, value, tentative_value.get(), &parse_err)) {
absl::string_view err_sep = parse_err.empty() ? "" : "; ";
- *err = absl::StrCat("Illegal value '", value, "' specified for flag '",
- Name(), "'", err_sep, parse_err);
+ err = absl::StrCat("Illegal value '", value, "' specified for flag '",
+ Name(), "'", err_sep, parse_err);
return nullptr;
}
@@ -244,32 +444,62 @@ std::unique_ptr<void, DynValueDeleter> FlagImpl::TryParse(
}
void FlagImpl::Read(void* dst) const {
- absl::ReaderMutexLock l(DataGuard());
+ auto* guard = DataGuard(); // Make sure flag initialized
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kValueAndInitBit:
+ case FlagValueStorageKind::kOneWordAtomic: {
+ const int64_t one_word_val =
+ OneWordValue().load(std::memory_order_acquire);
+ std::memcpy(dst, &one_word_val, Sizeof(op_));
+ break;
+ }
+ case FlagValueStorageKind::kSequenceLocked: {
+ ReadSequenceLockedData(dst);
+ break;
+ }
+ case FlagValueStorageKind::kAlignedBuffer: {
+ absl::MutexLock l(guard);
+ flags_internal::CopyConstruct(op_, AlignedBufferValue(), dst);
+ break;
+ }
+ }
+}
- flags_internal::CopyConstruct(op_, value_.dynamic, dst);
+int64_t FlagImpl::ReadOneWord() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kOneWordAtomic ||
+ ValueStorageKind() == FlagValueStorageKind::kValueAndInitBit);
+ auto* guard = DataGuard(); // Make sure flag initialized
+ (void)guard;
+ return OneWordValue().load(std::memory_order_acquire);
}
-void FlagImpl::StoreAtomic() {
- size_t data_size = flags_internal::Sizeof(op_);
+bool FlagImpl::ReadOneBool() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kValueAndInitBit);
+ auto* guard = DataGuard(); // Make sure flag initialized
+ (void)guard;
+ return absl::bit_cast<FlagValueAndInitBit<bool>>(
+ OneWordValue().load(std::memory_order_acquire))
+ .value;
+}
- if (data_size <= sizeof(int64_t)) {
- int64_t t = 0;
- std::memcpy(&t, value_.dynamic, data_size);
- value_.atomics.small_atomic.store(t, std::memory_order_release);
- }
-#if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD)
- else if (data_size <= sizeof(FlagsInternalTwoWordsType)) {
- FlagsInternalTwoWordsType t{0, 0};
- std::memcpy(&t, value_.dynamic, data_size);
- value_.atomics.big_atomic.store(t, std::memory_order_release);
+void FlagImpl::ReadSequenceLockedData(void* dst) const {
+ int size = Sizeof(op_);
+ // Attempt to read using the sequence lock.
+ if (ABSL_PREDICT_TRUE(seq_lock_.TryRead(dst, AtomicBufferValue(), size))) {
+ return;
}
-#endif
+ // We failed due to contention. Acquire the lock to prevent contention
+ // and try again.
+ absl::ReaderMutexLock l(DataGuard());
+ bool success = seq_lock_.TryRead(dst, AtomicBufferValue(), size);
+ assert(success);
+ static_cast<void>(success);
}
void FlagImpl::Write(const void* src) {
absl::MutexLock l(DataGuard());
- if (ShouldValidateFlagValue(flags_internal::StaticTypeId(op_))) {
+ if (ShouldValidateFlagValue(flags_internal::FastTypeId(op_))) {
std::unique_ptr<void, DynValueDeleter> obj{flags_internal::Clone(op_, src),
DynValueDeleter{op_}};
std::string ignored_error;
@@ -291,8 +521,8 @@ void FlagImpl::Write(const void* src) {
// * Update the flag's default value
// * Update the current flag value if it was never set before
// The mode is selected based on 'set_mode' parameter.
-bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode,
- ValueSource source, std::string* err) {
+bool FlagImpl::ParseFrom(absl::string_view value, FlagSettingMode set_mode,
+ ValueSource source, std::string& err) {
absl::MutexLock l(DataGuard());
switch (set_mode) {
@@ -339,7 +569,7 @@ bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode,
}
if (!modified_) {
- // Need to set both default value *and* current, in this case
+ // Need to set both default value *and* current, in this case.
StoreValue(default_value_.dynamic_value);
modified_ = false;
}
@@ -361,7 +591,7 @@ void FlagImpl::CheckDefaultValueParsingRoundtrip() const {
ABSL_INTERNAL_LOG(
FATAL,
absl::StrCat("Flag ", Name(), " (from ", Filename(),
- "): std::string form of default value '", v,
+ "): string form of default value '", v,
"' could not be parsed; error=", error));
}
diff --git a/third_party/abseil-cpp/absl/flags/internal/flag.h b/third_party/abseil-cpp/absl/flags/internal/flag.h
index 35a148cf66..124a2f1c03 100644
--- a/third_party/abseil-cpp/absl/flags/internal/flag.h
+++ b/third_party/abseil-cpp/absl/flags/internal/flag.h
@@ -16,94 +16,95 @@
#ifndef ABSL_FLAGS_INTERNAL_FLAG_H_
#define ABSL_FLAGS_INTERNAL_FLAG_H_
+#include <stddef.h>
#include <stdint.h>
#include <atomic>
#include <cstring>
#include <memory>
+#include <new>
#include <string>
#include <type_traits>
+#include <typeinfo>
+#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
+#include "absl/base/casts.h"
#include "absl/base/config.h"
+#include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h"
+#include "absl/flags/commandlineflag.h"
#include "absl/flags/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h"
-#include "absl/memory/memory.h"
-#include "absl/strings/str_cat.h"
+#include "absl/flags/internal/sequence_lock.h"
+#include "absl/flags/marshalling.h"
+#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
+#include "absl/utility/utility.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
+
+///////////////////////////////////////////////////////////////////////////////
+// Forward declaration of absl::Flag<T> public API.
namespace flags_internal {
+template <typename T>
+class Flag;
+} // namespace flags_internal
+#if defined(_MSC_VER) && !defined(__clang__)
template <typename T>
class Flag;
+#else
+template <typename T>
+using Flag = flags_internal::Flag<T>;
+#endif
+
+template <typename T>
+ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
+
+template <typename T>
+void SetFlag(absl::Flag<T>* flag, const T& v);
+
+template <typename T, typename V>
+void SetFlag(absl::Flag<T>* flag, const V& v);
+
+template <typename U>
+const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<U>& f);
///////////////////////////////////////////////////////////////////////////////
// Flag value type operations, eg., parsing, copying, etc. are provided
// by function specific to that type with a signature matching FlagOpFn.
+namespace flags_internal {
+
enum class FlagOp {
+ kAlloc,
kDelete,
- kClone,
kCopy,
kCopyConstruct,
kSizeof,
- kStaticTypeId,
+ kFastTypeId,
+ kRuntimeTypeId,
kParse,
kUnparse,
+ kValueOffset,
};
using FlagOpFn = void* (*)(FlagOp, const void*, void*, void*);
-// Flag value specific operations routine.
+// Forward declaration for Flag value specific operations.
template <typename T>
-void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
- switch (op) {
- case FlagOp::kDelete:
- delete static_cast<const T*>(v1);
- return nullptr;
- case FlagOp::kClone:
- return new T(*static_cast<const T*>(v1));
- case FlagOp::kCopy:
- *static_cast<T*>(v2) = *static_cast<const T*>(v1);
- return nullptr;
- case FlagOp::kCopyConstruct:
- new (v2) T(*static_cast<const T*>(v1));
- return nullptr;
- case FlagOp::kSizeof:
- return reinterpret_cast<void*>(sizeof(T));
- case FlagOp::kStaticTypeId:
- return reinterpret_cast<void*>(&FlagStaticTypeIdGen<T>);
- case FlagOp::kParse: {
- // Initialize the temporary instance of type T based on current value in
- // destination (which is going to be flag's default value).
- T temp(*static_cast<T*>(v2));
- if (!absl::ParseFlag<T>(*static_cast<const absl::string_view*>(v1), &temp,
- static_cast<std::string*>(v3))) {
- return nullptr;
- }
- *static_cast<T*>(v2) = std::move(temp);
- return v2;
- }
- case FlagOp::kUnparse:
- *static_cast<std::string*>(v2) =
- absl::UnparseFlag<T>(*static_cast<const T*>(v1));
- return nullptr;
- default:
- return nullptr;
- }
-}
+void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3);
-// Deletes memory interpreting obj as flag value type pointer.
-inline void Delete(FlagOpFn op, const void* obj) {
- op(FlagOp::kDelete, obj, nullptr, nullptr);
+// Allocate aligned memory for a flag value.
+inline void* Alloc(FlagOpFn op) {
+ return op(FlagOp::kAlloc, nullptr, nullptr, nullptr);
}
-// Makes a copy of flag value pointed by obj.
-inline void* Clone(FlagOpFn op, const void* obj) {
- return op(FlagOp::kClone, obj, nullptr, nullptr);
+// Deletes memory interpreting obj as flag value type pointer.
+inline void Delete(FlagOpFn op, void* obj) {
+ op(FlagOp::kDelete, nullptr, obj, nullptr);
}
// Copies src to dst interpreting as flag value type pointers.
inline void Copy(FlagOpFn op, const void* src, void* dst) {
@@ -114,6 +115,12 @@ inline void Copy(FlagOpFn op, const void* src, void* dst) {
inline void CopyConstruct(FlagOpFn op, const void* src, void* dst) {
op(FlagOp::kCopyConstruct, src, dst, nullptr);
}
+// Makes a copy of flag value pointed by obj.
+inline void* Clone(FlagOpFn op, const void* obj) {
+ void* res = flags_internal::Alloc(op);
+ flags_internal::CopyConstruct(op, obj, res);
+ return res;
+}
// Returns true if parsing of input text is successfull.
inline bool Parse(FlagOpFn op, absl::string_view text, void* dst,
std::string* error) {
@@ -132,41 +139,36 @@ inline size_t Sizeof(FlagOpFn op) {
return static_cast<size_t>(reinterpret_cast<intptr_t>(
op(FlagOp::kSizeof, nullptr, nullptr, nullptr)));
}
-// Returns static type id coresponding to the value type.
-inline FlagStaticTypeId StaticTypeId(FlagOpFn op) {
- return reinterpret_cast<FlagStaticTypeId>(
- op(FlagOp::kStaticTypeId, nullptr, nullptr, nullptr));
+// Returns fast type id coresponding to the value type.
+inline FlagFastTypeId FastTypeId(FlagOpFn op) {
+ return reinterpret_cast<FlagFastTypeId>(
+ op(FlagOp::kFastTypeId, nullptr, nullptr, nullptr));
+}
+// Returns fast type id coresponding to the value type.
+inline const std::type_info* RuntimeTypeId(FlagOpFn op) {
+ return reinterpret_cast<const std::type_info*>(
+ op(FlagOp::kRuntimeTypeId, nullptr, nullptr, nullptr));
+}
+// Returns offset of the field value_ from the field impl_ inside of
+// absl::Flag<T> data. Given FlagImpl pointer p you can get the
+// location of the corresponding value as:
+// reinterpret_cast<char*>(p) + ValueOffset().
+inline ptrdiff_t ValueOffset(FlagOpFn op) {
+ // This sequence of casts reverses the sequence from
+ // `flags_internal::FlagOps()`
+ return static_cast<ptrdiff_t>(reinterpret_cast<intptr_t>(
+ op(FlagOp::kValueOffset, nullptr, nullptr, nullptr)));
}
-///////////////////////////////////////////////////////////////////////////////
-// Persistent state of the flag data.
-
+// Returns an address of RTTI's typeid(T).
template <typename T>
-class FlagState : public flags_internal::FlagStateInterface {
- public:
- FlagState(Flag<T>* flag, T&& cur, bool modified, bool on_command_line,
- int64_t counter)
- : flag_(flag),
- cur_value_(std::move(cur)),
- modified_(modified),
- on_command_line_(on_command_line),
- counter_(counter) {}
-
- ~FlagState() override = default;
-
- private:
- friend class Flag<T>;
-
- // Restores the flag to the saved state.
- void Restore() const override;
-
- // Flag and saved flag data.
- Flag<T>* flag_;
- T cur_value_;
- bool modified_;
- bool on_command_line_;
- int64_t counter_;
-};
+inline const std::type_info* GenRuntimeTypeId() {
+#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
+ return &typeid(T);
+#else
+ return nullptr;
+#endif
+}
///////////////////////////////////////////////////////////////////////////////
// Flag help auxiliary structs.
@@ -176,6 +178,28 @@ class FlagState : public flags_internal::FlagStateInterface {
// cases.
using HelpGenFunc = std::string (*)();
+template <size_t N>
+struct FixedCharArray {
+ char value[N];
+
+ template <size_t... I>
+ static constexpr FixedCharArray<N> FromLiteralString(
+ absl::string_view str, absl::index_sequence<I...>) {
+ return (void)str, FixedCharArray<N>({{str[I]..., '\0'}});
+ }
+};
+
+template <typename Gen, size_t N = Gen::Value().size()>
+constexpr FixedCharArray<N + 1> HelpStringAsArray(int) {
+ return FixedCharArray<N + 1>::FromLiteralString(
+ Gen::Value(), absl::make_index_sequence<N>{});
+}
+
+template <typename Gen>
+constexpr std::false_type HelpStringAsArray(char) {
+ return std::false_type{};
+}
+
union FlagHelpMsg {
constexpr explicit FlagHelpMsg(const char* help_msg) : literal(help_msg) {}
constexpr explicit FlagHelpMsg(HelpGenFunc help_gen) : gen_func(help_gen) {}
@@ -193,40 +217,28 @@ struct FlagHelpArg {
extern const char kStrippedFlagHelp[];
-// HelpConstexprWrap is used by struct AbslFlagHelpGenFor##name generated by
-// ABSL_FLAG macro. It is only used to silence the compiler in the case where
-// help message expression is not constexpr and does not have type const char*.
-// If help message expression is indeed constexpr const char* HelpConstexprWrap
-// is just a trivial identity function.
-template <typename T>
-const char* HelpConstexprWrap(const T&) {
- return nullptr;
-}
-constexpr const char* HelpConstexprWrap(const char* p) { return p; }
-constexpr const char* HelpConstexprWrap(char* p) { return p; }
-
// These two HelpArg overloads allows us to select at compile time one of two
// way to pass Help argument to absl::Flag. We'll be passing
-// AbslFlagHelpGenFor##name as T and integer 0 as a single argument to prefer
-// first overload if possible. If T::Const is evaluatable on constexpr
-// context (see non template int parameter below) we'll choose first overload.
-// In this case the help message expression is immediately evaluated and is used
-// to construct the absl::Flag. No additionl code is generated by ABSL_FLAG.
-// Otherwise SFINAE kicks in and first overload is dropped from the
+// AbslFlagHelpGenFor##name as Gen and integer 0 as a single argument to prefer
+// first overload if possible. If help message is evaluatable on constexpr
+// context We'll be able to make FixedCharArray out of it and we'll choose first
+// overload. In this case the help message expression is immediately evaluated
+// and is used to construct the absl::Flag. No additionl code is generated by
+// ABSL_FLAG Otherwise SFINAE kicks in and first overload is dropped from the
// consideration, in which case the second overload will be used. The second
// overload does not attempt to evaluate the help message expression
// immediately and instead delays the evaluation by returing the function
// pointer (&T::NonConst) genering the help message when necessary. This is
// evaluatable in constexpr context, but the cost is an extra function being
// generated in the ABSL_FLAG code.
-template <typename T, int = (T::Const(), 1)>
-constexpr FlagHelpArg HelpArg(int) {
- return {FlagHelpMsg(T::Const()), FlagHelpKind::kLiteral};
+template <typename Gen, size_t N>
+constexpr FlagHelpArg HelpArg(const FixedCharArray<N>& value) {
+ return {FlagHelpMsg(value.value), FlagHelpKind::kLiteral};
}
-template <typename T>
-constexpr FlagHelpArg HelpArg(char) {
- return {FlagHelpMsg(&T::NonConst), FlagHelpKind::kGenFunc};
+template <typename Gen>
+constexpr FlagHelpArg HelpArg(std::false_type) {
+ return {FlagHelpMsg(&Gen::NonConst), FlagHelpKind::kGenFunc};
}
///////////////////////////////////////////////////////////////////////////////
@@ -234,110 +246,159 @@ constexpr FlagHelpArg HelpArg(char) {
// Signature for the function generating the initial flag value (usually
// based on default value supplied in flag's definition)
-using FlagDfltGenFunc = void* (*)();
+using FlagDfltGenFunc = void (*)(void*);
union FlagDefaultSrc {
constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)
: gen_func(gen_func_arg) {}
+#define ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE(T, name) \
+ T name##_value; \
+ constexpr explicit FlagDefaultSrc(T value) : name##_value(value) {} // NOLINT
+ ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE)
+#undef ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE
+
void* dynamic_value;
FlagDfltGenFunc gen_func;
};
-enum class FlagDefaultKind : uint8_t { kDynamicValue = 0, kGenFunc = 1 };
+enum class FlagDefaultKind : uint8_t {
+ kDynamicValue = 0,
+ kGenFunc = 1,
+ kOneWord = 2 // for default values UP to one word in size
+};
+
+struct FlagDefaultArg {
+ FlagDefaultSrc source;
+ FlagDefaultKind kind;
+};
+
+// 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 =
+ ((void)GenT{}, 0)>
+constexpr FlagDefaultArg DefaultArg(int) {
+ return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord};
+}
+
+template <typename ValueT, typename GenT>
+constexpr FlagDefaultArg DefaultArg(char) {
+ return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc};
+}
///////////////////////////////////////////////////////////////////////////////
// Flag current value auxiliary structs.
-// The minimum atomic size we believe to generate lock free code, i.e. all
-// trivially copyable types not bigger this size generate lock free code.
-static constexpr int kMinLockFreeAtomicSize = 8;
+constexpr int64_t UninitializedFlagValue() { return 0xababababababababll; }
-// The same as kMinLockFreeAtomicSize but maximum atomic size. As double words
-// might use two registers, we want to dispatch the logic for them.
-#if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD)
-static constexpr int kMaxLockFreeAtomicSize = 16;
-#else
-static constexpr int kMaxLockFreeAtomicSize = 8;
-#endif
-
-// We can use atomic in cases when it fits in the register, trivially copyable
-// in order to make memcpy operations.
template <typename T>
-struct IsAtomicFlagTypeTrait {
- static constexpr bool value =
- (sizeof(T) <= kMaxLockFreeAtomicSize &&
- type_traits_internal::is_trivially_copyable<T>::value);
-};
+using FlagUseValueAndInitBitStorage = std::integral_constant<
+ bool, absl::type_traits_internal::is_trivially_copyable<T>::value &&
+ std::is_default_constructible<T>::value && (sizeof(T) < 8)>;
-// Clang does not always produce cmpxchg16b instruction when alignment of a 16
-// bytes type is not 16.
-struct alignas(16) FlagsInternalTwoWordsType {
- int64_t first;
- int64_t second;
+template <typename T>
+using FlagUseOneWordStorage = std::integral_constant<
+ bool, absl::type_traits_internal::is_trivially_copyable<T>::value &&
+ (sizeof(T) <= 8)>;
+
+template <class T>
+using FlagUseSequenceLockStorage = std::integral_constant<
+ bool, absl::type_traits_internal::is_trivially_copyable<T>::value &&
+ (sizeof(T) > 8)>;
+
+enum class FlagValueStorageKind : uint8_t {
+ kValueAndInitBit = 0,
+ kOneWordAtomic = 1,
+ kSequenceLocked = 2,
+ kAlignedBuffer = 3,
};
-constexpr bool operator==(const FlagsInternalTwoWordsType& that,
- const FlagsInternalTwoWordsType& other) {
- return that.first == other.first && that.second == other.second;
-}
-constexpr bool operator!=(const FlagsInternalTwoWordsType& that,
- const FlagsInternalTwoWordsType& other) {
- return !(that == other);
+template <typename T>
+static constexpr FlagValueStorageKind StorageKind() {
+ return FlagUseValueAndInitBitStorage<T>::value
+ ? FlagValueStorageKind::kValueAndInitBit
+ : FlagUseOneWordStorage<T>::value
+ ? FlagValueStorageKind::kOneWordAtomic
+ : FlagUseSequenceLockStorage<T>::value
+ ? FlagValueStorageKind::kSequenceLocked
+ : FlagValueStorageKind::kAlignedBuffer;
}
-constexpr int64_t SmallAtomicInit() { return 0xababababababababll; }
+struct FlagOneWordValue {
+ constexpr explicit FlagOneWordValue(int64_t v) : value(v) {}
+ std::atomic<int64_t> value;
+};
-template <typename T, typename S = void>
-struct BestAtomicType {
- using type = int64_t;
- static constexpr int64_t AtomicInit() { return SmallAtomicInit(); }
+template <typename T>
+struct alignas(8) FlagValueAndInitBit {
+ T value;
+ // Use an int instead of a bool to guarantee that a non-zero value has
+ // a bit set.
+ uint8_t init;
};
+template <typename T,
+ FlagValueStorageKind Kind = flags_internal::StorageKind<T>()>
+struct FlagValue;
+
template <typename T>
-struct BestAtomicType<
- T, typename std::enable_if<(kMinLockFreeAtomicSize < sizeof(T) &&
- sizeof(T) <= kMaxLockFreeAtomicSize),
- void>::type> {
- using type = FlagsInternalTwoWordsType;
- static constexpr FlagsInternalTwoWordsType AtomicInit() {
- return {SmallAtomicInit(), SmallAtomicInit()};
+struct FlagValue<T, FlagValueStorageKind::kValueAndInitBit> : FlagOneWordValue {
+ constexpr FlagValue() : FlagOneWordValue(0) {}
+ bool Get(const SequenceLock&, T& dst) const {
+ int64_t storage = value.load(std::memory_order_acquire);
+ if (ABSL_PREDICT_FALSE(storage == 0)) {
+ return false;
+ }
+ dst = absl::bit_cast<FlagValueAndInitBit<T>>(storage).value;
+ return true;
}
};
-struct FlagValue {
- // Heap allocated value.
- void* dynamic = nullptr;
- // For some types, a copy of the current value is kept in an atomically
- // accessible field.
- union Atomics {
- // Using small atomic for small types.
- std::atomic<int64_t> small_atomic;
- template <typename T,
- typename K = typename std::enable_if<
- (sizeof(T) <= kMinLockFreeAtomicSize), void>::type>
- int64_t load() const {
- return small_atomic.load(std::memory_order_acquire);
+template <typename T>
+struct FlagValue<T, FlagValueStorageKind::kOneWordAtomic> : FlagOneWordValue {
+ constexpr FlagValue() : FlagOneWordValue(UninitializedFlagValue()) {}
+ bool Get(const SequenceLock&, T& dst) const {
+ int64_t one_word_val = value.load(std::memory_order_acquire);
+ if (ABSL_PREDICT_FALSE(one_word_val == UninitializedFlagValue())) {
+ return false;
}
+ std::memcpy(&dst, static_cast<const void*>(&one_word_val), sizeof(T));
+ return true;
+ }
+};
-#if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD)
- // Using big atomics for big types.
- std::atomic<FlagsInternalTwoWordsType> big_atomic;
- template <typename T, typename K = typename std::enable_if<
- (kMinLockFreeAtomicSize < sizeof(T) &&
- sizeof(T) <= kMaxLockFreeAtomicSize),
- void>::type>
- FlagsInternalTwoWordsType load() const {
- return big_atomic.load(std::memory_order_acquire);
- }
- constexpr Atomics()
- : big_atomic{FlagsInternalTwoWordsType{SmallAtomicInit(),
- SmallAtomicInit()}} {}
-#else
- constexpr Atomics() : small_atomic{SmallAtomicInit()} {}
-#endif
- };
- Atomics atomics{};
+template <typename T>
+struct FlagValue<T, FlagValueStorageKind::kSequenceLocked> {
+ bool Get(const SequenceLock& lock, T& dst) const {
+ return lock.TryRead(&dst, value_words, sizeof(T));
+ }
+
+ static constexpr int kNumWords =
+ flags_internal::AlignUp(sizeof(T), sizeof(uint64_t)) / sizeof(uint64_t);
+
+ alignas(T) alignas(
+ std::atomic<uint64_t>) std::atomic<uint64_t> value_words[kNumWords];
+};
+
+template <typename T>
+struct FlagValue<T, FlagValueStorageKind::kAlignedBuffer> {
+ bool Get(const SequenceLock&, T&) const { return false; }
+
+ alignas(T) char value[sizeof(T)];
};
///////////////////////////////////////////////////////////////////////////////
@@ -358,134 +419,161 @@ struct FlagCallback {
// The class encapsulates the Flag's data and access to it.
struct DynValueDeleter {
- explicit DynValueDeleter(FlagOpFn op_arg = nullptr) : op(op_arg) {}
- void operator()(void* ptr) const {
- if (op != nullptr) Delete(op, ptr);
- }
+ explicit DynValueDeleter(FlagOpFn op_arg = nullptr);
+ void operator()(void* ptr) const;
FlagOpFn op;
};
-class FlagImpl {
+class FlagState;
+
+class FlagImpl final : public CommandLineFlag {
public:
constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op,
- FlagHelpArg help, FlagDfltGenFunc default_value_gen)
+ FlagHelpArg help, FlagValueStorageKind value_kind,
+ FlagDefaultArg default_arg)
: name_(name),
filename_(filename),
op_(op),
help_(help.source),
help_source_kind_(static_cast<uint8_t>(help.kind)),
- def_kind_(static_cast<uint8_t>(FlagDefaultKind::kGenFunc)),
+ value_storage_kind_(static_cast<uint8_t>(value_kind)),
+ def_kind_(static_cast<uint8_t>(default_arg.kind)),
modified_(false),
on_command_line_(false),
- counter_(0),
callback_(nullptr),
- default_value_(default_value_gen),
+ default_value_(default_arg.source),
data_guard_{} {}
// Constant access methods
- absl::string_view Name() const;
- std::string Filename() const;
- std::string Help() const;
- bool IsModified() const ABSL_LOCKS_EXCLUDED(*DataGuard());
- bool IsSpecifiedOnCommandLine() const ABSL_LOCKS_EXCLUDED(*DataGuard());
- std::string DefaultValue() const ABSL_LOCKS_EXCLUDED(*DataGuard());
- std::string CurrentValue() const ABSL_LOCKS_EXCLUDED(*DataGuard());
- void Read(void* dst) const ABSL_LOCKS_EXCLUDED(*DataGuard());
-
- template <typename T, typename std::enable_if<
- !IsAtomicFlagTypeTrait<T>::value, int>::type = 0>
- void Get(T* dst) const {
- AssertValidType(&flags_internal::FlagStaticTypeIdGen<T>);
- Read(dst);
+ int64_t ReadOneWord() const ABSL_LOCKS_EXCLUDED(*DataGuard());
+ bool ReadOneBool() const ABSL_LOCKS_EXCLUDED(*DataGuard());
+ void Read(void* dst) const override ABSL_LOCKS_EXCLUDED(*DataGuard());
+ void Read(bool* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
+ *value = ReadOneBool();
}
- // Overload for `GetFlag()` for types that support lock-free reads.
- template <typename T, typename std::enable_if<IsAtomicFlagTypeTrait<T>::value,
- int>::type = 0>
- void Get(T* dst) const {
- // For flags of types which can be accessed "atomically" we want to avoid
- // slowing down flag value access due to type validation. That's why
- // this validation is hidden behind !NDEBUG
-#ifndef NDEBUG
- AssertValidType(&flags_internal::FlagStaticTypeIdGen<T>);
-#endif
- using U = flags_internal::BestAtomicType<T>;
- typename U::type r = value_.atomics.template load<T>();
- if (r != U::AtomicInit()) {
- std::memcpy(static_cast<void*>(dst), &r, sizeof(T));
- } else {
- Read(dst);
- }
+ template <typename T,
+ absl::enable_if_t<flags_internal::StorageKind<T>() ==
+ FlagValueStorageKind::kOneWordAtomic,
+ int> = 0>
+ void Read(T* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
+ int64_t v = ReadOneWord();
+ std::memcpy(value, static_cast<const void*>(&v), sizeof(T));
}
- template <typename T>
- void Set(const T& src) {
- AssertValidType(&flags_internal::FlagStaticTypeIdGen<T>);
- Write(&src);
+ template <typename T,
+ typename std::enable_if<flags_internal::StorageKind<T>() ==
+ FlagValueStorageKind::kValueAndInitBit,
+ int>::type = 0>
+ void Read(T* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
+ *value = absl::bit_cast<FlagValueAndInitBit<T>>(ReadOneWord()).value;
}
// Mutating access methods
void Write(const void* src) ABSL_LOCKS_EXCLUDED(*DataGuard());
- bool SetFromString(absl::string_view value, FlagSettingMode set_mode,
- ValueSource source, std::string* err)
- ABSL_LOCKS_EXCLUDED(*DataGuard());
- // If possible, updates copy of the Flag's value that is stored in an
- // atomic word.
- void StoreAtomic() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
// Interfaces to operate on callbacks.
void SetCallback(const FlagCallbackFunc mutation_callback)
ABSL_LOCKS_EXCLUDED(*DataGuard());
void InvokeCallback() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
- // Interfaces to save/restore mutable flag data
- template <typename T>
- std::unique_ptr<FlagStateInterface> SaveState(Flag<T>* flag) const
- ABSL_LOCKS_EXCLUDED(*DataGuard()) {
- T&& cur_value = flag->Get();
- absl::MutexLock l(DataGuard());
-
- return absl::make_unique<FlagState<T>>(
- flag, std::move(cur_value), modified_, on_command_line_, counter_);
- }
- bool RestoreState(const void* value, bool modified, bool on_command_line,
- int64_t counter) ABSL_LOCKS_EXCLUDED(*DataGuard());
-
- // Value validation interfaces.
- void CheckDefaultValueParsingRoundtrip() const
- ABSL_LOCKS_EXCLUDED(*DataGuard());
- bool ValidateInputValue(absl::string_view value) const
- ABSL_LOCKS_EXCLUDED(*DataGuard());
+ // Used in read/write operations to validate source/target has correct type.
+ // For example if flag is declared as absl::Flag<int> FLAGS_foo, a call to
+ // absl::GetFlag(FLAGS_foo) validates that the type of FLAGS_foo is indeed
+ // int. To do that we pass the "assumed" type id (which is deduced from type
+ // int) as an argument `type_id`, which is in turn is validated against the
+ // type id stored in flag object by flag definition statement.
+ void AssertValidType(FlagFastTypeId type_id,
+ const std::type_info* (*gen_rtti)()) const;
private:
+ template <typename T>
+ friend class Flag;
+ friend class FlagState;
+
// Ensures that `data_guard_` is initialized and returns it.
- absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED((absl::Mutex*)&data_guard_);
+ absl::Mutex* DataGuard() const
+ ABSL_LOCK_RETURNED(reinterpret_cast<absl::Mutex*>(data_guard_));
// Returns heap allocated value of type T initialized with default value.
std::unique_ptr<void, DynValueDeleter> MakeInitValue() const
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
// Flag initialization called via absl::call_once.
void Init();
- // Attempts to parse supplied `value` std::string. If parsing is successful,
+
+ // Offset value access methods. One per storage kind. These methods to not
+ // respect const correctness, so be very carefull using them.
+
+ // This is a shared helper routine which encapsulates most of the magic. Since
+ // it is only used inside the three routines below, which are defined in
+ // flag.cc, we can define it in that file as well.
+ template <typename StorageT>
+ StorageT* OffsetValue() const;
+ // This is an accessor for a value stored in an aligned buffer storage
+ // used for non-trivially-copyable data types.
+ // Returns a mutable pointer to the start of a buffer.
+ void* AlignedBufferValue() const;
+
+ // The same as above, but used for sequencelock-protected storage.
+ std::atomic<uint64_t>* AtomicBufferValue() const;
+
+ // This is an accessor for a value stored as one word atomic. Returns a
+ // mutable reference to an atomic value.
+ std::atomic<int64_t>& OneWordValue() const;
+
+ // Attempts to parse supplied `value` string. If parsing is successful,
// returns new value. Otherwise returns nullptr.
std::unique_ptr<void, DynValueDeleter> TryParse(absl::string_view value,
- std::string* err) const
+ std::string& err) const
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
// Stores the flag value based on the pointer to the source.
void StoreValue(const void* src) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
+ // Copy the flag data, protected by `seq_lock_` into `dst`.
+ //
+ // REQUIRES: ValueStorageKind() == kSequenceLocked.
+ void ReadSequenceLockedData(void* dst) const
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+
FlagHelpKind HelpSourceKind() const {
return static_cast<FlagHelpKind>(help_source_kind_);
}
+ FlagValueStorageKind ValueStorageKind() const {
+ return static_cast<FlagValueStorageKind>(value_storage_kind_);
+ }
FlagDefaultKind DefaultKind() const
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()) {
return static_cast<FlagDefaultKind>(def_kind_);
}
- // Used in read/write operations to validate source/target has correct type.
- // For example if flag is declared as absl::Flag<int> FLAGS_foo, a call to
- // absl::GetFlag(FLAGS_foo) validates that the type of FLAGS_foo is indeed
- // int. To do that we pass the "assumed" type id (which is deduced from type
- // int) as an argument `op`, which is in turn is validated against the type id
- // stored in flag object by flag definition statement.
- void AssertValidType(FlagStaticTypeId type_id) const;
+
+ // CommandLineFlag interface implementation
+ absl::string_view Name() const override;
+ std::string Filename() const override;
+ std::string Help() const override;
+ FlagFastTypeId TypeId() const override;
+ bool IsSpecifiedOnCommandLine() const override
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+ std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
+ std::string CurrentValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
+ bool ValidateInputValue(absl::string_view value) const override
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+ void CheckDefaultValueParsingRoundtrip() const override
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+
+ int64_t ModificationCount() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
+
+ // Interfaces to save and restore flags to/from persistent state.
+ // Returns current flag state or nullptr if flag does not support
+ // saving and restoring a state.
+ std::unique_ptr<FlagStateInterface> SaveState() override
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+
+ // Restores the flag state to the supplied state object. If there is
+ // nothing to restore returns false. Otherwise returns true.
+ bool RestoreState(const FlagState& flag_state)
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
+
+ bool ParseFrom(absl::string_view value, FlagSettingMode set_mode,
+ ValueSource source, std::string& error) override
+ ABSL_LOCKS_EXCLUDED(*DataGuard());
// Immutable flag's state.
@@ -499,40 +587,35 @@ class FlagImpl {
const FlagHelpMsg help_;
// Indicates if help message was supplied as literal or generator func.
const uint8_t help_source_kind_ : 1;
+ // Kind of storage this flag is using for the flag's value.
+ const uint8_t value_storage_kind_ : 2;
- // ------------------------------------------------------------------------
- // The bytes containing the const bitfields must not be shared with bytes
- // containing the mutable bitfields.
- // ------------------------------------------------------------------------
-
- // Unique tag for absl::call_once call to initialize this flag.
- //
- // The placement of this variable between the immutable and mutable bitfields
- // is important as prevents them from occupying the same byte. If you remove
- // this variable, make sure to maintain this property.
- absl::once_flag init_control_;
+ uint8_t : 0; // The bytes containing the const bitfields must not be
+ // shared with bytes containing the mutable bitfields.
// Mutable flag's state (guarded by `data_guard_`).
- // If def_kind_ == kDynamicValue, default_value_ holds a dynamically allocated
- // value.
- uint8_t def_kind_ : 1 ABSL_GUARDED_BY(*DataGuard());
+ // def_kind_ is not guard by DataGuard() since it is accessed in Init without
+ // locks.
+ uint8_t def_kind_ : 2;
// Has this flag's value been modified?
bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard());
// Has this flag been specified on command line.
bool on_command_line_ : 1 ABSL_GUARDED_BY(*DataGuard());
- // Mutation counter
- int64_t counter_ ABSL_GUARDED_BY(*DataGuard());
+ // Unique tag for absl::call_once call to initialize this flag.
+ absl::once_flag init_control_;
+
+ // Sequence lock / mutation counter.
+ flags_internal::SequenceLock seq_lock_;
+
// Optional flag's callback and absl::Mutex to guard the invocations.
FlagCallback* callback_ ABSL_GUARDED_BY(*DataGuard());
// Either a pointer to the function generating the default value based on the
// value specified in ABSL_FLAG or pointer to the dynamically set default
// value via SetCommandLineOptionWithMode. def_kind_ is used to distinguish
// these two cases.
- FlagDefaultSrc default_value_ ABSL_GUARDED_BY(*DataGuard());
- // Current Flag Value
- FlagValue value_;
+ FlagDefaultSrc default_value_;
// This is reserved space for an absl::Mutex to guard flag data. It will be
// initialized in FlagImpl::Init via placement new.
@@ -549,11 +632,29 @@ class FlagImpl {
// flag reflection handle interface.
template <typename T>
-class Flag final : public flags_internal::CommandLineFlag {
+class Flag {
public:
- constexpr Flag(const char* name, const char* filename, const FlagHelpArg help,
- const FlagDfltGenFunc default_value_gen)
- : impl_(name, filename, &FlagOps<T>, help, default_value_gen) {}
+ constexpr Flag(const char* name, const char* filename, FlagHelpArg help,
+ const FlagDefaultArg default_arg)
+ : impl_(name, filename, &FlagOps<T>, help,
+ flags_internal::StorageKind<T>(), default_arg),
+ value_() {}
+
+ // CommandLineFlag interface
+ absl::string_view Name() const { return impl_.Name(); }
+ std::string Filename() const { return impl_.Filename(); }
+ std::string Help() const { return impl_.Help(); }
+ // Do not use. To be removed.
+ bool IsSpecifiedOnCommandLine() const {
+ return impl_.IsSpecifiedOnCommandLine();
+ }
+ std::string DefaultValue() const { return impl_.DefaultValue(); }
+ std::string CurrentValue() const { return impl_.CurrentValue(); }
+
+ private:
+ template <typename, bool>
+ friend class FlagRegistrar;
+ friend class FlagImplPeer;
T Get() const {
// See implementation notes in CommandLineFlag::Get().
@@ -564,106 +665,132 @@ class Flag final : public flags_internal::CommandLineFlag {
};
U u;
- impl_.Get(&u.value);
+#if !defined(NDEBUG)
+ impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
+#endif
+
+ if (ABSL_PREDICT_FALSE(!value_.Get(impl_.seq_lock_, u.value))) {
+ impl_.Read(&u.value);
+ }
return std::move(u.value);
}
- void Set(const T& v) { impl_.Set(v); }
- void SetCallback(const FlagCallbackFunc mutation_callback) {
- impl_.SetCallback(mutation_callback);
+ void Set(const T& v) {
+ impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
+ impl_.Write(&v);
}
- // CommandLineFlag interface
- absl::string_view Name() const override { return impl_.Name(); }
- std::string Filename() const override { return impl_.Filename(); }
- absl::string_view Typename() const override { return ""; }
- std::string Help() const override { return impl_.Help(); }
- bool IsModified() const override { return impl_.IsModified(); }
- bool IsSpecifiedOnCommandLine() const override {
- return impl_.IsSpecifiedOnCommandLine();
- }
- std::string DefaultValue() const override { return impl_.DefaultValue(); }
- std::string CurrentValue() const override { return impl_.CurrentValue(); }
- bool ValidateInputValue(absl::string_view value) const override {
- return impl_.ValidateInputValue(value);
- }
+ // Access to the reflection.
+ const CommandLineFlag& Reflect() const { return impl_; }
- // Interfaces to save and restore flags to/from persistent state.
- // Returns current flag state or nullptr if flag does not support
- // saving and restoring a state.
- std::unique_ptr<FlagStateInterface> SaveState() override {
- return impl_.SaveState(this);
- }
+ // Flag's data
+ // The implementation depends on value_ field to be placed exactly after the
+ // impl_ field, so that impl_ can figure out the offset to the value and
+ // access it.
+ FlagImpl impl_;
+ FlagValue<T> value_;
+};
- // Restores the flag state to the supplied state object. If there is
- // nothing to restore returns false. Otherwise returns true.
- bool RestoreState(const FlagState<T>& flag_state) {
- return impl_.RestoreState(&flag_state.cur_value_, flag_state.modified_,
- flag_state.on_command_line_, flag_state.counter_);
+///////////////////////////////////////////////////////////////////////////////
+// Trampoline for friend access
+
+class FlagImplPeer {
+ public:
+ template <typename T, typename FlagType>
+ static T InvokeGet(const FlagType& flag) {
+ return flag.Get();
}
- bool SetFromString(absl::string_view value, FlagSettingMode set_mode,
- ValueSource source, std::string* error) override {
- return impl_.SetFromString(value, set_mode, source, error);
+ template <typename FlagType, typename T>
+ static void InvokeSet(FlagType& flag, const T& v) {
+ flag.Set(v);
}
- void CheckDefaultValueParsingRoundtrip() const override {
- impl_.CheckDefaultValueParsingRoundtrip();
+ template <typename FlagType>
+ static const CommandLineFlag& InvokeReflect(const FlagType& f) {
+ return f.Reflect();
}
-
- private:
- friend class FlagState<T>;
-
- void Read(void* dst) const override { impl_.Read(dst); }
- FlagStaticTypeId TypeId() const override { return &FlagStaticTypeIdGen<T>; }
-
- // Flag's data
- FlagImpl impl_;
};
+///////////////////////////////////////////////////////////////////////////////
+// Implementation of Flag value specific operations routine.
template <typename T>
-inline void FlagState<T>::Restore() const {
- if (flag_->RestoreState(*this)) {
- ABSL_INTERNAL_LOG(INFO,
- absl::StrCat("Restore saved value of ", flag_->Name(),
- " to: ", flag_->CurrentValue()));
+void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
+ switch (op) {
+ case FlagOp::kAlloc: {
+ std::allocator<T> alloc;
+ return std::allocator_traits<std::allocator<T>>::allocate(alloc, 1);
+ }
+ case FlagOp::kDelete: {
+ T* p = static_cast<T*>(v2);
+ p->~T();
+ std::allocator<T> alloc;
+ std::allocator_traits<std::allocator<T>>::deallocate(alloc, p, 1);
+ return nullptr;
+ }
+ case FlagOp::kCopy:
+ *static_cast<T*>(v2) = *static_cast<const T*>(v1);
+ return nullptr;
+ case FlagOp::kCopyConstruct:
+ new (v2) T(*static_cast<const T*>(v1));
+ return nullptr;
+ case FlagOp::kSizeof:
+ return reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(T)));
+ case FlagOp::kFastTypeId:
+ return const_cast<void*>(base_internal::FastTypeId<T>());
+ case FlagOp::kRuntimeTypeId:
+ return const_cast<std::type_info*>(GenRuntimeTypeId<T>());
+ case FlagOp::kParse: {
+ // Initialize the temporary instance of type T based on current value in
+ // destination (which is going to be flag's default value).
+ T temp(*static_cast<T*>(v2));
+ if (!absl::ParseFlag<T>(*static_cast<const absl::string_view*>(v1), &temp,
+ static_cast<std::string*>(v3))) {
+ return nullptr;
+ }
+ *static_cast<T*>(v2) = std::move(temp);
+ return v2;
+ }
+ case FlagOp::kUnparse:
+ *static_cast<std::string*>(v2) =
+ absl::UnparseFlag<T>(*static_cast<const T*>(v1));
+ return nullptr;
+ case FlagOp::kValueOffset: {
+ // Round sizeof(FlagImp) to a multiple of alignof(FlagValue<T>) to get the
+ // offset of the data.
+ ptrdiff_t round_to = alignof(FlagValue<T>);
+ ptrdiff_t offset =
+ (sizeof(FlagImpl) + round_to - 1) / round_to * round_to;
+ return reinterpret_cast<void*>(offset);
+ }
}
+ return nullptr;
}
+///////////////////////////////////////////////////////////////////////////////
// This class facilitates Flag object registration and tail expression-based
// flag definition, for example:
// ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher);
+struct FlagRegistrarEmpty {};
template <typename T, bool do_register>
class FlagRegistrar {
public:
- explicit FlagRegistrar(Flag<T>* flag) : flag_(flag) {
- if (do_register) flags_internal::RegisterCommandLineFlag(flag_);
+ explicit FlagRegistrar(Flag<T>& flag, const char* filename) : flag_(flag) {
+ if (do_register)
+ flags_internal::RegisterCommandLineFlag(flag_.impl_, filename);
}
- FlagRegistrar& OnUpdate(FlagCallbackFunc cb) && {
- flag_->SetCallback(cb);
+ FlagRegistrar OnUpdate(FlagCallbackFunc cb) && {
+ flag_.impl_.SetCallback(cb);
return *this;
}
- // Make the registrar "die" gracefully as a bool on a line where registration
- // happens. Registrar objects are intended to live only as temporary.
- operator bool() const { return true; } // NOLINT
+ // Make the registrar "die" gracefully as an empty struct on a line where
+ // registration happens. Registrar objects are intended to live only as
+ // temporary.
+ operator FlagRegistrarEmpty() const { return {}; } // NOLINT
private:
- 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>
-T* MakeFromDefaultValue(T t) {
- return new T(std::move(t));
-}
-
-template <typename T>
-T* MakeFromDefaultValue(EmptyBraces) {
- return new T;
-}
-
} // namespace flags_internal
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/flag_msvc.inc b/third_party/abseil-cpp/absl/flags/internal/flag_msvc.inc
new file mode 100644
index 0000000000..c31bd27fd8
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/flag_msvc.inc
@@ -0,0 +1,116 @@
+//
+// Copyright 2021 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Do not include this file directly.
+// Include absl/flags/flag.h instead.
+
+// MSVC debug builds do not implement initialization with constexpr constructors
+// correctly. To work around this we add a level of indirection, so that the
+// class `absl::Flag` contains an `internal::Flag*` (instead of being an alias
+// to that class) and dynamically allocates an instance when necessary. We also
+// forward all calls to internal::Flag methods via trampoline methods. In this
+// setup the `absl::Flag` class does not have constructor and virtual methods,
+// all the data members are public and thus MSVC is able to initialize it at
+// link time. To deal with multiple threads accessing the flag for the first
+// time concurrently we use an atomic boolean indicating if flag object is
+// initialized. We also employ the double-checked locking pattern where the
+// second level of protection is a global Mutex, so if two threads attempt to
+// construct the flag concurrently only one wins.
+//
+// This solution is based on a recomendation here:
+// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html?childToView=648454#comment-648454
+
+namespace flags_internal {
+absl::Mutex* GetGlobalConstructionGuard();
+} // namespace flags_internal
+
+// Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
+// See https://abseil.io/docs/cpp/guides/flags
+template <typename T>
+class Flag {
+ public:
+ // No constructor and destructor to ensure this is an aggregate type.
+ // Visual Studio 2015 still requires the constructor for class to be
+ // constexpr initializable.
+#if _MSC_VER <= 1900
+ constexpr Flag(const char* name, const char* filename,
+ const flags_internal::HelpGenFunc help_gen,
+ const flags_internal::FlagDfltGenFunc default_value_gen)
+ : name_(name),
+ filename_(filename),
+ help_gen_(help_gen),
+ default_value_gen_(default_value_gen),
+ inited_(false),
+ impl_(nullptr) {}
+#endif
+
+ flags_internal::Flag<T>& GetImpl() const {
+ if (!inited_.load(std::memory_order_acquire)) {
+ absl::MutexLock l(flags_internal::GetGlobalConstructionGuard());
+
+ if (inited_.load(std::memory_order_acquire)) {
+ return *impl_;
+ }
+
+ impl_ = new flags_internal::Flag<T>(
+ name_, filename_,
+ {flags_internal::FlagHelpMsg(help_gen_),
+ flags_internal::FlagHelpKind::kGenFunc},
+ {flags_internal::FlagDefaultSrc(default_value_gen_),
+ flags_internal::FlagDefaultKind::kGenFunc});
+ inited_.store(true, std::memory_order_release);
+ }
+
+ return *impl_;
+ }
+
+ // Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
+ // See https://abseil.io/docs/cpp/guides/flags
+ bool IsRetired() const { return GetImpl().IsRetired(); }
+ absl::string_view Name() const { return GetImpl().Name(); }
+ std::string Help() const { return GetImpl().Help(); }
+ bool IsModified() const { return GetImpl().IsModified(); }
+ bool IsSpecifiedOnCommandLine() const {
+ return GetImpl().IsSpecifiedOnCommandLine();
+ }
+ std::string Filename() const { return GetImpl().Filename(); }
+ std::string DefaultValue() const { return GetImpl().DefaultValue(); }
+ std::string CurrentValue() const { return GetImpl().CurrentValue(); }
+ template <typename U>
+ inline bool IsOfType() const {
+ return GetImpl().template IsOfType<U>();
+ }
+ T Get() const {
+ return flags_internal::FlagImplPeer::InvokeGet<T>(GetImpl());
+ }
+ void Set(const T& v) {
+ flags_internal::FlagImplPeer::InvokeSet(GetImpl(), v);
+ }
+ void InvokeCallback() { GetImpl().InvokeCallback(); }
+
+ const CommandLineFlag& Reflect() const {
+ return flags_internal::FlagImplPeer::InvokeReflect(GetImpl());
+ }
+
+ // The data members are logically private, but they need to be public for
+ // this to be an aggregate type.
+ const char* name_;
+ const char* filename_;
+ const flags_internal::HelpGenFunc help_gen_;
+ const flags_internal::FlagDfltGenFunc default_value_gen_;
+
+ mutable std::atomic<bool> inited_;
+ mutable flags_internal::Flag<T>* impl_;
+};
diff --git a/third_party/abseil-cpp/absl/flags/internal/parse.h b/third_party/abseil-cpp/absl/flags/internal/parse.h
index 03e8a07bf3..de706c8984 100644
--- a/third_party/abseil-cpp/absl/flags/internal/parse.h
+++ b/third_party/abseil-cpp/absl/flags/internal/parse.h
@@ -21,6 +21,7 @@
#include "absl/base/config.h"
#include "absl/flags/declare.h"
+#include "absl/strings/string_view.h"
ABSL_DECLARE_FLAG(std::vector<std::string>, flagfile);
ABSL_DECLARE_FLAG(std::vector<std::string>, fromenv);
@@ -44,6 +45,13 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
UsageFlagsAction usage_flag_act,
OnUndefinedFlag on_undef_flag);
+// --------------------------------------------------------------------
+// Inspect original command line
+
+// Returns true if flag with specified name was either present on the original
+// command line or specified in flag file present on the original command line.
+bool WasPresentOnCommandLine(absl::string_view flag_name);
+
} // namespace flags_internal
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/path_util.h b/third_party/abseil-cpp/absl/flags/internal/path_util.h
index 365c830522..a6594d3347 100644
--- a/third_party/abseil-cpp/absl/flags/internal/path_util.h
+++ b/third_party/abseil-cpp/absl/flags/internal/path_util.h
@@ -17,7 +17,6 @@
#define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
#include "absl/base/config.h"
-#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
namespace absl {
diff --git a/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.cc b/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.cc
new file mode 100644
index 0000000000..a7eb58b6d4
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.cc
@@ -0,0 +1,65 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/internal/private_handle_accessor.h"
+
+#include <memory>
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/flags/commandlineflag.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+
+FlagFastTypeId PrivateHandleAccessor::TypeId(const CommandLineFlag& flag) {
+ return flag.TypeId();
+}
+
+std::unique_ptr<FlagStateInterface> PrivateHandleAccessor::SaveState(
+ CommandLineFlag& flag) {
+ return flag.SaveState();
+}
+
+bool PrivateHandleAccessor::IsSpecifiedOnCommandLine(
+ const CommandLineFlag& flag) {
+ return flag.IsSpecifiedOnCommandLine();
+}
+
+bool PrivateHandleAccessor::ValidateInputValue(const CommandLineFlag& flag,
+ absl::string_view value) {
+ return flag.ValidateInputValue(value);
+}
+
+void PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip(
+ const CommandLineFlag& flag) {
+ flag.CheckDefaultValueParsingRoundtrip();
+}
+
+bool PrivateHandleAccessor::ParseFrom(CommandLineFlag& flag,
+ absl::string_view value,
+ flags_internal::FlagSettingMode set_mode,
+ flags_internal::ValueSource source,
+ std::string& error) {
+ return flag.ParseFrom(value, set_mode, source, error);
+}
+
+} // namespace flags_internal
+ABSL_NAMESPACE_END
+} // namespace absl
+
diff --git a/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.h b/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.h
new file mode 100644
index 0000000000..c64435cd61
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/private_handle_accessor.h
@@ -0,0 +1,61 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_
+#define ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_
+
+#include <memory>
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/flags/commandlineflag.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+
+// 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 PrivateHandleAccessor {
+ 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::IsSpecifiedOnCommandLine.
+ static bool IsSpecifiedOnCommandLine(const CommandLineFlag& flag);
+
+ // Access to CommandLineFlag::ValidateInputValue.
+ static bool ValidateInputValue(const CommandLineFlag& flag,
+ absl::string_view value);
+
+ // Access to CommandLineFlag::CheckDefaultValueParsingRoundtrip.
+ static void CheckDefaultValueParsingRoundtrip(const CommandLineFlag& flag);
+
+ static bool ParseFrom(CommandLineFlag& flag, absl::string_view value,
+ flags_internal::FlagSettingMode set_mode,
+ flags_internal::ValueSource source, std::string& error);
+};
+
+} // namespace flags_internal
+ABSL_NAMESPACE_END
+} // namespace absl
+
+#endif // ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_
diff --git a/third_party/abseil-cpp/absl/flags/internal/program_name_test.cc b/third_party/abseil-cpp/absl/flags/internal/program_name_test.cc
index 269142f225..aff9f6315e 100644
--- a/third_party/abseil-cpp/absl/flags/internal/program_name_test.cc
+++ b/third_party/abseil-cpp/absl/flags/internal/program_name_test.cc
@@ -25,7 +25,7 @@ namespace {
namespace flags = absl::flags_internal;
-TEST(FlagsPathUtilTest, TestInitialProgamName) {
+TEST(FlagsPathUtilTest, TestProgamNameInterfaces) {
flags::SetProgramInvocationName("absl/flags/program_name_test");
std::string program_name = flags::ProgramInvocationName();
for (char& c : program_name)
@@ -43,9 +43,7 @@ TEST(FlagsPathUtilTest, TestInitialProgamName) {
EXPECT_TRUE(absl::EndsWith(program_name, expect_name)) << program_name;
EXPECT_EQ(flags::ShortProgramInvocationName(), expect_basename);
-}
-TEST(FlagsPathUtilTest, TestProgamNameInterfaces) {
flags::SetProgramInvocationName("a/my_test");
EXPECT_EQ(flags::ProgramInvocationName(), "a/my_test");
diff --git a/third_party/abseil-cpp/absl/flags/internal/registry.cc b/third_party/abseil-cpp/absl/flags/internal/registry.cc
deleted file mode 100644
index e434a85912..0000000000
--- a/third_party/abseil-cpp/absl/flags/internal/registry.cc
+++ /dev/null
@@ -1,351 +0,0 @@
-//
-// Copyright 2019 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "absl/flags/internal/registry.h"
-
-#include <assert.h>
-#include <stdlib.h>
-
-#include <functional>
-#include <map>
-#include <memory>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "absl/base/config.h"
-#include "absl/base/internal/raw_logging.h"
-#include "absl/base/thread_annotations.h"
-#include "absl/flags/internal/commandlineflag.h"
-#include "absl/flags/usage_config.h"
-#include "absl/strings/str_cat.h"
-#include "absl/strings/string_view.h"
-#include "absl/synchronization/mutex.h"
-
-// --------------------------------------------------------------------
-// FlagRegistry implementation
-// A FlagRegistry holds all flag objects indexed
-// by their names so that if you know a flag's name you can access or
-// set it.
-
-namespace absl {
-ABSL_NAMESPACE_BEGIN
-namespace flags_internal {
-
-// --------------------------------------------------------------------
-// FlagRegistry
-// A FlagRegistry singleton object holds all flag objects indexed
-// by their names so that if you know a flag's name (as a C
-// string), you can access or set it. If the function is named
-// FooLocked(), you must own the registry lock before calling
-// the function; otherwise, you should *not* hold the lock, and
-// the function will acquire it itself if needed.
-// --------------------------------------------------------------------
-
-class FlagRegistry {
- public:
- FlagRegistry() = default;
- ~FlagRegistry() = default;
-
- // Store a flag in this registry. Takes ownership of *flag.
- void RegisterFlag(CommandLineFlag* flag);
-
- void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); }
- void Unlock() ABSL_UNLOCK_FUNCTION(lock_) { lock_.Unlock(); }
-
- // Returns the flag object for the specified name, or nullptr if not found.
- // Will emit a warning if a 'retired' flag is specified.
- CommandLineFlag* FindFlagLocked(absl::string_view name);
-
- // Returns the retired flag object for the specified name, or nullptr if not
- // found or not retired. Does not emit a warning.
- CommandLineFlag* FindRetiredFlagLocked(absl::string_view name);
-
- static FlagRegistry* GlobalRegistry(); // returns a singleton registry
-
- private:
- friend class FlagSaverImpl; // reads all the flags in order to copy them
- friend void ForEachFlagUnlocked(
- std::function<void(CommandLineFlag*)> visitor);
-
- // The map from name to flag, for FindFlagLocked().
- using FlagMap = std::map<absl::string_view, CommandLineFlag*>;
- using FlagIterator = FlagMap::iterator;
- using FlagConstIterator = FlagMap::const_iterator;
- FlagMap flags_;
-
- absl::Mutex lock_;
-
- // Disallow
- FlagRegistry(const FlagRegistry&);
- FlagRegistry& operator=(const FlagRegistry&);
-};
-
-FlagRegistry* FlagRegistry::GlobalRegistry() {
- static FlagRegistry* global_registry = new FlagRegistry;
- return global_registry;
-}
-
-namespace {
-
-class FlagRegistryLock {
- public:
- explicit FlagRegistryLock(FlagRegistry* fr) : fr_(fr) { fr_->Lock(); }
- ~FlagRegistryLock() { fr_->Unlock(); }
-
- private:
- FlagRegistry* const fr_;
-};
-
-void DestroyRetiredFlag(CommandLineFlag* flag);
-} // namespace
-
-void FlagRegistry::RegisterFlag(CommandLineFlag* flag) {
- FlagRegistryLock registry_lock(this);
- std::pair<FlagIterator, bool> ins =
- flags_.insert(FlagMap::value_type(flag->Name(), flag));
- if (ins.second == false) { // means the name was already in the map
- CommandLineFlag* old_flag = ins.first->second;
- if (flag->IsRetired() != old_flag->IsRetired()) {
- // All registrations must agree on the 'retired' flag.
- flags_internal::ReportUsageError(
- absl::StrCat(
- "Retired flag '", flag->Name(),
- "' was defined normally in file '",
- (flag->IsRetired() ? old_flag->Filename() : flag->Filename()),
- "'."),
- true);
- } else if (flag->TypeId() != old_flag->TypeId()) {
- flags_internal::ReportUsageError(
- absl::StrCat("Flag '", flag->Name(),
- "' was defined more than once but with "
- "differing types. Defined in files '",
- old_flag->Filename(), "' and '", flag->Filename(),
- "' with types '", old_flag->Typename(), "' and '",
- flag->Typename(), "', respectively."),
- true);
- } else if (old_flag->IsRetired()) {
- // Retired flag can just be deleted.
- DestroyRetiredFlag(flag);
- return;
- } else if (old_flag->Filename() != flag->Filename()) {
- flags_internal::ReportUsageError(
- absl::StrCat("Flag '", flag->Name(),
- "' was defined more than once (in files '",
- old_flag->Filename(), "' and '", flag->Filename(),
- "')."),
- true);
- } else {
- flags_internal::ReportUsageError(
- absl::StrCat(
- "Something wrong with flag '", flag->Name(), "' in file '",
- flag->Filename(), "'. One possibility: file '", flag->Filename(),
- "' is being linked both statically and dynamically into this "
- "executable. e.g. some files listed as srcs to a test and also "
- "listed as srcs of some shared lib deps of the same test."),
- true);
- }
- // All cases above are fatal, except for the retired flags.
- std::exit(1);
- }
-}
-
-CommandLineFlag* FlagRegistry::FindFlagLocked(absl::string_view name) {
- FlagConstIterator i = flags_.find(name);
- if (i == flags_.end()) {
- return nullptr;
- }
-
- if (i->second->IsRetired()) {
- flags_internal::ReportUsageError(
- absl::StrCat("Accessing retired flag '", name, "'"), false);
- }
-
- return i->second;
-}
-
-CommandLineFlag* FlagRegistry::FindRetiredFlagLocked(absl::string_view name) {
- FlagConstIterator i = flags_.find(name);
- if (i == flags_.end() || !i->second->IsRetired()) {
- return nullptr;
- }
-
- return i->second;
-}
-
-// --------------------------------------------------------------------
-// FlagSaver
-// FlagSaverImpl
-// This class stores the states of all flags at construct time,
-// and restores all flags to that state at destruct time.
-// Its major implementation challenge is that it never modifies
-// pointers in the 'main' registry, so global FLAG_* vars always
-// point to the right place.
-// --------------------------------------------------------------------
-
-class FlagSaverImpl {
- public:
- FlagSaverImpl() = default;
- FlagSaverImpl(const FlagSaverImpl&) = delete;
- void operator=(const FlagSaverImpl&) = delete;
-
- // Saves the flag states from the flag registry into this object.
- // It's an error to call this more than once.
- void SaveFromRegistry() {
- assert(backup_registry_.empty()); // call only once!
- flags_internal::ForEachFlag([&](flags_internal::CommandLineFlag* flag) {
- if (auto flag_state = flag->SaveState()) {
- backup_registry_.emplace_back(std::move(flag_state));
- }
- });
- }
-
- // Restores the saved flag states into the flag registry.
- void RestoreToRegistry() {
- for (const auto& flag_state : backup_registry_) {
- flag_state->Restore();
- }
- }
-
- private:
- std::vector<std::unique_ptr<flags_internal::FlagStateInterface>>
- backup_registry_;
-};
-
-FlagSaver::FlagSaver() : impl_(new FlagSaverImpl) { impl_->SaveFromRegistry(); }
-
-void FlagSaver::Ignore() {
- delete impl_;
- impl_ = nullptr;
-}
-
-FlagSaver::~FlagSaver() {
- if (!impl_) return;
-
- impl_->RestoreToRegistry();
- delete impl_;
-}
-
-// --------------------------------------------------------------------
-
-CommandLineFlag* FindCommandLineFlag(absl::string_view name) {
- if (name.empty()) return nullptr;
- FlagRegistry* const registry = FlagRegistry::GlobalRegistry();
- FlagRegistryLock frl(registry);
-
- return registry->FindFlagLocked(name);
-}
-
-CommandLineFlag* FindRetiredFlag(absl::string_view name) {
- FlagRegistry* const registry = FlagRegistry::GlobalRegistry();
- FlagRegistryLock frl(registry);
-
- return registry->FindRetiredFlagLocked(name);
-}
-
-// --------------------------------------------------------------------
-
-void ForEachFlagUnlocked(std::function<void(CommandLineFlag*)> visitor) {
- FlagRegistry* const registry = FlagRegistry::GlobalRegistry();
- for (FlagRegistry::FlagConstIterator i = registry->flags_.begin();
- i != registry->flags_.end(); ++i) {
- visitor(i->second);
- }
-}
-
-void ForEachFlag(std::function<void(CommandLineFlag*)> visitor) {
- FlagRegistry* const registry = FlagRegistry::GlobalRegistry();
- FlagRegistryLock frl(registry);
- ForEachFlagUnlocked(visitor);
-}
-
-// --------------------------------------------------------------------
-
-bool RegisterCommandLineFlag(CommandLineFlag* flag) {
- FlagRegistry::GlobalRegistry()->RegisterFlag(flag);
- return true;
-}
-
-// --------------------------------------------------------------------
-
-namespace {
-
-class RetiredFlagObj final : public flags_internal::CommandLineFlag {
- public:
- constexpr RetiredFlagObj(const char* name, FlagStaticTypeId type_id)
- : name_(name), type_id_(type_id) {}
-
- private:
- absl::string_view Name() const override { return name_; }
- std::string Filename() const override { return "RETIRED"; }
- absl::string_view Typename() const override { return ""; }
- FlagStaticTypeId TypeId() const override { return type_id_; }
- std::string Help() const override { return ""; }
- bool IsRetired() const override { return true; }
- bool IsModified() const override { return false; }
- bool IsSpecifiedOnCommandLine() const override { return false; }
- std::string DefaultValue() const override { return ""; }
- std::string CurrentValue() const override { return ""; }
-
- // Any input is valid
- bool ValidateInputValue(absl::string_view) const override { return true; }
-
- std::unique_ptr<flags_internal::FlagStateInterface> SaveState() override {
- return nullptr;
- }
-
- bool SetFromString(absl::string_view, flags_internal::FlagSettingMode,
- flags_internal::ValueSource, std::string*) override {
- return false;
- }
-
- void CheckDefaultValueParsingRoundtrip() const override {}
-
- void Read(void*) const override {}
-
- // Data members
- const char* const name_;
- const FlagStaticTypeId type_id_;
-};
-
-void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) {
- assert(flag->IsRetired());
- delete static_cast<RetiredFlagObj*>(flag);
-}
-
-} // namespace
-
-bool Retire(const char* name, FlagStaticTypeId type_id) {
- auto* flag = new flags_internal::RetiredFlagObj(name, type_id);
- FlagRegistry::GlobalRegistry()->RegisterFlag(flag);
- return true;
-}
-
-// --------------------------------------------------------------------
-
-bool IsRetiredFlag(absl::string_view name, bool* type_is_bool) {
- assert(!name.empty());
- CommandLineFlag* flag = flags_internal::FindRetiredFlag(name);
- if (flag == nullptr) {
- return false;
- }
- assert(type_is_bool);
- *type_is_bool = flag->IsOfType<bool>();
- return true;
-}
-
-} // namespace flags_internal
-ABSL_NAMESPACE_END
-} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/registry.h b/third_party/abseil-cpp/absl/flags/internal/registry.h
index 69ff889fb1..4b68c85f5c 100644
--- a/third_party/abseil-cpp/absl/flags/internal/registry.h
+++ b/third_party/abseil-cpp/absl/flags/internal/registry.h
@@ -17,11 +17,9 @@
#define ABSL_FLAGS_INTERNAL_REGISTRY_H_
#include <functional>
-#include <map>
-#include <string>
#include "absl/base/config.h"
-#include "absl/base/macros.h"
+#include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
@@ -32,19 +30,15 @@ namespace absl {
ABSL_NAMESPACE_BEGIN
namespace flags_internal {
-CommandLineFlag* FindCommandLineFlag(absl::string_view name);
-CommandLineFlag* FindRetiredFlag(absl::string_view name);
-
-// Executes specified visitor for each non-retired flag in the registry.
-// Requires the caller hold the registry lock.
-void ForEachFlagUnlocked(std::function<void(CommandLineFlag*)> visitor);
// Executes specified visitor for each non-retired flag in the registry. While
// callback are executed, the registry is locked and can't be changed.
-void ForEachFlag(std::function<void(CommandLineFlag*)> visitor);
+void ForEachFlag(std::function<void(CommandLineFlag&)> visitor);
//-----------------------------------------------------------------------------
-bool RegisterCommandLineFlag(CommandLineFlag*);
+bool RegisterCommandLineFlag(CommandLineFlag&, const char* filename);
+
+void FinalizeRegistry();
//-----------------------------------------------------------------------------
// Retired registrations:
@@ -79,42 +73,21 @@ bool RegisterCommandLineFlag(CommandLineFlag*);
//
// Retire flag with name "name" and type indicated by ops.
-bool Retire(const char* name, FlagStaticTypeId type_id);
+void Retire(const char* name, FlagFastTypeId type_id, char* buf);
+
+constexpr size_t kRetiredFlagObjSize = 3 * sizeof(void*);
+constexpr size_t kRetiredFlagObjAlignment = alignof(void*);
// Registered a retired flag with name 'flag_name' and type 'T'.
template <typename T>
-inline bool RetiredFlag(const char* flag_name) {
- return flags_internal::Retire(flag_name, &FlagStaticTypeIdGen<T>);
-}
-
-// If the flag is retired, returns true and indicates in |*type_is_bool|
-// whether the type of the retired flag is a bool.
-// Only to be called by code that needs to explicitly ignore retired flags.
-bool IsRetiredFlag(absl::string_view name, bool* type_is_bool);
-
-//-----------------------------------------------------------------------------
-// Saves the states (value, default value, whether the user has set
-// the flag, registered validators, etc) of all flags, and restores
-// them when the FlagSaver is destroyed.
-//
-// This class is thread-safe. However, its destructor writes to
-// exactly the set of flags that have changed value during its
-// lifetime, so concurrent _direct_ access to those flags
-// (i.e. FLAGS_foo instead of {Get,Set}CommandLineOption()) is unsafe.
-
-class FlagSaver {
+class RetiredFlag {
public:
- FlagSaver();
- ~FlagSaver();
-
- FlagSaver(const FlagSaver&) = delete;
- void operator=(const FlagSaver&) = delete;
-
- // Prevents saver from restoring the saved state of flags.
- void Ignore();
+ void Retire(const char* flag_name) {
+ flags_internal::Retire(flag_name, base_internal::FastTypeId<T>(), buf_);
+ }
private:
- class FlagSaverImpl* impl_; // we use pimpl here to keep API steady
+ alignas(kRetiredFlagObjAlignment) char buf_[kRetiredFlagObjSize];
};
} // namespace flags_internal
diff --git a/third_party/abseil-cpp/absl/flags/internal/sequence_lock.h b/third_party/abseil-cpp/absl/flags/internal/sequence_lock.h
new file mode 100644
index 0000000000..36318ab9d3
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/sequence_lock.h
@@ -0,0 +1,187 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_FLAGS_INTERNAL_SEQUENCE_LOCK_H_
+#define ABSL_FLAGS_INTERNAL_SEQUENCE_LOCK_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <atomic>
+#include <cassert>
+#include <cstring>
+
+#include "absl/base/optimization.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+
+// Align 'x' up to the nearest 'align' bytes.
+inline constexpr size_t AlignUp(size_t x, size_t align) {
+ return align * ((x + align - 1) / align);
+}
+
+// A SequenceLock implements lock-free reads. A sequence counter is incremented
+// before and after each write, and readers access the counter before and after
+// accessing the protected data. If the counter is verified to not change during
+// the access, and the sequence counter value was even, then the reader knows
+// that the read was race-free and valid. Otherwise, the reader must fall back
+// to a Mutex-based code path.
+//
+// This particular SequenceLock starts in an "uninitialized" state in which
+// TryRead() returns false. It must be enabled by calling MarkInitialized().
+// This serves as a marker that the associated flag value has not yet been
+// initialized and a slow path needs to be taken.
+//
+// The memory reads and writes protected by this lock must use the provided
+// `TryRead()` and `Write()` functions. These functions behave similarly to
+// `memcpy()`, with one oddity: the protected data must be an array of
+// `std::atomic<uint64>`. This is to comply with the C++ standard, which
+// considers data races on non-atomic objects to be undefined behavior. See "Can
+// Seqlocks Get Along With Programming Language Memory Models?"[1] by Hans J.
+// Boehm for more details.
+//
+// [1] https://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf
+class SequenceLock {
+ public:
+ constexpr SequenceLock() : lock_(kUninitialized) {}
+
+ // Mark that this lock is ready for use.
+ void MarkInitialized() {
+ assert(lock_.load(std::memory_order_relaxed) == kUninitialized);
+ lock_.store(0, std::memory_order_release);
+ }
+
+ // Copy "size" bytes of data from "src" to "dst", protected as a read-side
+ // critical section of the sequence lock.
+ //
+ // Unlike traditional sequence lock implementations which loop until getting a
+ // clean read, this implementation returns false in the case of concurrent
+ // calls to `Write`. In such a case, the caller should fall back to a
+ // locking-based slow path.
+ //
+ // Returns false if the sequence lock was not yet marked as initialized.
+ //
+ // NOTE: If this returns false, "dst" may be overwritten with undefined
+ // (potentially uninitialized) data.
+ bool TryRead(void* dst, const std::atomic<uint64_t>* src, size_t size) const {
+ // Acquire barrier ensures that no loads done by f() are reordered
+ // above the first load of the sequence counter.
+ int64_t seq_before = lock_.load(std::memory_order_acquire);
+ if (ABSL_PREDICT_FALSE(seq_before & 1) == 1) return false;
+ RelaxedCopyFromAtomic(dst, src, size);
+ // Another acquire fence ensures that the load of 'lock_' below is
+ // strictly ordered after the RelaxedCopyToAtomic call above.
+ std::atomic_thread_fence(std::memory_order_acquire);
+ int64_t seq_after = lock_.load(std::memory_order_relaxed);
+ return ABSL_PREDICT_TRUE(seq_before == seq_after);
+ }
+
+ // Copy "size" bytes from "src" to "dst" as a write-side critical section
+ // of the sequence lock. Any concurrent readers will be forced to retry
+ // until they get a read that does not conflict with this write.
+ //
+ // This call must be externally synchronized against other calls to Write,
+ // but may proceed concurrently with reads.
+ void Write(std::atomic<uint64_t>* dst, const void* src, size_t size) {
+ // We can use relaxed instructions to increment the counter since we
+ // are extenally synchronized. The std::atomic_thread_fence below
+ // ensures that the counter updates don't get interleaved with the
+ // copy to the data.
+ int64_t orig_seq = lock_.load(std::memory_order_relaxed);
+ assert((orig_seq & 1) == 0); // Must be initially unlocked.
+ lock_.store(orig_seq + 1, std::memory_order_relaxed);
+
+ // We put a release fence between update to lock_ and writes to shared data.
+ // Thus all stores to shared data are effectively release operations and
+ // update to lock_ above cannot be re-ordered past any of them. Note that
+ // this barrier is not for the fetch_add above. A release barrier for the
+ // fetch_add would be before it, not after.
+ std::atomic_thread_fence(std::memory_order_release);
+ RelaxedCopyToAtomic(dst, src, size);
+ // "Release" semantics ensure that none of the writes done by
+ // RelaxedCopyToAtomic() can be reordered after the following modification.
+ lock_.store(orig_seq + 2, std::memory_order_release);
+ }
+
+ // Return the number of times that Write() has been called.
+ //
+ // REQUIRES: This must be externally synchronized against concurrent calls to
+ // `Write()` or `IncrementModificationCount()`.
+ // REQUIRES: `MarkInitialized()` must have been previously called.
+ int64_t ModificationCount() const {
+ int64_t val = lock_.load(std::memory_order_relaxed);
+ assert(val != kUninitialized && (val & 1) == 0);
+ return val / 2;
+ }
+
+ // REQUIRES: This must be externally synchronized against concurrent calls to
+ // `Write()` or `ModificationCount()`.
+ // REQUIRES: `MarkInitialized()` must have been previously called.
+ void IncrementModificationCount() {
+ int64_t val = lock_.load(std::memory_order_relaxed);
+ assert(val != kUninitialized);
+ lock_.store(val + 2, std::memory_order_relaxed);
+ }
+
+ private:
+ // Perform the equivalent of "memcpy(dst, src, size)", but using relaxed
+ // atomics.
+ static void RelaxedCopyFromAtomic(void* dst, const std::atomic<uint64_t>* src,
+ size_t size) {
+ char* dst_byte = static_cast<char*>(dst);
+ while (size >= sizeof(uint64_t)) {
+ uint64_t word = src->load(std::memory_order_relaxed);
+ std::memcpy(dst_byte, &word, sizeof(word));
+ dst_byte += sizeof(word);
+ src++;
+ size -= sizeof(word);
+ }
+ if (size > 0) {
+ uint64_t word = src->load(std::memory_order_relaxed);
+ std::memcpy(dst_byte, &word, size);
+ }
+ }
+
+ // Perform the equivalent of "memcpy(dst, src, size)", but using relaxed
+ // atomics.
+ static void RelaxedCopyToAtomic(std::atomic<uint64_t>* dst, const void* src,
+ size_t size) {
+ const char* src_byte = static_cast<const char*>(src);
+ while (size >= sizeof(uint64_t)) {
+ uint64_t word;
+ std::memcpy(&word, src_byte, sizeof(word));
+ dst->store(word, std::memory_order_relaxed);
+ src_byte += sizeof(word);
+ dst++;
+ size -= sizeof(word);
+ }
+ if (size > 0) {
+ uint64_t word = 0;
+ std::memcpy(&word, src_byte, size);
+ dst->store(word, std::memory_order_relaxed);
+ }
+ }
+
+ static constexpr int64_t kUninitialized = -1;
+ std::atomic<int64_t> lock_;
+};
+
+} // namespace flags_internal
+ABSL_NAMESPACE_END
+} // namespace absl
+
+#endif // ABSL_FLAGS_INTERNAL_SEQUENCE_LOCK_H_
diff --git a/third_party/abseil-cpp/absl/flags/internal/sequence_lock_test.cc b/third_party/abseil-cpp/absl/flags/internal/sequence_lock_test.cc
new file mode 100644
index 0000000000..c3ec372ed8
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/internal/sequence_lock_test.cc
@@ -0,0 +1,169 @@
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#include "absl/flags/internal/sequence_lock.h"
+
+#include <algorithm>
+#include <atomic>
+#include <thread> // NOLINT(build/c++11)
+#include <tuple>
+#include <vector>
+
+#include "gtest/gtest.h"
+#include "absl/base/internal/sysinfo.h"
+#include "absl/container/fixed_array.h"
+#include "absl/time/clock.h"
+
+namespace {
+
+namespace flags = absl::flags_internal;
+
+class ConcurrentSequenceLockTest
+ : public testing::TestWithParam<std::tuple<int, int>> {
+ public:
+ ConcurrentSequenceLockTest()
+ : buf_bytes_(std::get<0>(GetParam())),
+ num_threads_(std::get<1>(GetParam())) {}
+
+ protected:
+ const int buf_bytes_;
+ const int num_threads_;
+};
+
+TEST_P(ConcurrentSequenceLockTest, ReadAndWrite) {
+ const int buf_words =
+ flags::AlignUp(buf_bytes_, sizeof(uint64_t)) / sizeof(uint64_t);
+
+ // The buffer that will be protected by the SequenceLock.
+ absl::FixedArray<std::atomic<uint64_t>> protected_buf(buf_words);
+ for (auto& v : protected_buf) v = -1;
+
+ flags::SequenceLock seq_lock;
+ std::atomic<bool> stop{false};
+ std::atomic<int64_t> bad_reads{0};
+ std::atomic<int64_t> good_reads{0};
+ std::atomic<int64_t> unsuccessful_reads{0};
+
+ // Start a bunch of threads which read 'protected_buf' under the sequence
+ // lock. The main thread will concurrently update 'protected_buf'. The updates
+ // always consist of an array of identical integers. The reader ensures that
+ // any data it reads matches that pattern (i.e. the reads are not "torn").
+ std::vector<std::thread> threads;
+ for (int i = 0; i < num_threads_; i++) {
+ threads.emplace_back([&]() {
+ absl::FixedArray<char> local_buf(buf_bytes_);
+ while (!stop.load(std::memory_order_relaxed)) {
+ if (seq_lock.TryRead(local_buf.data(), protected_buf.data(),
+ buf_bytes_)) {
+ bool good = true;
+ for (const auto& v : local_buf) {
+ if (v != local_buf[0]) good = false;
+ }
+ if (good) {
+ good_reads.fetch_add(1, std::memory_order_relaxed);
+ } else {
+ bad_reads.fetch_add(1, std::memory_order_relaxed);
+ }
+ } else {
+ unsuccessful_reads.fetch_add(1, std::memory_order_relaxed);
+ }
+ }
+ });
+ }
+ while (unsuccessful_reads.load(std::memory_order_relaxed) < num_threads_) {
+ absl::SleepFor(absl::Milliseconds(1));
+ }
+ seq_lock.MarkInitialized();
+
+ // Run a maximum of 5 seconds. On Windows, the scheduler behavior seems
+ // somewhat unfair and without an explicit timeout for this loop, the tests
+ // can run a long time.
+ absl::Time deadline = absl::Now() + absl::Seconds(5);
+ for (int i = 0; i < 100 && absl::Now() < deadline; i++) {
+ absl::FixedArray<char> writer_buf(buf_bytes_);
+ for (auto& v : writer_buf) v = i;
+ seq_lock.Write(protected_buf.data(), writer_buf.data(), buf_bytes_);
+ absl::SleepFor(absl::Microseconds(10));
+ }
+ stop.store(true, std::memory_order_relaxed);
+ for (auto& t : threads) t.join();
+ ASSERT_GE(good_reads, 0);
+ ASSERT_EQ(bad_reads, 0);
+}
+
+// Simple helper for generating a range of thread counts.
+// Generates [low, low*scale, low*scale^2, ...high)
+// (even if high is between low*scale^k and low*scale^(k+1)).
+std::vector<int> MultiplicativeRange(int low, int high, int scale) {
+ std::vector<int> result;
+ for (int current = low; current < high; current *= scale) {
+ result.push_back(current);
+ }
+ result.push_back(high);
+ return result;
+}
+
+#ifndef ABSL_HAVE_THREAD_SANITIZER
+const int kMaxThreads = absl::base_internal::NumCPUs();
+#else
+// With TSAN, a lot of threads contending for atomic access on the sequence
+// lock make this test run too slowly.
+const int kMaxThreads = std::min(absl::base_internal::NumCPUs(), 4);
+#endif
+
+// Return all of the interesting buffer sizes worth testing:
+// powers of two and adjacent values.
+std::vector<int> InterestingBufferSizes() {
+ std::vector<int> ret;
+ for (int v : MultiplicativeRange(1, 128, 2)) {
+ ret.push_back(v);
+ if (v > 1) {
+ ret.push_back(v - 1);
+ }
+ ret.push_back(v + 1);
+ }
+ return ret;
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ TestManyByteSizes, ConcurrentSequenceLockTest,
+ testing::Combine(
+ // Buffer size (bytes).
+ testing::ValuesIn(InterestingBufferSizes()),
+ // Number of reader threads.
+ testing::ValuesIn(MultiplicativeRange(1, kMaxThreads, 2))));
+
+// Simple single-threaded test, parameterized by the size of the buffer to be
+// protected.
+class SequenceLockTest : public testing::TestWithParam<int> {};
+
+TEST_P(SequenceLockTest, SingleThreaded) {
+ const int size = GetParam();
+ absl::FixedArray<std::atomic<uint64_t>> protected_buf(
+ flags::AlignUp(size, sizeof(uint64_t)) / sizeof(uint64_t));
+
+ flags::SequenceLock seq_lock;
+ seq_lock.MarkInitialized();
+
+ std::vector<char> src_buf(size, 'x');
+ seq_lock.Write(protected_buf.data(), src_buf.data(), size);
+
+ std::vector<char> dst_buf(size, '0');
+ ASSERT_TRUE(seq_lock.TryRead(dst_buf.data(), protected_buf.data(), size));
+ ASSERT_EQ(src_buf, dst_buf);
+}
+INSTANTIATE_TEST_SUITE_P(TestManyByteSizes, SequenceLockTest,
+ // Buffer size (bytes).
+ testing::Range(1, 128));
+
+} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/internal/type_erased.cc b/third_party/abseil-cpp/absl/flags/internal/type_erased.cc
deleted file mode 100644
index 490bc4ebae..0000000000
--- a/third_party/abseil-cpp/absl/flags/internal/type_erased.cc
+++ /dev/null
@@ -1,90 +0,0 @@
-//
-// Copyright 2019 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "absl/flags/internal/type_erased.h"
-
-#include <assert.h>
-
-#include <string>
-
-#include "absl/base/config.h"
-#include "absl/base/internal/raw_logging.h"
-#include "absl/flags/internal/commandlineflag.h"
-#include "absl/flags/internal/registry.h"
-#include "absl/flags/usage_config.h"
-#include "absl/strings/string_view.h"
-
-namespace absl {
-ABSL_NAMESPACE_BEGIN
-namespace flags_internal {
-
-bool GetCommandLineOption(absl::string_view name, std::string* value) {
- if (name.empty()) return false;
- assert(value);
-
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
- if (flag == nullptr || flag->IsRetired()) {
- return false;
- }
-
- *value = flag->CurrentValue();
- return true;
-}
-
-bool SetCommandLineOption(absl::string_view name, absl::string_view value) {
- return SetCommandLineOptionWithMode(name, value,
- flags_internal::SET_FLAGS_VALUE);
-}
-
-bool SetCommandLineOptionWithMode(absl::string_view name,
- absl::string_view value,
- FlagSettingMode set_mode) {
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
-
- if (!flag || flag->IsRetired()) return false;
-
- std::string error;
- if (!flag->SetFromString(value, set_mode, kProgrammaticChange, &error)) {
- // Errors here are all of the form: the provided name was a recognized
- // flag, but the value was invalid (bad type, or validation failed).
- flags_internal::ReportUsageError(error, false);
- return false;
- }
-
- return true;
-}
-
-// --------------------------------------------------------------------
-
-bool IsValidFlagValue(absl::string_view name, absl::string_view value) {
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
-
- return flag != nullptr &&
- (flag->IsRetired() || flag->ValidateInputValue(value));
-}
-
-// --------------------------------------------------------------------
-
-bool SpecifiedOnCommandLine(absl::string_view name) {
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
- if (flag != nullptr && !flag->IsRetired()) {
- return flag->IsSpecifiedOnCommandLine();
- }
- return false;
-}
-
-} // namespace flags_internal
-ABSL_NAMESPACE_END
-} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/internal/type_erased.h b/third_party/abseil-cpp/absl/flags/internal/type_erased.h
deleted file mode 100644
index 188429c771..0000000000
--- a/third_party/abseil-cpp/absl/flags/internal/type_erased.h
+++ /dev/null
@@ -1,90 +0,0 @@
-//
-// Copyright 2019 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#ifndef ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
-#define ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
-
-#include <string>
-
-#include "absl/base/config.h"
-#include "absl/flags/internal/commandlineflag.h"
-#include "absl/flags/internal/registry.h"
-#include "absl/strings/string_view.h"
-
-// --------------------------------------------------------------------
-// Registry interfaces operating on type erased handles.
-
-namespace absl {
-ABSL_NAMESPACE_BEGIN
-namespace flags_internal {
-
-// If a flag named "name" exists, store its current value in *OUTPUT
-// and return true. Else return false without changing *OUTPUT.
-// Thread-safe.
-bool GetCommandLineOption(absl::string_view name, std::string* value);
-
-// Set the value of the flag named "name" to value. If successful,
-// returns true. If not successful (e.g., the flag was not found or
-// the value is not a valid value), returns false.
-// Thread-safe.
-bool SetCommandLineOption(absl::string_view name, absl::string_view value);
-
-bool SetCommandLineOptionWithMode(absl::string_view name,
- absl::string_view value,
- FlagSettingMode set_mode);
-
-//-----------------------------------------------------------------------------
-
-// Returns true iff all of the following conditions are true:
-// (a) "name" names a registered flag
-// (b) "value" can be parsed succesfully according to the type of the flag
-// (c) parsed value passes any validator associated with the flag
-bool IsValidFlagValue(absl::string_view name, absl::string_view value);
-
-//-----------------------------------------------------------------------------
-
-// Returns true iff a flag named "name" was specified on the command line
-// (either directly, or via one of --flagfile or --fromenv or --tryfromenv).
-//
-// Any non-command-line modification of the flag does not affect the
-// result of this function. So for example, if a flag was passed on
-// the command line but then reset via SET_FLAGS_DEFAULT, this
-// function will still return true.
-bool SpecifiedOnCommandLine(absl::string_view name);
-
-//-----------------------------------------------------------------------------
-
-// If a flag with specified "name" exists and has type T, store
-// its current value in *dst and return true. Else return false
-// without touching *dst. T must obey all of the requirements for
-// types passed to DEFINE_FLAG.
-template <typename T>
-inline bool GetByName(absl::string_view name, T* dst) {
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
- if (!flag) return false;
-
- if (auto val = flag->Get<T>()) {
- *dst = *val;
- return true;
- }
-
- return false;
-}
-
-} // namespace flags_internal
-ABSL_NAMESPACE_END
-} // namespace absl
-
-#endif // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
diff --git a/third_party/abseil-cpp/absl/flags/internal/type_erased_test.cc b/third_party/abseil-cpp/absl/flags/internal/type_erased_test.cc
deleted file mode 100644
index 4ce5981047..0000000000
--- a/third_party/abseil-cpp/absl/flags/internal/type_erased_test.cc
+++ /dev/null
@@ -1,157 +0,0 @@
-//
-// Copyright 2019 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "absl/flags/internal/type_erased.h"
-
-#include <memory>
-#include <string>
-
-#include "gtest/gtest.h"
-#include "absl/flags/flag.h"
-#include "absl/flags/internal/commandlineflag.h"
-#include "absl/flags/internal/registry.h"
-#include "absl/flags/marshalling.h"
-#include "absl/memory/memory.h"
-
-ABSL_FLAG(int, int_flag, 1, "int_flag help");
-ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
-ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
-
-namespace {
-
-namespace flags = absl::flags_internal;
-
-class TypeErasedTest : public testing::Test {
- protected:
- void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
- void TearDown() override { flag_saver_.reset(); }
-
- private:
- std::unique_ptr<flags::FlagSaver> flag_saver_;
-};
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestGetCommandLineOption) {
- std::string value;
- EXPECT_TRUE(flags::GetCommandLineOption("int_flag", &value));
- EXPECT_EQ(value, "1");
-
- EXPECT_TRUE(flags::GetCommandLineOption("string_flag", &value));
- EXPECT_EQ(value, "dflt");
-
- EXPECT_FALSE(flags::GetCommandLineOption("bool_retired_flag", &value));
-
- EXPECT_FALSE(flags::GetCommandLineOption("unknown_flag", &value));
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestSetCommandLineOption) {
- EXPECT_TRUE(flags::SetCommandLineOption("int_flag", "101"));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
-
- EXPECT_TRUE(flags::SetCommandLineOption("string_flag", "asdfgh"));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
-
- EXPECT_FALSE(flags::SetCommandLineOption("bool_retired_flag", "true"));
-
- EXPECT_FALSE(flags::SetCommandLineOption("unknown_flag", "true"));
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_VALUE) {
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
- flags::SET_FLAGS_VALUE));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
-
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
- flags::SET_FLAGS_VALUE));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
- flags::SET_FLAGS_VALUE));
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
- flags::SET_FLAGS_VALUE));
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAG_IF_DEFAULT) {
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
- flags::SET_FLAG_IF_DEFAULT));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
-
- // This semantic is broken. We return true instead of false. Value is not
- // updated.
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
- flags::SET_FLAG_IF_DEFAULT));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
-
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
- flags::SET_FLAG_IF_DEFAULT));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
- flags::SET_FLAG_IF_DEFAULT));
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
- flags::SET_FLAG_IF_DEFAULT));
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_DEFAULT) {
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
- flags::SET_FLAGS_DEFAULT));
-
- // Set it again to ensure that resetting logic is covered.
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "102",
- flags::SET_FLAGS_DEFAULT));
-
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "103",
- flags::SET_FLAGS_DEFAULT));
-
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
- flags::SET_FLAGS_DEFAULT));
- EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
- flags::SET_FLAGS_DEFAULT));
-
- EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
- flags::SET_FLAGS_DEFAULT));
-
- // This should be successfull, since flag is still is not set
- EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
- flags::SET_FLAG_IF_DEFAULT));
- EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 202);
-}
-
-// --------------------------------------------------------------------
-
-TEST_F(TypeErasedTest, TestIsValidFlagValue) {
- EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "57"));
- EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "-101"));
- EXPECT_FALSE(flags::IsValidFlagValue("int_flag", "1.1"));
-
- EXPECT_TRUE(flags::IsValidFlagValue("string_flag", "#%^#%^$%DGHDG$W%adsf"));
-
- EXPECT_TRUE(flags::IsValidFlagValue("bool_retired_flag", "true"));
-}
-
-} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/internal/usage.cc b/third_party/abseil-cpp/absl/flags/internal/usage.cc
index ff90716194..949709e883 100644
--- a/third_party/abseil-cpp/absl/flags/internal/usage.cc
+++ b/third_party/abseil-cpp/absl/flags/internal/usage.cc
@@ -15,6 +15,8 @@
#include "absl/flags/internal/usage.h"
+#include <stdint.h>
+
#include <functional>
#include <map>
#include <ostream>
@@ -23,10 +25,11 @@
#include <vector>
#include "absl/base/config.h"
+#include "absl/flags/commandlineflag.h"
#include "absl/flags/flag.h"
-#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/flag.h"
#include "absl/flags/internal/path_util.h"
+#include "absl/flags/internal/private_handle_accessor.h"
#include "absl/flags/internal/program_name.h"
#include "absl/flags/internal/registry.h"
#include "absl/flags/usage_config.h"
@@ -34,46 +37,25 @@
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
-ABSL_FLAG(bool, help, false,
- "show help on important flags for this binary [tip: all flags can "
- "have two dashes]");
-ABSL_FLAG(bool, helpfull, false, "show help on all flags");
-ABSL_FLAG(bool, helpshort, false,
- "show help on only the main module for this program");
-ABSL_FLAG(bool, helppackage, false,
- "show help on all modules in the main package");
-ABSL_FLAG(bool, version, false, "show version and build info and exit");
-ABSL_FLAG(bool, only_check_args, false, "exit after checking all flags");
-ABSL_FLAG(std::string, helpon, "",
- "show help on the modules named by this flag value");
-ABSL_FLAG(std::string, helpmatch, "",
- "show help on modules whose name contains the specified substr");
+// Dummy global variables to prevent anyone else defining these.
+bool FLAGS_help = false;
+bool FLAGS_helpfull = false;
+bool FLAGS_helpshort = false;
+bool FLAGS_helppackage = false;
+bool FLAGS_version = false;
+bool FLAGS_only_check_args = false;
+bool FLAGS_helpon = false;
+bool FLAGS_helpmatch = false;
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace flags_internal {
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
+using PerFlagFilter = std::function<bool(const absl::CommandLineFlag&)>;
- if (flag.IsOfType<std::string>()) {
- return "string";
- }
-
- return "";
-}
+// Maximum length size in a human readable format.
+constexpr size_t kHrfMaxLineLength = 80;
// 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.
@@ -127,21 +109,24 @@ class FlagHelpPrettyPrinter {
public:
// Pretty printer holds on to the std::ostream& reference to direct an output
// to that stream.
- FlagHelpPrettyPrinter(int max_line_len, std::ostream* out)
- : out_(*out),
+ FlagHelpPrettyPrinter(size_t max_line_len, size_t min_line_len,
+ size_t wrapped_line_indent, std::ostream& out)
+ : out_(out),
max_line_len_(max_line_len),
+ min_line_len_(min_line_len),
+ wrapped_line_indent_(wrapped_line_indent),
line_len_(0),
first_line_(true) {}
void Write(absl::string_view str, bool wrap_line = false) {
- // Empty std::string - do nothing.
+ // Empty string - do nothing.
if (str.empty()) return;
std::vector<absl::string_view> tokens;
if (wrap_line) {
for (auto line : absl::StrSplit(str, absl::ByAnyChar("\n\r"))) {
if (!tokens.empty()) {
- // Keep line separators in the input std::string.
+ // Keep line separators in the input string.
tokens.push_back("\n");
}
for (auto token :
@@ -156,14 +141,15 @@ class FlagHelpPrettyPrinter {
for (auto token : tokens) {
bool new_line = (line_len_ == 0);
- // Respect line separators in the input std::string.
+ // Respect line separators in the input string.
if (token == "\n") {
EndLine();
continue;
}
- // Write the token, ending the std::string first if necessary/possible.
- if (!new_line && (line_len_ + token.size() >= max_line_len_)) {
+ // Write the token, ending the string first if necessary/possible.
+ if (!new_line &&
+ (line_len_ + static_cast<int>(token.size()) >= max_line_len_)) {
EndLine();
new_line = true;
}
@@ -182,13 +168,12 @@ class FlagHelpPrettyPrinter {
void StartLine() {
if (first_line_) {
- out_ << " ";
- line_len_ = 4;
+ line_len_ = min_line_len_;
first_line_ = false;
} else {
- out_ << " ";
- line_len_ = 6;
+ line_len_ = min_line_len_ + wrapped_line_indent_;
}
+ out_ << std::string(line_len_, ' ');
}
void EndLine() {
out_ << '\n';
@@ -197,14 +182,15 @@ class FlagHelpPrettyPrinter {
private:
std::ostream& out_;
- const int max_line_len_;
- int line_len_;
+ const size_t max_line_len_;
+ const size_t min_line_len_;
+ const size_t wrapped_line_indent_;
+ size_t line_len_;
bool first_line_;
};
-void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag,
- std::ostream* out) {
- FlagHelpPrettyPrinter printer(80, out); // Max line length is 80.
+void FlagHelpHumanReadable(const CommandLineFlag& flag, std::ostream& out) {
+ FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 4, 2, out);
// Flag name.
printer.Write(absl::StrCat("--", flag.Name()));
@@ -212,23 +198,20 @@ void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag,
// Flag help.
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
// definition in the originating source file, unless the value has
// subsequently been modified using SetCommandLineOption() with mode
// SET_FLAGS_DEFAULT.
std::string dflt_val = flag.DefaultValue();
+ std::string curr_val = flag.CurrentValue();
+ bool is_modified = curr_val != dflt_val;
+
if (flag.IsOfType<std::string>()) {
dflt_val = absl::StrCat("\"", dflt_val, "\"");
}
printer.Write(absl::StrCat("default: ", dflt_val, ";"));
- if (flag.IsModified()) {
- std::string curr_val = flag.CurrentValue();
+ if (is_modified) {
if (flag.IsOfType<std::string>()) {
curr_val = absl::StrCat("\"", curr_val, "\"");
}
@@ -243,7 +226,7 @@ void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag,
// If a flag's help message has been stripped (e.g. by adding '#define
// STRIP_FLAG_HELP 1' then this flag will not be displayed by '--help'
// and its variants.
-void FlagsHelpImpl(std::ostream& out, flags_internal::FlagKindFilter filter_cb,
+void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb,
HelpFormat format, absl::string_view program_usage_message) {
if (format == HelpFormat::kHumanReadable) {
out << flags_internal::ShortProgramInvocationName() << ": "
@@ -262,50 +245,54 @@ void FlagsHelpImpl(std::ostream& out, flags_internal::FlagKindFilter filter_cb,
<< XMLElement("usage", program_usage_message) << '\n';
}
- // Map of package name to
+ // Ordered map of package name to
// map of file name to
// vector of flags in the file.
// This map is used to output matching flags grouped by package and file
// name.
std::map<std::string,
- std::map<std::string,
- std::vector<const flags_internal::CommandLineFlag*>>>
+ std::map<std::string, std::vector<const absl::CommandLineFlag*>>>
matching_flags;
- flags_internal::ForEachFlag([&](flags_internal::CommandLineFlag* flag) {
- std::string flag_filename = flag->Filename();
-
+ flags_internal::ForEachFlag([&](absl::CommandLineFlag& flag) {
// Ignore retired flags.
- if (flag->IsRetired()) return;
+ if (flag.IsRetired()) return;
// If the flag has been stripped, pretend that it doesn't exist.
- if (flag->Help() == flags_internal::kStrippedFlagHelp) return;
+ if (flag.Help() == flags_internal::kStrippedFlagHelp) return;
// Make sure flag satisfies the filter
- if (!filter_cb || !filter_cb(flag_filename)) return;
+ if (!filter_cb(flag)) return;
+
+ std::string flag_filename = flag.Filename();
matching_flags[std::string(flags_internal::Package(flag_filename))]
[flag_filename]
- .push_back(flag);
+ .push_back(&flag);
});
- absl::string_view
- package_separator; // controls blank lines between packages.
- absl::string_view file_separator; // controls blank lines between files.
- for (const auto& package : matching_flags) {
+ absl::string_view package_separator; // controls blank lines between packages
+ absl::string_view file_separator; // controls blank lines between files
+ for (auto& package : matching_flags) {
if (format == HelpFormat::kHumanReadable) {
out << package_separator;
package_separator = "\n\n";
}
file_separator = "";
- for (const auto& flags_in_file : package.second) {
+ for (auto& flags_in_file : package.second) {
if (format == HelpFormat::kHumanReadable) {
out << file_separator << " Flags from " << flags_in_file.first
<< ":\n";
file_separator = "\n";
}
+ std::sort(std::begin(flags_in_file.second),
+ std::end(flags_in_file.second),
+ [](const CommandLineFlag* lhs, const CommandLineFlag* rhs) {
+ return lhs->Name() < rhs->Name();
+ });
+
for (const auto* flag : flags_in_file.second) {
flags_internal::FlagHelp(out, *flag, format);
}
@@ -313,27 +300,46 @@ void FlagsHelpImpl(std::ostream& out, flags_internal::FlagKindFilter filter_cb,
}
if (format == HelpFormat::kHumanReadable) {
+ FlagHelpPrettyPrinter printer(kHrfMaxLineLength, 0, 0, out);
+
if (filter_cb && matching_flags.empty()) {
- out << " No modules matched: use -helpfull\n";
+ printer.Write("No flags matched.\n", true);
}
+ printer.EndLine();
+ printer.Write(
+ "Try --helpfull to get a list of all flags or --help=substring "
+ "shows help for flags which include specified substring in either "
+ "in the name, or description or path.\n",
+ true);
} else {
// The end of the document.
out << "</AllFlags>\n";
}
}
+void FlagsHelpImpl(std::ostream& out,
+ flags_internal::FlagKindFilter filename_filter_cb,
+ HelpFormat format, absl::string_view program_usage_message) {
+ FlagsHelpImpl(
+ out,
+ [&](const absl::CommandLineFlag& flag) {
+ return filename_filter_cb && filename_filter_cb(flag.Filename());
+ },
+ format, program_usage_message);
+}
+
} // namespace
// --------------------------------------------------------------------
// Produces the help message describing specific flag.
-void FlagHelp(std::ostream& out, const flags_internal::CommandLineFlag& flag,
+void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
HelpFormat format) {
if (format == HelpFormat::kHumanReadable)
- flags_internal::FlagHelpHumanReadable(flag, &out);
+ flags_internal::FlagHelpHumanReadable(flag, out);
}
// --------------------------------------------------------------------
-// Produces the help messages for all flags matching the filter.
+// Produces the help messages for all flags matching the filename filter.
// If filter is empty produces help messages for all flags.
void FlagsHelp(std::ostream& out, absl::string_view filter, HelpFormat format,
absl::string_view program_usage_message) {
@@ -348,66 +354,169 @@ void FlagsHelp(std::ostream& out, absl::string_view filter, HelpFormat format,
// If so, handles them appropriately.
int HandleUsageFlags(std::ostream& out,
absl::string_view program_usage_message) {
- if (absl::GetFlag(FLAGS_helpshort)) {
- flags_internal::FlagsHelpImpl(
- out, flags_internal::GetUsageConfig().contains_helpshort_flags,
- HelpFormat::kHumanReadable, program_usage_message);
- return 1;
- }
+ switch (GetFlagsHelpMode()) {
+ case HelpMode::kNone:
+ break;
+ case HelpMode::kImportant:
+ flags_internal::FlagsHelpImpl(
+ out, flags_internal::GetUsageConfig().contains_help_flags,
+ GetFlagsHelpFormat(), program_usage_message);
+ return 1;
+
+ case HelpMode::kShort:
+ flags_internal::FlagsHelpImpl(
+ out, flags_internal::GetUsageConfig().contains_helpshort_flags,
+ GetFlagsHelpFormat(), program_usage_message);
+ return 1;
+
+ case HelpMode::kFull:
+ flags_internal::FlagsHelp(out, "", GetFlagsHelpFormat(),
+ program_usage_message);
+ return 1;
+
+ case HelpMode::kPackage:
+ flags_internal::FlagsHelpImpl(
+ out, flags_internal::GetUsageConfig().contains_helppackage_flags,
+ GetFlagsHelpFormat(), program_usage_message);
+
+ return 1;
+
+ case HelpMode::kMatch: {
+ std::string substr = GetFlagsHelpMatchSubstr();
+ if (substr.empty()) {
+ // show all options
+ flags_internal::FlagsHelp(out, substr, GetFlagsHelpFormat(),
+ program_usage_message);
+ } else {
+ auto filter_cb = [&substr](const absl::CommandLineFlag& flag) {
+ if (absl::StrContains(flag.Name(), substr)) return true;
+ if (absl::StrContains(flag.Filename(), substr)) return true;
+ if (absl::StrContains(flag.Help(), substr)) return true;
+
+ return false;
+ };
+ flags_internal::FlagsHelpImpl(
+ out, filter_cb, HelpFormat::kHumanReadable, program_usage_message);
+ }
- if (absl::GetFlag(FLAGS_helpfull)) {
- // show all options
- flags_internal::FlagsHelp(out, "", HelpFormat::kHumanReadable,
- program_usage_message);
- return 1;
+ return 1;
+ }
+ case HelpMode::kVersion:
+ if (flags_internal::GetUsageConfig().version_string)
+ out << flags_internal::GetUsageConfig().version_string();
+ // Unlike help, we may be asking for version in a script, so return 0
+ return 0;
+
+ case HelpMode::kOnlyCheckArgs:
+ return 0;
}
- if (!absl::GetFlag(FLAGS_helpon).empty()) {
- flags_internal::FlagsHelp(
- out, absl::StrCat("/", absl::GetFlag(FLAGS_helpon), "."),
- HelpFormat::kHumanReadable, program_usage_message);
- return 1;
- }
+ return -1;
+}
- if (!absl::GetFlag(FLAGS_helpmatch).empty()) {
- flags_internal::FlagsHelp(out, absl::GetFlag(FLAGS_helpmatch),
- HelpFormat::kHumanReadable,
- program_usage_message);
- return 1;
- }
+// --------------------------------------------------------------------
+// Globals representing usage reporting flags
+
+namespace {
- if (absl::GetFlag(FLAGS_help)) {
- flags_internal::FlagsHelpImpl(
- out, flags_internal::GetUsageConfig().contains_help_flags,
- HelpFormat::kHumanReadable, program_usage_message);
+ABSL_CONST_INIT absl::Mutex help_attributes_guard(absl::kConstInit);
+ABSL_CONST_INIT std::string* match_substr
+ ABSL_GUARDED_BY(help_attributes_guard) = nullptr;
+ABSL_CONST_INIT HelpMode help_mode ABSL_GUARDED_BY(help_attributes_guard) =
+ HelpMode::kNone;
+ABSL_CONST_INIT HelpFormat help_format ABSL_GUARDED_BY(help_attributes_guard) =
+ HelpFormat::kHumanReadable;
- out << "\nTry --helpfull to get a list of all flags.\n";
+} // namespace
- return 1;
- }
+std::string GetFlagsHelpMatchSubstr() {
+ absl::MutexLock l(&help_attributes_guard);
+ if (match_substr == nullptr) return "";
+ return *match_substr;
+}
+
+void SetFlagsHelpMatchSubstr(absl::string_view substr) {
+ absl::MutexLock l(&help_attributes_guard);
+ if (match_substr == nullptr) match_substr = new std::string;
+ match_substr->assign(substr.data(), substr.size());
+}
+
+HelpMode GetFlagsHelpMode() {
+ absl::MutexLock l(&help_attributes_guard);
+ return help_mode;
+}
- if (absl::GetFlag(FLAGS_helppackage)) {
- flags_internal::FlagsHelpImpl(
- out, flags_internal::GetUsageConfig().contains_helppackage_flags,
- HelpFormat::kHumanReadable, program_usage_message);
+void SetFlagsHelpMode(HelpMode mode) {
+ absl::MutexLock l(&help_attributes_guard);
+ help_mode = mode;
+}
- out << "\nTry --helpfull to get a list of all flags.\n";
+HelpFormat GetFlagsHelpFormat() {
+ absl::MutexLock l(&help_attributes_guard);
+ return help_format;
+}
- return 1;
+void SetFlagsHelpFormat(HelpFormat format) {
+ absl::MutexLock l(&help_attributes_guard);
+ help_format = format;
+}
+
+// Deduces usage flags from the input argument in a form --name=value or
+// --name. argument is already split into name and value before we call this
+// function.
+bool DeduceUsageFlags(absl::string_view name, absl::string_view value) {
+ if (absl::ConsumePrefix(&name, "help")) {
+ if (name == "") {
+ if (value.empty()) {
+ SetFlagsHelpMode(HelpMode::kImportant);
+ } else {
+ SetFlagsHelpMode(HelpMode::kMatch);
+ SetFlagsHelpMatchSubstr(value);
+ }
+ return true;
+ }
+
+ if (name == "match") {
+ SetFlagsHelpMode(HelpMode::kMatch);
+ SetFlagsHelpMatchSubstr(value);
+ return true;
+ }
+
+ if (name == "on") {
+ SetFlagsHelpMode(HelpMode::kMatch);
+ SetFlagsHelpMatchSubstr(absl::StrCat("/", value, "."));
+ return true;
+ }
+
+ if (name == "full") {
+ SetFlagsHelpMode(HelpMode::kFull);
+ return true;
+ }
+
+ if (name == "short") {
+ SetFlagsHelpMode(HelpMode::kShort);
+ return true;
+ }
+
+ if (name == "package") {
+ SetFlagsHelpMode(HelpMode::kPackage);
+ return true;
+ }
+
+ return false;
}
- if (absl::GetFlag(FLAGS_version)) {
- if (flags_internal::GetUsageConfig().version_string)
- out << flags_internal::GetUsageConfig().version_string();
- // Unlike help, we may be asking for version in a script, so return 0
- return 0;
+ if (name == "version") {
+ SetFlagsHelpMode(HelpMode::kVersion);
+ return true;
}
- if (absl::GetFlag(FLAGS_only_check_args)) {
- return 0;
+ if (name == "only_check_args") {
+ SetFlagsHelpMode(HelpMode::kOnlyCheckArgs);
+ return true;
}
- return -1;
+ return false;
}
} // namespace flags_internal
diff --git a/third_party/abseil-cpp/absl/flags/internal/usage.h b/third_party/abseil-cpp/absl/flags/internal/usage.h
index 6b080fd1ee..c0bcac5762 100644
--- a/third_party/abseil-cpp/absl/flags/internal/usage.h
+++ b/third_party/abseil-cpp/absl/flags/internal/usage.h
@@ -20,8 +20,8 @@
#include <string>
#include "absl/base/config.h"
+#include "absl/flags/commandlineflag.h"
#include "absl/flags/declare.h"
-#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
// --------------------------------------------------------------------
@@ -36,8 +36,9 @@ enum class HelpFormat {
kHumanReadable,
};
-// Outputs the help message describing specific flag.
-void FlagHelp(std::ostream& out, const flags_internal::CommandLineFlag& flag,
+// Streams the help message describing `flag` to `out`.
+// The default value for `flag` is included in the output.
+void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
HelpFormat format = HelpFormat::kHumanReadable);
// Produces the help messages for all flags matching the filter. A flag matches
@@ -65,17 +66,39 @@ void FlagsHelp(std::ostream& out, absl::string_view filter,
int HandleUsageFlags(std::ostream& out,
absl::string_view program_usage_message);
+// --------------------------------------------------------------------
+// Globals representing usage reporting flags
+
+enum class HelpMode {
+ kNone,
+ kImportant,
+ kShort,
+ kFull,
+ kPackage,
+ kMatch,
+ kVersion,
+ kOnlyCheckArgs
+};
+
+// Returns substring to filter help output (--help=substr argument)
+std::string GetFlagsHelpMatchSubstr();
+// Returns the requested help mode.
+HelpMode GetFlagsHelpMode();
+// Returns the requested help format.
+HelpFormat GetFlagsHelpFormat();
+
+// These are corresponding setters to the attributes above.
+void SetFlagsHelpMatchSubstr(absl::string_view);
+void SetFlagsHelpMode(HelpMode);
+void SetFlagsHelpFormat(HelpFormat);
+
+// Deduces usage flags from the input argument in a form --name=value or
+// --name. argument is already split into name and value before we call this
+// function.
+bool DeduceUsageFlags(absl::string_view name, absl::string_view value);
+
} // namespace flags_internal
ABSL_NAMESPACE_END
} // namespace absl
-ABSL_DECLARE_FLAG(bool, help);
-ABSL_DECLARE_FLAG(bool, helpfull);
-ABSL_DECLARE_FLAG(bool, helpshort);
-ABSL_DECLARE_FLAG(bool, helppackage);
-ABSL_DECLARE_FLAG(bool, version);
-ABSL_DECLARE_FLAG(bool, only_check_args);
-ABSL_DECLARE_FLAG(std::string, helpon);
-ABSL_DECLARE_FLAG(std::string, helpmatch);
-
#endif // ABSL_FLAGS_INTERNAL_USAGE_H_
diff --git a/third_party/abseil-cpp/absl/flags/internal/usage_test.cc b/third_party/abseil-cpp/absl/flags/internal/usage_test.cc
index e1e57e5570..044d71c87d 100644
--- a/third_party/abseil-cpp/absl/flags/internal/usage_test.cc
+++ b/third_party/abseil-cpp/absl/flags/internal/usage_test.cc
@@ -21,15 +21,13 @@
#include <string>
#include "gtest/gtest.h"
-#include "absl/flags/declare.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/parse.h"
#include "absl/flags/internal/path_util.h"
#include "absl/flags/internal/program_name.h"
-#include "absl/flags/internal/registry.h"
+#include "absl/flags/reflection.h"
#include "absl/flags/usage.h"
#include "absl/flags/usage_config.h"
-#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
@@ -47,6 +45,7 @@ static const char kTestUsageMessage[] = "Custom usage message";
struct UDT {
UDT() = default;
UDT(const UDT&) = default;
+ UDT& operator=(const UDT&) = default;
};
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
std::string AbslUnparseFlag(const UDT&) { return "UDT{}"; }
@@ -89,9 +88,14 @@ class UsageReportingTest : public testing::Test {
default_config.normalize_filename = &NormalizeFileName;
absl::SetFlagsUsageConfig(default_config);
}
+ ~UsageReportingTest() override {
+ flags::SetFlagsHelpMode(flags::HelpMode::kNone);
+ flags::SetFlagsHelpMatchSubstr("");
+ flags::SetFlagsHelpFormat(flags::HelpFormat::kHumanReadable);
+ }
private:
- flags::FlagSaver flag_saver_;
+ absl::FlagSaver flag_saver_;
};
// --------------------------------------------------------------------
@@ -103,15 +107,16 @@ TEST_F(UsageReportingDeathTest, TestSetProgramUsageMessage) {
#ifndef _WIN32
// TODO(rogeeff): figure out why this does not work on Windows.
- EXPECT_DEATH(absl::SetProgramUsageMessage("custom usage message"),
- ".*SetProgramUsageMessage\\(\\) called twice.*");
+ EXPECT_DEATH_IF_SUPPORTED(
+ absl::SetProgramUsageMessage("custom usage message"),
+ ".*SetProgramUsageMessage\\(\\) called twice.*");
#endif
}
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
- const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_01");
+ const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_01");
std::stringstream test_buf;
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
@@ -123,7 +128,7 @@ TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
}
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
- const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_02");
+ const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_02");
std::stringstream test_buf;
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
@@ -135,7 +140,7 @@ TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
}
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
- const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_03");
+ const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_03");
std::stringstream test_buf;
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
@@ -147,7 +152,7 @@ TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
}
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
- const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_04");
+ const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_04");
std::stringstream test_buf;
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
@@ -159,7 +164,7 @@ TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
}
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_05) {
- const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_05");
+ const auto* flag = absl::FindCommandLineFlag("usage_reporting_test_flag_05");
std::stringstream test_buf;
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
@@ -192,6 +197,10 @@ TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
Some more help.
Even more long long long long long long long long long long long long help
message.); default: "";
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)";
std::stringstream test_buf_01;
@@ -215,7 +224,11 @@ TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
EXPECT_EQ(test_buf_04.str(),
R"(usage_test: Custom usage message
- No modules matched: use -helpfull
+No flags matched.
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
std::stringstream test_buf_05;
@@ -227,12 +240,8 @@ TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
absl::StartsWith(test_out_str, "usage_test: Custom usage message"));
EXPECT_TRUE(absl::StrContains(
test_out_str, "Flags from absl/flags/internal/usage_test.cc:"));
- EXPECT_TRUE(absl::StrContains(test_out_str,
- "Flags from absl/flags/internal/usage.cc:"));
EXPECT_TRUE(
absl::StrContains(test_out_str, "-usage_reporting_test_flag_01 "));
- EXPECT_TRUE(absl::StrContains(test_out_str, "-help (show help"))
- << test_out_str;
}
// --------------------------------------------------------------------
@@ -245,7 +254,40 @@ TEST_F(UsageReportingTest, TestNoUsageFlags) {
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
- absl::SetFlag(&FLAGS_helpshort, true);
+ flags::SetFlagsHelpMode(flags::HelpMode::kShort);
+
+ std::stringstream test_buf;
+ EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
+ EXPECT_EQ(test_buf.str(),
+ R"(usage_test: Custom usage message
+
+ Flags from absl/flags/internal/usage_test.cc:
+ --usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
+ default: 101;
+ --usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
+ default: false;
+ --usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
+ default: 1.03;
+ --usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
+ default: 1000000000000004;
+ --usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
+ default: UDT{};
+ --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
+
+ Some more help.
+ Even more long long long long long long long long long long long long help
+ message.); default: "";
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
+)");
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(UsageReportingTest, TestUsageFlag_help_simple) {
+ flags::SetFlagsHelpMode(flags::HelpMode::kImportant);
std::stringstream test_buf;
EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
@@ -268,13 +310,42 @@ TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
Some more help.
Even more long long long long long long long long long long long long help
message.); default: "";
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
+)");
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(UsageReportingTest, TestUsageFlag_help_one_flag) {
+ flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
+ flags::SetFlagsHelpMatchSubstr("usage_reporting_test_flag_06");
+
+ std::stringstream test_buf;
+ EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
+ EXPECT_EQ(test_buf.str(),
+ R"(usage_test: Custom usage message
+
+ Flags from absl/flags/internal/usage_test.cc:
+ --usage_reporting_test_flag_06 (usage_reporting_test_flag_06 help message.
+
+ Some more help.
+ Even more long long long long long long long long long long long long help
+ message.); default: "";
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
}
// --------------------------------------------------------------------
-TEST_F(UsageReportingTest, TestUsageFlag_help) {
- absl::SetFlag(&FLAGS_help, true);
+TEST_F(UsageReportingTest, TestUsageFlag_help_multiple_flag) {
+ flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
+ flags::SetFlagsHelpMatchSubstr("test_flag");
std::stringstream test_buf;
EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
@@ -298,14 +369,16 @@ TEST_F(UsageReportingTest, TestUsageFlag_help) {
Even more long long long long long long long long long long long long help
message.); default: "";
-Try --helpfull to get a list of all flags.
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
}
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
- absl::SetFlag(&FLAGS_helppackage, true);
+ flags::SetFlagsHelpMode(flags::HelpMode::kPackage);
std::stringstream test_buf;
EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 1);
@@ -329,14 +402,16 @@ TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
Even more long long long long long long long long long long long long help
message.); default: "";
-Try --helpfull to get a list of all flags.
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
}
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestUsageFlag_version) {
- absl::SetFlag(&FLAGS_version, true);
+ flags::SetFlagsHelpMode(flags::HelpMode::kVersion);
std::stringstream test_buf;
EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0);
@@ -350,7 +425,7 @@ TEST_F(UsageReportingTest, TestUsageFlag_version) {
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
- absl::SetFlag(&FLAGS_only_check_args, true);
+ flags::SetFlagsHelpMode(flags::HelpMode::kOnlyCheckArgs);
std::stringstream test_buf;
EXPECT_EQ(flags::HandleUsageFlags(test_buf, kTestUsageMessage), 0);
@@ -360,17 +435,22 @@ TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
// --------------------------------------------------------------------
TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
- absl::SetFlag(&FLAGS_helpon, "bla-bla");
+ flags::SetFlagsHelpMode(flags::HelpMode::kMatch);
+ flags::SetFlagsHelpMatchSubstr("/bla-bla.");
std::stringstream test_buf_01;
EXPECT_EQ(flags::HandleUsageFlags(test_buf_01, kTestUsageMessage), 1);
EXPECT_EQ(test_buf_01.str(),
R"(usage_test: Custom usage message
- No modules matched: use -helpfull
+No flags matched.
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
- absl::SetFlag(&FLAGS_helpon, "usage_test");
+ flags::SetFlagsHelpMatchSubstr("/usage_test.");
std::stringstream test_buf_02;
EXPECT_EQ(flags::HandleUsageFlags(test_buf_02, kTestUsageMessage), 1);
@@ -393,6 +473,10 @@ TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
Some more help.
Even more long long long long long long long long long long long long help
message.); default: "";
+
+Try --helpfull to get a list of all flags or --help=substring shows help for
+flags which include specified substring in either in the name, or description or
+path.
)");
}
diff --git a/third_party/abseil-cpp/absl/flags/marshalling.cc b/third_party/abseil-cpp/absl/flags/marshalling.cc
index 6f2ddda8c3..81f9cebd6f 100644
--- a/third_party/abseil-cpp/absl/flags/marshalling.cc
+++ b/third_party/abseil-cpp/absl/flags/marshalling.cc
@@ -74,15 +74,16 @@ static int NumericBase(absl::string_view text) {
}
template <typename IntType>
-inline bool ParseFlagImpl(absl::string_view text, IntType* dst) {
+inline bool ParseFlagImpl(absl::string_view text, IntType& dst) {
text = absl::StripAsciiWhitespace(text);
- return absl::numbers_internal::safe_strtoi_base(text, dst, NumericBase(text));
+ return absl::numbers_internal::safe_strtoi_base(text, &dst,
+ NumericBase(text));
}
bool AbslParseFlag(absl::string_view text, short* dst, std::string*) {
int val;
- if (!ParseFlagImpl(text, &val)) return false;
+ if (!ParseFlagImpl(text, val)) return false;
if (static_cast<short>(val) != val) // worked, but number out of range
return false;
*dst = static_cast<short>(val);
@@ -91,7 +92,7 @@ bool AbslParseFlag(absl::string_view text, short* dst, std::string*) {
bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) {
unsigned int val;
- if (!ParseFlagImpl(text, &val)) return false;
+ if (!ParseFlagImpl(text, val)) return false;
if (static_cast<unsigned short>(val) !=
val) // worked, but number out of range
return false;
@@ -100,28 +101,28 @@ bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) {
}
bool AbslParseFlag(absl::string_view text, int* dst, std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
bool AbslParseFlag(absl::string_view text, long* dst, std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
bool AbslParseFlag(absl::string_view text, unsigned long long* dst,
std::string*) {
- return ParseFlagImpl(text, dst);
+ return ParseFlagImpl(text, *dst);
}
// --------------------------------------------------------------------
@@ -172,7 +173,7 @@ std::string Unparse(long long v) { return absl::StrCat(v); }
std::string Unparse(unsigned long long v) { return absl::StrCat(v); }
template <typename T>
std::string UnparseFloatingPointVal(T v) {
- // digits10 is guaranteed to roundtrip correctly in std::string -> value -> std::string
+ // digits10 is guaranteed to roundtrip correctly in string -> value -> string
// conversions, but may not be enough to represent all the values correctly.
std::string digit10_str =
absl::StrFormat("%.*g", std::numeric_limits<T>::digits10, v);
diff --git a/third_party/abseil-cpp/absl/flags/marshalling.h b/third_party/abseil-cpp/absl/flags/marshalling.h
index 0b5033547e..7cbc136d57 100644
--- a/third_party/abseil-cpp/absl/flags/marshalling.h
+++ b/third_party/abseil-cpp/absl/flags/marshalling.h
@@ -83,7 +83,7 @@
// // AbslParseFlag converts from a string to OutputMode.
// // Must be in same namespace as OutputMode.
//
-// // Parses an OutputMode from the command line flag value `text. Returns
+// // Parses an OutputMode from the command line flag value `text`. Returns
// // `true` and sets `*mode` on success; returns `false` and sets `*error`
// // on failure.
// bool AbslParseFlag(absl::string_view text,
@@ -139,7 +139,7 @@
//
// // Within the implementation, `AbslParseFlag()` will, in turn invoke
// // `absl::ParseFlag()` on its constituent `int` and `std::string` types
-// // (which have built-in Abseil flag support.
+// // (which have built-in Abseil flag support).
//
// bool AbslParseFlag(absl::string_view text, MyFlagType* flag,
// std::string* err) {
diff --git a/third_party/abseil-cpp/absl/flags/parse.cc b/third_party/abseil-cpp/absl/flags/parse.cc
index 812e498189..dd1a6796ca 100644
--- a/third_party/abseil-cpp/absl/flags/parse.cc
+++ b/third_party/abseil-cpp/absl/flags/parse.cc
@@ -34,14 +34,16 @@
#include "absl/base/config.h"
#include "absl/base/const_init.h"
#include "absl/base/thread_annotations.h"
+#include "absl/flags/commandlineflag.h"
#include "absl/flags/config.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/flag.h"
#include "absl/flags/internal/parse.h"
+#include "absl/flags/internal/private_handle_accessor.h"
#include "absl/flags/internal/program_name.h"
-#include "absl/flags/internal/registry.h"
#include "absl/flags/internal/usage.h"
+#include "absl/flags/reflection.h"
#include "absl/flags/usage.h"
#include "absl/flags/usage_config.h"
#include "absl/strings/ascii.h"
@@ -66,6 +68,22 @@ ABSL_CONST_INIT bool fromenv_needs_processing
ABSL_CONST_INIT bool tryfromenv_needs_processing
ABSL_GUARDED_BY(processing_checks_guard) = false;
+ABSL_CONST_INIT absl::Mutex specified_flags_guard(absl::kConstInit);
+ABSL_CONST_INIT std::vector<const CommandLineFlag*>* specified_flags
+ ABSL_GUARDED_BY(specified_flags_guard) = nullptr;
+
+struct SpecifiedFlagsCompare {
+ bool operator()(const CommandLineFlag* a, const CommandLineFlag* b) const {
+ return a->Name() < b->Name();
+ }
+ bool operator()(const CommandLineFlag* a, absl::string_view b) const {
+ return a->Name() < b;
+ }
+ bool operator()(absl::string_view a, const CommandLineFlag* b) const {
+ return a < b->Name();
+ }
+};
+
} // namespace
} // namespace flags_internal
ABSL_NAMESPACE_END
@@ -205,7 +223,7 @@ bool ArgsList::ReadFromFlagfile(const std::string& flag_file_name) {
// Reads the environment variable with name `name` and stores results in
// `value`. If variable is not present in environment returns false, otherwise
// returns true.
-bool GetEnvVar(const char* var_name, std::string* var_value) {
+bool GetEnvVar(const char* var_name, std::string& var_value) {
#ifdef _WIN32
char buf[1024];
auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf));
@@ -217,14 +235,14 @@ bool GetEnvVar(const char* var_name, std::string* var_value) {
return false;
}
- *var_value = std::string(buf, get_res);
+ var_value = std::string(buf, get_res);
#else
const char* val = ::getenv(var_name);
if (val == nullptr) {
return false;
}
- *var_value = val;
+ var_value = val;
#endif
return true;
@@ -272,11 +290,11 @@ std::tuple<absl::string_view, absl::string_view, bool> SplitNameAndValue(
// found flag or nullptr
// is negative in case of --nofoo
std::tuple<CommandLineFlag*, bool> LocateFlag(absl::string_view flag_name) {
- CommandLineFlag* flag = flags_internal::FindCommandLineFlag(flag_name);
+ CommandLineFlag* flag = absl::FindCommandLineFlag(flag_name);
bool is_negative = false;
if (!flag && absl::ConsumePrefix(&flag_name, "no")) {
- flag = flags_internal::FindCommandLineFlag(flag_name);
+ flag = absl::FindCommandLineFlag(flag_name);
is_negative = true;
}
@@ -289,16 +307,17 @@ std::tuple<CommandLineFlag*, bool> LocateFlag(absl::string_view flag_name) {
// back.
void CheckDefaultValuesParsingRoundtrip() {
#ifndef NDEBUG
- flags_internal::ForEachFlag([&](CommandLineFlag* flag) {
- if (flag->IsRetired()) return;
+ flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
+ if (flag.IsRetired()) return;
-#define IGNORE_TYPE(T) \
- if (flag->IsOfType<T>()) return;
+#define ABSL_FLAGS_INTERNAL_IGNORE_TYPE(T, _) \
+ if (flag.IsOfType<T>()) return;
- ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(IGNORE_TYPE)
-#undef IGNORE_TYPE
+ ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(ABSL_FLAGS_INTERNAL_IGNORE_TYPE)
+#undef ABSL_FLAGS_INTERNAL_IGNORE_TYPE
- flag->CheckDefaultValueParsingRoundtrip();
+ flags_internal::PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip(
+ flag);
});
#endif
}
@@ -311,13 +330,13 @@ void CheckDefaultValuesParsingRoundtrip() {
// the first flagfile in the input list are processed before the second flagfile
// etc.
bool ReadFlagfiles(const std::vector<std::string>& flagfiles,
- std::vector<ArgsList>* input_args) {
+ std::vector<ArgsList>& input_args) {
bool success = true;
for (auto it = flagfiles.rbegin(); it != flagfiles.rend(); ++it) {
ArgsList al;
if (al.ReadFromFlagfile(*it)) {
- input_args->push_back(al);
+ input_args.push_back(al);
} else {
success = false;
}
@@ -332,7 +351,7 @@ bool ReadFlagfiles(const std::vector<std::string>& flagfiles,
// `flag_name` is a string from the input flag_names list. If successful we
// append a single ArgList at the end of the input_args.
bool ReadFlagsFromEnv(const std::vector<std::string>& flag_names,
- std::vector<ArgsList>* input_args,
+ std::vector<ArgsList>& input_args,
bool fail_on_absent_in_env) {
bool success = true;
std::vector<std::string> args;
@@ -353,7 +372,7 @@ bool ReadFlagsFromEnv(const std::vector<std::string>& flag_names,
const std::string envname = absl::StrCat("FLAGS_", flag_name);
std::string envval;
- if (!GetEnvVar(envname.c_str(), &envval)) {
+ if (!GetEnvVar(envname.c_str(), envval)) {
if (fail_on_absent_in_env) {
flags_internal::ReportUsageError(
absl::StrCat(envname, " not found in environment"), true);
@@ -368,7 +387,7 @@ bool ReadFlagsFromEnv(const std::vector<std::string>& flag_names,
}
if (success) {
- input_args->emplace_back(args);
+ input_args.emplace_back(args);
}
return success;
@@ -378,8 +397,8 @@ bool ReadFlagsFromEnv(const std::vector<std::string>& flag_names,
// Returns success status, which is true if were able to handle all generator
// flags (flagfile, fromenv, tryfromemv) successfully.
-bool HandleGeneratorFlags(std::vector<ArgsList>* input_args,
- std::vector<std::string>* flagfile_value) {
+bool HandleGeneratorFlags(std::vector<ArgsList>& input_args,
+ std::vector<std::string>& flagfile_value) {
bool success = true;
absl::MutexLock l(&flags_internal::processing_checks_guard);
@@ -404,9 +423,9 @@ bool HandleGeneratorFlags(std::vector<ArgsList>* input_args,
if (flags_internal::flagfile_needs_processing) {
auto flagfiles = absl::GetFlag(FLAGS_flagfile);
- if (input_args->size() == 1) {
- flagfile_value->insert(flagfile_value->end(), flagfiles.begin(),
- flagfiles.end());
+ if (input_args.size() == 1) {
+ flagfile_value.insert(flagfile_value.end(), flagfiles.begin(),
+ flagfiles.end());
}
success &= ReadFlagfiles(flagfiles, input_args);
@@ -533,10 +552,10 @@ std::tuple<bool, absl::string_view> DeduceFlagValue(const CommandLineFlag& flag,
curr_list->PopFront();
value = curr_list->Front();
- // Heuristic to detect the case where someone treats a std::string arg
+ // Heuristic to detect the case where someone treats a string arg
// like a bool or just forgets to pass a value:
// --my_string_var --foo=bar
- // We look for a flag of std::string type, whose value begins with a
+ // We look for a flag of string type, whose value begins with a
// dash and corresponds to known flag or standalone --.
if (!value.empty() && value[0] == '-' && flag.IsOfType<std::string>()) {
auto maybe_flag_name = std::get<0>(SplitNameAndValue(value.substr(1)));
@@ -575,12 +594,28 @@ bool CanIgnoreUndefinedFlag(absl::string_view flag_name) {
// --------------------------------------------------------------------
+bool WasPresentOnCommandLine(absl::string_view flag_name) {
+ absl::MutexLock l(&specified_flags_guard);
+ ABSL_INTERNAL_CHECK(specified_flags != nullptr,
+ "ParseCommandLine is not invoked yet");
+
+ return std::binary_search(specified_flags->begin(), specified_flags->end(),
+ flag_name, SpecifiedFlagsCompare{});
+}
+
+// --------------------------------------------------------------------
+
std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
ArgvListAction arg_list_act,
UsageFlagsAction usage_flag_act,
OnUndefinedFlag on_undef_flag) {
ABSL_INTERNAL_CHECK(argc > 0, "Missing argv[0]");
+ // Once parsing has started we will not have more flag registrations.
+ // If we did, they would be missing during parsing, which is a problem on
+ // itself.
+ flags_internal::FinalizeRegistry();
+
// This routine does not return anything since we abort on failure.
CheckDefaultValuesParsingRoundtrip();
@@ -605,13 +640,20 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
}
output_args.push_back(argv[0]);
+ absl::MutexLock l(&specified_flags_guard);
+ if (specified_flags == nullptr) {
+ specified_flags = new std::vector<const CommandLineFlag*>;
+ } else {
+ specified_flags->clear();
+ }
+
// Iterate through the list of the input arguments. First level are arguments
// originated from argc/argv. Following levels are arguments originated from
// recursive parsing of flagfile(s).
bool success = true;
while (!input_args.empty()) {
// 10. First we process the built-in generator flags.
- success &= HandleGeneratorFlags(&input_args, &flagfile_value);
+ success &= HandleGeneratorFlags(input_args, flagfile_value);
// 30. Select top-most (most recent) arguments list. If it is empty drop it
// and re-try.
@@ -646,7 +688,7 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
// 60. Split the current argument on '=' to figure out the argument
// name and value. If flag name is empty it means we've got "--". value
- // can be empty either if there were no '=' in argument std::string at all or
+ // can be empty either if there were no '=' in argument string at all or
// an argument looked like "--foo=". In a latter case is_empty_value is
// true.
absl::string_view flag_name;
@@ -671,6 +713,11 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
std::tie(flag, is_negative) = LocateFlag(flag_name);
if (flag == nullptr) {
+ // Usage flags are not modeled as Abseil flags. Locate them separately.
+ if (flags_internal::DeduceUsageFlags(flag_name, value)) {
+ continue;
+ }
+
if (on_undef_flag != OnUndefinedFlag::kIgnoreUndefined) {
undefined_flag_names.emplace_back(arg_from_argv,
std::string(flag_name));
@@ -692,13 +739,17 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
}
// 100. Set the located flag to a new new value, unless it is retired.
- // Setting retired flag fails, but we ignoring it here.
- if (flag->IsRetired()) continue;
-
+ // Setting retired flag fails, but we ignoring it here while also reporting
+ // access to retired flag.
std::string error;
- if (!flag->SetFromString(value, SET_FLAGS_VALUE, kCommandLine, &error)) {
+ if (!flags_internal::PrivateHandleAccessor::ParseFrom(
+ *flag, value, SET_FLAGS_VALUE, kCommandLine, error)) {
+ if (flag->IsRetired()) continue;
+
flags_internal::ReportUsageError(error, true);
success = false;
+ } else {
+ specified_flags->push_back(flag);
}
}
@@ -750,6 +801,10 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
}
}
+ // Trim and sort the vector.
+ specified_flags->shrink_to_fit();
+ std::sort(specified_flags->begin(), specified_flags->end(),
+ SpecifiedFlagsCompare{});
return output_args;
}
diff --git a/third_party/abseil-cpp/absl/flags/parse.h b/third_party/abseil-cpp/absl/flags/parse.h
index f37b0602e6..929de2cb40 100644
--- a/third_party/abseil-cpp/absl/flags/parse.h
+++ b/third_party/abseil-cpp/absl/flags/parse.h
@@ -23,7 +23,6 @@
#ifndef ABSL_FLAGS_PARSE_H_
#define ABSL_FLAGS_PARSE_H_
-#include <string>
#include <vector>
#include "absl/base/config.h"
diff --git a/third_party/abseil-cpp/absl/flags/parse_test.cc b/third_party/abseil-cpp/absl/flags/parse_test.cc
index 6f49377a93..8dc91db2b3 100644
--- a/third_party/abseil-cpp/absl/flags/parse_test.cc
+++ b/third_party/abseil-cpp/absl/flags/parse_test.cc
@@ -28,7 +28,8 @@
#include "absl/flags/declare.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/parse.h"
-#include "absl/flags/internal/registry.h"
+#include "absl/flags/internal/usage.h"
+#include "absl/flags/reflection.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
@@ -45,6 +46,7 @@ using absl::base_internal::ScopedSetEnv;
struct UDT {
UDT() = default;
UDT(const UDT&) = default;
+ UDT& operator=(const UDT&) = default;
UDT(int v) : value(v) {} // NOLINT
int value;
@@ -171,8 +173,8 @@ constexpr const char* const ff2_data[] = {
// temporary directory location. This way we can test inclusion of one flagfile
// from another flagfile.
const char* GetFlagfileFlag(const std::vector<FlagfileData>& ffd,
- std::string* flagfile_flag) {
- *flagfile_flag = "--flagfile=";
+ std::string& flagfile_flag) {
+ flagfile_flag = "--flagfile=";
absl::string_view separator;
for (const auto& flagfile_data : ffd) {
std::string flagfile_name =
@@ -183,11 +185,11 @@ const char* GetFlagfileFlag(const std::vector<FlagfileData>& ffd,
flagfile_out << absl::Substitute(line, GetTestTempDir()) << "\n";
}
- absl::StrAppend(flagfile_flag, separator, flagfile_name);
+ absl::StrAppend(&flagfile_flag, separator, flagfile_name);
separator = ",";
}
- return flagfile_flag->c_str();
+ return flagfile_flag.c_str();
}
} // namespace
@@ -207,8 +209,11 @@ namespace flags = absl::flags_internal;
using testing::ElementsAreArray;
class ParseTest : public testing::Test {
+ public:
+ ~ParseTest() override { flags::SetFlagsHelpMode(flags::HelpMode::kNone); }
+
private:
- flags::FlagSaver flag_saver_;
+ absl::FlagSaver flag_saver_;
};
// --------------------------------------------------------------------
@@ -481,21 +486,22 @@ TEST_F(ParseDeathTest, TestUndefinedArg) {
"testbin",
"--undefined_flag",
};
- EXPECT_DEATH(InvokeParse(in_args1),
- "Unknown command line flag 'undefined_flag'");
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
+ "Unknown command line flag 'undefined_flag'");
const char* in_args2[] = {
"testbin",
"--noprefixed_flag",
};
- EXPECT_DEATH(InvokeParse(in_args2),
- "Unknown command line flag 'noprefixed_flag'");
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2),
+ "Unknown command line flag 'noprefixed_flag'");
const char* in_args3[] = {
"testbin",
"--Int_flag=1",
};
- EXPECT_DEATH(InvokeParse(in_args3), "Unknown command line flag 'Int_flag'");
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args3),
+ "Unknown command line flag 'Int_flag'");
}
// --------------------------------------------------------------------
@@ -505,7 +511,7 @@ TEST_F(ParseDeathTest, TestInvalidBoolFlagFormat) {
"testbin",
"--bool_flag=",
};
- EXPECT_DEATH(
+ EXPECT_DEATH_IF_SUPPORTED(
InvokeParse(in_args1),
"Missing the value after assignment for the boolean flag 'bool_flag'");
@@ -513,7 +519,7 @@ TEST_F(ParseDeathTest, TestInvalidBoolFlagFormat) {
"testbin",
"--nobool_flag=true",
};
- EXPECT_DEATH(InvokeParse(in_args2),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2),
"Negative form with assignment is not valid for the boolean "
"flag 'bool_flag'");
}
@@ -525,14 +531,14 @@ TEST_F(ParseDeathTest, TestInvalidNonBoolFlagFormat) {
"testbin",
"--nostring_flag",
};
- EXPECT_DEATH(InvokeParse(in_args1),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
"Negative form is not valid for the flag 'string_flag'");
const char* in_args2[] = {
"testbin",
"--int_flag",
};
- EXPECT_DEATH(InvokeParse(in_args2),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2),
"Missing the value for the flag 'int_flag'");
}
@@ -543,7 +549,7 @@ TEST_F(ParseDeathTest, TestInvalidUDTFlagFormat) {
"testbin",
"--udt_flag=1",
};
- EXPECT_DEATH(InvokeParse(in_args1),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
"Illegal value '1' specified for flag 'udt_flag'; Use values A, "
"AAA instead");
@@ -552,7 +558,7 @@ TEST_F(ParseDeathTest, TestInvalidUDTFlagFormat) {
"--udt_flag",
"AA",
};
- EXPECT_DEATH(InvokeParse(in_args2),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2),
"Illegal value 'AA' specified for flag 'udt_flag'; Use values "
"A, AAA instead");
}
@@ -587,14 +593,14 @@ TEST_F(ParseTest, TestSimpleValidFlagfile) {
const char* in_args1[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff1", absl::MakeConstSpan(ff1_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
TestParse(in_args1, -1, 0.1, "q2w2 ", true);
const char* in_args2[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff2", absl::MakeConstSpan(ff2_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
TestParse(in_args2, 100, 0.1, "q2w2 ", false);
}
@@ -608,7 +614,7 @@ TEST_F(ParseTest, TestValidMultiFlagfile) {
"testbin",
GetFlagfileFlag({{"parse_test.ff2", absl::MakeConstSpan(ff2_data)},
{"parse_test.ff1", absl::MakeConstSpan(ff1_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
TestParse(in_args1, -1, 0.1, "q2w2 ", true);
}
@@ -621,7 +627,7 @@ TEST_F(ParseTest, TestFlagfileMixedWithRegularFlags) {
const char* in_args1[] = {
"testbin", "--int_flag=3",
GetFlagfileFlag({{"parse_test.ff1", absl::MakeConstSpan(ff1_data)}},
- &flagfile_flag),
+ flagfile_flag),
"-double_flag=0.2"};
TestParse(in_args1, -1, 0.2, "q2w2 ", true);
}
@@ -636,10 +642,14 @@ TEST_F(ParseTest, TestFlagfileInFlagfile) {
"--flagfile=$0/parse_test.ff2",
};
+ GetFlagfileFlag({{"parse_test.ff2", absl::MakeConstSpan(ff2_data)},
+ {"parse_test.ff1", absl::MakeConstSpan(ff1_data)}},
+ flagfile_flag);
+
const char* in_args1[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff3", absl::MakeConstSpan(ff3_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
TestParse(in_args1, 100, 0.1, "q2w2 ", false);
}
@@ -656,9 +666,9 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) {
const char* in_args1[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff4",
- absl::MakeConstSpan(ff4_data)}}, &flagfile_flag),
+ absl::MakeConstSpan(ff4_data)}}, flagfile_flag),
};
- EXPECT_DEATH(InvokeParse(in_args1),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
"Unknown command line flag 'unknown_flag'");
constexpr const char* const ff5_data[] = {
@@ -668,9 +678,9 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) {
const char* in_args2[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff5",
- absl::MakeConstSpan(ff5_data)}}, &flagfile_flag),
+ absl::MakeConstSpan(ff5_data)}}, flagfile_flag),
};
- EXPECT_DEATH(InvokeParse(in_args2),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2),
"Unknown command line flag 'int_flag 10'");
constexpr const char* const ff6_data[] = {
@@ -680,16 +690,17 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) {
const char* in_args3[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff6", absl::MakeConstSpan(ff6_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
- EXPECT_DEATH(InvokeParse(in_args3),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args3),
"Flagfile can't contain position arguments or --");
const char* in_args4[] = {
"testbin",
"--flagfile=invalid_flag_file",
};
- EXPECT_DEATH(InvokeParse(in_args4), "Can't open flagfile invalid_flag_file");
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args4),
+ "Can't open flagfile invalid_flag_file");
constexpr const char* const ff7_data[] = {
"--int_flag=10",
@@ -700,9 +711,9 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) {
const char* in_args5[] = {
"testbin",
GetFlagfileFlag({{"parse_test.ff7", absl::MakeConstSpan(ff7_data)}},
- &flagfile_flag),
+ flagfile_flag),
};
- EXPECT_DEATH(InvokeParse(in_args5),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args5),
"Unexpected line in the flagfile .*: \\*bin\\*");
}
@@ -724,7 +735,7 @@ TEST_F(ParseTest, TestReadingRequiredFlagsFromEnv) {
TEST_F(ParseDeathTest, TestReadingUnsetRequiredFlagsFromEnv) {
const char* in_args1[] = {"testbin", "--fromenv=int_flag"};
- EXPECT_DEATH(InvokeParse(in_args1),
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
"FLAGS_int_flag not found in environment");
}
@@ -735,7 +746,8 @@ TEST_F(ParseDeathTest, TestRecursiveFlagsFromEnv) {
ScopedSetEnv set_tryfromenv("FLAGS_tryfromenv", "int_flag");
- EXPECT_DEATH(InvokeParse(in_args1), "Infinite recursion on flag tryfromenv");
+ EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1),
+ "Infinite recursion on flag tryfromenv");
}
// --------------------------------------------------------------------
@@ -844,7 +856,7 @@ TEST_F(ParseTest, TestIgnoreUndefinedFlags) {
// --------------------------------------------------------------------
-TEST_F(ParseDeathTest, TestHelpFlagHandling) {
+TEST_F(ParseDeathTest, TestSimpleHelpFlagHandling) {
const char* in_args1[] = {
"testbin",
"--help",
@@ -863,7 +875,56 @@ TEST_F(ParseDeathTest, TestHelpFlagHandling) {
flags::UsageFlagsAction::kIgnoreUsage,
flags::OnUndefinedFlag::kAbortIfUndefined);
+ EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant);
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 3);
}
+// --------------------------------------------------------------------
+
+TEST_F(ParseDeathTest, TestSubstringHelpFlagHandling) {
+ const char* in_args1[] = {
+ "testbin",
+ "--help=abcd",
+ };
+
+ auto out_args1 = flags::ParseCommandLineImpl(
+ 2, const_cast<char**>(in_args1), flags::ArgvListAction::kRemoveParsedArgs,
+ flags::UsageFlagsAction::kIgnoreUsage,
+ flags::OnUndefinedFlag::kAbortIfUndefined);
+
+ EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kMatch);
+ EXPECT_EQ(flags::GetFlagsHelpMatchSubstr(), "abcd");
+
+ const char* in_args2[] = {"testbin", "--help", "some_positional_arg"};
+
+ auto out_args2 = flags::ParseCommandLineImpl(
+ 3, const_cast<char**>(in_args2), flags::ArgvListAction::kRemoveParsedArgs,
+ flags::UsageFlagsAction::kIgnoreUsage,
+ flags::OnUndefinedFlag::kAbortIfUndefined);
+
+ EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ParseTest, WasPresentOnCommandLine) {
+ const char* in_args1[] = {
+ "testbin", "arg1", "--bool_flag",
+ "--int_flag=211", "arg2", "--double_flag=1.1",
+ "--string_flag", "asd", "--",
+ "--some_flag", "arg4",
+ };
+
+ InvokeParse(in_args1);
+
+ EXPECT_TRUE(flags::WasPresentOnCommandLine("bool_flag"));
+ EXPECT_TRUE(flags::WasPresentOnCommandLine("int_flag"));
+ EXPECT_TRUE(flags::WasPresentOnCommandLine("double_flag"));
+ EXPECT_TRUE(flags::WasPresentOnCommandLine("string_flag"));
+ EXPECT_FALSE(flags::WasPresentOnCommandLine("some_flag"));
+ EXPECT_FALSE(flags::WasPresentOnCommandLine("another_flag"));
+}
+
+// --------------------------------------------------------------------
+
} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/reflection.cc b/third_party/abseil-cpp/absl/flags/reflection.cc
new file mode 100644
index 0000000000..dbce4032ab
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/reflection.cc
@@ -0,0 +1,354 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/reflection.h"
+
+#include <assert.h>
+
+#include <atomic>
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/base/thread_annotations.h"
+#include "absl/container/flat_hash_map.h"
+#include "absl/flags/commandlineflag.h"
+#include "absl/flags/internal/private_handle_accessor.h"
+#include "absl/flags/internal/registry.h"
+#include "absl/flags/usage_config.h"
+#include "absl/strings/str_cat.h"
+#include "absl/strings/string_view.h"
+#include "absl/synchronization/mutex.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+
+// --------------------------------------------------------------------
+// FlagRegistry
+// A FlagRegistry singleton object holds all flag objects indexed by their
+// names so that if you know a flag's name, you can access or set it. If the
+// function is named FooLocked(), you must own the registry lock before
+// calling the function; otherwise, you should *not* hold the lock, and the
+// function will acquire it itself if needed.
+// --------------------------------------------------------------------
+
+class FlagRegistry {
+ public:
+ FlagRegistry() = default;
+ ~FlagRegistry() = default;
+
+ // Store a flag in this registry. Takes ownership of *flag.
+ void RegisterFlag(CommandLineFlag& flag, const char* filename);
+
+ void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); }
+ void Unlock() ABSL_UNLOCK_FUNCTION(lock_) { lock_.Unlock(); }
+
+ // Returns the flag object for the specified name, or nullptr if not found.
+ // Will emit a warning if a 'retired' flag is specified.
+ CommandLineFlag* FindFlag(absl::string_view name);
+
+ static FlagRegistry& GlobalRegistry(); // returns a singleton registry
+
+ private:
+ friend class flags_internal::FlagSaverImpl; // reads all the flags in order
+ // to copy them
+ friend void ForEachFlag(std::function<void(CommandLineFlag&)> visitor);
+ friend void FinalizeRegistry();
+
+ // The map from name to flag, for FindFlag().
+ using FlagMap = absl::flat_hash_map<absl::string_view, CommandLineFlag*>;
+ using FlagIterator = FlagMap::iterator;
+ using FlagConstIterator = FlagMap::const_iterator;
+ FlagMap flags_;
+ std::vector<CommandLineFlag*> flat_flags_;
+ std::atomic<bool> finalized_flags_{false};
+
+ absl::Mutex lock_;
+
+ // Disallow
+ FlagRegistry(const FlagRegistry&);
+ FlagRegistry& operator=(const FlagRegistry&);
+};
+
+namespace {
+
+class FlagRegistryLock {
+ public:
+ explicit FlagRegistryLock(FlagRegistry& fr) : fr_(fr) { fr_.Lock(); }
+ ~FlagRegistryLock() { fr_.Unlock(); }
+
+ private:
+ FlagRegistry& fr_;
+};
+
+} // namespace
+
+CommandLineFlag* FlagRegistry::FindFlag(absl::string_view name) {
+ if (finalized_flags_.load(std::memory_order_acquire)) {
+ // We could save some gcus here if we make `Name()` be non-virtual.
+ // We could move the `const char*` name to the base class.
+ auto it = std::partition_point(
+ flat_flags_.begin(), flat_flags_.end(),
+ [=](CommandLineFlag* f) { return f->Name() < name; });
+ if (it != flat_flags_.end() && (*it)->Name() == name) return *it;
+ }
+
+ FlagRegistryLock frl(*this);
+ auto it = flags_.find(name);
+ return it != flags_.end() ? it->second : nullptr;
+}
+
+void FlagRegistry::RegisterFlag(CommandLineFlag& flag, const char* filename) {
+ if (filename != nullptr &&
+ flag.Filename() != GetUsageConfig().normalize_filename(filename)) {
+ flags_internal::ReportUsageError(
+ absl::StrCat(
+ "Inconsistency between flag object and registration for flag '",
+ flag.Name(),
+ "', likely due to duplicate flags or an ODR violation. Relevant "
+ "files: ",
+ flag.Filename(), " and ", filename),
+ true);
+ std::exit(1);
+ }
+
+ FlagRegistryLock registry_lock(*this);
+
+ std::pair<FlagIterator, bool> ins =
+ flags_.insert(FlagMap::value_type(flag.Name(), &flag));
+ if (ins.second == false) { // means the name was already in the map
+ CommandLineFlag& old_flag = *ins.first->second;
+ if (flag.IsRetired() != old_flag.IsRetired()) {
+ // All registrations must agree on the 'retired' flag.
+ flags_internal::ReportUsageError(
+ absl::StrCat(
+ "Retired flag '", flag.Name(), "' was defined normally in file '",
+ (flag.IsRetired() ? old_flag.Filename() : flag.Filename()), "'."),
+ true);
+ } else if (flags_internal::PrivateHandleAccessor::TypeId(flag) !=
+ flags_internal::PrivateHandleAccessor::TypeId(old_flag)) {
+ flags_internal::ReportUsageError(
+ absl::StrCat("Flag '", flag.Name(),
+ "' was defined more than once but with "
+ "differing types. Defined in files '",
+ old_flag.Filename(), "' and '", flag.Filename(), "'."),
+ true);
+ } else if (old_flag.IsRetired()) {
+ return;
+ } else if (old_flag.Filename() != flag.Filename()) {
+ flags_internal::ReportUsageError(
+ absl::StrCat("Flag '", flag.Name(),
+ "' was defined more than once (in files '",
+ old_flag.Filename(), "' and '", flag.Filename(), "')."),
+ true);
+ } else {
+ flags_internal::ReportUsageError(
+ absl::StrCat(
+ "Something is wrong with flag '", flag.Name(), "' in file '",
+ flag.Filename(), "'. One possibility: file '", flag.Filename(),
+ "' is being linked both statically and dynamically into this "
+ "executable. e.g. some files listed as srcs to a test and also "
+ "listed as srcs of some shared lib deps of the same test."),
+ true);
+ }
+ // All cases above are fatal, except for the retired flags.
+ std::exit(1);
+ }
+}
+
+FlagRegistry& FlagRegistry::GlobalRegistry() {
+ static FlagRegistry* global_registry = new FlagRegistry;
+ return *global_registry;
+}
+
+// --------------------------------------------------------------------
+
+void ForEachFlag(std::function<void(CommandLineFlag&)> visitor) {
+ FlagRegistry& registry = FlagRegistry::GlobalRegistry();
+
+ if (registry.finalized_flags_.load(std::memory_order_acquire)) {
+ for (const auto& i : registry.flat_flags_) visitor(*i);
+ }
+
+ FlagRegistryLock frl(registry);
+ for (const auto& i : registry.flags_) visitor(*i.second);
+}
+
+// --------------------------------------------------------------------
+
+bool RegisterCommandLineFlag(CommandLineFlag& flag, const char* filename) {
+ FlagRegistry::GlobalRegistry().RegisterFlag(flag, filename);
+ return true;
+}
+
+void FinalizeRegistry() {
+ auto& registry = FlagRegistry::GlobalRegistry();
+ FlagRegistryLock frl(registry);
+ if (registry.finalized_flags_.load(std::memory_order_relaxed)) {
+ // Was already finalized. Ignore the second time.
+ return;
+ }
+ registry.flat_flags_.reserve(registry.flags_.size());
+ for (const auto& f : registry.flags_) {
+ registry.flat_flags_.push_back(f.second);
+ }
+ std::sort(std::begin(registry.flat_flags_), std::end(registry.flat_flags_),
+ [](const CommandLineFlag* lhs, const CommandLineFlag* rhs) {
+ return lhs->Name() < rhs->Name();
+ });
+ registry.flags_.clear();
+ registry.finalized_flags_.store(true, std::memory_order_release);
+}
+
+// --------------------------------------------------------------------
+
+namespace {
+
+class RetiredFlagObj final : public CommandLineFlag {
+ public:
+ constexpr RetiredFlagObj(const char* name, FlagFastTypeId type_id)
+ : name_(name), type_id_(type_id) {}
+
+ private:
+ absl::string_view Name() const override { return name_; }
+ std::string Filename() const override {
+ OnAccess();
+ return "RETIRED";
+ }
+ FlagFastTypeId TypeId() const override { return type_id_; }
+ std::string Help() const override {
+ OnAccess();
+ return "";
+ }
+ bool IsRetired() const override { return true; }
+ bool IsSpecifiedOnCommandLine() const override {
+ OnAccess();
+ return false;
+ }
+ std::string DefaultValue() const override {
+ OnAccess();
+ return "";
+ }
+ std::string CurrentValue() const override {
+ OnAccess();
+ return "";
+ }
+
+ // Any input is valid
+ bool ValidateInputValue(absl::string_view) const override {
+ OnAccess();
+ return true;
+ }
+
+ std::unique_ptr<flags_internal::FlagStateInterface> SaveState() override {
+ return nullptr;
+ }
+
+ bool ParseFrom(absl::string_view, flags_internal::FlagSettingMode,
+ flags_internal::ValueSource, std::string&) override {
+ OnAccess();
+ return false;
+ }
+
+ void CheckDefaultValueParsingRoundtrip() const override { OnAccess(); }
+
+ void Read(void*) const override { OnAccess(); }
+
+ void OnAccess() const {
+ flags_internal::ReportUsageError(
+ absl::StrCat("Accessing retired flag '", name_, "'"), false);
+ }
+
+ // Data members
+ const char* const name_;
+ const FlagFastTypeId type_id_;
+};
+
+} // namespace
+
+void Retire(const char* name, FlagFastTypeId type_id, char* buf) {
+ static_assert(sizeof(RetiredFlagObj) == kRetiredFlagObjSize, "");
+ static_assert(alignof(RetiredFlagObj) == kRetiredFlagObjAlignment, "");
+ auto* flag = ::new (static_cast<void*>(buf))
+ flags_internal::RetiredFlagObj(name, type_id);
+ FlagRegistry::GlobalRegistry().RegisterFlag(*flag, nullptr);
+}
+
+// --------------------------------------------------------------------
+
+class FlagSaverImpl {
+ public:
+ FlagSaverImpl() = default;
+ FlagSaverImpl(const FlagSaverImpl&) = delete;
+ void operator=(const FlagSaverImpl&) = delete;
+
+ // Saves the flag states from the flag registry into this object.
+ // It's an error to call this more than once.
+ void SaveFromRegistry() {
+ assert(backup_registry_.empty()); // call only once!
+ flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
+ if (auto flag_state =
+ flags_internal::PrivateHandleAccessor::SaveState(flag)) {
+ backup_registry_.emplace_back(std::move(flag_state));
+ }
+ });
+ }
+
+ // Restores the saved flag states into the flag registry.
+ void RestoreToRegistry() {
+ for (const auto& flag_state : backup_registry_) {
+ flag_state->Restore();
+ }
+ }
+
+ private:
+ std::vector<std::unique_ptr<flags_internal::FlagStateInterface>>
+ backup_registry_;
+};
+
+} // namespace flags_internal
+
+FlagSaver::FlagSaver() : impl_(new flags_internal::FlagSaverImpl) {
+ impl_->SaveFromRegistry();
+}
+
+FlagSaver::~FlagSaver() {
+ if (!impl_) return;
+
+ impl_->RestoreToRegistry();
+ delete impl_;
+}
+
+// --------------------------------------------------------------------
+
+CommandLineFlag* FindCommandLineFlag(absl::string_view name) {
+ if (name.empty()) return nullptr;
+ flags_internal::FlagRegistry& registry =
+ flags_internal::FlagRegistry::GlobalRegistry();
+ return registry.FindFlag(name);
+}
+
+// --------------------------------------------------------------------
+
+absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags() {
+ absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> res;
+ flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
+ if (!flag.IsRetired()) res.insert({flag.Name(), &flag});
+ });
+ return res;
+}
+
+ABSL_NAMESPACE_END
+} // namespace absl
diff --git a/third_party/abseil-cpp/absl/flags/reflection.h b/third_party/abseil-cpp/absl/flags/reflection.h
new file mode 100644
index 0000000000..e6baf5de4b
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/reflection.h
@@ -0,0 +1,90 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: reflection.h
+// -----------------------------------------------------------------------------
+//
+// This file defines the routines to access and operate on an Abseil Flag's
+// reflection handle.
+
+#ifndef ABSL_FLAGS_REFLECTION_H_
+#define ABSL_FLAGS_REFLECTION_H_
+
+#include <string>
+
+#include "absl/base/config.h"
+#include "absl/container/flat_hash_map.h"
+#include "absl/flags/commandlineflag.h"
+#include "absl/flags/internal/commandlineflag.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+class FlagSaverImpl;
+} // namespace flags_internal
+
+// FindCommandLineFlag()
+//
+// Returns the reflection handle of an Abseil flag of the specified name, or
+// `nullptr` if not found. This function will emit a warning if the name of a
+// 'retired' flag is specified.
+absl::CommandLineFlag* FindCommandLineFlag(absl::string_view name);
+
+// Returns current state of the Flags registry in a form of mapping from flag
+// name to a flag reflection handle.
+absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags();
+
+//------------------------------------------------------------------------------
+// FlagSaver
+//------------------------------------------------------------------------------
+//
+// A FlagSaver object stores the state of flags in the scope where the FlagSaver
+// is defined, allowing modification of those flags within that scope and
+// automatic restoration of the flags to their previous state upon leaving the
+// scope.
+//
+// A FlagSaver can be used within tests to temporarily change the test
+// environment and restore the test case to its previous state.
+//
+// Example:
+//
+// void MyFunc() {
+// absl::FlagSaver fs;
+// ...
+// absl::SetFlag(&FLAGS_myFlag, otherValue);
+// ...
+// } // scope of FlagSaver left, flags return to previous state
+//
+// This class is thread-safe.
+
+class FlagSaver {
+ public:
+ FlagSaver();
+ ~FlagSaver();
+
+ FlagSaver(const FlagSaver&) = delete;
+ void operator=(const FlagSaver&) = delete;
+
+ private:
+ flags_internal::FlagSaverImpl* impl_;
+};
+
+//-----------------------------------------------------------------------------
+
+ABSL_NAMESPACE_END
+} // namespace absl
+
+#endif // ABSL_FLAGS_REFLECTION_H_
diff --git a/third_party/abseil-cpp/absl/flags/reflection_test.cc b/third_party/abseil-cpp/absl/flags/reflection_test.cc
new file mode 100644
index 0000000000..79cfa90c3a
--- /dev/null
+++ b/third_party/abseil-cpp/absl/flags/reflection_test.cc
@@ -0,0 +1,265 @@
+//
+// Copyright 2019 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/reflection.h"
+
+#include <memory>
+#include <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/flags/declare.h"
+#include "absl/flags/flag.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/flags/marshalling.h"
+#include "absl/memory/memory.h"
+#include "absl/strings/str_cat.h"
+#include "absl/strings/str_split.h"
+
+ABSL_FLAG(int, int_flag, 1, "int_flag help");
+ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
+ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
+
+namespace {
+
+class ReflectionTest : public testing::Test {
+ protected:
+ void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
+ void TearDown() override { flag_saver_.reset(); }
+
+ private:
+ std::unique_ptr<absl::FlagSaver> flag_saver_;
+};
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestFindCommandLineFlag) {
+ auto* handle = absl::FindCommandLineFlag("some_flag");
+ EXPECT_EQ(handle, nullptr);
+
+ handle = absl::FindCommandLineFlag("int_flag");
+ EXPECT_NE(handle, nullptr);
+
+ handle = absl::FindCommandLineFlag("string_flag");
+ EXPECT_NE(handle, nullptr);
+
+ handle = absl::FindCommandLineFlag("bool_retired_flag");
+ EXPECT_NE(handle, nullptr);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestGetAllFlags) {
+ auto all_flags = absl::GetAllFlags();
+ EXPECT_NE(all_flags.find("int_flag"), all_flags.end());
+ EXPECT_EQ(all_flags.find("bool_retired_flag"), all_flags.end());
+ EXPECT_EQ(all_flags.find("some_undefined_flag"), all_flags.end());
+
+ std::vector<absl::string_view> flag_names_first_attempt;
+ auto all_flags_1 = absl::GetAllFlags();
+ for (auto f : all_flags_1) {
+ flag_names_first_attempt.push_back(f.first);
+ }
+
+ std::vector<absl::string_view> flag_names_second_attempt;
+ auto all_flags_2 = absl::GetAllFlags();
+ for (auto f : all_flags_2) {
+ flag_names_second_attempt.push_back(f.first);
+ }
+
+ EXPECT_THAT(flag_names_first_attempt,
+ ::testing::UnorderedElementsAreArray(flag_names_second_attempt));
+}
+
+// --------------------------------------------------------------------
+
+struct CustomUDT {
+ CustomUDT() : a(1), b(1) {}
+ CustomUDT(int a_, int b_) : a(a_), b(b_) {}
+
+ friend bool operator==(const CustomUDT& f1, const CustomUDT& f2) {
+ return f1.a == f2.a && f1.b == f2.b;
+ }
+
+ int a;
+ int b;
+};
+bool AbslParseFlag(absl::string_view in, CustomUDT* f, std::string*) {
+ std::vector<absl::string_view> parts =
+ absl::StrSplit(in, ':', absl::SkipWhitespace());
+
+ if (parts.size() != 2) return false;
+
+ if (!absl::SimpleAtoi(parts[0], &f->a)) return false;
+
+ if (!absl::SimpleAtoi(parts[1], &f->b)) return false;
+
+ return true;
+}
+std::string AbslUnparseFlag(const CustomUDT& f) {
+ return absl::StrCat(f.a, ":", f.b);
+}
+
+} // namespace
+
+// --------------------------------------------------------------------
+
+ABSL_FLAG(bool, test_flag_01, true, "");
+ABSL_FLAG(int, test_flag_02, 1234, "");
+ABSL_FLAG(int16_t, test_flag_03, -34, "");
+ABSL_FLAG(uint16_t, test_flag_04, 189, "");
+ABSL_FLAG(int32_t, test_flag_05, 10765, "");
+ABSL_FLAG(uint32_t, test_flag_06, 40000, "");
+ABSL_FLAG(int64_t, test_flag_07, -1234567, "");
+ABSL_FLAG(uint64_t, test_flag_08, 9876543, "");
+ABSL_FLAG(double, test_flag_09, -9.876e-50, "");
+ABSL_FLAG(float, test_flag_10, 1.234e12f, "");
+ABSL_FLAG(std::string, test_flag_11, "", "");
+ABSL_FLAG(absl::Duration, test_flag_12, absl::Minutes(10), "");
+static int counter = 0;
+ABSL_FLAG(int, test_flag_13, 200, "").OnUpdate([]() { counter++; });
+ABSL_FLAG(CustomUDT, test_flag_14, {}, "");
+
+namespace {
+
+TEST_F(ReflectionTest, TestFlagSaverInScope) {
+ {
+ absl::FlagSaver s;
+ counter = 0;
+ absl::SetFlag(&FLAGS_test_flag_01, false);
+ absl::SetFlag(&FLAGS_test_flag_02, -1021);
+ absl::SetFlag(&FLAGS_test_flag_03, 6009);
+ absl::SetFlag(&FLAGS_test_flag_04, 44);
+ absl::SetFlag(&FLAGS_test_flag_05, +800);
+ absl::SetFlag(&FLAGS_test_flag_06, -40978756);
+ absl::SetFlag(&FLAGS_test_flag_07, 23405);
+ absl::SetFlag(&FLAGS_test_flag_08, 975310);
+ absl::SetFlag(&FLAGS_test_flag_09, 1.00001);
+ absl::SetFlag(&FLAGS_test_flag_10, -3.54f);
+ absl::SetFlag(&FLAGS_test_flag_11, "asdf");
+ absl::SetFlag(&FLAGS_test_flag_12, absl::Hours(20));
+ absl::SetFlag(&FLAGS_test_flag_13, 4);
+ absl::SetFlag(&FLAGS_test_flag_14, CustomUDT{-1, -2});
+ }
+
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_04), 189);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_05), 10765);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_06), 40000);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_07), -1234567);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Minutes(10));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), 200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_14), CustomUDT{});
+ EXPECT_EQ(counter, 2);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestFlagSaverVsUpdateViaReflection) {
+ {
+ absl::FlagSaver s;
+ counter = 0;
+ std::string error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_01")->ParseFrom("false", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_02")->ParseFrom("-4536", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_03")->ParseFrom("111", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_04")->ParseFrom("909", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_05")->ParseFrom("-2004", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_06")->ParseFrom("1000023", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_07")->ParseFrom("69305", &error))
+ << error;
+ EXPECT_TRUE(absl::FindCommandLineFlag("test_flag_08")
+ ->ParseFrom("1000000001", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_09")->ParseFrom("2.09021", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_10")->ParseFrom("-33.1", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_11")->ParseFrom("ADD_FOO", &error))
+ << error;
+ EXPECT_TRUE(absl::FindCommandLineFlag("test_flag_12")
+ ->ParseFrom("3h11m16s", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_13")->ParseFrom("0", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_14")->ParseFrom("10:1", &error))
+ << error;
+ }
+
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_04), 189);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_05), 10765);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_06), 40000);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_07), -1234567);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Minutes(10));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), 200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_14), CustomUDT{});
+ EXPECT_EQ(counter, 2);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestMultipleFlagSaversInEnclosedScopes) {
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, 10);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 10);
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, 20);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 20);
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, -200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), -200);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 20);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 10);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+}
+
+} // namespace
diff --git a/third_party/abseil-cpp/absl/flags/usage_config.cc b/third_party/abseil-cpp/absl/flags/usage_config.cc
index 2d837ec590..5d7426db31 100644
--- a/third_party/abseil-cpp/absl/flags/usage_config.cc
+++ b/third_party/abseil-cpp/absl/flags/usage_config.cc
@@ -15,6 +15,7 @@
#include "absl/flags/usage_config.h"
+#include <functional>
#include <iostream>
#include <string>
@@ -33,7 +34,8 @@ extern "C" {
// Additional report of fatal usage error message before we std::exit. Error is
// fatal if is_fatal argument to ReportUsageError is true.
-ABSL_ATTRIBUTE_WEAK void AbslInternalReportFatalUsageError(absl::string_view) {}
+ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(
+ AbslInternalReportFatalUsageError)(absl::string_view) {}
} // extern "C"
@@ -50,10 +52,15 @@ namespace {
bool ContainsHelpshortFlags(absl::string_view filename) {
// By default we only want flags in binary's main. We expect the main
// routine to reside in <program>.cc or <program>-main.cc or
- // <program>_main.cc, where the <program> is the name of the binary.
+ // <program>_main.cc, where the <program> is the name of the binary
+ // (without .exe on Windows).
auto suffix = flags_internal::Basename(filename);
- if (!absl::ConsumePrefix(&suffix,
- flags_internal::ShortProgramInvocationName()))
+ auto program_name = flags_internal::ShortProgramInvocationName();
+ absl::string_view program_name_ref = program_name;
+#if defined(_WIN32)
+ absl::ConsumeSuffix(&program_name_ref, ".exe");
+#endif
+ if (!absl::ConsumePrefix(&suffix, program_name_ref))
return false;
return absl::StartsWith(suffix, ".") || absl::StartsWith(suffix, "-main.") ||
absl::StartsWith(suffix, "_main.");
@@ -122,7 +129,7 @@ void ReportUsageError(absl::string_view msg, bool is_fatal) {
std::cerr << "ERROR: " << msg << std::endl;
if (is_fatal) {
- AbslInternalReportFatalUsageError(msg);
+ ABSL_INTERNAL_C_SYMBOL(AbslInternalReportFatalUsageError)(msg);
}
}
diff --git a/third_party/abseil-cpp/absl/flags/usage_config.h b/third_party/abseil-cpp/absl/flags/usage_config.h
index 0ed7e1b47c..ded70300f0 100644
--- a/third_party/abseil-cpp/absl/flags/usage_config.h
+++ b/third_party/abseil-cpp/absl/flags/usage_config.h
@@ -90,7 +90,7 @@ struct FlagsUsageConfig {
// program output.
flags_internal::FlagKindFilter contains_helppackage_flags;
- // Generates std::string containing program version. This is the std::string reported
+ // Generates string containing program version. This is the string reported
// when user specifies --version in a command line.
std::function<std::string()> version_string;
@@ -127,7 +127,8 @@ extern "C" {
// Additional report of fatal usage error message before we std::exit. Error is
// fatal if is_fatal argument to ReportUsageError is true.
-void AbslInternalReportFatalUsageError(absl::string_view);
+void ABSL_INTERNAL_C_SYMBOL(AbslInternalReportFatalUsageError)(
+ absl::string_view);
} // extern "C"
diff --git a/third_party/abseil-cpp/absl/flags/usage_config_test.cc b/third_party/abseil-cpp/absl/flags/usage_config_test.cc
index 70eca30b8f..e57a8832f6 100644
--- a/third_party/abseil-cpp/absl/flags/usage_config_test.cc
+++ b/third_party/abseil-cpp/absl/flags/usage_config_test.cc
@@ -84,7 +84,11 @@ TEST_F(FlagsUsageConfigTest, TestGetSetFlagsUsageConfig) {
// --------------------------------------------------------------------
TEST_F(FlagsUsageConfigTest, TestContainsHelpshortFlags) {
+#if defined(_WIN32)
+ flags::SetProgramInvocationName("usage_config_test.exe");
+#else
flags::SetProgramInvocationName("usage_config_test");
+#endif
auto config = flags::GetUsageConfig();
EXPECT_TRUE(config.contains_helpshort_flags("adir/cd/usage_config_test.cc"));