LLVM 17.0.0git
FormatVariadic.cpp
Go to the documentation of this file.
1//===- FormatVariadic.cpp - Format string parsing and analysis ----*-C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//===----------------------------------------------------------------------===//
7
9#include <cassert>
10#include <optional>
11
12using namespace llvm;
13
14static std::optional<AlignStyle> translateLocChar(char C) {
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 size_t &Align, char &Pad) {
30 Where = AlignStyle::Right;
31 Align = 0;
32 Pad = ' ';
33 if (Spec.empty())
34 return true;
35
36 if (Spec.size() > 1) {
37 // A maximum of 2 characters at the beginning can be used for something
38 // other
39 // than the width.
40 // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
41 // contains the width.
42 // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
43 // Otherwise, Spec[0:...] contains the width.
44 if (auto Loc = translateLocChar(Spec[1])) {
45 Pad = Spec[0];
46 Where = *Loc;
47 Spec = Spec.drop_front(2);
48 } else if (auto Loc = translateLocChar(Spec[0])) {
49 Where = *Loc;
50 Spec = Spec.drop_front(1);
51 }
52 }
53
54 bool Failed = Spec.consumeInteger(0, Align);
55 return !Failed;
56}
57
58std::optional<ReplacementItem>
60 StringRef RepString = Spec.trim("{}");
61
62 // If the replacement sequence does not start with a non-negative integer,
63 // this is an error.
64 char Pad = ' ';
65 std::size_t Align = 0;
68 size_t Index = 0;
69 RepString = RepString.trim();
70 if (RepString.consumeInteger(0, Index)) {
71 assert(false && "Invalid replacement sequence index!");
72 return ReplacementItem{};
73 }
74 RepString = RepString.trim();
75 if (!RepString.empty() && RepString.front() == ',') {
76 RepString = RepString.drop_front();
77 if (!consumeFieldLayout(RepString, Where, Align, Pad))
78 assert(false && "Invalid replacement field layout specification!");
79 }
80 RepString = RepString.trim();
81 if (!RepString.empty() && RepString.front() == ':') {
82 Options = RepString.drop_front().trim();
83 RepString = StringRef();
84 }
85 RepString = RepString.trim();
86 if (!RepString.empty()) {
87 assert(false && "Unexpected characters found in replacement string!");
88 }
89
90 return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
91}
92
93std::pair<ReplacementItem, StringRef>
95 while (!Fmt.empty()) {
96 // Everything up until the first brace is a literal.
97 if (Fmt.front() != '{') {
98 std::size_t BO = Fmt.find_first_of('{');
99 return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
100 }
101
102 StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
103 // If there is more than one brace, then some of them are escaped. Treat
104 // these as replacements.
105 if (Braces.size() > 1) {
106 size_t NumEscapedBraces = Braces.size() / 2;
107 StringRef Middle = Fmt.take_front(NumEscapedBraces);
108 StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
109 return std::make_pair(ReplacementItem{Middle}, Right);
110 }
111 // An unterminated open brace is undefined. We treat the rest of the string
112 // as a literal replacement, but we assert to indicate that this is
113 // undefined and that we consider it an error.
114 std::size_t BC = Fmt.find_first_of('}');
115 if (BC == StringRef::npos) {
116 assert(
117 false &&
118 "Unterminated brace sequence. Escape with {{ for a literal brace.");
119 return std::make_pair(ReplacementItem{Fmt}, StringRef());
120 }
121
122 // Even if there is a closing brace, if there is another open brace before
123 // this closing brace, treat this portion as literal, and try again with the
124 // next one.
125 std::size_t BO2 = Fmt.find_first_of('{', 1);
126 if (BO2 < BC)
127 return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
128 Fmt.substr(BO2));
129
130 StringRef Spec = Fmt.slice(1, BC);
131 StringRef Right = Fmt.substr(BC + 1);
132
133 auto RI = parseReplacementItem(Spec);
134 if (RI)
135 return std::make_pair(*RI, Right);
136
137 // If there was an error parsing the replacement item, treat it as an
138 // invalid replacement spec, and just continue.
139 Fmt = Fmt.drop_front(BC + 1);
140 }
141 return std::make_pair(ReplacementItem{Fmt}, StringRef());
142}
143
148 while (!Fmt.empty()) {
149 std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
150 if (I.Type != ReplacementType::Empty)
151 Replacements.push_back(I);
152 }
153 return Replacements;
154}
155
156void detail::format_adapter::anchor() { }
static std::optional< AlignStyle > translateLocChar(char C)
static LVOptions Options
Definition: LVOptions.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:569
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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...
Definition: StringRef.h:595
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:607
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:682
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
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.
Definition: StringRef.h:375
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition: StringRef.h:578
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:813
static constexpr size_t npos
Definition: StringRef.h:52
static SmallVector< ReplacementItem, 2 > parseFormatString(StringRef Fmt)
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where, size_t &Align, char &Pad)
static std::optional< ReplacementItem > parseReplacementItem(StringRef Spec)
static std::pair< ReplacementItem, StringRef > splitLiteralAndReplacement(StringRef Fmt)
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AlignStyle
Definition: FormatCommon.h:17
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition: Error.h:198
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39