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