From 2562ef37a103e642bfd20b88413fcc97bc6a5796 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sun, 30 Jul 2017 14:31:01 -0700 Subject: [PATCH] Outline throw statements in format Summary: [Folly] Outline `throw` statements in `format`. Reviewed By: Orvid Differential Revision: D5523933 fbshipit-source-id: 371c9ecc707e48dcc05fa4aab4fd326111466161 --- folly/Format-inl.h | 12 ++++++------ folly/FormatArg.cpp | 27 +++++++++++++++++++++++++++ folly/FormatArg.h | 9 +++++---- folly/Makefile.am | 1 + 4 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 folly/FormatArg.cpp diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 7cea87a4..940224c2 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -180,7 +180,7 @@ void BaseFormatter::operator()( p = q; if (p == end || *p != '}') { - throw BadFormatArg("folly::format: single '}' in format string"); + throwBadFormatArg("folly::format: single '}' in format string"); } ++p; } @@ -202,7 +202,7 @@ void BaseFormatter::operator()( p = q + 1; if (p == end) { - throw BadFormatArg("folly::format: '}' at end of format string"); + throwBadFormatArg("folly::format: '}' at end of format string"); } // "{{" -> "{" @@ -215,7 +215,7 @@ void BaseFormatter::operator()( // Format string q = static_cast(memchr(p, '}', size_t(end - p))); if (q == nullptr) { - throw BadFormatArg("folly::format: missing ending '}'"); + throwBadFormatArg("folly::format: missing ending '}'"); } FormatArg arg(StringPiece(p, q)); p = q + 1; @@ -264,7 +264,7 @@ void BaseFormatter::operator()( } if (hasDefaultArgIndex && hasExplicitArgIndex) { - throw BadFormatArg( + throwBadFormatArg( "folly::format: may not have both default and explicit arg indexes"); } @@ -290,10 +290,10 @@ namespace format_value { template void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { if (arg.width != FormatArg::kDefaultWidth && arg.width < 0) { - throw BadFormatArg("folly::format: invalid width"); + throwBadFormatArg("folly::format: invalid width"); } if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) { - throw BadFormatArg("folly::format: invalid precision"); + throwBadFormatArg("folly::format: invalid precision"); } if (arg.precision != FormatArg::kDefaultPrecision && diff --git a/folly/FormatArg.cpp b/folly/FormatArg.cpp new file mode 100644 index 00000000..1e19aa5f --- /dev/null +++ b/folly/FormatArg.cpp @@ -0,0 +1,27 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * 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 + * + * http://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 + +namespace folly { + +[[noreturn]] void throwBadFormatArg(char const* msg) { + throw BadFormatArg(msg); +} +[[noreturn]] void throwBadFormatArg(std::string const& msg) { + throw BadFormatArg(msg); +} +} diff --git a/folly/FormatArg.h b/folly/FormatArg.h index fec6e811..d3ee03aa 100644 --- a/folly/FormatArg.h +++ b/folly/FormatArg.h @@ -26,11 +26,12 @@ namespace folly { class BadFormatArg : public std::invalid_argument { - public: - explicit BadFormatArg(const std::string& msg) - : std::invalid_argument(msg) {} + using invalid_argument::invalid_argument; }; +[[noreturn]] void throwBadFormatArg(char const* msg); +[[noreturn]] void throwBadFormatArg(std::string const& msg); + /** * Parsed format argument. */ @@ -213,7 +214,7 @@ inline std::string FormatArg::errorStr(Args&&... args) const { template [[noreturn]] inline void FormatArg::error(Args&&... args) const { - throw BadFormatArg(errorStr(std::forward(args)...)); + throwBadFormatArg(errorStr(std::forward(args)...)); } template diff --git a/folly/Makefile.am b/folly/Makefile.am index 54788e69..e629366f 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -454,6 +454,7 @@ libfollybase_la_SOURCES = \ detail/RangeCommon.cpp \ EscapeTables.cpp \ Format.cpp \ + FormatArg.cpp \ FormatTables.cpp \ MallctlHelper.cpp \ portability/BitsFunctexcept.cpp \ -- 2.34.1