Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
report.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <optional>
5#include <iostream>
6#include <string>
7#include <map>
8#include <set>
9
10#include "label.hpp"
11
12namespace pretty_diagnostics {
13class IReporterRenderer;
14
18enum class Severity {
19 Error,
20 Warning,
21 Info,
22 Unknown,
23};
24
28class LineGroup {
29public:
36 LineGroup(size_t line_number, std::set<Label> labels);
37
43 [[nodiscard]] size_t line_number() const { return _line_number; }
44
50 [[nodiscard]] const std::set<Label>& labels() const { return _labels; }
51
57 [[nodiscard]] std::set<Label>& labels() { return _labels; }
58
59private:
60 std::set<Label> _labels;
61 size_t _line_number;
62};
63
67class FileGroup {
68public:
69 using MappedLineGroups = std::map<size_t, LineGroup>;
70
71public:
78 FileGroup(const std::shared_ptr<Source>& source, MappedLineGroups line_groups);
79
85 [[nodiscard]] const MappedLineGroups& line_groups() const { return _line_groups; }
86
92 [[nodiscard]] MappedLineGroups& line_groups() { return _line_groups; }
93
99 [[nodiscard]] const std::shared_ptr<Source>& source() const { return _source; }
100
101private:
102 std::shared_ptr<Source> _source;
103 MappedLineGroups _line_groups;
104};
105
109class Report {
110public:
111 using MappedFileGroups = std::unordered_map<std::shared_ptr<Source>, FileGroup>;
112 class Builder;
113
114public:
125 Report(std::string message, std::optional<std::string> code, Severity severity, MappedFileGroups file_groups,
126 std::optional<std::string> note, std::optional<std::string> help);
127
134 void render(IReporterRenderer& renderer, std::ostream& stream = std::cout) const;
135
141 [[nodiscard]] const MappedFileGroups& file_groups() const { return _file_groups; }
142
148 [[nodiscard]] MappedFileGroups& file_groups() { return _file_groups; }
149
155 [[nodiscard]] Severity severity() const { return _severity; }
156
162 [[nodiscard]] const std::string& message() const { return _message; }
163
169 [[nodiscard]] const std::optional<std::string>& note() const { return _note; }
170
176 [[nodiscard]] const std::optional<std::string>& help() const { return _help; }
177
183 [[nodiscard]] const std::optional<std::string>& code() const { return _code; }
184
185private:
186 std::optional<std::string> _code, _note, _help;
187 MappedFileGroups _file_groups;
188 std::string _message;
189 Severity _severity;
190};
191
196public:
197 virtual ~IReporterRenderer() = default;
198
205 virtual void render(const Severity& severity, std::ostream& stream) = 0;
206
213 virtual void render(const Report& report, std::ostream& stream) = 0;
214
221 virtual void render(const FileGroup& file_group, std::ostream& stream) = 0;
222
229 virtual void render(const LineGroup& line_group, std::ostream& stream) = 0;
230};
231
236public:
245
253 Builder& message(std::string message);
254
262 Builder& code(std::string code);
263
272 Builder& label(std::string text, Span span);
273
281 Builder& note(std::string note);
282
290 Builder& help(std::string help);
291
298 [[nodiscard]] Report build() const;
299
300private:
301 std::optional<std::string> _message, _note, _help, _code;
302 std::optional<Severity> _severity;
303 MappedFileGroups _file_groups;
304};
305} // namespace pretty_diagnostics
306
307// BSD 3-Clause License
308//
309// Copyright (c) 2025, Timo Behrend
310//
311// Redistribution and use in source and binary forms, with or without
312// modification, are permitted provided that the following conditions are met:
313//
314// 1. Redistributions of source code must retain the above copyright notice, this
315// list of conditions and the following disclaimer.
316//
317// 2. Redistributions in binary form must reproduce the above copyright notice,
318// this list of conditions and the following disclaimer in the documentation
319// and/or other materials provided with the distribution.
320//
321// 3. Neither the name of the copyright holder nor the names of its
322// contributors may be used to endorse or promote products derived from
323// this software without specific prior written permission.
324//
325// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
326// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
327// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
328// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
329// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
330// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
331// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
332// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
333// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
334// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Groups LineGroups belonging to the same Source
Definition: report.hpp:67
FileGroup(const std::shared_ptr< Source > &source, MappedLineGroups line_groups)
Constructs a group for a source file with its line groups.
MappedLineGroups & line_groups()
Returns the map of line groups.
Definition: report.hpp:92
std::map< size_t, LineGroup > MappedLineGroups
Definition: report.hpp:69
const MappedLineGroups & line_groups() const
Returns the map of line groups.
Definition: report.hpp:85
const std::shared_ptr< Source > & source() const
Returns the source this file group refers to.
Definition: report.hpp:99
Interface implemented by renderers that turn reports into output (e.g., text)
Definition: report.hpp:195
virtual void render(const FileGroup &file_group, std::ostream &stream)=0
Renders a single file group.
virtual void render(const LineGroup &line_group, std::ostream &stream)=0
Renders a single line group.
virtual void render(const Report &report, std::ostream &stream)=0
Renders an entire report.
virtual void render(const Severity &severity, std::ostream &stream)=0
Renders just the severity label (e.g. "error", "warning")
A set of labels that belong to the same 1-based line number.
Definition: report.hpp:28
size_t line_number() const
Returns the 1-based line number.
Definition: report.hpp:43
std::set< Label > & labels()
Returns the set of labels for this line.
Definition: report.hpp:57
LineGroup(size_t line_number, std::set< Label > labels)
Constructs a group for a single line and its labels.
const std::set< Label > & labels() const
Returns the set of labels for this line.
Definition: report.hpp:50
Fluent builder for constructing Report instances.
Definition: report.hpp:235
Builder & severity(Severity severity)
Sets report severity.
Builder & help(std::string help)
Sets optional help text.
Report build() const
Builds a complete Report
Builder & message(std::string message)
Sets the main diagnostic message.
Builder & code(std::string code)
Sets an optional error code or identifier.
Builder & note(std::string note)
Sets an optional note.
Builder & label(std::string text, Span span)
Adds a label to the report.
Represents a fully constructed diagnostic report to be rendered.
Definition: report.hpp:109
MappedFileGroups & file_groups()
Returns the mapping of sources to file groups.
Definition: report.hpp:148
const std::optional< std::string > & code() const
Returns an optional error code or identifier.
Definition: report.hpp:183
void render(IReporterRenderer &renderer, std::ostream &stream=std::cout) const
Renders the report using the provided renderer to the output stream.
const std::string & message() const
Returns the primary diagnostic message.
Definition: report.hpp:162
std::unordered_map< std::shared_ptr< Source >, FileGroup > MappedFileGroups
Definition: report.hpp:111
const MappedFileGroups & file_groups() const
Returns the mapping of sources to file groups.
Definition: report.hpp:141
Severity severity() const
Returns the severity of this report.
Definition: report.hpp:155
Report(std::string message, std::optional< std::string > code, Severity severity, MappedFileGroups file_groups, std::optional< std::string > note, std::optional< std::string > help)
Constructs a report.
const std::optional< std::string > & help() const
Returns optional help text with suggestions.
Definition: report.hpp:176
const std::optional< std::string > & note() const
Returns an optional note with additional context.
Definition: report.hpp:169
Represents a contiguous region within a Source
Definition: span.hpp:12
Definition: label.hpp:5
Severity
Indicates the importance of a diagnostic.
Definition: report.hpp:18
@ Warning
Suspicious or suboptimal situation.
@ Info
Informational message.
@ Unknown
Unspecified or not set.
@ Error
Serious problem that usually prevents progress.