Export of internal Abseil changes

--
0b3113ff8d252be11c35d8a77cdb23cc03e993d2 by Benjamin Barenblat <bbaren@google.com>:

Correct Apache license headers

These files have always been licensed under the Apache license (per the
repository LICENSE), but they lacked headers to locally indicate that
fact. Add the appropriate headers.

PiperOrigin-RevId: 310223650

--
4f92b0819781279c02b48bcfce4c10d547e35a49 by Greg Falcon <gfalcon@google.com>:

Internal change

PiperOrigin-RevId: 310223203

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

Internal Change

PiperOrigin-RevId: 310180198

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

Internal change

PiperOrigin-RevId: 310003669

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

Make 4 argument version of ParseFrom interface private in CommandLineFlag. Instroduce 2 argument version of this interface.

PiperOrigin-RevId: 309995363
GitOrigin-RevId: 0b3113ff8d252be11c35d8a77cdb23cc03e993d2
Change-Id: Ibfdf948d3d081f5630ce7ebff992ab85c75cecba
This commit is contained in:
Abseil Team 2020-05-06 14:06:16 -07:00 committed by vslashg
parent b115744656
commit bd317cae3b
10 changed files with 103 additions and 54 deletions

View file

@ -1,9 +1,18 @@
// Copyright 2011 and onwards Google Inc. // Copyright 2017 The Abseil Authors.
// All rights reserved. //
// 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.
// //
// Author: Doug Kwan
// This is inspired by Craig Silverstein's PowerPC stacktrace code. // This is inspired by Craig Silverstein's PowerPC stacktrace code.
//
#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_ #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_ #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_

View file

@ -1,7 +1,16 @@
// Copyright 2000 - 2007 Google Inc. // Copyright 2017 The Abseil Authors.
// All rights reserved.
// //
// Author: Sanjay Ghemawat // 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.
// //
// Portable implementation - just use glibc // Portable implementation - just use glibc
// //

View file

@ -23,6 +23,11 @@ FlagStateInterface::~FlagStateInterface() {}
bool CommandLineFlag::IsRetired() const { return false; } 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);
}
FlagFastTypeId PrivateHandleInterface::TypeId(const CommandLineFlag& flag) { FlagFastTypeId PrivateHandleInterface::TypeId(const CommandLineFlag& flag) {
return flag.TypeId(); return flag.TypeId();
} }
@ -42,6 +47,14 @@ void PrivateHandleInterface::CheckDefaultValueParsingRoundtrip(
flag.CheckDefaultValueParsingRoundtrip(); flag.CheckDefaultValueParsingRoundtrip();
} }
bool PrivateHandleInterface::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 } // namespace flags_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl

View file

@ -138,6 +138,17 @@ class CommandLineFlag {
virtual std::string DefaultValue() const = 0; virtual std::string DefaultValue() const = 0;
virtual std::string CurrentValue() const = 0; virtual std::string CurrentValue() const = 0;
// 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 PrivateHandleInterface;
// Sets the value of the flag based on specified string `value`. If the flag // Sets the value of the flag based on specified string `value`. If the flag
// was successfully set to new value, it returns true. Otherwise, sets `error` // was successfully set to new value, it returns true. Otherwise, sets `error`
// to indicate the error, leaves the flag unchanged, and returns false. There // to indicate the error, leaves the flag unchanged, and returns false. There
@ -151,12 +162,6 @@ class CommandLineFlag {
flags_internal::ValueSource source, flags_internal::ValueSource source,
std::string* error) = 0; std::string* error) = 0;
protected:
~CommandLineFlag() = default;
private:
friend class PrivateHandleInterface;
// Returns id of the flag's value type. // Returns id of the flag's value type.
virtual FlagFastTypeId TypeId() const = 0; virtual FlagFastTypeId TypeId() const = 0;
@ -168,7 +173,6 @@ class CommandLineFlag {
// the dst based on the current flag's value. // the dst based on the current flag's value.
virtual void Read(void* dst) const = 0; virtual void Read(void* dst) const = 0;
// Interfaces to operate on validators.
// Validates supplied value usign validator or parseflag routine // Validates supplied value usign validator or parseflag routine
virtual bool ValidateInputValue(absl::string_view value) const = 0; virtual bool ValidateInputValue(absl::string_view value) const = 0;
@ -194,6 +198,10 @@ class PrivateHandleInterface {
// Access to CommandLineFlag::CheckDefaultValueParsingRoundtrip. // Access to CommandLineFlag::CheckDefaultValueParsingRoundtrip.
static void CheckDefaultValueParsingRoundtrip(const CommandLineFlag& flag); 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);
}; };
// This macro is the "source of truth" for the list of supported flag built-in // This macro is the "source of truth" for the list of supported flag built-in

View file

@ -122,49 +122,53 @@ TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) {
auto* flag_01 = flags::FindCommandLineFlag("int_flag"); auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(flag_01->ParseFrom("11", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(flag_01->ParseFrom("-123", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(!flag_01->ParseFrom("xyz", flags::SET_FLAGS_VALUE, EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'"); EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(!flag_01->ParseFrom("A1", flags::SET_FLAGS_VALUE, EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'"); EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(flag_01->ParseFrom("0x10", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_FALSE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(flag_01->ParseFrom("011", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kCommandLine, &err)); flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
EXPECT_TRUE(flag_01->IsSpecifiedOnCommandLine()); EXPECT_TRUE(flag_01->IsSpecifiedOnCommandLine());
EXPECT_TRUE(!flag_01->ParseFrom("", flags::SET_FLAGS_VALUE, EXPECT_TRUE(!flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'"); EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
auto* flag_02 = flags::FindCommandLineFlag("string_flag"); auto* flag_02 = flags::FindCommandLineFlag("string_flag");
EXPECT_TRUE(flag_02->ParseFrom("xyz", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz"); EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
EXPECT_TRUE(flag_02->ParseFrom("", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), ""); EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
} }
@ -175,14 +179,16 @@ TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) {
auto* flag_01 = flags::FindCommandLineFlag("int_flag"); auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_TRUE(flag_01->ParseFrom("111", flags::SET_FLAGS_DEFAULT, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
&err));
EXPECT_EQ(flag_01->DefaultValue(), "111"); EXPECT_EQ(flag_01->DefaultValue(), "111");
auto* flag_02 = flags::FindCommandLineFlag("string_flag"); auto* flag_02 = flags::FindCommandLineFlag("string_flag");
EXPECT_TRUE(flag_02->ParseFrom("abc", flags::SET_FLAGS_DEFAULT, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
&err));
EXPECT_EQ(flag_02->DefaultValue(), "abc"); EXPECT_EQ(flag_02->DefaultValue(), "abc");
} }
@ -193,22 +199,26 @@ TEST_F(CommandLineFlagTest, TestParseFromIfDefault) {
auto* flag_01 = flags::FindCommandLineFlag("int_flag"); auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_TRUE(flag_01->ParseFrom("22", flags::SET_FLAG_IF_DEFAULT, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)) flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
&err))
<< err; << err;
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
EXPECT_TRUE(flag_01->ParseFrom("33", flags::SET_FLAG_IF_DEFAULT, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
// EXPECT_EQ(err, "ERROR: int_flag is already set to 22"); // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
// Reset back to default value // Reset back to default value
EXPECT_TRUE(flag_01->ParseFrom("201", flags::SET_FLAGS_VALUE, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
&err));
EXPECT_TRUE(flag_01->ParseFrom("33", flags::SET_FLAG_IF_DEFAULT, EXPECT_TRUE(flags::PrivateHandleInterface::ParseFrom(
flags::kProgrammaticChange, &err)); flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
&err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
// EXPECT_EQ(err, "ERROR: int_flag is already set to 201"); // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
} }

View file

@ -56,7 +56,8 @@ bool SetCommandLineOptionWithMode(absl::string_view name,
if (!flag || flag->IsRetired()) return false; if (!flag || flag->IsRetired()) return false;
std::string error; std::string error;
if (!flag->ParseFrom(value, set_mode, kProgrammaticChange, &error)) { if (!flags_internal::PrivateHandleInterface::ParseFrom(
flag, value, set_mode, kProgrammaticChange, &error)) {
// Errors here are all of the form: the provided name was a recognized // Errors here are all of the form: the provided name was a recognized
// flag, but the value was invalid (bad type, or validation failed). // flag, but the value was invalid (bad type, or validation failed).
flags_internal::ReportUsageError(error, false); flags_internal::ReportUsageError(error, false);

View file

@ -697,7 +697,8 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
if (flag->IsRetired()) continue; if (flag->IsRetired()) continue;
std::string error; std::string error;
if (!flag->ParseFrom(value, SET_FLAGS_VALUE, kCommandLine, &error)) { if (!flags_internal::PrivateHandleInterface::ParseFrom(
flag, value, SET_FLAGS_VALUE, kCommandLine, &error)) {
flags_internal::ReportUsageError(error, true); flags_internal::ReportUsageError(error, true);
success = false; success = false;
} }

View file

@ -143,7 +143,7 @@ struct Flags {
// clang-format off // clang-format off
#define ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP) \ #define ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP) \
/* text */ \ /* text */ \
X_VAL(c) X_SEP X_VAL(C) X_SEP X_VAL(s) X_SEP X_VAL(S) X_SEP \ X_VAL(c) X_SEP X_VAL(s) X_SEP \
/* ints */ \ /* ints */ \
X_VAL(d) X_SEP X_VAL(i) X_SEP X_VAL(o) X_SEP \ X_VAL(d) X_SEP X_VAL(i) X_SEP X_VAL(o) X_SEP \
X_VAL(u) X_SEP X_VAL(x) X_SEP X_VAL(X) X_SEP \ X_VAL(u) X_SEP X_VAL(x) X_SEP X_VAL(X) X_SEP \
@ -170,7 +170,7 @@ struct FormatConversionCharInternal {
private: private:
// clang-format off // clang-format off
enum class Enum : uint8_t { enum class Enum : uint8_t {
c, C, s, S, // text c, s, // text
d, i, o, u, x, X, // int d, i, o, u, x, X, // int
f, F, e, E, g, G, a, A, // float f, F, e, E, g, G, a, A, // float
n, p, // misc n, p, // misc
@ -444,7 +444,7 @@ class FormatConversionSpec {
// clang-format off // clang-format off
enum class FormatConversionChar : uint8_t { enum class FormatConversionChar : uint8_t {
c, C, s, S, // text c, s, // text
d, i, o, u, x, X, // int d, i, o, u, x, X, // int
f, F, e, E, g, G, a, A, // float f, F, e, E, g, G, a, A, // float
n, p // misc n, p // misc
@ -454,9 +454,7 @@ enum class FormatConversionChar : uint8_t {
enum class FormatConversionCharSet : uint64_t { enum class FormatConversionCharSet : uint64_t {
// text // text
c = str_format_internal::FormatConversionCharToConvInt('c'), c = str_format_internal::FormatConversionCharToConvInt('c'),
C = str_format_internal::FormatConversionCharToConvInt('C'),
s = str_format_internal::FormatConversionCharToConvInt('s'), s = str_format_internal::FormatConversionCharToConvInt('s'),
S = str_format_internal::FormatConversionCharToConvInt('S'),
// integer // integer
d = str_format_internal::FormatConversionCharToConvInt('d'), d = str_format_internal::FormatConversionCharToConvInt('d'),
i = str_format_internal::FormatConversionCharToConvInt('i'), i = str_format_internal::FormatConversionCharToConvInt('i'),

View file

@ -29,9 +29,9 @@ ABSL_CONST_INIT const ConvTag kTags[256] = {
{}, {}, {}, {}, {}, {}, {}, {}, // 28-2f {}, {}, {}, {}, {}, {}, {}, {}, // 28-2f
{}, {}, {}, {}, {}, {}, {}, {}, // 30-37 {}, {}, {}, {}, {}, {}, {}, {}, // 30-37
{}, {}, {}, {}, {}, {}, {}, {}, // 38-3f {}, {}, {}, {}, {}, {}, {}, {}, // 38-3f
{}, CC::A, {}, CC::C, {}, CC::E, CC::F, CC::G, // @ABCDEFG {}, CC::A, {}, {}, {}, CC::E, CC::F, CC::G, // @ABCDEFG
{}, {}, {}, {}, LM::L, {}, {}, {}, // HIJKLMNO {}, {}, {}, {}, LM::L, {}, {}, {}, // HIJKLMNO
{}, {}, {}, CC::S, {}, {}, {}, {}, // PQRSTUVW {}, {}, {}, {}, {}, {}, {}, {}, // PQRSTUVW
CC::X, {}, {}, {}, {}, {}, {}, {}, // XYZ[\]^_ CC::X, {}, {}, {}, {}, {}, {}, {}, // XYZ[\]^_
{}, CC::a, {}, CC::c, CC::d, CC::e, CC::f, CC::g, // `abcdefg {}, CC::a, {}, CC::c, CC::d, CC::e, CC::f, CC::g, // `abcdefg
LM::h, CC::i, LM::j, {}, LM::l, {}, CC::n, CC::o, // hijklmno LM::h, CC::i, LM::j, {}, LM::l, {}, CC::n, CC::o, // hijklmno

View file

@ -47,7 +47,7 @@ TEST(ConversionCharTest, Names) {
// clang-format off // clang-format off
const Expectation kExpect[] = { const Expectation kExpect[] = {
#define X(c) {FormatConversionCharInternal::c, #c[0]} #define X(c) {FormatConversionCharInternal::c, #c[0]}
X(c), X(C), X(s), X(S), // text X(c), X(s), // text
X(d), X(i), X(o), X(u), X(x), X(X), // int X(d), X(i), X(o), X(u), X(x), X(X), // int
X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float
X(n), X(p), // misc X(n), X(p), // misc