Skip to content

Commit

Permalink
[Support] Add formatv support for StringLiteral
Browse files Browse the repository at this point in the history
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
  • Loading branch information
labath committed Feb 14, 2017
1 parent 06f1a4b commit de348d3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 3 additions & 4 deletions include/llvm/Support/FormatProviders.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
};
Expand Down
2 changes: 2 additions & 0 deletions unittests/Support/FormatVariadicTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de348d3

Please sign in to comment.