2013-05-14 17:13:00 +08:00
|
|
|
//===--- Diagnostics.cpp - Helper class for error diagnostics -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/Dynamic/Diagnostics.h"
|
|
|
|
|
2015-03-10 01:00:48 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
namespace dynamic {
|
2013-07-19 03:47:59 +08:00
|
|
|
Diagnostics::ArgStream Diagnostics::pushContextFrame(ContextType Type,
|
|
|
|
SourceRange Range) {
|
2015-05-30 03:42:19 +08:00
|
|
|
ContextStack.emplace_back();
|
2013-07-19 03:47:59 +08:00
|
|
|
ContextFrame& data = ContextStack.back();
|
|
|
|
data.Type = Type;
|
|
|
|
data.Range = Range;
|
|
|
|
return ArgStream(&data.Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Diagnostics::Context::Context(ConstructMatcherEnum, Diagnostics *Error,
|
|
|
|
StringRef MatcherName,
|
2015-10-04 12:53:55 +08:00
|
|
|
SourceRange MatcherRange)
|
2013-07-19 03:47:59 +08:00
|
|
|
: Error(Error) {
|
|
|
|
Error->pushContextFrame(CT_MatcherConstruct, MatcherRange) << MatcherName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diagnostics::Context::Context(MatcherArgEnum, Diagnostics *Error,
|
|
|
|
StringRef MatcherName,
|
2015-10-04 12:53:55 +08:00
|
|
|
SourceRange MatcherRange,
|
2013-07-19 03:47:59 +08:00
|
|
|
unsigned ArgNumber)
|
|
|
|
: Error(Error) {
|
|
|
|
Error->pushContextFrame(CT_MatcherArg, MatcherRange) << ArgNumber
|
|
|
|
<< MatcherName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diagnostics::Context::~Context() { Error->ContextStack.pop_back(); }
|
|
|
|
|
2013-07-23 00:13:57 +08:00
|
|
|
Diagnostics::OverloadContext::OverloadContext(Diagnostics *Error)
|
|
|
|
: Error(Error), BeginIndex(Error->Errors.size()) {}
|
|
|
|
|
|
|
|
Diagnostics::OverloadContext::~OverloadContext() {
|
|
|
|
// Merge all errors that happened while in this context.
|
|
|
|
if (BeginIndex < Error->Errors.size()) {
|
|
|
|
Diagnostics::ErrorContent &Dest = Error->Errors[BeginIndex];
|
|
|
|
for (size_t i = BeginIndex + 1, e = Error->Errors.size(); i < e; ++i) {
|
|
|
|
Dest.Messages.push_back(Error->Errors[i].Messages[0]);
|
|
|
|
}
|
|
|
|
Error->Errors.resize(BeginIndex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Diagnostics::OverloadContext::revertErrors() {
|
|
|
|
// Revert the errors.
|
|
|
|
Error->Errors.resize(BeginIndex);
|
|
|
|
}
|
|
|
|
|
2013-07-19 03:47:59 +08:00
|
|
|
Diagnostics::ArgStream &Diagnostics::ArgStream::operator<<(const Twine &Arg) {
|
2013-05-14 17:13:00 +08:00
|
|
|
Out->push_back(Arg.str());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-04 12:53:55 +08:00
|
|
|
Diagnostics::ArgStream Diagnostics::addError(SourceRange Range,
|
2013-07-19 03:47:59 +08:00
|
|
|
ErrorType Error) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Errors.emplace_back();
|
2013-07-19 03:47:59 +08:00
|
|
|
ErrorContent &Last = Errors.back();
|
|
|
|
Last.ContextStack = ContextStack;
|
2015-05-30 03:42:19 +08:00
|
|
|
Last.Messages.emplace_back();
|
2013-07-23 00:13:57 +08:00
|
|
|
Last.Messages.back().Range = Range;
|
|
|
|
Last.Messages.back().Type = Error;
|
|
|
|
return ArgStream(&Last.Messages.back().Args);
|
2013-07-19 03:47:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-10 00:47:52 +08:00
|
|
|
static StringRef contextTypeToFormatString(Diagnostics::ContextType Type) {
|
2013-07-19 03:47:59 +08:00
|
|
|
switch (Type) {
|
|
|
|
case Diagnostics::CT_MatcherConstruct:
|
|
|
|
return "Error building matcher $0.";
|
|
|
|
case Diagnostics::CT_MatcherArg:
|
|
|
|
return "Error parsing argument $0 for matcher $1.";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown ContextType value.");
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2015-03-10 00:47:52 +08:00
|
|
|
static StringRef errorTypeToFormatString(Diagnostics::ErrorType Type) {
|
2013-05-14 17:13:00 +08:00
|
|
|
switch (Type) {
|
2014-04-14 21:51:21 +08:00
|
|
|
case Diagnostics::ET_RegistryMatcherNotFound:
|
2013-05-14 17:13:00 +08:00
|
|
|
return "Matcher not found: $0";
|
|
|
|
case Diagnostics::ET_RegistryWrongArgCount:
|
|
|
|
return "Incorrect argument count. (Expected = $0) != (Actual = $1)";
|
|
|
|
case Diagnostics::ET_RegistryWrongArgType:
|
2013-06-20 22:28:32 +08:00
|
|
|
return "Incorrect type for arg $0. (Expected = $1) != (Actual = $2)";
|
2013-06-04 03:31:08 +08:00
|
|
|
case Diagnostics::ET_RegistryNotBindable:
|
|
|
|
return "Matcher does not support binding.";
|
2013-07-23 00:13:57 +08:00
|
|
|
case Diagnostics::ET_RegistryAmbiguousOverload:
|
|
|
|
// TODO: Add type info about the overload error.
|
|
|
|
return "Ambiguous matcher overload.";
|
2014-04-14 21:51:21 +08:00
|
|
|
case Diagnostics::ET_RegistryValueNotFound:
|
|
|
|
return "Value not found: $0";
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
case Diagnostics::ET_ParserStringError:
|
|
|
|
return "Error parsing string token: <$0>";
|
|
|
|
case Diagnostics::ET_ParserNoOpenParen:
|
|
|
|
return "Error parsing matcher. Found token <$0> while looking for '('.";
|
|
|
|
case Diagnostics::ET_ParserNoCloseParen:
|
|
|
|
return "Error parsing matcher. Found end-of-code while looking for ')'.";
|
|
|
|
case Diagnostics::ET_ParserNoComma:
|
|
|
|
return "Error parsing matcher. Found token <$0> while looking for ','.";
|
|
|
|
case Diagnostics::ET_ParserNoCode:
|
|
|
|
return "End of code found while looking for token.";
|
|
|
|
case Diagnostics::ET_ParserNotAMatcher:
|
|
|
|
return "Input value is not a matcher expression.";
|
|
|
|
case Diagnostics::ET_ParserInvalidToken:
|
|
|
|
return "Invalid token <$0> found when looking for a value.";
|
2013-06-04 03:31:08 +08:00
|
|
|
case Diagnostics::ET_ParserMalformedBindExpr:
|
|
|
|
return "Malformed bind() expression.";
|
|
|
|
case Diagnostics::ET_ParserTrailingCode:
|
|
|
|
return "Expected end of code.";
|
2013-06-04 23:46:22 +08:00
|
|
|
case Diagnostics::ET_ParserUnsignedError:
|
|
|
|
return "Error parsing unsigned token: <$0>";
|
2013-06-21 23:51:31 +08:00
|
|
|
case Diagnostics::ET_ParserOverloadedType:
|
|
|
|
return "Input value has unresolved overloaded type: $0";
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
case Diagnostics::ET_None:
|
|
|
|
return "<N/A>";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown ErrorType value.");
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:47:52 +08:00
|
|
|
static void formatErrorString(StringRef FormatString,
|
|
|
|
ArrayRef<std::string> Args,
|
|
|
|
llvm::raw_ostream &OS) {
|
2013-05-14 17:13:00 +08:00
|
|
|
while (!FormatString.empty()) {
|
|
|
|
std::pair<StringRef, StringRef> Pieces = FormatString.split("$");
|
2013-07-20 04:02:35 +08:00
|
|
|
OS << Pieces.first.str();
|
2013-05-14 17:13:00 +08:00
|
|
|
if (Pieces.second.empty()) break;
|
|
|
|
|
|
|
|
const char Next = Pieces.second.front();
|
|
|
|
FormatString = Pieces.second.drop_front();
|
|
|
|
if (Next >= '0' && Next <= '9') {
|
|
|
|
const unsigned Index = Next - '0';
|
|
|
|
if (Index < Args.size()) {
|
2013-07-20 04:02:35 +08:00
|
|
|
OS << Args[Index];
|
2013-05-14 17:13:00 +08:00
|
|
|
} else {
|
2013-07-20 04:02:35 +08:00
|
|
|
OS << "<Argument_Not_Provided>";
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 12:53:55 +08:00
|
|
|
static void maybeAddLineAndColumn(SourceRange Range,
|
2013-07-20 04:02:35 +08:00
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
if (Range.Start.Line > 0 && Range.Start.Column > 0) {
|
|
|
|
OS << Range.Start.Line << ":" << Range.Start.Column << ": ";
|
|
|
|
}
|
2013-07-19 03:47:59 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 04:02:35 +08:00
|
|
|
static void printContextFrameToStream(const Diagnostics::ContextFrame &Frame,
|
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
maybeAddLineAndColumn(Frame.Range, OS);
|
|
|
|
formatErrorString(contextTypeToFormatString(Frame.Type), Frame.Args, OS);
|
2013-07-19 03:47:59 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 00:13:57 +08:00
|
|
|
static void
|
|
|
|
printMessageToStream(const Diagnostics::ErrorContent::Message &Message,
|
|
|
|
const Twine Prefix, llvm::raw_ostream &OS) {
|
|
|
|
maybeAddLineAndColumn(Message.Range, OS);
|
|
|
|
OS << Prefix;
|
|
|
|
formatErrorString(errorTypeToFormatString(Message.Type), Message.Args, OS);
|
|
|
|
}
|
|
|
|
|
2013-07-20 04:02:35 +08:00
|
|
|
static void printErrorContentToStream(const Diagnostics::ErrorContent &Content,
|
|
|
|
llvm::raw_ostream &OS) {
|
2013-07-23 00:13:57 +08:00
|
|
|
if (Content.Messages.size() == 1) {
|
|
|
|
printMessageToStream(Content.Messages[0], "", OS);
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0, e = Content.Messages.size(); i != e; ++i) {
|
|
|
|
if (i != 0) OS << "\n";
|
|
|
|
printMessageToStream(Content.Messages[i],
|
|
|
|
"Candidate " + Twine(i + 1) + ": ", OS);
|
|
|
|
}
|
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 04:02:35 +08:00
|
|
|
void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
|
2013-07-19 03:47:59 +08:00
|
|
|
for (size_t i = 0, e = Errors.size(); i != e; ++i) {
|
2013-07-20 04:02:35 +08:00
|
|
|
if (i != 0) OS << "\n";
|
|
|
|
printErrorContentToStream(Errors[i], OS);
|
2013-07-19 03:47:59 +08:00
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 04:02:35 +08:00
|
|
|
std::string Diagnostics::toString() const {
|
|
|
|
std::string S;
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
printToStream(OS);
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
|
2013-07-19 03:47:59 +08:00
|
|
|
for (size_t i = 0, e = Errors.size(); i != e; ++i) {
|
2013-07-20 04:02:35 +08:00
|
|
|
if (i != 0) OS << "\n";
|
2013-07-19 03:47:59 +08:00
|
|
|
const ErrorContent &Error = Errors[i];
|
|
|
|
for (size_t i = 0, e = Error.ContextStack.size(); i != e; ++i) {
|
2013-07-20 04:02:35 +08:00
|
|
|
printContextFrameToStream(Error.ContextStack[i], OS);
|
|
|
|
OS << "\n";
|
2013-07-19 03:47:59 +08:00
|
|
|
}
|
2013-07-20 04:02:35 +08:00
|
|
|
printErrorContentToStream(Error, OS);
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
2013-07-20 04:02:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Diagnostics::toStringFull() const {
|
|
|
|
std::string S;
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
printToStreamFull(OS);
|
|
|
|
return OS.str();
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
2015-03-10 01:00:48 +08:00
|
|
|
|
|
|
|
} // namespace dynamic
|
|
|
|
} // namespace ast_matchers
|
|
|
|
} // namespace clang
|