Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/ASTMatchers/Dynamic/Diagnostics.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Diagnostics.cpp - Helper class for error diagnostics ---*- 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
9
#include "clang/ASTMatchers/Dynamic/Diagnostics.h"
10
11
namespace clang {
12
namespace ast_matchers {
13
namespace dynamic {
14
Diagnostics::ArgStream Diagnostics::pushContextFrame(ContextType Type,
15
124
                                                     SourceRange Range) {
16
124
  ContextStack.emplace_back();
17
124
  ContextFrame& data = ContextStack.back();
18
124
  data.Type = Type;
19
124
  data.Range = Range;
20
124
  return ArgStream(&data.Args);
21
124
}
22
23
Diagnostics::Context::Context(ConstructMatcherEnum, Diagnostics *Error,
24
                              StringRef MatcherName,
25
                              SourceRange MatcherRange)
26
53
    : Error(Error) {
27
53
  Error->pushContextFrame(CT_MatcherConstruct, MatcherRange) << MatcherName;
28
53
}
29
30
Diagnostics::Context::Context(MatcherArgEnum, Diagnostics *Error,
31
                              StringRef MatcherName,
32
                              SourceRange MatcherRange,
33
                              unsigned ArgNumber)
34
71
    : Error(Error) {
35
71
  Error->pushContextFrame(CT_MatcherArg, MatcherRange) << ArgNumber
36
71
                                                       << MatcherName;
37
71
}
38
39
124
Diagnostics::Context::~Context() { Error->ContextStack.pop_back(); }
40
41
Diagnostics::OverloadContext::OverloadContext(Diagnostics *Error)
42
14
    : Error(Error), BeginIndex(Error->Errors.size()) {}
43
44
14
Diagnostics::OverloadContext::~OverloadContext() {
45
  // Merge all errors that happened while in this context.
46
14
  if (BeginIndex < Error->Errors.size()) {
47
1
    Diagnostics::ErrorContent &Dest = Error->Errors[BeginIndex];
48
2
    for (size_t i = BeginIndex + 1, e = Error->Errors.size(); i < e; 
++i1
) {
49
1
      Dest.Messages.push_back(Error->Errors[i].Messages[0]);
50
1
    }
51
1
    Error->Errors.resize(BeginIndex + 1);
52
1
  }
53
14
}
54
55
13
void Diagnostics::OverloadContext::revertErrors() {
56
  // Revert the errors.
57
13
  Error->Errors.resize(BeginIndex);
58
13
}
59
60
285
Diagnostics::ArgStream &Diagnostics::ArgStream::operator<<(const Twine &Arg) {
61
285
  Out->push_back(Arg.str());
62
285
  return *this;
63
285
}
64
65
Diagnostics::ArgStream Diagnostics::addError(SourceRange Range,
66
54
                                             ErrorType Error) {
67
54
  Errors.emplace_back();
68
54
  ErrorContent &Last = Errors.back();
69
54
  Last.ContextStack = ContextStack;
70
54
  Last.Messages.emplace_back();
71
54
  Last.Messages.back().Range = Range;
72
54
  Last.Messages.back().Type = Error;
73
54
  return ArgStream(&Last.Messages.back().Args);
74
54
}
75
76
20
static StringRef contextTypeToFormatString(Diagnostics::ContextType Type) {
77
20
  switch (Type) {
78
9
    case Diagnostics::CT_MatcherConstruct:
79
9
      return "Error building matcher $0.";
80
11
    case Diagnostics::CT_MatcherArg:
81
11
      return "Error parsing argument $0 for matcher $1.";
82
0
  }
83
0
  llvm_unreachable("Unknown ContextType value.");
84
0
}
85
86
43
static StringRef errorTypeToFormatString(Diagnostics::ErrorType Type) {
87
43
  switch (Type) {
88
4
  case Diagnostics::ET_RegistryMatcherNotFound:
89
4
    return "Matcher not found: $0";
90
4
  case Diagnostics::ET_RegistryWrongArgCount:
91
4
    return "Incorrect argument count. (Expected = $0) != (Actual = $1)";
92
7
  case Diagnostics::ET_RegistryWrongArgType:
93
7
    return "Incorrect type for arg $0. (Expected = $1) != (Actual = $2)";
94
1
  case Diagnostics::ET_RegistryNotBindable:
95
1
    return "Matcher does not support binding.";
96
0
  case Diagnostics::ET_RegistryAmbiguousOverload:
97
    // TODO: Add type info about the overload error.
98
0
    return "Ambiguous matcher overload.";
99
3
  case Diagnostics::ET_RegistryValueNotFound:
100
3
    return "Value not found: $0";
101
4
  case Diagnostics::ET_RegistryUnknownEnumWithReplace:
102
4
    return "Unknown value '$1' for arg $0; did you mean '$2'";
103
104
1
  case Diagnostics::ET_ParserStringError:
105
1
    return "Error parsing string token: <$0>";
106
2
  case Diagnostics::ET_ParserNoOpenParen:
107
2
    return "Error parsing matcher. Found token <$0> while looking for '('.";
108
2
  case Diagnostics::ET_ParserNoCloseParen:
109
2
    return "Error parsing matcher. Found end-of-code while looking for ')'.";
110
1
  case Diagnostics::ET_ParserNoComma:
111
1
    return "Error parsing matcher. Found token <$0> while looking for ','.";
112
1
  case Diagnostics::ET_ParserNoCode:
113
1
    return "End of code found while looking for token.";
114
1
  case Diagnostics::ET_ParserNotAMatcher:
115
1
    return "Input value is not a matcher expression.";
116
1
  case Diagnostics::ET_ParserInvalidToken:
117
1
    return "Invalid token <$0> found when looking for a value.";
118
6
  case Diagnostics::ET_ParserMalformedBindExpr:
119
6
    return "Malformed bind() expression.";
120
1
  case Diagnostics::ET_ParserTrailingCode:
121
1
    return "Expected end of code.";
122
3
  case Diagnostics::ET_ParserNumberError:
123
3
    return "Error parsing numeric literal: <$0>";
124
1
  case Diagnostics::ET_ParserOverloadedType:
125
1
    return "Input value has unresolved overloaded type: $0";
126
127
0
  case Diagnostics::ET_None:
128
0
    return "<N/A>";
129
0
  }
130
0
  llvm_unreachable("Unknown ErrorType value.");
131
0
}
132
133
static void formatErrorString(StringRef FormatString,
134
                              ArrayRef<std::string> Args,
135
63
                              llvm::raw_ostream &OS) {
136
151
  while (!FormatString.empty()) {
137
143
    std::pair<StringRef, StringRef> Pieces = FormatString.split("$");
138
143
    OS << Pieces.first.str();
139
143
    if (Pieces.second.empty()) 
break55
;
140
141
88
    const char Next = Pieces.second.front();
142
88
    FormatString = Pieces.second.drop_front();
143
88
    if (Next >= '0' && Next <= '9') {
144
88
      const unsigned Index = Next - '0';
145
88
      if (Index < Args.size()) {
146
88
        OS << Args[Index];
147
0
      } else {
148
0
        OS << "<Argument_Not_Provided>";
149
0
      }
150
88
    }
151
88
  }
152
63
}
153
154
static void maybeAddLineAndColumn(SourceRange Range,
155
63
                                  llvm::raw_ostream &OS) {
156
63
  if (Range.Start.Line > 0 && 
Range.Start.Column > 053
) {
157
53
    OS << Range.Start.Line << ":" << Range.Start.Column << ": ";
158
53
  }
159
63
}
160
161
static void printContextFrameToStream(const Diagnostics::ContextFrame &Frame,
162
20
                                      llvm::raw_ostream &OS) {
163
20
  maybeAddLineAndColumn(Frame.Range, OS);
164
20
  formatErrorString(contextTypeToFormatString(Frame.Type), Frame.Args, OS);
165
20
}
166
167
static void
168
printMessageToStream(const Diagnostics::ErrorContent::Message &Message,
169
43
                     const Twine Prefix, llvm::raw_ostream &OS) {
170
43
  maybeAddLineAndColumn(Message.Range, OS);
171
43
  OS << Prefix;
172
43
  formatErrorString(errorTypeToFormatString(Message.Type), Message.Args, OS);
173
43
}
174
175
static void printErrorContentToStream(const Diagnostics::ErrorContent &Content,
176
42
                                      llvm::raw_ostream &OS) {
177
42
  if (Content.Messages.size() == 1) {
178
41
    printMessageToStream(Content.Messages[0], "", OS);
179
1
  } else {
180
3
    for (size_t i = 0, e = Content.Messages.size(); i != e; 
++i2
) {
181
2
      if (i != 0) 
OS << "\n"1
;
182
2
      printMessageToStream(Content.Messages[i],
183
2
                           "Candidate " + Twine(i + 1) + ": ", OS);
184
2
    }
185
1
  }
186
42
}
187
188
8
void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
189
16
  for (size_t i = 0, e = Errors.size(); i != e; 
++i8
) {
190
8
    if (i != 0) 
OS << "\n"0
;
191
8
    printErrorContentToStream(Errors[i], OS);
192
8
  }
193
8
}
194
195
8
std::string Diagnostics::toString() const {
196
8
  std::string S;
197
8
  llvm::raw_string_ostream OS(S);
198
8
  printToStream(OS);
199
8
  return OS.str();
200
8
}
201
202
160
void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
203
194
  for (size_t i = 0, e = Errors.size(); i != e; 
++i34
) {
204
34
    if (i != 0) 
OS << "\n"3
;
205
34
    const ErrorContent &Error = Errors[i];
206
54
    for (size_t i = 0, e = Error.ContextStack.size(); i != e; 
++i20
) {
207
20
      printContextFrameToStream(Error.ContextStack[i], OS);
208
20
      OS << "\n";
209
20
    }
210
34
    printErrorContentToStream(Error, OS);
211
34
  }
212
160
}
213
214
160
std::string Diagnostics::toStringFull() const {
215
160
  std::string S;
216
160
  llvm::raw_string_ostream OS(S);
217
160
  printToStreamFull(OS);
218
160
  return OS.str();
219
160
}
220
221
}  // namespace dynamic
222
}  // namespace ast_matchers
223
}  // namespace clang