Create a basic print function for the diagnostic class

This commit is contained in:
Sameer Rahmani 2021-09-12 19:50:18 +01:00
parent 766cf2dfed
commit 91aa0148d6
7 changed files with 123 additions and 72 deletions

View File

@ -1,5 +1,5 @@
(def main
(fn () 4)))
(fn () 4))
(def main1 (fn (v y n) 3))
ht

View File

@ -28,9 +28,6 @@
#include "serene/context.h"
#include "serene/source_mgr.h"
// Sometimes we need this to make both analyzer happy
// and the fn signature right.
#define UNUSED(x) (void)(x)
namespace serene {} // namespace serene
#endif

View File

@ -28,6 +28,9 @@
#include "serene/namespace.h"
#include "serene/reader/location.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/ErrorHandling.h"
#include <llvm/ADT/SmallVector.h>
#include <llvm/Support/ErrorOr.h>
#include <llvm/Support/MemoryBuffer.h>
@ -103,6 +106,8 @@ private:
/// This is all of the buffers that we are reading from.
std::vector<SrcBuffer> buffers;
llvm::StringMap<unsigned> nsTable;
/// A mapping from the ns name to buffer id. The ns name is a reference to
/// the actual name that is stored in the Namespace instance.
llvm::DenseMap<llvm::StringRef, unsigned> nsToBufId;
@ -143,6 +148,18 @@ public:
return buffers[i - 1];
}
const SrcBuffer &getBufferInfo(llvm::StringRef ns) const {
auto bufferId = nsTable.lookup(ns);
if (bufferId == 0) {
// No such namespace
llvm_unreachable("couldn't find the src buffer for a namespace. It "
"should never happen.");
}
return buffers[bufferId - 1];
}
const llvm::MemoryBuffer *getMemoryBuffer(unsigned i) const {
assert(isValidBufferID(i));
return buffers[i - 1].buffer.get();

View File

@ -29,6 +29,10 @@
#include <variant>
// Sometimes we need this to make both analyzer happy
// and the fn signature right.
#define UNUSED(x) (void)(x)
// C++17 required. We can't go back to 14 any more :))
namespace serene {

View File

@ -32,6 +32,7 @@
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <llvm/Support/FormatAdapters.h>
#include <llvm/Support/FormatVariadic.h>
#include <memory>
@ -83,24 +84,9 @@ void Diagnostic::print(llvm::raw_ostream &os, llvm::StringRef prefix) {
llvm::WithColor s(os, llvm::raw_ostream::SAVEDCOLOR, true, false, mode);
s << "[";
if (err) {
writeColorByType(os, err->getErrId());
}
s << "] ";
writeColorByType(os, getPrefix(prefix));
s << ": ";
if (err) {
s << err->description << '\n';
}
if (message != "") {
s.changeColor(llvm::raw_ostream::Colors::YELLOW);
s << "With message";
s.resetColor();
s << ": " << message << "\n";
}
s << "\n[";
writeColorByType(os, "Error");
s << "]>\n";
s << "In ns '";
s.changeColor(llvm::raw_ostream::Colors::MAGENTA);
@ -121,6 +107,50 @@ void Diagnostic::print(llvm::raw_ostream &os, llvm::StringRef prefix) {
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';
s << "[";
if (err) {
writeColorByType(os, err->getErrId());
}
s << "] ";
writeColorByType(os, getPrefix(prefix));
s << ": ";
if (err) {
s << err->description << '\n';
}
if (message != "") {
s.changeColor(llvm::raw_ostream::Colors::YELLOW);
s << "With message";
s.resetColor();
s << ": " << message << "\n";
}
if (err) {
s << "For more information checkout";
s.changeColor(llvm::raw_ostream::Colors::CYAN);
s << " `serenec --explain ";
s << err->getErrId() << "`\n";
}
};
DiagnosticEngine::DiagnosticEngine(SereneContext &ctx)

View File

@ -175,7 +175,6 @@ exprs::Node Reader::readNumber(bool neg) {
LocationRange loc(getCurrentLocation());
if (!isdigit(*c)) {
ctx.diagEngine->emitSyntaxError(loc, errors::InvalidDigitForNumber);
exit(1);
}
@ -203,7 +202,8 @@ exprs::Node Reader::readNumber(bool neg) {
}
if ((std::isalpha(*c) && !empty) || empty) {
loc.end = getCurrentLocation();
advance();
loc.start = getCurrentLocation();
ctx.diagEngine->emitSyntaxError(loc, errors::InvalidDigitForNumber);
exit(1);
}

View File

@ -29,6 +29,7 @@
#include "serene/namespace.h"
#include "serene/reader/location.h"
#include "serene/reader/reader.h"
#include "serene/utils.h"
#include "llvm/Support/MemoryBufferRef.h"
@ -79,6 +80,8 @@ NSPtr SourceMgr::readNamespace(SereneContext &ctx, std::string name,
auto bufferId = AddNewSourceBuffer(std::move(*newBufOrErr), importLoc);
UNUSED(nsTable.insert_or_assign(name, bufferId));
if (bufferId == 0) {
auto msg = llvm::formatv("Couldn't add namespace '{0}'", name);
ctx.diagEngine->emitSyntaxError(importLoc, errors::NSAddToSMError,
@ -151,25 +154,25 @@ unsigned SourceMgr::AddNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> f,
// return 0;
// }
// template <typename T>
// static std::vector<T> &GetOrCreateOffsetCache(void *&offsetCache,
// llvm::MemoryBuffer *buffer) {
// if (offsetCache)
// return *static_cast<std::vector<T> *>(offsetCache);
template <typename T>
static std::vector<T> &GetOrCreateOffsetCache(void *&offsetCache,
llvm::MemoryBuffer *buffer) {
if (offsetCache)
return *static_cast<std::vector<T> *>(offsetCache);
// // Lazily fill in the offset cache.
// auto *offsets = new std::vector<T>();
// size_t sz = buffer->getBufferSize();
// assert(sz <= std::numeric_limits<T>::max());
// llvm::StringRef s = buffer->getBuffer();
// for (size_t n = 0; n < sz; ++n) {
// if (s[n] == '\n')
// offsets->push_back(static_cast<T>(n));
// }
// Lazily fill in the offset cache.
auto *offsets = new std::vector<T>();
size_t sz = buffer->getBufferSize();
assert(sz <= std::numeric_limits<T>::max());
llvm::StringRef s = buffer->getBuffer();
for (size_t n = 0; n < sz; ++n) {
if (s[n] == '\n')
offsets->push_back(static_cast<T>(n));
}
// offsetCache = offsets;
// return *offsets;
// }
offsetCache = offsets;
return *offsets;
}
// template <typename T>
// unsigned SourceMgr::SrcBuffer::getLineNumberSpecialized(const char *ptr)
@ -203,41 +206,41 @@ unsigned SourceMgr::AddNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> f,
// return getLineNumberSpecialized<uint64_t>(ptr);
// }
// template <typename T>
// const char *SourceMgr::SrcBuffer::getPointerForLineNumberSpecialized(
// unsigned lineNo) const {
// std::vector<T> &offsets =
// GetOrCreateOffsetCache<T>(offsetCache, buffer.get());
template <typename T>
const char *SourceMgr::SrcBuffer::getPointerForLineNumberSpecialized(
unsigned lineNo) const {
std::vector<T> &offsets =
GetOrCreateOffsetCache<T>(offsetCache, buffer.get());
// // We start counting line and column numbers from 1.
// if (lineNo != 0)
// --lineNo;
// We start counting line and column numbers from 1.
if (lineNo != 0)
--lineNo;
// const char *bufStart = buffer->getBufferStart();
const char *bufStart = buffer->getBufferStart();
// // The offset cache contains the location of the \n for the specified line,
// // we want the start of the line. As such, we look for the previous entry.
// if (lineNo == 0)
// return bufStart;
// if (lineNo > offsets.size())
// return nullptr;
// return bufStart + offsets[lineNo - 1] + 1;
// }
// The offset cache contains the location of the \n for the specified line,
// we want the start of the line. As such, we look for the previous entry.
if (lineNo == 0)
return bufStart;
if (lineNo > offsets.size())
return nullptr;
return bufStart + offsets[lineNo - 1] + 1;
}
// /// Return a pointer to the first character of the specified line number or
// /// null if the line number is invalid.
// const char *
// SourceMgr::SrcBuffer::getPointerForLineNumber(unsigned lineNo) const {
// size_t sz = buffer->getBufferSize();
// if (sz <= std::numeric_limits<uint8_t>::max())
// return getPointerForLineNumberSpecialized<uint8_t>(lineNo);
// else if (sz <= std::numeric_limits<uint16_t>::max())
// return getPointerForLineNumberSpecialized<uint16_t>(lineNo);
// else if (sz <= std::numeric_limits<uint32_t>::max())
// return getPointerForLineNumberSpecialized<uint32_t>(lineNo);
// else
// return getPointerForLineNumberSpecialized<uint64_t>(lineNo);
// }
/// Return a pointer to the first character of the specified line number or
/// null if the line number is invalid.
const char *
SourceMgr::SrcBuffer::getPointerForLineNumber(unsigned lineNo) const {
size_t sz = buffer->getBufferSize();
if (sz <= std::numeric_limits<uint8_t>::max())
return getPointerForLineNumberSpecialized<uint8_t>(lineNo);
else if (sz <= std::numeric_limits<uint16_t>::max())
return getPointerForLineNumberSpecialized<uint16_t>(lineNo);
else if (sz <= std::numeric_limits<uint32_t>::max())
return getPointerForLineNumberSpecialized<uint32_t>(lineNo);
else
return getPointerForLineNumberSpecialized<uint64_t>(lineNo);
}
SourceMgr::SrcBuffer::SrcBuffer(SourceMgr::SrcBuffer &&other)
: buffer(std::move(other.buffer)), offsetCache(other.offsetCache),