Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/FormatVariadic.cpp
Line
Count
Source (jump to first uncovered line)
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
10
using namespace llvm;
11
12
3.27k
static Optional<AlignStyle> translateLocChar(char C) {
13
3.27k
  switch (C) {
14
3.27k
  case '-':
15
481
    return AlignStyle::Left;
16
3.27k
  case '=':
17
246
    return AlignStyle::Center;
18
3.27k
  case '+':
19
468
    return AlignStyle::Right;
20
3.27k
  default:
21
2.08k
    return None;
22
0
  }
23
0
  LLVM_BUILTIN_UNREACHABLE;
24
0
}
25
26
bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
27
1.90k
                                             size_t &Align, char &Pad) {
28
1.90k
  Where = AlignStyle::Right;
29
1.90k
  Align = 0;
30
1.90k
  Pad = ' ';
31
1.90k
  if (Spec.empty())
32
0
    return true;
33
1.90k
34
1.90k
  if (Spec.size() > 1) {
35
1.64k
    // A maximum of 2 characters at the beginning can be used for something
36
1.64k
    // other
37
1.64k
    // than the width.
38
1.64k
    // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
39
1.64k
    // contains the width.
40
1.64k
    // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
41
1.64k
    // Otherwise, Spec[0:...] contains the width.
42
1.64k
    if (auto Loc = translateLocChar(Spec[1])) {
43
16
      Pad = Spec[0];
44
16
      Where = *Loc;
45
16
      Spec = Spec.drop_front(2);
46
1.63k
    } else if (auto Loc = translateLocChar(Spec[0])) {
47
1.17k
      Where = *Loc;
48
1.17k
      Spec = Spec.drop_front(1);
49
1.17k
    }
50
1.64k
  }
51
1.90k
52
1.90k
  bool Failed = Spec.consumeInteger(0, Align);
53
1.90k
  return !Failed;
54
1.90k
}
55
56
Optional<ReplacementItem>
57
152k
formatv_object_base::parseReplacementItem(StringRef Spec) {
58
152k
  StringRef RepString = Spec.trim("{}");
59
152k
60
152k
  // If the replacement sequence does not start with a non-negative integer,
61
152k
  // this is an error.
62
152k
  char Pad = ' ';
63
152k
  std::size_t Align = 0;
64
152k
  AlignStyle Where = AlignStyle::Right;
65
152k
  StringRef Options;
66
152k
  size_t Index = 0;
67
152k
  RepString = RepString.trim();
68
152k
  if (RepString.consumeInteger(0, Index)) {
69
0
    assert(false && "Invalid replacement sequence index!");
70
0
    return ReplacementItem{};
71
0
  }
72
152k
  RepString = RepString.trim();
73
152k
  if (!RepString.empty() && 
RepString.front() == ','16.0k
) {
74
1.90k
    RepString = RepString.drop_front();
75
1.90k
    if (!consumeFieldLayout(RepString, Where, Align, Pad))
76
1.90k
      assert(false && "Invalid replacement field layout specification!");
77
1.90k
  }
78
152k
  RepString = RepString.trim();
79
152k
  if (!RepString.empty() && 
RepString.front() == ':'14.6k
) {
80
14.6k
    Options = RepString.drop_front().trim();
81
14.6k
    RepString = StringRef();
82
14.6k
  }
83
152k
  RepString = RepString.trim();
84
152k
  if (!RepString.empty()) {
85
0
    assert(false && "Unexpected characters found in replacement string!");
86
0
  }
87
152k
88
152k
  return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
89
152k
}
90
91
std::pair<ReplacementItem, StringRef>
92
278k
formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
93
278k
  std::size_t From = 0;
94
278k
  while (From < Fmt.size() && From != StringRef::npos) {
95
278k
    std::size_t BO = Fmt.find_first_of('{', From);
96
278k
    // Everything up until the first brace is a literal.
97
278k
    if (BO != 0)
98
125k
      return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
99
152k
100
152k
    StringRef Braces =
101
305k
        Fmt.drop_front(BO).take_while([](char C) { return C == '{'; });
102
152k
    // If there is more than one brace, then some of them are escaped.  Treat
103
152k
    // these as replacements.
104
152k
    if (Braces.size() > 1) {
105
107
      size_t NumEscapedBraces = Braces.size() / 2;
106
107
      StringRef Middle = Fmt.substr(BO, NumEscapedBraces);
107
107
      StringRef Right = Fmt.drop_front(BO + NumEscapedBraces * 2);
108
107
      return std::make_pair(ReplacementItem{Middle}, Right);
109
107
    }
110
152k
    // An unterminated open brace is undefined.  We treat the rest of the string
111
152k
    // as a literal replacement, but we assert to indicate that this is
112
152k
    // undefined and that we consider it an error.
113
152k
    std::size_t BC = Fmt.find_first_of('}', BO);
114
152k
    if (BC == StringRef::npos) {
115
0
      assert(
116
0
          false &&
117
0
          "Unterminated brace sequence.  Escape with {{ for a literal brace.");
118
0
      return std::make_pair(ReplacementItem{Fmt}, StringRef());
119
0
    }
120
152k
121
152k
    // Even if there is a closing brace, if there is another open brace before
122
152k
    // this closing brace, treat this portion as literal, and try again with the
123
152k
    // next one.
124
152k
    std::size_t BO2 = Fmt.find_first_of('{', BO + 1);
125
152k
    if (BO2 < BC)
126
3
      return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
127
3
                            Fmt.substr(BO2));
128
152k
129
152k
    StringRef Spec = Fmt.slice(BO + 1, BC);
130
152k
    StringRef Right = Fmt.substr(BC + 1);
131
152k
132
152k
    auto RI = parseReplacementItem(Spec);
133
152k
    if (RI.hasValue())
134
152k
      return std::make_pair(*RI, Right);
135
0
136
0
    // If there was an error parsing the replacement item, treat it as an
137
0
    // invalid replacement spec, and just continue.
138
0
    From = BC + 1;
139
0
  }
140
278k
  
return std::make_pair(ReplacementItem{Fmt}, StringRef())0
;
141
278k
}
142
143
std::vector<ReplacementItem>
144
113k
formatv_object_base::parseFormatString(StringRef Fmt) {
145
113k
  std::vector<ReplacementItem> Replacements;
146
113k
  ReplacementItem I;
147
391k
  while (!Fmt.empty()) {
148
278k
    std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
149
278k
    if (I.Type != ReplacementType::Empty)
150
278k
      Replacements.push_back(I);
151
278k
  }
152
113k
  return Replacements;
153
113k
}
154
155
0
void detail::format_adapter::anchor() { }