LLVM: lib/Support/FormatVariadic.cpp Source File (original) (raw)
1
2
3
4
5
6
7
9#include
10#include
11
12using namespace llvm;
13
15 switch (C) {
16 case '-':
17 return AlignStyle::Left;
18 case '=':
19 return AlignStyle::Center;
20 case '+':
21 return AlignStyle::Right;
22 default:
23 return std::nullopt;
24 }
25 LLVM_BUILTIN_UNREACHABLE;
26}
27
29 unsigned &Align, char &Pad) {
30 Where = AlignStyle::Right;
32 Pad = ' ';
33 if (Spec.empty())
34 return true;
35
36 if (Spec.size() > 1) {
37
38
39
40
41
42
44 Pad = Spec[0];
45 Where = *Loc;
48 Where = *Loc;
50 }
51 }
52
55}
56
59
60
61
62 char Pad = ' ';
63 unsigned Align = 0;
66 unsigned Index = ~0U;
67 RepString = RepString.ltrim();
68
69
71
74 assert(false && "Invalid replacement field layout specification!");
75 return std::nullopt;
76 }
77 }
78 RepString = RepString.ltrim();
82 }
83 RepString = RepString.trim();
84 if (!RepString.empty()) {
85 assert(0 && "Unexpected characters found in replacement string!");
86 return std::nullopt;
87 }
88
90}
91
92static std::pair<std::optional, StringRef>
95
96 if (Fmt.front() != '{') {
99 }
100
102
103
104 if (Braces.size() > 1) {
105 size_t NumEscapedBraces = Braces.size() / 2;
109 }
110
111
112
116 "Unterminated brace sequence. Escape with {{ for a literal brace.");
117 return {ReplacementItem("Unterminated brace sequence. Escape with {{ for a "
118 "literal brace."),
120 }
121
122
123
124
126 if (BO2 < BC)
128
131
133}
134
135#ifndef NDEBUG
136#define ENABLE_VALIDATION 1
137#else
138#define ENABLE_VALIDATION 0
139#endif
140
143 bool Validate) {
145 unsigned NextAutomaticIndex = 0;
146
147#if ENABLE_VALIDATION
149 unsigned NumExpectedArgs = 0;
150 bool HasExplicitIndex = false;
151#endif
152
154 std::optional I;
156 if ()
157 continue;
159 if (I->Index == ~0U)
160 I->Index = NextAutomaticIndex++;
161#if ENABLE_VALIDATION
162 else
163 HasExplicitIndex = true;
164 NumExpectedArgs = std::max(NumExpectedArgs, I->Index + 1);
165#endif
166 }
167
169 }
170
171#if ENABLE_VALIDATION
173 return Replacements;
174
175
176
177
178
179
180
181
182 auto getErrorReplacements = [SavedFmtStr](StringLiteral ErrorMsg) {
186 };
187
188 if (NumExpectedArgs != NumArgs) {
189 errs() << formatv("Expected {} Args, but got {} for format string '{}'\n",
190 NumExpectedArgs, NumArgs, SavedFmtStr);
191 assert(0 && "Invalid formatv() call");
192 return getErrorReplacements("Unexpected number of arguments");
193 }
194
195
196
198 unsigned Count = 0;
201 continue;
202 Indices[I.Index] = true;
203 ++Count;
204 }
205
206 if (Count != NumExpectedArgs) {
208 "Replacement field indices cannot have holes for format string '{}'\n",
209 SavedFmtStr);
210 assert(0 && "Invalid format string");
211 return getErrorReplacements("Replacement indices have holes");
212 }
213
214
215 if (NextAutomaticIndex != 0 && HasExplicitIndex) {
217 "Cannot mix automatic and explicit indices for format string '{}'\n",
218 SavedFmtStr);
219 assert(0 && "Invalid format string");
220 return getErrorReplacements("Cannot mix automatic and explicit indices");
221 }
222#endif
223 return Replacements;
224}
225
226void support::detail::format_adapter::anchor() {}
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where, unsigned &Align, char &Pad)
static std::pair< std::optional< ReplacementItem >, StringRef > splitLiteralAndReplacement(StringRef Fmt)
static std::optional< ReplacementItem > parseReplacementItem(StringRef Spec)
static std::optional< AlignStyle > translateLocChar(char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
StringRef take_while(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that every character in the prefix satisfies the given predi...
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
char front() const
front - Get the first character in the string.
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
static constexpr size_t npos
static SmallVector< ReplacementItem, 2 > parseFormatString(StringRef Fmt, size_t NumArgs, bool Validate)
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
testing::Matcher< const detail::ErrorHolder & > Failed()
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This struct is a compact representation of a valid (non-zero power of two) alignment.