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
8#include "llvm/Support/FormatVariadic.h"
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
28bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
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(C: Spec[1])) {
45 Pad = Spec[0];
46 Where = *Loc;
47 Spec = Spec.drop_front(N: 2);
48 } else if (auto Loc = translateLocChar(C: Spec[0])) {
49 Where = *Loc;
50 Spec = Spec.drop_front(N: 1);
51 }
52 }
53
54 bool Failed = Spec.consumeInteger(Radix: 0, Result&: Align);
55 return !Failed;
56}
57
58std::optional<ReplacementItem>
59formatv_object_base::parseReplacementItem(StringRef Spec) {
60 StringRef RepString = Spec.trim(Chars: "{}");
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;
66 AlignStyle Where = AlignStyle::Right;
67 StringRef Options;
68 size_t Index = 0;
69 RepString = RepString.trim();
70 if (RepString.consumeInteger(Radix: 0, Result&: Index)) {
71 assert(false && "Invalid replacement sequence index!");
72 return ReplacementItem{};
73 }
74 RepString = RepString.trim();
75 if (RepString.consume_front(Prefix: ",")) {
76 if (!consumeFieldLayout(Spec&: RepString, Where, Align, Pad))
77 assert(false && "Invalid replacement field layout specification!");
78 }
79 RepString = RepString.trim();
80 if (RepString.consume_front(Prefix: ":")) {
81 Options = RepString.trim();
82 RepString = StringRef();
83 }
84 RepString = RepString.trim();
85 if (!RepString.empty()) {
86 assert(false && "Unexpected characters found in replacement string!");
87 }
88
89 return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
90}
91
92std::pair<ReplacementItem, StringRef>
93formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
94 while (!Fmt.empty()) {
95 // Everything up until the first brace is a literal.
96 if (Fmt.front() != '{') {
97 std::size_t BO = Fmt.find_first_of(C: '{');
98 return std::make_pair(x: ReplacementItem{Fmt.substr(Start: 0, N: BO)}, y: Fmt.substr(Start: BO));
99 }
100
101 StringRef Braces = Fmt.take_while(F: [](char C) { return C == '{'; });
102 // If there is more than one brace, then some of them are escaped. Treat
103 // these as replacements.
104 if (Braces.size() > 1) {
105 size_t NumEscapedBraces = Braces.size() / 2;
106 StringRef Middle = Fmt.take_front(N: NumEscapedBraces);
107 StringRef Right = Fmt.drop_front(N: NumEscapedBraces * 2);
108 return std::make_pair(x: ReplacementItem{Middle}, y&: Right);
109 }
110 // An unterminated open brace is undefined. We treat the rest of the string
111 // as a literal replacement, but we assert to indicate that this is
112 // undefined and that we consider it an error.
113 std::size_t BC = Fmt.find_first_of(C: '}');
114 if (BC == StringRef::npos) {
115 assert(
116 false &&
117 "Unterminated brace sequence. Escape with {{ for a literal brace.");
118 return std::make_pair(x: ReplacementItem{Fmt}, y: StringRef());
119 }
120
121 // Even if there is a closing brace, if there is another open brace before
122 // this closing brace, treat this portion as literal, and try again with the
123 // next one.
124 std::size_t BO2 = Fmt.find_first_of(C: '{', From: 1);
125 if (BO2 < BC)
126 return std::make_pair(x: ReplacementItem{Fmt.substr(Start: 0, N: BO2)},
127 y: Fmt.substr(Start: BO2));
128
129 StringRef Spec = Fmt.slice(Start: 1, End: BC);
130 StringRef Right = Fmt.substr(Start: BC + 1);
131
132 auto RI = parseReplacementItem(Spec);
133 if (RI)
134 return std::make_pair(x&: *RI, y&: Right);
135
136 // If there was an error parsing the replacement item, treat it as an
137 // invalid replacement spec, and just continue.
138 Fmt = Fmt.drop_front(N: BC + 1);
139 }
140 return std::make_pair(x: ReplacementItem{Fmt}, y: StringRef());
141}
142
143SmallVector<ReplacementItem, 2>
144formatv_object_base::parseFormatString(StringRef Fmt) {
145 SmallVector<ReplacementItem, 2> Replacements;
146 ReplacementItem I;
147 while (!Fmt.empty()) {
148 std::tie(args&: I, args&: Fmt) = splitLiteralAndReplacement(Fmt);
149 if (I.Type != ReplacementType::Empty)
150 Replacements.push_back(Elt: I);
151 }
152 return Replacements;
153}
154
155void support::detail::format_adapter::anchor() {}
156