2018-05-01 21:50:34 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-01-30 20:53:49 +01:00
|
|
|
#include "message.h"
|
2018-04-18 20:26:17 +02:00
|
|
|
#include "char-set.h"
|
2018-05-02 21:07:49 +02:00
|
|
|
#include <algorithm>
|
2018-02-21 21:12:52 +01:00
|
|
|
#include <cstdarg>
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-02-21 21:12:52 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2018-04-19 22:51:25 +02:00
|
|
|
#include <string>
|
2018-05-02 21:07:49 +02:00
|
|
|
#include <vector>
|
2018-01-30 20:53:49 +01:00
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-01-30 20:53:49 +01:00
|
|
|
|
2018-02-20 18:57:30 +01:00
|
|
|
std::ostream &operator<<(std::ostream &o, const MessageFixedText &t) {
|
2018-03-20 18:59:07 +01:00
|
|
|
for (std::size_t j{0}; j < t.size(); ++j) {
|
2018-02-17 00:47:30 +01:00
|
|
|
o << t.str()[j];
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:12:52 +01:00
|
|
|
std::string MessageFixedText::ToString() const {
|
|
|
|
return std::string{str_, /*not in std::*/ strnlen(str_, bytes_)};
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:51:04 +02:00
|
|
|
MessageFormattedText::MessageFormattedText(MessageFixedText text, ...)
|
|
|
|
: isFatal_{text.isFatal()} {
|
2018-02-21 21:12:52 +01:00
|
|
|
const char *p{text.str()};
|
|
|
|
std::string asString;
|
|
|
|
if (p[text.size()] != '\0') {
|
|
|
|
// not NUL-terminated
|
|
|
|
asString = text.ToString();
|
|
|
|
p = asString.data();
|
|
|
|
}
|
|
|
|
char buffer[256];
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, text);
|
|
|
|
vsnprintf(buffer, sizeof buffer, p, ap);
|
|
|
|
va_end(ap);
|
|
|
|
string_ = buffer;
|
|
|
|
}
|
|
|
|
|
2018-04-18 22:24:41 +02:00
|
|
|
void Message::Incorporate(Message &that) {
|
2018-04-21 01:56:58 +02:00
|
|
|
if (provenanceRange_.start() == that.provenanceRange_.start() &&
|
|
|
|
cookedSourceRange_.begin() == that.cookedSourceRange_.begin() &&
|
2018-04-19 02:05:07 +02:00
|
|
|
!expected_.empty()) {
|
|
|
|
expected_ = expected_.Union(that.expected_);
|
2018-04-18 22:24:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:26:17 +02:00
|
|
|
std::string Message::ToString() const {
|
|
|
|
std::string s{string_};
|
|
|
|
bool isExpected{isExpected_};
|
|
|
|
if (string_.empty()) {
|
|
|
|
if (fixedText_ != nullptr) {
|
2018-04-19 22:51:25 +02:00
|
|
|
if (fixedBytes_ > 0 && fixedBytes_ < std::string::npos) {
|
2018-04-18 21:55:27 +02:00
|
|
|
s = std::string(fixedText_, fixedBytes_);
|
2018-04-18 20:26:17 +02:00
|
|
|
} else {
|
|
|
|
s = std::string{fixedText_}; // NUL-terminated
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-18 22:24:41 +02:00
|
|
|
SetOfChars expect{expected_};
|
2018-04-19 02:05:07 +02:00
|
|
|
if (expect.Has('\n')) {
|
|
|
|
expect = expect.Difference('\n');
|
|
|
|
if (expect.empty()) {
|
2018-04-18 22:24:41 +02:00
|
|
|
return "expected end of line"_err_en_US.ToString();
|
|
|
|
} else {
|
2018-04-19 01:28:29 +02:00
|
|
|
s = expect.ToString();
|
|
|
|
if (s.size() == 1) {
|
2018-04-18 22:24:41 +02:00
|
|
|
return MessageFormattedText(
|
|
|
|
"expected end of line or '%s'"_err_en_US, s.data())
|
|
|
|
.MoveString();
|
|
|
|
} else {
|
|
|
|
return MessageFormattedText(
|
|
|
|
"expected end of line or one of '%s'"_err_en_US, s.data())
|
|
|
|
.MoveString();
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 20:26:17 +02:00
|
|
|
}
|
2018-04-19 01:28:29 +02:00
|
|
|
s = expect.ToString();
|
|
|
|
if (s.size() != 1) {
|
2018-04-18 22:24:41 +02:00
|
|
|
return MessageFormattedText("expected one of '%s'"_err_en_US, s.data())
|
|
|
|
.MoveString();
|
2018-04-18 20:26:17 +02:00
|
|
|
}
|
|
|
|
isExpected = true;
|
2018-02-21 21:12:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-18 20:26:17 +02:00
|
|
|
if (isExpected) {
|
2018-04-18 21:55:27 +02:00
|
|
|
return MessageFormattedText("expected '%s'"_err_en_US, s.data())
|
|
|
|
.MoveString();
|
2018-04-18 20:26:17 +02:00
|
|
|
}
|
|
|
|
return s;
|
2018-02-21 21:12:52 +01:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:02:00 +02:00
|
|
|
ProvenanceRange Message::GetProvenance(const CookedSource &cooked) const {
|
2018-04-21 01:56:58 +02:00
|
|
|
if (cookedSourceRange_.begin() != nullptr) {
|
2018-05-02 19:02:00 +02:00
|
|
|
return cooked.GetProvenance(cookedSourceRange_);
|
|
|
|
}
|
|
|
|
return provenanceRange_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message::Emit(
|
|
|
|
std::ostream &o, const CookedSource &cooked, bool echoSourceLine) const {
|
|
|
|
ProvenanceRange provenanceRange{GetProvenance(cooked)};
|
2018-05-02 21:07:49 +02:00
|
|
|
std::string text;
|
2018-04-03 00:51:04 +02:00
|
|
|
if (isFatal_) {
|
2018-05-02 21:07:49 +02:00
|
|
|
text += "ERROR: ";
|
|
|
|
}
|
|
|
|
text += ToString();
|
|
|
|
cooked.allSources().EmitMessage(o, provenanceRange, text, echoSourceLine);
|
|
|
|
for (const Message *context{context_.get()}; context != nullptr;
|
|
|
|
context = context->context_.get()) {
|
|
|
|
ProvenanceRange contextProvenance{context->GetProvenance(cooked)};
|
|
|
|
text = "in the context: ";
|
|
|
|
text += context->ToString();
|
2018-05-03 01:55:14 +02:00
|
|
|
// TODO: don't echo the source lines of a context when it's the
|
|
|
|
// same line (or maybe just never echo source for context)
|
2018-05-02 21:07:49 +02:00
|
|
|
cooked.allSources().EmitMessage(o, contextProvenance, text,
|
|
|
|
echoSourceLine && contextProvenance != provenanceRange);
|
|
|
|
provenanceRange = contextProvenance;
|
2018-04-03 00:51:04 +02:00
|
|
|
}
|
2018-01-30 20:53:49 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:24:41 +02:00
|
|
|
void Messages::Incorporate(Messages &that) {
|
|
|
|
if (messages_.empty()) {
|
|
|
|
*this = std::move(that);
|
|
|
|
} else if (!that.messages_.empty()) {
|
|
|
|
last_->Incorporate(*that.last_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:46:02 +02:00
|
|
|
void Messages::Copy(const Messages &that) {
|
2018-04-20 02:02:12 +02:00
|
|
|
for (const Message &m : that.messages_) {
|
|
|
|
Message copy{m};
|
|
|
|
Put(std::move(copy));
|
2018-04-20 00:46:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 21:07:49 +02:00
|
|
|
void Messages::Emit(
|
|
|
|
std::ostream &o, const CookedSource &cooked, bool echoSourceLines) const {
|
|
|
|
std::vector<const Message *> sorted;
|
2018-02-09 23:04:11 +01:00
|
|
|
for (const auto &msg : messages_) {
|
2018-05-02 21:07:49 +02:00
|
|
|
sorted.push_back(&msg);
|
|
|
|
}
|
|
|
|
std::sort(sorted.begin(), sorted.end(),
|
|
|
|
[](const Message *x, const Message *y) { return *x < *y; });
|
|
|
|
for (const Message *msg : sorted) {
|
|
|
|
msg->Emit(o, cooked, echoSourceLines);
|
2018-01-30 20:53:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-03 00:51:04 +02:00
|
|
|
|
|
|
|
bool Messages::AnyFatalError() const {
|
|
|
|
for (const auto &msg : messages_) {
|
|
|
|
if (msg.isFatal()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-02 22:48:12 +02:00
|
|
|
|
|
|
|
} // namespace Fortran::parser
|