diff include/llvm/Support/FormatVariadic.h @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children
line wrap: on
line diff
--- a/include/llvm/Support/FormatVariadic.h	Fri Nov 25 19:14:25 2016 +0900
+++ b/include/llvm/Support/FormatVariadic.h	Fri Oct 27 17:07:41 2017 +0900
@@ -26,17 +26,18 @@
 #ifndef LLVM_SUPPORT_FORMATVARIADIC_H
 #define LLVM_SUPPORT_FORMATVARIADIC_H
 
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
-#include "llvm/Support/DataTypes.h"
 #include "llvm/Support/FormatCommon.h"
 #include "llvm/Support/FormatProviders.h"
 #include "llvm/Support/FormatVariadicDetails.h"
 #include "llvm/Support/raw_ostream.h"
-
+#include <cstddef>
 #include <string>
 #include <tuple>
+#include <utility>
 #include <vector>
 
 namespace llvm {
@@ -44,13 +45,14 @@
 enum class ReplacementType { Empty, Format, Literal };
 
 struct ReplacementItem {
-  ReplacementItem() {}
+  ReplacementItem() = default;
   explicit ReplacementItem(StringRef Literal)
       : Type(ReplacementType::Literal), Spec(Literal) {}
   ReplacementItem(StringRef Spec, size_t Index, size_t Align, AlignStyle Where,
                   char Pad, StringRef Options)
       : Type(ReplacementType::Format), Spec(Spec), Index(Index), Align(Align),
         Where(Where), Pad(Pad), Options(Options) {}
+
   ReplacementType Type = ReplacementType::Empty;
   StringRef Spec;
   size_t Index = 0;
@@ -69,15 +71,15 @@
   // parameters in a template class that derives from a non-template superclass.
   // Essentially, we are converting a std::tuple<Derived<Ts...>> to a
   // std::vector<Base*>.
-  struct create_wrappers {
+  struct create_adapters {
     template <typename... Ts>
-    std::vector<detail::format_wrapper *> operator()(Ts &... Items) {
-      return std::vector<detail::format_wrapper *>{&Items...};
+    std::vector<detail::format_adapter *> operator()(Ts &... Items) {
+      return std::vector<detail::format_adapter *>{&Items...};
     }
   };
 
   StringRef Fmt;
-  std::vector<detail::format_wrapper *> Wrappers;
+  std::vector<detail::format_adapter *> Adapters;
   std::vector<ReplacementItem> Replacements;
 
   static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
@@ -89,10 +91,18 @@
 public:
   formatv_object_base(StringRef Fmt, std::size_t ParamCount)
       : Fmt(Fmt), Replacements(parseFormatString(Fmt)) {
-    Wrappers.reserve(ParamCount);
-    return;
+    Adapters.reserve(ParamCount);
   }
 
+  formatv_object_base(formatv_object_base const &rhs) = delete;
+
+  formatv_object_base(formatv_object_base &&rhs)
+      : Fmt(std::move(rhs.Fmt)),
+        Adapters(), // Adapters are initialized by formatv_object
+        Replacements(std::move(rhs.Replacements)) {
+    Adapters.reserve(rhs.Adapters.size());
+  };
+
   void format(raw_ostream &S) const {
     for (auto &R : Replacements) {
       if (R.Type == ReplacementType::Empty)
@@ -101,12 +111,12 @@
         S << R.Spec;
         continue;
       }
-      if (R.Index >= Wrappers.size()) {
+      if (R.Index >= Adapters.size()) {
         S << R.Spec;
         continue;
       }
 
-      auto W = Wrappers[R.Index];
+      auto W = Adapters[R.Index];
 
       FmtAlign Align(*W, R.Where, R.Align);
       Align.format(S, R.Options);
@@ -124,7 +134,7 @@
     return Result;
   }
 
-  template <unsigned N> llvm::SmallString<N> sstr() const {
+  template <unsigned N> SmallString<N> sstr() const {
     SmallString<N> Result;
     raw_svector_ostream Stream(Result);
     Stream << *this;
@@ -137,7 +147,7 @@
 };
 
 template <typename Tuple> class formatv_object : public formatv_object_base {
-  // Storage for the parameter wrappers.  Since the base class erases the type
+  // Storage for the parameter adapters.  Since the base class erases the type
   // of the parameters, we have to own the storage for the parameters here, and
   // have the base class store type-erased pointers into this tuple.
   Tuple Parameters;
@@ -146,7 +156,15 @@
   formatv_object(StringRef Fmt, Tuple &&Params)
       : formatv_object_base(Fmt, std::tuple_size<Tuple>::value),
         Parameters(std::move(Params)) {
-    Wrappers = apply_tuple(create_wrappers(), Parameters);
+    Adapters = apply_tuple(create_adapters(), Parameters);
+  }
+
+  formatv_object(formatv_object const &rhs) = delete;
+
+  formatv_object(formatv_object &&rhs)
+      : formatv_object_base(std::move(rhs)),
+        Parameters(std::move(rhs.Parameters)) {
+    Adapters = apply_tuple(create_adapters(), Parameters);
   }
 };
 
@@ -195,7 +213,7 @@
 // "}}" to print a literal '}'.
 //
 // ===Parameter Indexing===
-// `index` specifies the index of the paramter in the parameter pack to format
+// `index` specifies the index of the parameter in the parameter pack to format
 // into the output.  Note that it is possible to refer to the same parameter
 // index multiple times in a given format string.  This makes it possible to
 // output the same value multiple times without passing it multiple times to the
@@ -212,9 +230,8 @@
 // For a given parameter of type T, the following steps are executed in order
 // until a match is found:
 //
-//   1. If the parameter is of class type, and contains a method
-//      void format(raw_ostream &Stream, StringRef Options)
-//      Then this method is invoked to produce the formatted output.  The
+//   1. If the parameter is of class type, and inherits from format_adapter,
+//      Then format() is invoked on it to produce the formatted output.  The
 //      implementation should write the formatted text into `Stream`.
 //   2. If there is a suitable template specialization of format_provider<>
 //      for type T containing a method whose signature is:
@@ -233,14 +250,21 @@
 //
 template <typename... Ts>
 inline auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object<decltype(
-    std::make_tuple(detail::build_format_wrapper(std::forward<Ts>(Vals))...))> {
+    std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...))> {
   using ParamTuple = decltype(
-      std::make_tuple(detail::build_format_wrapper(std::forward<Ts>(Vals))...));
+      std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...));
   return formatv_object<ParamTuple>(
       Fmt,
-      std::make_tuple(detail::build_format_wrapper(std::forward<Ts>(Vals))...));
+      std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...));
 }
 
+// Allow a formatv_object to be formatted (no options supported).
+template <typename T> struct format_provider<formatv_object<T>> {
+  static void format(const formatv_object<T> &V, raw_ostream &OS, StringRef) {
+    OS << V;
+  }
+};
+
 } // end namespace llvm
 
-#endif
+#endif // LLVM_SUPPORT_FORMATVARIADIC_H