FE 0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
Loading...
Searching...
No Matches
diag.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include <functional>
7#include <iosfwd>
8#include <string>
9#include <string_view>
10
11#include "fe/api.h"
12#include "fe/loc.h"
13#include "fe/term.h"
14
15namespace fe {
16
17/// How a diagnostic lays out - and how much of it an Error keeps.
18/// The knobs below cover the common adjustments; derive and Driver::diag your subclass for the rest.
19/// @warning A subclass that refers back to its Driver does not survive a move; keep the Driver pinned.
20class FE_API Diag {
21public:
22 enum class Tag {
24 Warn,
25 Note,
26 E = Error,
27 W = Warn,
28 N = Note,
29 };
30
31 Diag() = default;
32 Diag(const Diag&) = delete;
33 virtual ~Diag();
34
35 uint32_t gutter = 5; ///< Width of the line-number column.
36 uint32_t max_rows = 8; ///< Rows a snippet streams before eliding its middle; `0` elides nothing.
37 uint32_t max_errors = 0; ///< Errors recorded before the rest is dropped; `0` keeps everything.
38 bool no_snippet = false; ///< If `true`, a diagnostic is only its header line.
39 bool werror = false; ///< If `true`, a warning is recorded as an error.
40 Loc::Style loc_style = Loc::Style::Full; ///< How Diag::loc spells out a position.
41
42 /// @name Layout
43 /// Each of these streams one piece of a diagnostic; override to lay that piece out differently.
44 ///@{
45 virtual void loc(std::ostream&, Loc) const; ///< Just the position.
46 virtual void header(std::ostream&, Loc, Tag, std::string_view) const; ///< The `loc: tag: msg` line.
47 virtual void snippet(std::ostream&, Loc, Tag) const; ///< The underlined source excerpt.
48 virtual void note(std::ostream&, Loc, std::string_view) const; ///< One note below the message it belongs to.
49 virtual void summary(std::ostream&, size_t num_errors, size_t num_warnings, bool truncated) const;
50 ///@}
51
52 /// Postprocesses the text of one Error::Msg, which is `` `citation` `` markup - see fe::Cite.
53 /// Resolves it to plain text here, keeping the backticks; see CodeDiag for the colored rendering.
54 /// An override receives the markup itself, so a renderer of your own can read that structure.
55 /// @warning @p fmt captures its arguments by reference and is only valid for that one call.
56 virtual std::string render(const std::function<std::string()>& fmt) const;
57
59};
60
61/// The Diag a Driver installs by default: colors a `` `citation` `` and drops its backticks - or keeps them
62/// verbatim without color; `` \` `` is a literal backtick and `\\` a literal backslash.
63/// @warning Renders when the message is *recorded*, so Mode::Auto needs term::resolve_mode up front.
64class FE_API CodeDiag : public Diag {
65public:
66 std::string render(const std::function<std::string()>&) const override;
67};
68
69FE_API std::ostream& operator<<(std::ostream&, Diag::Tag);
70
71} // namespace fe
The Diag a Driver installs by default: colors a `citation` and drops its backticks - or keeps them ...
Definition diag.h:64
std::string render(const std::function< std::string()> &) const override
Postprocesses the text of one Error::Msg, which is `citation` markup - see fe::Cite.
virtual void summary(std::ostream &, size_t num_errors, size_t num_warnings, bool truncated) const
virtual void header(std::ostream &, Loc, Tag, std::string_view) const
The loc: tag: msg line.
virtual void note(std::ostream &, Loc, std::string_view) const
One note below the message it belongs to.
uint32_t max_rows
Rows a snippet streams before eliding its middle; 0 elides nothing.
Definition diag.h:36
bool werror
If true, a warning is recorded as an error.
Definition diag.h:39
uint32_t max_errors
Errors recorded before the rest is dropped; 0 keeps everything.
Definition diag.h:37
virtual void snippet(std::ostream &, Loc, Tag) const
The underlined source excerpt.
static term::FG tag2color(Tag)
virtual ~Diag()
bool no_snippet
If true, a diagnostic is only its header line.
Definition diag.h:38
uint32_t gutter
Width of the line-number column.
Definition diag.h:35
Loc::Style loc_style
How Diag::loc spells out a position.
Definition diag.h:40
virtual void loc(std::ostream &, Loc) const
Just the position.
virtual std::string render(const std::function< std::string()> &fmt) const
Postprocesses the text of one Error::Msg, which is `citation` markup - see fe::Cite.
Diag(const Diag &)=delete
Diag()=default
Collects diagnostics and hands each to the Diag that lays it out.
Definition error.h:23
FG
Foreground colors that can be streamed into an std::ostream.
Definition term.h:50
Definition algo.h:17
FE_API std::ostream & operator<<(std::ostream &, Diag::Tag)
Location within a Src: the half-open byte range [Loc::begin, Loc::end).
Definition loc.h:42
Style
How much of a Loc a diagnostic spells out; see Diag::loc_style.
Definition loc.h:44
@ Full
path:row:col-row:col - the whole range.
Definition loc.h:45