2021-09-12 14:01:02 +01:00
|
|
|
/* -*- C++ -*-
|
|
|
|
* Serene programming language.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serene/diagnostics.h"
|
|
|
|
|
|
|
|
#include "serene/context.h"
|
|
|
|
#include "serene/reader/location.h"
|
|
|
|
#include "serene/source_mgr.h"
|
2021-09-27 13:05:15 +01:00
|
|
|
#include "serene/utils.h"
|
2021-09-12 14:01:02 +01:00
|
|
|
|
2021-09-25 16:18:33 +01:00
|
|
|
#include <llvm/ADT/StringRef.h>
|
2021-09-12 19:50:18 +01:00
|
|
|
#include <llvm/Support/FormatAdapters.h>
|
2021-09-12 14:01:02 +01:00
|
|
|
#include <llvm/Support/FormatVariadic.h>
|
2021-09-25 16:18:33 +01:00
|
|
|
#include <llvm/Support/WithColor.h>
|
|
|
|
#include <llvm/Support/raw_ostream.h>
|
2021-09-12 14:01:02 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
void Diagnostic::writeColorByType(llvm::raw_ostream &os, llvm::StringRef str) {
|
|
|
|
llvm::ColorMode mode =
|
|
|
|
ctx.opts.withColors ? llvm::ColorMode::Auto : llvm::ColorMode::Disable;
|
|
|
|
|
|
|
|
llvm::WithColor s(os, llvm::raw_ostream::SAVEDCOLOR, true, false, mode);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Type::Error:
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::RED);
|
|
|
|
break;
|
|
|
|
// case Type::Warning:
|
|
|
|
// s.changeColor(llvm::raw_ostream::Colors::YELLOW);
|
|
|
|
// break;
|
|
|
|
case Type::Note:
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::CYAN);
|
|
|
|
break;
|
|
|
|
case Type::Remark:
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::MAGENTA);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s << str;
|
|
|
|
s.resetColor();
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string Diagnostic::getPrefix(llvm::StringRef prefix) {
|
|
|
|
if (prefix != "") {
|
|
|
|
return prefix.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Type::Error:
|
|
|
|
return "Error";
|
|
|
|
break;
|
|
|
|
case Type::Note:
|
|
|
|
return "Note";
|
|
|
|
case Type::Remark:
|
|
|
|
return "Remark";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void Diagnostic::print(llvm::raw_ostream &os, llvm::StringRef prefix) {
|
|
|
|
llvm::ColorMode mode =
|
|
|
|
ctx.opts.withColors ? llvm::ColorMode::Auto : llvm::ColorMode::Disable;
|
|
|
|
|
|
|
|
llvm::WithColor s(os, llvm::raw_ostream::SAVEDCOLOR, true, false, mode);
|
|
|
|
|
2021-09-12 19:50:18 +01:00
|
|
|
s << "\n[";
|
|
|
|
writeColorByType(os, "Error");
|
|
|
|
s << "]>\n";
|
|
|
|
|
|
|
|
s << "In ns '";
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::MAGENTA);
|
|
|
|
s << loc.start.ns;
|
|
|
|
s.resetColor();
|
|
|
|
|
|
|
|
if (loc.start.filename.hasValue()) {
|
|
|
|
s << "' At: ";
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::YELLOW);
|
|
|
|
s << loc.start.filename.getValue();
|
|
|
|
s.resetColor();
|
|
|
|
s << ":";
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::CYAN);
|
|
|
|
s << loc.start.line;
|
|
|
|
s.resetColor();
|
|
|
|
s << ":";
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::CYAN);
|
|
|
|
s << loc.start.col;
|
|
|
|
s.resetColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
s << "\n\n";
|
|
|
|
|
|
|
|
auto &srcBuf = ctx.sourceManager.getBufferInfo(loc.start.ns);
|
|
|
|
const char *line = srcBuf.getPointerForLineNumber(loc.start.line);
|
|
|
|
|
|
|
|
while (*line != '\n' && line != srcBuf.buffer->getBufferEnd()) {
|
|
|
|
s << *line;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
s << '\n';
|
|
|
|
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::GREEN);
|
|
|
|
s << llvm::formatv("{0}", llvm::fmt_pad("^", (size_t)loc.start.col - 1, 0));
|
|
|
|
s.resetColor();
|
|
|
|
|
|
|
|
s << '\n';
|
|
|
|
|
2021-09-12 14:01:02 +01:00
|
|
|
s << "[";
|
2021-09-12 17:49:51 +01:00
|
|
|
if (err) {
|
|
|
|
writeColorByType(os, err->getErrId());
|
|
|
|
}
|
|
|
|
s << "] ";
|
2021-09-12 14:01:02 +01:00
|
|
|
writeColorByType(os, getPrefix(prefix));
|
2021-09-12 17:49:51 +01:00
|
|
|
s << ": ";
|
2021-09-12 14:01:02 +01:00
|
|
|
|
|
|
|
if (err) {
|
2021-09-12 17:49:51 +01:00
|
|
|
s << err->description << '\n';
|
2021-09-12 14:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message != "") {
|
|
|
|
s.changeColor(llvm::raw_ostream::Colors::YELLOW);
|
2021-09-12 17:49:51 +01:00
|
|
|
s << "With message";
|
2021-09-12 14:01:02 +01:00
|
|
|
s.resetColor();
|
2021-09-12 17:49:51 +01:00
|
|
|
s << ": " << message << "\n";
|
2021-09-12 14:01:02 +01:00
|
|
|
}
|
|
|
|
|
2021-09-12 19:50:18 +01:00
|
|
|
if (err) {
|
|
|
|
s << "For more information checkout";
|
2021-09-12 14:01:02 +01:00
|
|
|
s.changeColor(llvm::raw_ostream::Colors::CYAN);
|
2021-09-12 19:50:18 +01:00
|
|
|
s << " `serenec --explain ";
|
|
|
|
s << err->getErrId() << "`\n";
|
2021-09-12 14:01:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DiagnosticEngine::DiagnosticEngine(SereneContext &ctx)
|
|
|
|
: ctx(ctx), diagEngine(ctx.mlirContext.getDiagEngine()){};
|
|
|
|
|
2021-09-27 13:05:15 +01:00
|
|
|
void DiagnosticEngine::print(llvm::raw_ostream &os, Diagnostic &d) {
|
|
|
|
UNUSED(os);
|
|
|
|
UNUSED(d);
|
|
|
|
};
|
2021-09-12 14:01:02 +01:00
|
|
|
|
|
|
|
Diagnostic DiagnosticEngine::toDiagnostic(reader::LocationRange loc,
|
|
|
|
errors::ErrorVariant &e,
|
|
|
|
llvm::StringRef msg,
|
|
|
|
llvm::StringRef fn) {
|
|
|
|
|
|
|
|
return Diagnostic(ctx, loc, &e, msg, fn);
|
|
|
|
};
|
|
|
|
|
2021-09-25 16:18:33 +01:00
|
|
|
void DiagnosticEngine::enqueueError(llvm::StringRef msg) {
|
|
|
|
llvm::errs() << llvm::formatv("FIX ME (better emit error): {0}\n", msg);
|
|
|
|
exit(1);
|
|
|
|
};
|
|
|
|
|
2021-09-12 14:01:02 +01:00
|
|
|
void DiagnosticEngine::emitSyntaxError(reader::LocationRange loc,
|
|
|
|
errors::ErrorVariant &e,
|
|
|
|
llvm::StringRef msg) {
|
|
|
|
Diagnostic diag(ctx, loc, &e, msg);
|
|
|
|
|
|
|
|
diag.print(llvm::errs(), "SyntaxError");
|
|
|
|
exit(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<DiagnosticEngine> makeDiagnosticEngine(SereneContext &ctx) {
|
|
|
|
return std::make_unique<DiagnosticEngine>(ctx);
|
|
|
|
}
|
|
|
|
} // namespace serene
|