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