Yume
notes.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "token.hpp"
4#include "util.hpp"
5#include <llvm/Support/raw_ostream.h>
6#include <stdexcept>
7#include <utility>
8
9namespace yume {
10struct SourceFile;
11}
12
13namespace yume::diagnostic {
14
15enum struct Severity { Note, Warn, Error, Fatal };
16
17struct NotesHolder;
18
19struct Note {
20public:
22 string message;
25
28
29 Note(const Note&) = delete;
30 Note(Note&&) = default;
31 auto operator=(const Note&) -> Note& = delete;
32 auto operator=(Note&&) -> Note& = default;
33
34 ~Note() noexcept(false) {
35 emit();
36
37 if (this->severity == Severity::Fatal)
38 throw std::runtime_error(message);
39 }
40
41 void emit() const;
42
43 auto operator<<(auto&& other) -> Note& {
44 llvm::raw_string_ostream{this->message} << std::forward<decltype(other)>(other);
45 return *this;
46 }
47};
48
50public:
51 vector<SourceFile*> context_files;
52 llvm::raw_ostream* stream{&llvm::errs()};
54
55 auto emit(Loc location, Severity severity = Severity::Note) -> Note { return {this, "", location, severity}; }
56};
57
59public:
60 string buffer{};
61 unique_ptr<llvm::raw_string_ostream> buffer_stream = std::make_unique<llvm::raw_string_ostream>(buffer);
62
63 StringNotesHolder(vector<SourceFile*> context_files = {}) : NotesHolder{std::move(context_files), nullptr} {
64 this->stream = buffer_stream.get();
65 }
66};
67} // namespace yume::diagnostic
Definition: ast.cpp:8
Represents a location in source code, as a range starting at a line and column and ending at some oth...
Definition: token.hpp:26
void emit() const
Definition: notes.cpp:11
Note(Note &&)=default
auto operator=(Note &&) -> Note &=default
auto operator=(const Note &) -> Note &=delete
Note(const Note &)=delete
~Note() noexcept(false)
Definition: notes.hpp:34
NotesHolder * holder
Definition: notes.hpp:21
Note(NotesHolder *holder, string message, Loc location, Severity severity=Severity::Note)
Definition: notes.hpp:26
auto operator<<(auto &&other) -> Note &
Definition: notes.hpp:43
vector< SourceFile * > context_files
Definition: notes.hpp:51
auto emit(Loc location, Severity severity=Severity::Note) -> Note
Definition: notes.hpp:55
llvm::raw_ostream * stream
Definition: notes.hpp:52
unique_ptr< llvm::raw_string_ostream > buffer_stream
Definition: notes.hpp:61
StringNotesHolder(vector< SourceFile * > context_files={})
Definition: notes.hpp:63