LLVM  4.0.0
FormatVariadic.cpp
Go to the documentation of this file.
1 //===- FormatVariadic.cpp - Format string parsing and analysis ----*-C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
8 
10 
11 using namespace llvm;
12 
14  switch (C) {
15  case '-':
16  return AlignStyle::Left;
17  case '=':
18  return AlignStyle::Center;
19  case '+':
20  return AlignStyle::Right;
21  default:
22  return None;
23  }
24  LLVM_BUILTIN_UNREACHABLE;
25 }
26 
28  size_t &Align, char &Pad) {
29  Where = AlignStyle::Right;
30  Align = 0;
31  Pad = ' ';
32  if (Spec.empty())
33  return true;
34 
35  if (Spec.size() > 1) {
36  // A maximum of 2 characters at the beginning can be used for something
37  // other
38  // than the width.
39  // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
40  // contains the width.
41  // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
42  // Otherwise, Spec[0:...] contains the width.
43  if (auto Loc = translateLocChar(Spec[1])) {
44  Pad = Spec[0];
45  Where = *Loc;
46  Spec = Spec.drop_front(2);
47  } else if (auto Loc = translateLocChar(Spec[0])) {
48  Where = *Loc;
49  Spec = Spec.drop_front(1);
50  }
51  }
52 
53  bool Failed = Spec.consumeInteger(0, Align);
54  return !Failed;
55 }
56 
59  StringRef RepString = Spec.trim("{}");
60 
61  // If the replacement sequence does not start with a non-negative integer,
62  // this is an error.
63  char Pad = ' ';
64  std::size_t Align = 0;
66  StringRef Options;
67  size_t Index = 0;
68  RepString = RepString.trim();
69  if (RepString.consumeInteger(0, Index)) {
70  assert(false && "Invalid replacement sequence index!");
71  return ReplacementItem{};
72  }
73  RepString = RepString.trim();
74  if (!RepString.empty() && RepString.front() == ',') {
75  RepString = RepString.drop_front();
76  if (!consumeFieldLayout(RepString, Where, Align, Pad))
77  assert(false && "Invalid replacement field layout specification!");
78  }
79  RepString = RepString.trim();
80  if (!RepString.empty() && RepString.front() == ':') {
81  Options = RepString.drop_front().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 
92 std::pair<ReplacementItem, StringRef>
94  StringRef Rep;
95  StringRef Remainder;
96  std::size_t From = 0;
97  while (From < Fmt.size() && From != StringRef::npos) {
98  std::size_t BO = Fmt.find_first_of('{', From);
99  // Everything up until the first brace is a literal.
100  if (BO != 0)
101  return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
102 
103  StringRef Braces =
104  Fmt.drop_front(BO).take_while([](char C) { return C == '{'; });
105  // If there is more than one brace, then some of them are escaped. Treat
106  // these as replacements.
107  if (Braces.size() > 1) {
108  size_t NumEscapedBraces = Braces.size() / 2;
109  StringRef Middle = Fmt.substr(BO, NumEscapedBraces);
110  StringRef Right = Fmt.drop_front(BO + NumEscapedBraces * 2);
111  return std::make_pair(ReplacementItem{Middle}, Right);
112  }
113  // An unterminated open brace is undefined. We treat the rest of the string
114  // as a literal replacement, but we assert to indicate that this is
115  // undefined and that we consider it an error.
116  std::size_t BC = Fmt.find_first_of('}', BO);
117  if (BC == StringRef::npos) {
118  assert(
119  false &&
120  "Unterminated brace sequence. Escape with {{ for a literal brace.");
121  return std::make_pair(ReplacementItem{Fmt}, StringRef());
122  }
123 
124  // Even if there is a closing brace, if there is another open brace before
125  // this closing brace, treat this portion as literal, and try again with the
126  // next one.
127  std::size_t BO2 = Fmt.find_first_of('{', BO + 1);
128  if (BO2 < BC)
129  return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
130  Fmt.substr(BO2));
131 
132  StringRef Spec = Fmt.slice(BO + 1, BC);
133  StringRef Right = Fmt.substr(BC + 1);
134 
135  auto RI = parseReplacementItem(Spec);
136  if (RI.hasValue())
137  return std::make_pair(*RI, Right);
138 
139  // If there was an error parsing the replacement item, treat it as an
140  // invalid replacement spec, and just continue.
141  From = BC + 1;
142  }
143  return std::make_pair(ReplacementItem{Fmt}, StringRef());
144 }
145 
146 std::vector<ReplacementItem>
148  std::vector<ReplacementItem> Replacements;
150  while (!Fmt.empty()) {
151  std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
152  if (I.Type != ReplacementType::Empty)
153  Replacements.push_back(I);
154  }
155  return Replacements;
156 }
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:634
const NoneType None
Definition: None.h:23
static std::vector< ReplacementItem > parseFormatString(StringRef Fmt)
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:528
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:699
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:139
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE 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:618
AlignStyle
Definition: FormatCommon.h:18
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:587
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:825
static std::pair< ReplacementItem, StringRef > splitLiteralAndReplacement(StringRef Fmt)
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where, size_t &Align, char &Pad)
std::vector< ReplacementItem > Replacements
static Optional< ReplacementItem > parseReplacementItem(StringRef Spec)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
static const size_t npos
Definition: StringRef.h:51
#define I(x, y, z)
Definition: MD5.cpp:54
ReplacementType Type
LLVM_NODISCARD 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:392
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static Optional< AlignStyle > translateLocChar(char C)