annotate lib/Support/FormatVariadic.cpp @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
1 //===- FormatVariadic.cpp - Format string parsing and analysis ----*-C++-*-===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
2 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
4 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
8
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
9 #include "llvm/Support/FormatVariadic.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
10
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
11 using namespace llvm;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
12
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
13 static Optional<AlignStyle> translateLocChar(char C) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
14 switch (C) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
15 case '-':
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
16 return AlignStyle::Left;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
17 case '=':
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
18 return AlignStyle::Center;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
19 case '+':
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
20 return AlignStyle::Right;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
21 default:
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
22 return None;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
23 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
24 LLVM_BUILTIN_UNREACHABLE;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
25 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
26
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
27 bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
28 size_t &Align, char &Pad) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
29 Where = AlignStyle::Right;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
30 Align = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
31 Pad = ' ';
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
32 if (Spec.empty())
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
33 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
34
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
35 if (Spec.size() > 1) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
36 // A maximum of 2 characters at the beginning can be used for something
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
37 // other
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
38 // than the width.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
39 // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
40 // contains the width.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
41 // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
42 // Otherwise, Spec[0:...] contains the width.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
43 if (auto Loc = translateLocChar(Spec[1])) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
44 Pad = Spec[0];
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
45 Where = *Loc;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
46 Spec = Spec.drop_front(2);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
47 } else if (auto Loc = translateLocChar(Spec[0])) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
48 Where = *Loc;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
49 Spec = Spec.drop_front(1);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
50 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
51 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
52
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
53 bool Failed = Spec.consumeInteger(0, Align);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
54 return !Failed;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
55 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
56
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
57 Optional<ReplacementItem>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
58 formatv_object_base::parseReplacementItem(StringRef Spec) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
59 StringRef RepString = Spec.trim("{}");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
60
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
61 // If the replacement sequence does not start with a non-negative integer,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
62 // this is an error.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
63 char Pad = ' ';
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
64 std::size_t Align = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
65 AlignStyle Where = AlignStyle::Right;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
66 StringRef Options;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
67 size_t Index = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
68 RepString = RepString.trim();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
69 if (RepString.consumeInteger(0, Index)) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
70 assert(false && "Invalid replacement sequence index!");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
71 return ReplacementItem{};
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
72 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
73 RepString = RepString.trim();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
74 if (!RepString.empty() && RepString.front() == ',') {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
75 RepString = RepString.drop_front();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
76 if (!consumeFieldLayout(RepString, Where, Align, Pad))
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
77 assert(false && "Invalid replacement field layout specification!");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
78 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
79 RepString = RepString.trim();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
80 if (!RepString.empty() && RepString.front() == ':') {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
81 Options = RepString.drop_front().trim();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
82 RepString = StringRef();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
83 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
84 RepString = RepString.trim();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
85 if (!RepString.empty()) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
86 assert(false && "Unexpected characters found in replacement string!");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
87 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
88
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
89 return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
90 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
91
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
92 std::pair<ReplacementItem, StringRef>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
93 formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
94 std::size_t From = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
95 while (From < Fmt.size() && From != StringRef::npos) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
96 std::size_t BO = Fmt.find_first_of('{', From);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
97 // Everything up until the first brace is a literal.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
98 if (BO != 0)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
99 return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
100
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
101 StringRef Braces =
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
102 Fmt.drop_front(BO).take_while([](char C) { return C == '{'; });
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
103 // If there is more than one brace, then some of them are escaped. Treat
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
104 // these as replacements.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
105 if (Braces.size() > 1) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
106 size_t NumEscapedBraces = Braces.size() / 2;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
107 StringRef Middle = Fmt.substr(BO, NumEscapedBraces);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
108 StringRef Right = Fmt.drop_front(BO + NumEscapedBraces * 2);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
109 return std::make_pair(ReplacementItem{Middle}, Right);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
110 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
111 // An unterminated open brace is undefined. We treat the rest of the string
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
112 // as a literal replacement, but we assert to indicate that this is
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
113 // undefined and that we consider it an error.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
114 std::size_t BC = Fmt.find_first_of('}', BO);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
115 if (BC == StringRef::npos) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
116 assert(
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
117 false &&
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
118 "Unterminated brace sequence. Escape with {{ for a literal brace.");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
119 return std::make_pair(ReplacementItem{Fmt}, StringRef());
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
120 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
121
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
122 // Even if there is a closing brace, if there is another open brace before
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
123 // this closing brace, treat this portion as literal, and try again with the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
124 // next one.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
125 std::size_t BO2 = Fmt.find_first_of('{', BO + 1);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
126 if (BO2 < BC)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
127 return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
128 Fmt.substr(BO2));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
129
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
130 StringRef Spec = Fmt.slice(BO + 1, BC);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
131 StringRef Right = Fmt.substr(BC + 1);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
132
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
133 auto RI = parseReplacementItem(Spec);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
134 if (RI.hasValue())
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
135 return std::make_pair(*RI, Right);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
136
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
137 // If there was an error parsing the replacement item, treat it as an
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
138 // invalid replacement spec, and just continue.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
139 From = BC + 1;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
140 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
141 return std::make_pair(ReplacementItem{Fmt}, StringRef());
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
142 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
143
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
144 std::vector<ReplacementItem>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
145 formatv_object_base::parseFormatString(StringRef Fmt) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
146 std::vector<ReplacementItem> Replacements;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
147 ReplacementItem I;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
148 while (!Fmt.empty()) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
149 std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
150 if (I.Type != ReplacementType::Empty)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
151 Replacements.push_back(I);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
152 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
153 return Replacements;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
154 }