]> granicus.if.org Git - llvm/commitdiff
[Support] Add formatv support for StringLiteral
authorPavel Labath <labath@google.com>
Tue, 14 Feb 2017 16:35:56 +0000 (16:35 +0000)
committerPavel Labath <labath@google.com>
Tue, 14 Feb 2017 16:35:56 +0000 (16:35 +0000)
Summary:
This is achieved by generalizing the expression selecting the StringRef
format_provider. Now, anything that can be converted to a StringRef will
use it's formatter.

Reviewers: zturner

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29898

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295064 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FormatProviders.h
unittests/Support/FormatVariadicTest.cpp

index 1f0768c3ab08b38ad4569d6031c9dced82adaa04..54a37169d3af6a0491f09e2201410b57d5dd9435 100644 (file)
@@ -45,9 +45,8 @@ struct is_cstring
 
 template <typename T>
 struct use_string_formatter
-    : public std::integral_constant<
-          bool, is_one_of<T, llvm::StringRef, std::string>::value ||
-                    is_cstring<T>::value> {};
+    : public std::integral_constant<bool,
+                                    std::is_convertible<T, llvm::StringRef>::value> {};
 
 template <typename T>
 struct use_pointer_formatter
@@ -205,7 +204,7 @@ struct format_provider<
     if (!Style.empty() && Style.getAsInteger(10, N)) {
       assert(false && "Style is not a valid integer");
     }
-    llvm::StringRef S(V);
+    llvm::StringRef S = V;
     Stream << S.substr(0, N);
   }
 };
index 9307c6d8e09b53c81cddcc8b330eabf7fe2647b1..b0c843870afc24a486d76952cbabd60f375c5c17 100644 (file)
@@ -324,11 +324,13 @@ TEST(FormatVariadicTest, StringFormatting) {
   const char FooArray[] = "FooArray";
   const char *FooPtr = "FooPtr";
   llvm::StringRef FooRef("FooRef");
+  constexpr StringLiteral FooLiteral("FooLiteral");
   std::string FooString("FooString");
   // 1. Test that we can print various types of strings.
   EXPECT_EQ(FooArray, formatv("{0}", FooArray).str());
   EXPECT_EQ(FooPtr, formatv("{0}", FooPtr).str());
   EXPECT_EQ(FooRef, formatv("{0}", FooRef).str());
+  EXPECT_EQ(FooLiteral, formatv("{0}", FooLiteral).str());
   EXPECT_EQ(FooString, formatv("{0}", FooString).str());
 
   // 2. Test that the precision specifier prints the correct number of