Coverage Report

Created: 2021-08-24 07:12

/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
130
                                                     SourceRange Range) {
16
130
  ContextStack.emplace_back();
17
130
  ContextFrame& data = ContextStack.back();
18
130
  data.Type = Type;
19
130
  data.Range = Range;
20
130
  return ArgStream(&data.Args);
21
130
}
22
23
Diagnostics::Context::Context(ConstructMatcherEnum, Diagnostics *Error,
24
                              StringRef MatcherName,
25
                              SourceRange MatcherRange)
26
54
    : Error(Error) {
27
54
  Error->pushContextFrame(CT_MatcherConstruct, MatcherRange) << MatcherName;
28
54
}
29
30
Diagnostics::Context::Context(MatcherArgEnum, Diagnostics *Error,
31
                              StringRef MatcherName,
32
                              SourceRange MatcherRange,
33
                              unsigned ArgNumber)
34
76
    : Error(Error) {
35
76
  Error->pushContextFrame(CT_MatcherArg, MatcherRange) << ArgNumber
36
76
                                                       << MatcherName;
37
76
}
38
39
130
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
299
Diagnostics::ArgStream &Diagnostics::ArgStream::operator<<(const Twine &Arg) {
61
299
  Out->push_back(Arg.str());
62
299
  return *this;
63
299
}
64
65
Diagnostics::ArgStream Diagnostics::addError(SourceRange Range,
66
59
                                             ErrorType Error) {
67
59
  Errors.emplace_back();
68
59
  ErrorContent &Last = Errors.back();
69
59
  Last.ContextStack = ContextStack;
70
59
  Last.Messages.emplace_back();
71
59
  Last.Messages.back().Range = Range;
72
59
  Last.Messages.back().Type = Error;
73
59
  return ArgStream(&Last.Messages.back().Args);
74
59
}
75
76
22
static StringRef contextTypeToFormatString(Diagnostics::ContextType Type) {
77
22
  switch (Type) {
78
10
    case Diagnostics::CT_MatcherConstruct:
79
10
      return "Error building matcher $0.";
80
12
    case Diagnostics::CT_MatcherArg:
81
12
      return "Error parsing argument $0 for matcher $1.";
82
22
  }
83
0
  llvm_unreachable("Unknown ContextType value.");
84
0
}
85
86
48
static StringRef errorTypeToFormatString(Diagnostics::ErrorType Type) {
87
48
  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
0
  case Diagnostics::ET_RegistryNonNodeMatcher:
104
0
    return "Matcher not a node matcher: $0";
105
1
  case Diagnostics::ET_RegistryMatcherNoWithSupport:
106
1
    return "Matcher does not support with call.";
107
108
1
  case Diagnostics::ET_ParserStringError:
109
1
    return "Error parsing string token: <$0>";
110
3
  case Diagnostics::ET_ParserNoOpenParen:
111
3
    return "Error parsing matcher. Found token <$0> while looking for '('.";
112
3
  case Diagnostics::ET_ParserNoCloseParen:
113
3
    return "Error parsing matcher. Found end-of-code while looking for ')'.";
114
1
  case Diagnostics::ET_ParserNoComma:
115
1
    return "Error parsing matcher. Found token <$0> while looking for ','.";
116
1
  case Diagnostics::ET_ParserNoCode:
117
1
    return "End of code found while looking for token.";
118
1
  case Diagnostics::ET_ParserNotAMatcher:
119
1
    return "Input value is not a matcher expression.";
120
1
  case Diagnostics::ET_ParserInvalidToken:
121
1
    return "Invalid token <$0> found when looking for a value.";
122
4
  case Diagnostics::ET_ParserMalformedBindExpr:
123
4
    return "Malformed bind() expression.";
124
1
  case Diagnostics::ET_ParserTrailingCode:
125
1
    return "Expected end of code.";
126
3
  case Diagnostics::ET_ParserNumberError:
127
3
    return "Error parsing numeric literal: <$0>";
128
1
  case Diagnostics::ET_ParserOverloadedType:
129
1
    return "Input value has unresolved overloaded type: $0";
130
2
  case Diagnostics::ET_ParserMalformedChainedExpr:
131
2
    return "Period not followed by valid chained call.";
132
2
  case Diagnostics::ET_ParserFailedToBuildMatcher:
133
2
    return "Failed to build matcher: $0.";
134
135
0
  case Diagnostics::ET_None:
136
0
    return "<N/A>";
137
48
  }
138
0
  llvm_unreachable("Unknown ErrorType value.");
139
0
}
140
141
static void formatErrorString(StringRef FormatString,
142
                              ArrayRef<std::string> Args,
143
70
                              llvm::raw_ostream &OS) {
144
164
  while (!FormatString.empty()) {
145
156
    std::pair<StringRef, StringRef> Pieces = FormatString.split("$");
146
156
    OS << Pieces.first.str();
147
156
    if (Pieces.second.empty()) 
break62
;
148
149
94
    const char Next = Pieces.second.front();
150
94
    FormatString = Pieces.second.drop_front();
151
94
    if (Next >= '0' && Next <= '9') {
152
94
      const unsigned Index = Next - '0';
153
94
      if (Index < Args.size()) {
154
94
        OS << Args[Index];
155
94
      } else {
156
0
        OS << "<Argument_Not_Provided>";
157
0
      }
158
94
    }
159
94
  }
160
70
}
161
162
static void maybeAddLineAndColumn(SourceRange Range,
163
70
                                  llvm::raw_ostream &OS) {
164
70
  if (Range.Start.Line > 0 && 
Range.Start.Column > 060
) {
165
60
    OS << Range.Start.Line << ":" << Range.Start.Column << ": ";
166
60
  }
167
70
}
168
169
static void printContextFrameToStream(const Diagnostics::ContextFrame &Frame,
170
22
                                      llvm::raw_ostream &OS) {
171
22
  maybeAddLineAndColumn(Frame.Range, OS);
172
22
  formatErrorString(contextTypeToFormatString(Frame.Type), Frame.Args, OS);
173
22
}
174
175
static void
176
printMessageToStream(const Diagnostics::ErrorContent::Message &Message,
177
48
                     const Twine Prefix, llvm::raw_ostream &OS) {
178
48
  maybeAddLineAndColumn(Message.Range, OS);
179
48
  OS << Prefix;
180
48
  formatErrorString(errorTypeToFormatString(Message.Type), Message.Args, OS);
181
48
}
182
183
static void printErrorContentToStream(const Diagnostics::ErrorContent &Content,
184
47
                                      llvm::raw_ostream &OS) {
185
47
  if (Content.Messages.size() == 1) {
186
46
    printMessageToStream(Content.Messages[0], "", OS);
187
46
  } else {
188
3
    for (size_t i = 0, e = Content.Messages.size(); i != e; 
++i2
) {
189
2
      if (i != 0) 
OS << "\n"1
;
190
2
      printMessageToStream(Content.Messages[i],
191
2
                           "Candidate " + Twine(i + 1) + ": ", OS);
192
2
    }
193
1
  }
194
47
}
195
196
8
void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
197
16
  for (size_t i = 0, e = Errors.size(); i != e; 
++i8
) {
198
8
    if (i != 0) 
OS << "\n"0
;
199
8
    printErrorContentToStream(Errors[i], OS);
200
8
  }
201
8
}
202
203
8
std::string Diagnostics::toString() const {
204
8
  std::string S;
205
8
  llvm::raw_string_ostream OS(S);
206
8
  printToStream(OS);
207
8
  return OS.str();
208
8
}
209
210
168
void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
211
207
  for (size_t i = 0, e = Errors.size(); i != e; 
++i39
) {
212
39
    if (i != 0) 
OS << "\n"3
;
213
39
    const ErrorContent &Error = Errors[i];
214
61
    for (size_t i = 0, e = Error.ContextStack.size(); i != e; 
++i22
) {
215
22
      printContextFrameToStream(Error.ContextStack[i], OS);
216
22
      OS << "\n";
217
22
    }
218
39
    printErrorContentToStream(Error, OS);
219
39
  }
220
168
}
221
222
168
std::string Diagnostics::toStringFull() const {
223
168
  std::string S;
224
168
  llvm::raw_string_ostream OS(S);
225
168
  printToStreamFull(OS);
226
168
  return OS.str();
227
168
}
228
229
}  // namespace dynamic
230
}  // namespace ast_matchers
231
}  // namespace clang