llvm.org GIT mirror llvm / 02ee503
Support formatting formatv_objects. Summary: Support formatting formatv_objects. While here, fix documentation about member-formatters, and attempted perfect-forwarding (I think). Reviewers: zturner Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D38997 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316330 91177308-0d34-0410-b5e6-96231b3b80d8 Sam McCall 1 year, 10 months ago
3 changed file(s) with 39 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
229229 // For a given parameter of type T, the following steps are executed in order
230230 // until a match is found:
231231 //
232 // 1. If the parameter is of class type, and contains a method
233 // void format(raw_ostream &Stream, StringRef Options)
234 // Then this method is invoked to produce the formatted output. The
232 // 1. If the parameter is of class type, and inherits from format_adapter,
233 // Then format() is invoked on it to produce the formatted output. The
235234 // implementation should write the formatted text into `Stream`.
236235 // 2. If there is a suitable template specialization of format_provider<>
237236 // for type T containing a method whose signature is:
258257 std::make_tuple(detail::build_format_adapter(std::forward(Vals))...));
259258 }
260259
260 // Allow a formatv_object to be formatted (no options supported).
261 template struct format_provider> {
262 static void format(const formatv_object &V, raw_ostream &OS, StringRef) {
263 OS << V;
264 }
265 };
266
261267 } // end namespace llvm
262268
263269 #endif // LLVM_SUPPORT_FORMATVARIADIC_H
3030 T Item;
3131
3232 public:
33 explicit provider_format_adapter(T &&Item) : Item(Item) {}
33 explicit provider_format_adapter(T &&Item) : Item(std::forward(Item)) {}
3434
3535 void format(llvm::raw_ostream &S, StringRef Options) override {
3636 format_provider::type>::format(Item, S, Options);
577577 // const Format cvar(1);
578578 // EXPECT_EQ("Format", formatv("{0}", cvar).str());
579579 }
580
581 TEST(FormatVariadicTest, FormatFormatvObject) {
582 EXPECT_EQ("Format", formatv("F{0}t", formatv("o{0}a", "rm")).str());
583 EXPECT_EQ("[ ! ]", formatv("[{0,+5}]", formatv("{0,-2}", "!")).str());
584 }
585
586 namespace {
587 struct Recorder {
588 int Copied = 0, Moved = 0;
589 Recorder() = default;
590 Recorder(const Recorder &Copy) : Copied(1 + Copy.Copied), Moved(Copy.Moved) {}
591 Recorder(const Recorder &&Move)
592 : Copied(Move.Copied), Moved(1 + Move.Moved) {}
593 };
594 } // namespace
595 template <> struct llvm::format_provider {
596 static void format(const Recorder &R, raw_ostream &OS, StringRef style) {
597 OS << R.Copied << "C " << R.Moved << "M";
598 }
599 };
600
601 TEST(FormatVariadicTest, CopiesAndMoves) {
602 Recorder R;
603 EXPECT_EQ("0C 0M", formatv("{0}", R).str());
604 EXPECT_EQ("0C 3M", formatv("{0}", std::move(R)).str());
605 EXPECT_EQ("0C 3M", formatv("{0}", Recorder()).str());
606 EXPECT_EQ(0, R.Copied);
607 EXPECT_EQ(0, R.Moved);
608 }