Outline throw statements in format
authorYedidya Feldblum <yfeldblum@fb.com>
Sun, 30 Jul 2017 21:31:01 +0000 (14:31 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sun, 30 Jul 2017 21:41:58 +0000 (14:41 -0700)
Summary: [Folly] Outline `throw` statements in `format`.

Reviewed By: Orvid

Differential Revision: D5523933

fbshipit-source-id: 371c9ecc707e48dcc05fa4aab4fd326111466161

folly/Format-inl.h
folly/FormatArg.cpp [new file with mode: 0644]
folly/FormatArg.h
folly/Makefile.am

index 7cea87a4b5e07cc2f4ab39a7adbb206898d07ea9..940224c2bf60e39a2cad2eda016d681d454b550e 100644 (file)
@@ -180,7 +180,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(
       p = q;
 
       if (p == end || *p != '}') {
       p = q;
 
       if (p == end || *p != '}') {
-        throw BadFormatArg("folly::format: single '}' in format string");
+        throwBadFormatArg("folly::format: single '}' in format string");
       }
       ++p;
     }
       }
       ++p;
     }
@@ -202,7 +202,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(
     p = q + 1;
 
     if (p == end) {
     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<Derived, containerMode, Args...>::operator()(
     // Format string
     q = static_cast<const char*>(memchr(p, '}', size_t(end - p)));
     if (q == nullptr) {
     // Format string
     q = static_cast<const char*>(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;
     }
     FormatArg arg(StringPiece(p, q));
     p = q + 1;
@@ -264,7 +264,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(
     }
 
     if (hasDefaultArgIndex && hasExplicitArgIndex) {
     }
 
     if (hasDefaultArgIndex && hasExplicitArgIndex) {
-      throw BadFormatArg(
+      throwBadFormatArg(
           "folly::format: may not have both default and explicit arg indexes");
     }
 
           "folly::format: may not have both default and explicit arg indexes");
     }
 
@@ -290,10 +290,10 @@ namespace format_value {
 template <class FormatCallback>
 void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) {
   if (arg.width != FormatArg::kDefaultWidth && arg.width < 0) {
 template <class FormatCallback>
 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) {
   }
   if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) {
-    throw BadFormatArg("folly::format: invalid precision");
+    throwBadFormatArg("folly::format: invalid precision");
   }
 
   if (arg.precision != FormatArg::kDefaultPrecision &&
   }
 
   if (arg.precision != FormatArg::kDefaultPrecision &&
diff --git a/folly/FormatArg.cpp b/folly/FormatArg.cpp
new file mode 100644 (file)
index 0000000..1e19aa5
--- /dev/null
@@ -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 <folly/FormatArg.h>
+
+namespace folly {
+
+[[noreturn]] void throwBadFormatArg(char const* msg) {
+  throw BadFormatArg(msg);
+}
+[[noreturn]] void throwBadFormatArg(std::string const& msg) {
+  throw BadFormatArg(msg);
+}
+}
index fec6e811773abe422486cdf2dd11c6f234c462c5..d3ee03aa142d0723b7ab93f92ecef0c525adce54 100644 (file)
 namespace folly {
 
 class BadFormatArg : public std::invalid_argument {
 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.
  */
 /**
  * Parsed format argument.
  */
@@ -213,7 +214,7 @@ inline std::string FormatArg::errorStr(Args&&... args) const {
 
 template <typename... Args>
 [[noreturn]] inline void FormatArg::error(Args&&... args) const {
 
 template <typename... Args>
 [[noreturn]] inline void FormatArg::error(Args&&... args) const {
-  throw BadFormatArg(errorStr(std::forward<Args>(args)...));
+  throwBadFormatArg(errorStr(std::forward<Args>(args)...));
 }
 
 template <bool emptyOk>
 }
 
 template <bool emptyOk>
index 54788e69e5d4287ea1d899851392b88e72c958bf..e629366f12ca74ff6346ed6469465abb082a93dd 100644 (file)
@@ -454,6 +454,7 @@ libfollybase_la_SOURCES = \
        detail/RangeCommon.cpp \
        EscapeTables.cpp \
        Format.cpp \
        detail/RangeCommon.cpp \
        EscapeTables.cpp \
        Format.cpp \
+       FormatArg.cpp \
        FormatTables.cpp \
        MallctlHelper.cpp \
        portability/BitsFunctexcept.cpp \
        FormatTables.cpp \
        MallctlHelper.cpp \
        portability/BitsFunctexcept.cpp \