xref: /src/contrib/llvm-project/llvm/lib/Support/FormatVariadic.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1b915e9e0SDimitry Andric //===- FormatVariadic.cpp - Format string parsing and analysis ----*-C++-*-===//
2b915e9e0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
7b915e9e0SDimitry Andric 
8b915e9e0SDimitry Andric #include "llvm/Support/FormatVariadic.h"
9cfca06d7SDimitry Andric #include <cassert>
10e3b55780SDimitry Andric #include <optional>
11b915e9e0SDimitry Andric 
12b915e9e0SDimitry Andric using namespace llvm;
13b915e9e0SDimitry Andric 
translateLocChar(char C)14e3b55780SDimitry Andric static std::optional<AlignStyle> translateLocChar(char C) {
15b915e9e0SDimitry Andric   switch (C) {
16b915e9e0SDimitry Andric   case '-':
17b915e9e0SDimitry Andric     return AlignStyle::Left;
18b915e9e0SDimitry Andric   case '=':
19b915e9e0SDimitry Andric     return AlignStyle::Center;
20b915e9e0SDimitry Andric   case '+':
21b915e9e0SDimitry Andric     return AlignStyle::Right;
22b915e9e0SDimitry Andric   default:
23e3b55780SDimitry Andric     return std::nullopt;
24b915e9e0SDimitry Andric   }
25b915e9e0SDimitry Andric   LLVM_BUILTIN_UNREACHABLE;
26b915e9e0SDimitry Andric }
27b915e9e0SDimitry Andric 
consumeFieldLayout(StringRef & Spec,AlignStyle & Where,size_t & Align,char & Pad)28b915e9e0SDimitry Andric bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
29b915e9e0SDimitry Andric                                              size_t &Align, char &Pad) {
30b915e9e0SDimitry Andric   Where = AlignStyle::Right;
31b915e9e0SDimitry Andric   Align = 0;
32b915e9e0SDimitry Andric   Pad = ' ';
33b915e9e0SDimitry Andric   if (Spec.empty())
34b915e9e0SDimitry Andric     return true;
35b915e9e0SDimitry Andric 
36b915e9e0SDimitry Andric   if (Spec.size() > 1) {
37b915e9e0SDimitry Andric     // A maximum of 2 characters at the beginning can be used for something
38b915e9e0SDimitry Andric     // other
39b915e9e0SDimitry Andric     // than the width.
40b915e9e0SDimitry Andric     // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
41b915e9e0SDimitry Andric     // contains the width.
42b915e9e0SDimitry Andric     // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
43b915e9e0SDimitry Andric     // Otherwise, Spec[0:...] contains the width.
44b915e9e0SDimitry Andric     if (auto Loc = translateLocChar(Spec[1])) {
45b915e9e0SDimitry Andric       Pad = Spec[0];
46b915e9e0SDimitry Andric       Where = *Loc;
47b915e9e0SDimitry Andric       Spec = Spec.drop_front(2);
48b915e9e0SDimitry Andric     } else if (auto Loc = translateLocChar(Spec[0])) {
49b915e9e0SDimitry Andric       Where = *Loc;
50b915e9e0SDimitry Andric       Spec = Spec.drop_front(1);
51b915e9e0SDimitry Andric     }
52b915e9e0SDimitry Andric   }
53b915e9e0SDimitry Andric 
54b915e9e0SDimitry Andric   bool Failed = Spec.consumeInteger(0, Align);
55b915e9e0SDimitry Andric   return !Failed;
56b915e9e0SDimitry Andric }
57b915e9e0SDimitry Andric 
58e3b55780SDimitry Andric std::optional<ReplacementItem>
parseReplacementItem(StringRef Spec)59b915e9e0SDimitry Andric formatv_object_base::parseReplacementItem(StringRef Spec) {
60b915e9e0SDimitry Andric   StringRef RepString = Spec.trim("{}");
61b915e9e0SDimitry Andric 
62b915e9e0SDimitry Andric   // If the replacement sequence does not start with a non-negative integer,
63b915e9e0SDimitry Andric   // this is an error.
64b915e9e0SDimitry Andric   char Pad = ' ';
65b915e9e0SDimitry Andric   std::size_t Align = 0;
66b915e9e0SDimitry Andric   AlignStyle Where = AlignStyle::Right;
67b915e9e0SDimitry Andric   StringRef Options;
68b915e9e0SDimitry Andric   size_t Index = 0;
69b915e9e0SDimitry Andric   RepString = RepString.trim();
70b915e9e0SDimitry Andric   if (RepString.consumeInteger(0, Index)) {
71b915e9e0SDimitry Andric     assert(false && "Invalid replacement sequence index!");
72b915e9e0SDimitry Andric     return ReplacementItem{};
73b915e9e0SDimitry Andric   }
74b915e9e0SDimitry Andric   RepString = RepString.trim();
754df029ccSDimitry Andric   if (RepString.consume_front(",")) {
76b915e9e0SDimitry Andric     if (!consumeFieldLayout(RepString, Where, Align, Pad))
77b915e9e0SDimitry Andric       assert(false && "Invalid replacement field layout specification!");
78b915e9e0SDimitry Andric   }
79b915e9e0SDimitry Andric   RepString = RepString.trim();
80ac9a064cSDimitry Andric   if (RepString.consume_front(":")) {
81ac9a064cSDimitry Andric     Options = RepString.trim();
82b915e9e0SDimitry Andric     RepString = StringRef();
83b915e9e0SDimitry Andric   }
84b915e9e0SDimitry Andric   RepString = RepString.trim();
85b915e9e0SDimitry Andric   if (!RepString.empty()) {
86b915e9e0SDimitry Andric     assert(false && "Unexpected characters found in replacement string!");
87b915e9e0SDimitry Andric   }
88b915e9e0SDimitry Andric 
89b915e9e0SDimitry Andric   return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
90b915e9e0SDimitry Andric }
91b915e9e0SDimitry Andric 
92b915e9e0SDimitry Andric std::pair<ReplacementItem, StringRef>
splitLiteralAndReplacement(StringRef Fmt)93b915e9e0SDimitry Andric formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
94b60736ecSDimitry Andric   while (!Fmt.empty()) {
95b915e9e0SDimitry Andric     // Everything up until the first brace is a literal.
96b60736ecSDimitry Andric     if (Fmt.front() != '{') {
97b60736ecSDimitry Andric       std::size_t BO = Fmt.find_first_of('{');
98b915e9e0SDimitry Andric       return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
99b60736ecSDimitry Andric     }
100b915e9e0SDimitry Andric 
101b60736ecSDimitry Andric     StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
102b915e9e0SDimitry Andric     // If there is more than one brace, then some of them are escaped.  Treat
103b915e9e0SDimitry Andric     // these as replacements.
104b915e9e0SDimitry Andric     if (Braces.size() > 1) {
105b915e9e0SDimitry Andric       size_t NumEscapedBraces = Braces.size() / 2;
106b60736ecSDimitry Andric       StringRef Middle = Fmt.take_front(NumEscapedBraces);
107b60736ecSDimitry Andric       StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
108b915e9e0SDimitry Andric       return std::make_pair(ReplacementItem{Middle}, Right);
109b915e9e0SDimitry Andric     }
110b915e9e0SDimitry Andric     // An unterminated open brace is undefined.  We treat the rest of the string
111b915e9e0SDimitry Andric     // as a literal replacement, but we assert to indicate that this is
112b915e9e0SDimitry Andric     // undefined and that we consider it an error.
113b60736ecSDimitry Andric     std::size_t BC = Fmt.find_first_of('}');
114b915e9e0SDimitry Andric     if (BC == StringRef::npos) {
115b915e9e0SDimitry Andric       assert(
116b915e9e0SDimitry Andric           false &&
117b915e9e0SDimitry Andric           "Unterminated brace sequence.  Escape with {{ for a literal brace.");
118b915e9e0SDimitry Andric       return std::make_pair(ReplacementItem{Fmt}, StringRef());
119b915e9e0SDimitry Andric     }
120b915e9e0SDimitry Andric 
121b915e9e0SDimitry Andric     // Even if there is a closing brace, if there is another open brace before
122b915e9e0SDimitry Andric     // this closing brace, treat this portion as literal, and try again with the
123b915e9e0SDimitry Andric     // next one.
124b60736ecSDimitry Andric     std::size_t BO2 = Fmt.find_first_of('{', 1);
125b915e9e0SDimitry Andric     if (BO2 < BC)
126b915e9e0SDimitry Andric       return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
127b915e9e0SDimitry Andric                             Fmt.substr(BO2));
128b915e9e0SDimitry Andric 
129b60736ecSDimitry Andric     StringRef Spec = Fmt.slice(1, BC);
130b915e9e0SDimitry Andric     StringRef Right = Fmt.substr(BC + 1);
131b915e9e0SDimitry Andric 
132b915e9e0SDimitry Andric     auto RI = parseReplacementItem(Spec);
133145449b1SDimitry Andric     if (RI)
134b915e9e0SDimitry Andric       return std::make_pair(*RI, Right);
135b915e9e0SDimitry Andric 
136b915e9e0SDimitry Andric     // If there was an error parsing the replacement item, treat it as an
137b915e9e0SDimitry Andric     // invalid replacement spec, and just continue.
138b60736ecSDimitry Andric     Fmt = Fmt.drop_front(BC + 1);
139b915e9e0SDimitry Andric   }
140b915e9e0SDimitry Andric   return std::make_pair(ReplacementItem{Fmt}, StringRef());
141b915e9e0SDimitry Andric }
142b915e9e0SDimitry Andric 
143cfca06d7SDimitry Andric SmallVector<ReplacementItem, 2>
parseFormatString(StringRef Fmt)144b915e9e0SDimitry Andric formatv_object_base::parseFormatString(StringRef Fmt) {
145cfca06d7SDimitry Andric   SmallVector<ReplacementItem, 2> Replacements;
146b915e9e0SDimitry Andric   ReplacementItem I;
147b915e9e0SDimitry Andric   while (!Fmt.empty()) {
148b915e9e0SDimitry Andric     std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
149b915e9e0SDimitry Andric     if (I.Type != ReplacementType::Empty)
150b915e9e0SDimitry Andric       Replacements.push_back(I);
151b915e9e0SDimitry Andric   }
152b915e9e0SDimitry Andric   return Replacements;
153b915e9e0SDimitry Andric }
154d8e91e46SDimitry Andric 
anchor()155ac9a064cSDimitry Andric void support::detail::format_adapter::anchor() {}
156