clang  10.0.0svn
Diagnostics.cpp
Go to the documentation of this file.
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 
10 
11 namespace clang {
12 namespace ast_matchers {
13 namespace dynamic {
14 Diagnostics::ArgStream Diagnostics::pushContextFrame(ContextType Type,
15  SourceRange Range) {
16  ContextStack.emplace_back();
17  ContextFrame& data = ContextStack.back();
18  data.Type = Type;
19  data.Range = Range;
20  return ArgStream(&data.Args);
21 }
22 
24  StringRef MatcherName,
25  SourceRange MatcherRange)
26  : Error(Error) {
27  Error->pushContextFrame(CT_MatcherConstruct, MatcherRange) << MatcherName;
28 }
29 
31  StringRef MatcherName,
32  SourceRange MatcherRange,
33  unsigned ArgNumber)
34  : Error(Error) {
35  Error->pushContextFrame(CT_MatcherArg, MatcherRange) << ArgNumber
36  << MatcherName;
37 }
38 
39 Diagnostics::Context::~Context() { Error->ContextStack.pop_back(); }
40 
42  : Error(Error), BeginIndex(Error->Errors.size()) {}
43 
45  // Merge all errors that happened while in this context.
46  if (BeginIndex < Error->Errors.size()) {
47  Diagnostics::ErrorContent &Dest = Error->Errors[BeginIndex];
48  for (size_t i = BeginIndex + 1, e = Error->Errors.size(); i < e; ++i) {
49  Dest.Messages.push_back(Error->Errors[i].Messages[0]);
50  }
51  Error->Errors.resize(BeginIndex + 1);
52  }
53 }
54 
56  // Revert the errors.
57  Error->Errors.resize(BeginIndex);
58 }
59 
61  Out->push_back(Arg.str());
62  return *this;
63 }
64 
66  ErrorType Error) {
67  Errors.emplace_back();
68  ErrorContent &Last = Errors.back();
69  Last.ContextStack = ContextStack;
70  Last.Messages.emplace_back();
71  Last.Messages.back().Range = Range;
72  Last.Messages.back().Type = Error;
73  return ArgStream(&Last.Messages.back().Args);
74 }
75 
77  switch (Type) {
79  return "Error building matcher $0.";
81  return "Error parsing argument $0 for matcher $1.";
82  }
83  llvm_unreachable("Unknown ContextType value.");
84 }
85 
87  switch (Type) {
89  return "Matcher not found: $0";
91  return "Incorrect argument count. (Expected = $0) != (Actual = $1)";
93  return "Incorrect type for arg $0. (Expected = $1) != (Actual = $2)";
95  return "Matcher does not support binding.";
97  // TODO: Add type info about the overload error.
98  return "Ambiguous matcher overload.";
100  return "Value not found: $0";
101 
103  return "Error parsing string token: <$0>";
105  return "Error parsing matcher. Found token <$0> while looking for '('.";
107  return "Error parsing matcher. Found end-of-code while looking for ')'.";
109  return "Error parsing matcher. Found token <$0> while looking for ','.";
111  return "End of code found while looking for token.";
113  return "Input value is not a matcher expression.";
115  return "Invalid token <$0> found when looking for a value.";
117  return "Malformed bind() expression.";
119  return "Expected end of code.";
121  return "Error parsing numeric literal: <$0>";
123  return "Input value has unresolved overloaded type: $0";
124 
126  return "<N/A>";
127  }
128  llvm_unreachable("Unknown ErrorType value.");
129 }
130 
131 static void formatErrorString(StringRef FormatString,
133  llvm::raw_ostream &OS) {
134  while (!FormatString.empty()) {
135  std::pair<StringRef, StringRef> Pieces = FormatString.split("$");
136  OS << Pieces.first.str();
137  if (Pieces.second.empty()) break;
138 
139  const char Next = Pieces.second.front();
140  FormatString = Pieces.second.drop_front();
141  if (Next >= '0' && Next <= '9') {
142  const unsigned Index = Next - '0';
143  if (Index < Args.size()) {
144  OS << Args[Index];
145  } else {
146  OS << "<Argument_Not_Provided>";
147  }
148  }
149  }
150 }
151 
153  llvm::raw_ostream &OS) {
154  if (Range.Start.Line > 0 && Range.Start.Column > 0) {
155  OS << Range.Start.Line << ":" << Range.Start.Column << ": ";
156  }
157 }
158 
160  llvm::raw_ostream &OS) {
161  maybeAddLineAndColumn(Frame.Range, OS);
163 }
164 
165 static void
167  const Twine Prefix, llvm::raw_ostream &OS) {
168  maybeAddLineAndColumn(Message.Range, OS);
169  OS << Prefix;
170  formatErrorString(errorTypeToFormatString(Message.Type), Message.Args, OS);
171 }
172 
174  llvm::raw_ostream &OS) {
175  if (Content.Messages.size() == 1) {
176  printMessageToStream(Content.Messages[0], "", OS);
177  } else {
178  for (size_t i = 0, e = Content.Messages.size(); i != e; ++i) {
179  if (i != 0) OS << "\n";
180  printMessageToStream(Content.Messages[i],
181  "Candidate " + Twine(i + 1) + ": ", OS);
182  }
183  }
184 }
185 
186 void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
187  for (size_t i = 0, e = Errors.size(); i != e; ++i) {
188  if (i != 0) OS << "\n";
189  printErrorContentToStream(Errors[i], OS);
190  }
191 }
192 
193 std::string Diagnostics::toString() const {
194  std::string S;
195  llvm::raw_string_ostream OS(S);
196  printToStream(OS);
197  return OS.str();
198 }
199 
200 void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
201  for (size_t i = 0, e = Errors.size(); i != e; ++i) {
202  if (i != 0) OS << "\n";
203  const ErrorContent &Error = Errors[i];
204  for (size_t i = 0, e = Error.ContextStack.size(); i != e; ++i) {
206  OS << "\n";
207  }
208  printErrorContentToStream(Error, OS);
209  }
210 }
211 
212 std::string Diagnostics::toStringFull() const {
213  std::string S;
214  llvm::raw_string_ostream OS(S);
215  printToStreamFull(OS);
216  return OS.str();
217 }
218 
219 } // namespace dynamic
220 } // namespace ast_matchers
221 } // namespace clang
static void printErrorContentToStream(const Diagnostics::ErrorContent &Content, llvm::raw_ostream &OS)
static void maybeAddLineAndColumn(SourceRange Range, llvm::raw_ostream &OS)
static StringRef contextTypeToFormatString(Diagnostics::ContextType Type)
Definition: Diagnostics.cpp:76
MatcherArgEnum
About to recurse into parsing one argument for a matcher.
Definition: Diagnostics.h:108
Context(ConstructMatcherEnum, Diagnostics *Error, StringRef MatcherName, SourceRange MatcherRange)
Definition: Diagnostics.cpp:23
ConstructMatcherEnum
About to call the constructor for a matcher.
Definition: Diagnostics.h:104
void revertErrors()
Revert all errors that happened within this context.
Definition: Diagnostics.cpp:55
void printToStream(llvm::raw_ostream &OS) const
Returns a simple string representation of each error.
void printToStreamFull(llvm::raw_ostream &OS) const
Returns the full string representation of each error.
Diagnostics class to manage error messages.
Helper class to manage error messages.
Definition: Diagnostics.h:50
static void printContextFrameToStream(const Diagnostics::ContextFrame &Frame, llvm::raw_ostream &OS)
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
Definition: Diagnostics.cpp:65
static void formatErrorString(StringRef FormatString, ArrayRef< std::string > Args, llvm::raw_ostream &OS)
Dataflow Directional Tag Classes.
ErrorType
All errors from the system.
Definition: Diagnostics.h:59
Information stored for each error found.
Definition: Diagnostics.h:149
static void printMessageToStream(const Diagnostics::ErrorContent::Message &Message, const Twine Prefix, llvm::raw_ostream &OS)
static StringRef errorTypeToFormatString(Diagnostics::ErrorType Type)
Definition: Diagnostics.cpp:86
Information stored for one frame of the context.
Definition: Diagnostics.h:142