diff --git a/source/common/common/fmt.h b/source/common/common/fmt.h index 1c37d0cf32b67..fe808a2139a1e 100644 --- a/source/common/common/fmt.h +++ b/source/common/common/fmt.h @@ -12,8 +12,8 @@ namespace fmt { // Provide an implementation of formatter for fmt::format that allows absl::string_view to be // formatted with the same format specifiers available to std::string. -// TODO(zuercher): Once absl::string_view is replaced with std::string_view, this can be removed -// as fmtlib handles std::string_view natively. +// TODO(zuercher): Once absl::string_view is replaced with the std type, this can be removed +// as fmtlib handles string_view natively. // NOLINTNEXTLINE(readability-identifier-naming) template <> struct formatter : formatter { auto format(absl::string_view absl_string_view, fmt::format_context& ctx) -> decltype(ctx.out()) { diff --git a/source/extensions/filters/http/cache/cache_headers_utils.cc b/source/extensions/filters/http/cache/cache_headers_utils.cc index 7762c3091a5fb..9a68669a73713 100644 --- a/source/extensions/filters/http/cache/cache_headers_utils.cc +++ b/source/extensions/filters/http/cache/cache_headers_utils.cc @@ -237,9 +237,9 @@ namespace { // https://tools.ietf.org/html/rfc2616#section-4.2. // Used to separate the values of different headers. -constexpr std::string_view header_separator = "\n"; +constexpr absl::string_view header_separator = "\n"; // Used to separate multiple values of a same header. -constexpr std::string_view in_value_separator = "\r"; +constexpr absl::string_view in_value_separator = "\r"; }; // namespace std::string VaryHeader::createVaryKey(const Http::HeaderEntry* vary_header, diff --git a/source/extensions/quic_listeners/quiche/envoy_quic_utils.h b/source/extensions/quic_listeners/quiche/envoy_quic_utils.h index 5c321ab749f1d..287576d3dd539 100644 --- a/source/extensions/quic_listeners/quiche/envoy_quic_utils.h +++ b/source/extensions/quic_listeners/quiche/envoy_quic_utils.h @@ -51,7 +51,7 @@ template std::unique_ptr spdyHeaderBlockToEnvoyHeaders(const spdy::SpdyHeaderBlock& header_block) { auto headers = T::create(); for (auto entry : header_block) { - // TODO(danzh): Avoid temporary strings and addCopy() with std::string_view. + // TODO(danzh): Avoid temporary strings and addCopy() with string_view. std::string key(entry.first); std::string value(entry.second); headers->addCopy(Http::LowerCaseString(key), value); diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index ea5b4ebcd48d0..c9f96418bb065 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -687,17 +687,27 @@ def checkSourceLine(line, file_path, reportError): # The std::atomic_* free functions are functionally equivalent to calling # operations on std::atomic objects, so prefer to use that instead. reportError("Don't use free std::atomic_* functions, use std::atomic members instead.") - # Blocking the use of std::any, std::optional, std::variant for now as iOS 11/macOS 10.13 - # does not support these functions at runtime. + # Block usage of certain std types/functions as iOS 11 and macOS 10.13 + # do not support these at runtime. # See: https://github.com/envoyproxy/envoy/issues/12341 if tokenInLine("std::any", line): reportError("Don't use std::any; use absl::any instead") + if tokenInLine("std::get_if", line): + reportError("Don't use std::get_if; use absl::get_if instead") + if tokenInLine("std::holds_alternative", line): + reportError("Don't use std::holds_alternative; use absl::holds_alternative instead") if tokenInLine("std::make_optional", line): reportError("Don't use std::make_optional; use absl::make_optional instead") + if tokenInLine("std::monostate", line): + reportError("Don't use std::monostate; use absl::monostate instead") if tokenInLine("std::optional", line): reportError("Don't use std::optional; use absl::optional instead") + if tokenInLine("std::string_view", line): + reportError("Don't use std::string_view; use absl::string_view instead") if tokenInLine("std::variant", line): reportError("Don't use std::variant; use absl::variant instead") + if tokenInLine("std::visit", line): + reportError("Don't use std::visit; use absl::visit instead") if "__attribute__((packed))" in line and file_path != "./include/envoy/common/platform.h": # __attribute__((packed)) is not supported by MSVC, we have a PACKED_STRUCT macro that # can be used instead diff --git a/tools/code_format/check_format_test_helper.py b/tools/code_format/check_format_test_helper.py index fcfaf8b9218ef..03129a1093f4b 100755 --- a/tools/code_format/check_format_test_helper.py +++ b/tools/code_format/check_format_test_helper.py @@ -243,12 +243,21 @@ def runChecks(): "std_unordered_set.cc", "Don't use std::unordered_set; use absl::flat_hash_set instead " + "or absl::node_hash_set if pointer stability of keys/values is required") errors += checkUnfixableError("std_any.cc", "Don't use std::any; use absl::any instead") + errors += checkUnfixableError("std_get_if.cc", "Don't use std::get_if; use absl::get_if instead") + errors += checkUnfixableError( + "std_holds_alternative.cc", + "Don't use std::holds_alternative; use absl::holds_alternative instead") errors += checkUnfixableError("std_make_optional.cc", "Don't use std::make_optional; use absl::make_optional instead") + errors += checkUnfixableError("std_monostate.cc", + "Don't use std::monostate; use absl::monostate instead") errors += checkUnfixableError("std_optional.cc", "Don't use std::optional; use absl::optional instead") + errors += checkUnfixableError("std_string_view.cc", + "Don't use std::string_view; use absl::string_view instead") errors += checkUnfixableError("std_variant.cc", "Don't use std::variant; use absl::variant instead") + errors += checkUnfixableError("std_visit.cc", "Don't use std::visit; use absl::visit instead") errors += checkUnfixableError( "throw.cc", "Don't introduce throws into exception-free files, use error statuses instead.") errors += checkFileExpectingOK("commented_throw.cc") diff --git a/tools/testdata/.DS_Store b/tools/testdata/.DS_Store new file mode 100644 index 0000000000000..f24b797279219 Binary files /dev/null and b/tools/testdata/.DS_Store differ diff --git a/tools/testdata/check_format/std_get_if.cc b/tools/testdata/check_format/std_get_if.cc new file mode 100644 index 0000000000000..289ff5727c6e0 --- /dev/null +++ b/tools/testdata/check_format/std_get_if.cc @@ -0,0 +1,8 @@ +#include + +namespace Envoy { + void foo() { + absl::variant x{12}; + auto y = std::get_if(&x); + } +} // namespace Envoy diff --git a/tools/testdata/check_format/std_holds_alternative.cc b/tools/testdata/check_format/std_holds_alternative.cc new file mode 100644 index 0000000000000..25e8468548d66 --- /dev/null +++ b/tools/testdata/check_format/std_holds_alternative.cc @@ -0,0 +1,8 @@ +#include + +namespace Envoy { + void foo() { + absl::variant x{12}; + auto y = std::holds_alternative(x); + } +} // namespace Envoy diff --git a/tools/testdata/check_format/std_monostate.cc b/tools/testdata/check_format/std_monostate.cc new file mode 100644 index 0000000000000..e2bf8d42daeba --- /dev/null +++ b/tools/testdata/check_format/std_monostate.cc @@ -0,0 +1,12 @@ +#include + +namespace Envoy { + struct S { + S(int i) : i(i) {} + int i; + }; + + void foo() { + absl::variant x; + } +} // namespace Envoy diff --git a/tools/testdata/check_format/std_string_view.cc b/tools/testdata/check_format/std_string_view.cc new file mode 100644 index 0000000000000..f92585f74cf71 --- /dev/null +++ b/tools/testdata/check_format/std_string_view.cc @@ -0,0 +1,7 @@ +#include + +namespace Envoy { + void foo() { + std::string_view x("a string literal"); + } +} // namespace Envoy diff --git a/tools/testdata/check_format/std_visit.cc b/tools/testdata/check_format/std_visit.cc new file mode 100644 index 0000000000000..ee1ac550e8984 --- /dev/null +++ b/tools/testdata/check_format/std_visit.cc @@ -0,0 +1,14 @@ +#include + +namespace Envoy { + struct SomeVisitorFunctor { + template + void operator()(const T& i) const {} + }; + + void foo() { + absl::variant x{12}; + SomeVisitorFunctor visitor; + std::visit(visitor, x); + } +} // namespace Envoy