clang  16.0.0git
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";
102  return "Unknown value '$1' for arg $0; did you mean '$2'";
104  return "Matcher not a node matcher: $0";
106  return "Matcher does not support with call.";
107 
109  return "Error parsing string token: <$0>";
111  return "Error parsing matcher. Found token <$0> while looking for '('.";
113  return "Error parsing matcher. Found end-of-code while looking for ')'.";
115  return "Error parsing matcher. Found token <$0> while looking for ','.";
117  return "End of code found while looking for token.";
119  return "Input value is not a matcher expression.";
121  return "Invalid token <$0> found when looking for a value.";
123  return "Malformed bind() expression.";
125  return "Expected end of code.";
127  return "Error parsing numeric literal: <$0>";
129  return "Input value has unresolved overloaded type: $0";
131  return "Period not followed by valid chained call.";
133  return "Failed to build matcher: $0.";
134 
136  return "<N/A>";
137  }
138  llvm_unreachable("Unknown ErrorType value.");
139 }
140 
141 static void formatErrorString(StringRef FormatString,
143  llvm::raw_ostream &OS) {
144  while (!FormatString.empty()) {
145  std::pair<StringRef, StringRef> Pieces = FormatString.split("$");
146  OS << Pieces.first.str();
147  if (Pieces.second.empty()) break;
148 
149  const char Next = Pieces.second.front();
150  FormatString = Pieces.second.drop_front();
151  if (Next >= '0' && Next <= '9') {
152  const unsigned Index = Next - '0';
153  if (Index < Args.size()) {
154  OS << Args[Index];
155  } else {
156  OS << "<Argument_Not_Provided>";
157  }
158  }
159  }
160 }
161 
163  llvm::raw_ostream &OS) {
164  if (Range.Start.Line > 0 && Range.Start.Column > 0) {
165  OS << Range.Start.Line << ":" << Range.Start.Column << ": ";
166  }
167 }
168 
170  llvm::raw_ostream &OS) {
171  maybeAddLineAndColumn(Frame.Range, OS);
173 }
174 
175 static void
177  const Twine Prefix, llvm::raw_ostream &OS) {
178  maybeAddLineAndColumn(Message.Range, OS);
179  OS << Prefix;
180  formatErrorString(errorTypeToFormatString(Message.Type), Message.Args, OS);
181 }
182 
184  llvm::raw_ostream &OS) {
185  if (Content.Messages.size() == 1) {
186  printMessageToStream(Content.Messages[0], "", OS);
187  } else {
188  for (size_t i = 0, e = Content.Messages.size(); i != e; ++i) {
189  if (i != 0) OS << "\n";
190  printMessageToStream(Content.Messages[i],
191  "Candidate " + Twine(i + 1) + ": ", OS);
192  }
193  }
194 }
195 
196 void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
197  for (size_t i = 0, e = Errors.size(); i != e; ++i) {
198  if (i != 0) OS << "\n";
199  printErrorContentToStream(Errors[i], OS);
200  }
201 }
202 
204  std::string S;
205  llvm::raw_string_ostream OS(S);
206  printToStream(OS);
207  return S;
208 }
209 
210 void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
211  for (size_t i = 0, e = Errors.size(); i != e; ++i) {
212  if (i != 0) OS << "\n";
213  const ErrorContent &Error = Errors[i];
214  for (size_t i = 0, e = Error.ContextStack.size(); i != e; ++i) {
215  printContextFrameToStream(Error.ContextStack[i], OS);
216  OS << "\n";
217  }
219  }
220 }
221 
223  std::string S;
224  llvm::raw_string_ostream OS(S);
225  printToStreamFull(OS);
226  return S;
227 }
228 
229 } // namespace dynamic
230 } // namespace ast_matchers
231 } // namespace clang
clang::ast_matchers::dynamic::contextTypeToFormatString
static StringRef contextTypeToFormatString(Diagnostics::ContextType Type)
Definition: Diagnostics.cpp:76
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryMatcherNotFound
@ ET_RegistryMatcherNotFound
Definition: Diagnostics.h:62
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ast_matchers::dynamic::Diagnostics::CT_MatcherArg
@ CT_MatcherArg
Definition: Diagnostics.h:54
clang::ast_matchers::dynamic::Diagnostics::Context::~Context
~Context()
Definition: Diagnostics.cpp:39
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::OverloadContext
OverloadContext(Diagnostics *Error)
Definition: Diagnostics.cpp:41
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::revertErrors
void revertErrors()
Revert all errors that happened within this context.
Definition: Diagnostics.cpp:55
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNotAMatcher
@ ET_ParserNotAMatcher
Definition: Diagnostics.h:77
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::~OverloadContext
~OverloadContext()
Definition: Diagnostics.cpp:44
clang::ast_matchers::dynamic::maybeAddLineAndColumn
static void maybeAddLineAndColumn(SourceRange Range, llvm::raw_ostream &OS)
Definition: Diagnostics.cpp:162
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryMatcherNoWithSupport
@ ET_RegistryMatcherNoWithSupport
Definition: Diagnostics.h:70
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryValueNotFound
@ ET_RegistryValueNotFound
Definition: Diagnostics.h:67
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1565
clang::ast_matchers::dynamic::Diagnostics::ET_ParserMalformedChainedExpr
@ ET_ParserMalformedChainedExpr
Definition: Diagnostics.h:83
clang::ast_matchers::dynamic::Diagnostics::ErrorType
ErrorType
All errors from the system.
Definition: Diagnostics.h:59
clang::ast_matchers::dynamic::Diagnostics::toStringFull
std::string toStringFull() const
Definition: Diagnostics.cpp:222
clang::ast_matchers::dynamic::printContextFrameToStream
static void printContextFrameToStream(const Diagnostics::ContextFrame &Frame, llvm::raw_ostream &OS)
Definition: Diagnostics.cpp:169
clang::ast_matchers::dynamic::Diagnostics::ArgStream
Helper stream class.
Definition: Diagnostics.h:88
clang::ast_matchers::dynamic::Diagnostics::toString
std::string toString() const
Definition: Diagnostics.cpp:203
ContextType
enum clang::format::@1192::AnnotatingParser::Context::@327 ContextType
clang::ast_matchers::dynamic::printMessageToStream
static void printMessageToStream(const Diagnostics::ErrorContent::Message &Message, const Twine Prefix, llvm::raw_ostream &OS)
Definition: Diagnostics.cpp:176
clang::ast_matchers::dynamic::Diagnostics::ArgStream::operator<<
ArgStream & operator<<(const T &Arg)
Definition: Diagnostics.h:91
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoComma
@ ET_ParserNoComma
Definition: Diagnostics.h:75
clang::ast_matchers::dynamic::Diagnostics::ContextType
ContextType
Parser context types.
Definition: Diagnostics.h:53
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoCode
@ ET_ParserNoCode
Definition: Diagnostics.h:76
clang::ast_matchers::dynamic::Diagnostics::Context::ConstructMatcherEnum
ConstructMatcherEnum
About to call the constructor for a matcher.
Definition: Diagnostics.h:109
clang::ast_matchers::dynamic::Diagnostics::ET_ParserStringError
@ ET_ParserStringError
Definition: Diagnostics.h:72
clang::ast_matchers::dynamic::Diagnostics::ET_ParserTrailingCode
@ ET_ParserTrailingCode
Definition: Diagnostics.h:80
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryNonNodeMatcher
@ ET_RegistryNonNodeMatcher
Definition: Diagnostics.h:69
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Messages
std::vector< Message > Messages
Definition: Diagnostics.h:161
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Range
SourceRange Range
Definition: Diagnostics.h:149
clang::ast_matchers::dynamic::Diagnostics::Context::Context
Context(ConstructMatcherEnum, Diagnostics *Error, StringRef MatcherName, SourceRange MatcherRange)
Definition: Diagnostics.cpp:23
clang::ast_matchers::dynamic::formatErrorString
static void formatErrorString(StringRef FormatString, ArrayRef< std::string > Args, llvm::raw_ostream &OS)
Definition: Diagnostics.cpp:141
Diagnostics.h
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Type
ContextType Type
Definition: Diagnostics.h:148
clang::transformer::EditKind::Range
@ Range
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryWrongArgCount
@ ET_RegistryWrongArgCount
Definition: Diagnostics.h:63
clang::ast_matchers::dynamic::Diagnostics::Context::MatcherArgEnum
MatcherArgEnum
About to recurse into parsing one argument for a matcher.
Definition: Diagnostics.h:113
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Args
std::vector< std::string > Args
Definition: Diagnostics.h:150
clang::ast_matchers::dynamic::errorTypeToFormatString
static StringRef errorTypeToFormatString(Diagnostics::ErrorType Type)
Definition: Diagnostics.cpp:86
llvm::ArrayRef< std::string >
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryUnknownEnumWithReplace
@ ET_RegistryUnknownEnumWithReplace
Definition: Diagnostics.h:68
clang::ast_matchers::dynamic::SourceRange
Definition: Diagnostics.h:36
clang::ast_matchers::dynamic::Diagnostics::ET_ParserFailedToBuildMatcher
@ ET_ParserFailedToBuildMatcher
Definition: Diagnostics.h:84
clang::ast_matchers::dynamic::Diagnostics::ET_None
@ ET_None
Definition: Diagnostics.h:60
clang::ast_matchers::dynamic::Diagnostics::printToStreamFull
void printToStreamFull(llvm::raw_ostream &OS) const
Returns the full string representation of each error.
Definition: Diagnostics.cpp:210
clang::ast_matchers::dynamic::Diagnostics
Helper class to manage error messages.
Definition: Diagnostics.h:50
clang::ast_matchers::dynamic::Diagnostics::ET_ParserInvalidToken
@ ET_ParserInvalidToken
Definition: Diagnostics.h:78
clang
Definition: CalledOnceCheck.h:17
clang::ast_matchers::dynamic::Diagnostics::ContextFrame
Information stored for one frame of the context.
Definition: Diagnostics.h:147
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryWrongArgType
@ ET_RegistryWrongArgType
Definition: Diagnostics.h:64
clang::ast_matchers::dynamic::printErrorContentToStream
static void printErrorContentToStream(const Diagnostics::ErrorContent &Content, llvm::raw_ostream &OS)
Definition: Diagnostics.cpp:183
clang::ComparisonCategoryType::Last
@ Last
clang::ast_matchers::dynamic::Diagnostics::addError
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
Definition: Diagnostics.cpp:65
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoCloseParen
@ ET_ParserNoCloseParen
Definition: Diagnostics.h:74
clang::ast_matchers::dynamic::Diagnostics::ErrorContent
Information stored for each error found.
Definition: Diagnostics.h:154
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryNotBindable
@ ET_RegistryNotBindable
Definition: Diagnostics.h:65
clang::ast_matchers::dynamic::Diagnostics::ET_ParserOverloadedType
@ ET_ParserOverloadedType
Definition: Diagnostics.h:82
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNumberError
@ ET_ParserNumberError
Definition: Diagnostics.h:81
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoOpenParen
@ ET_ParserNoOpenParen
Definition: Diagnostics.h:73
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryAmbiguousOverload
@ ET_RegistryAmbiguousOverload
Definition: Diagnostics.h:66
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Message
Definition: Diagnostics.h:156
clang::ast_matchers::dynamic::Diagnostics::ET_ParserMalformedBindExpr
@ ET_ParserMalformedBindExpr
Definition: Diagnostics.h:79
clang::ast_matchers::dynamic::Diagnostics::printToStream
void printToStream(llvm::raw_ostream &OS) const
Returns a simple string representation of each error.
Definition: Diagnostics.cpp:196
clang::ast_matchers::dynamic::Diagnostics::CT_MatcherConstruct
@ CT_MatcherConstruct
Definition: Diagnostics.h:55
Type
MatchType Type
Definition: ASTMatchFinder.cpp:71