Yume
notes.cpp
Go to the documentation of this file.
2#include "compiler/vals.hpp"
3#include <filesystem>
4#include <fstream>
5#include <limits>
6#include <llvm/Support/Format.h>
7#include <ranges>
8#include <string>
9
10namespace yume::diagnostic {
11void Note::emit() const {
12 if (!holder->context_files.empty() && (holder->prev_loc != this->location || !holder->prev_loc.valid())) {
13 holder->prev_loc = this->location;
14
15 auto matching_file = std::ranges::find_if(holder->context_files,
16 [&](const SourceFile* src) { return src->name == this->location.file; });
17 if (matching_file != holder->context_files.end()) {
18 *holder->stream << "\n |\n" << llvm::right_justify(std::to_string(this->location.begin_line), 4) << "| ";
19 std::string line;
20 auto file = std::fstream{(*matching_file)->path};
21 for (int i = 0; i < this->location.begin_line - 1; ++i)
22 file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
23 std::getline(file, line);
24 *holder->stream << line << "\n |" << std::string(this->location.begin_col, ' ') << "^\n";
25 // TODO(rymiel): Handle diagnostic locations spanning multiple columns.
26 // TODO(rymiel): Handle diagnostic locations spanning multiple lines???
27 // TODO(rymiel): Add a splash of color.
28 }
29 }
30
31 switch (this->severity) {
32 case Severity::Note: *holder->stream << "note: "; break;
33 case Severity::Warn: *holder->stream << "warn: "; break;
34 case Severity::Error: *holder->stream << "error: "; break;
35 case Severity::Fatal: *holder->stream << "FATAL: "; break;
36 }
37 *holder->stream << this->location.single().to_string() << ": " << this->message << "\n";
38}
39
40} // namespace yume::diagnostic
auto valid() const -> bool
Definition: token.hpp:70
auto single() const -> Loc
Return a new Loc which refers to the first character of the current Loc.
Definition: token.hpp:73
int begin_col
Definition: token.hpp:28
int begin_line
Definition: token.hpp:27
A source file with its associated Syntax Tree.
Definition: vals.hpp:281
void emit() const
Definition: notes.cpp:11
NotesHolder * holder
Definition: notes.hpp:21
vector< SourceFile * > context_files
Definition: notes.hpp:51
llvm::raw_ostream * stream
Definition: notes.hpp:52