1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include "errors.hh"
#include "location.hh"
#include <cstdint>
#include <format>
#include <iostream>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
namespace src {
namespace {
class FileErrors : public Errors {
public:
FileErrors(std::string filename, std::shared_ptr<ErrorsOutput> output)
: filename_(std::move(filename)), output_(std::move(output)) {}
void err(Location loc, std::string_view msg) override {
++errors_;
output_->println(std::format("{}:{}:{}: Error: {}", filename_, loc.line,
loc.column, msg));
}
void warn(Location loc, std::string_view msg) override {
++warnings_;
output_->println(std::format("{}:{}:{}: Warning: {}", filename_, loc.line,
loc.column, msg));
}
#ifndef NDEBUG
void dbg(Location loc, std::string_view msg) override {
output_->println(std::format("{}:{}:{}: Debug: {}", filename_, loc.line,
loc.column, msg));
}
#endif
[[nodiscard]]
uint64_t errors() const override {
return errors_;
}
[[nodiscard]]
uint64_t warnings() const override {
return warnings_;
}
private:
std::string const filename_;
std::shared_ptr<ErrorsOutput> output_;
uint64_t errors_{0};
uint64_t warnings_{0};
};
class IgnoreErrors : public Errors {
public:
IgnoreErrors() = default;
void err(Location /* loc */, std::string_view /* msg */) override {}
void warn(Location /* loc */, std::string_view /* msg */) override {}
#ifndef NDEBUG
void dbg(Location /* loc */, std::string_view /* msg */) override {}
#endif
[[nodiscard]]
uint64_t errors() const override {
return 0;
}
[[nodiscard]]
uint64_t warnings() const override {
return 0;
}
};
class OutputStreamErrorsOutput : public ErrorsOutput {
public:
explicit OutputStreamErrorsOutput(std::ostream& out) : out_(out) {}
void println(std::string_view line) override { out_ << line << '\n'; }
private:
std::ostream& out_;
};
} // namespace
[[nodiscard]]
std::unique_ptr<Errors> file_errors(std::string filename,
std::shared_ptr<ErrorsOutput> output) {
if (!output) {
static std::shared_ptr<ErrorsOutput> g_stderr_output;
// TODO: Make thread-safe when needed
if (!g_stderr_output)
g_stderr_output = std::make_shared<OutputStreamErrorsOutput>(std::cerr);
output = g_stderr_output;
}
return std::make_unique<FileErrors>(std::move(filename), std::move(output));
}
[[nodiscard]]
std::unique_ptr<Errors> ignore_errors() {
return std::make_unique<IgnoreErrors>();
}
[[nodiscard]]
std::unique_ptr<ErrorsOutput> errors_output_ios(std::ostream& out) {
return std::make_unique<OutputStreamErrorsOutput>(out);
}
} // namespace src
|