Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::ParseWarningSink Class Reference

Collects parse warnings during parsing. More...

#include "donner/base/ParseWarningSink.h"

Public Member Functions

 ParseWarningSink ()=default
 Construct a sink that collects warnings.
ParseWarningSink remainingAllowance () const
 A fresh buffer restricted to what this destination can still retain.
std::size_t retainedCount () const
 Number of diagnostics retained in the vector or delivered to a streaming consumer.
bool isEnabled () const
 Returns true if the sink is enabled (will store warnings).
template<typename Factory>
requires std::invocable<Factory> && std::same_as<std::invoke_result_t<Factory>, ParseDiagnostic>
void add (Factory &&factory)
 Add a warning via a factory callable.
void add (ParseDiagnostic &&warning)
 Add a pre-constructed warning (for cases where the diagnostic is already built).
const std::vector< ParseDiagnostic > & warnings () const
 Access warnings collected in this sink. A streaming sink delivers them to its consumer.
bool hasWarnings () const
 Returns true if any warnings have been added.
std::size_t warningAttempts () const
 Number of submitted warnings, including those sent to a disabled or saturated sink.
bool resourceLimitExceeded () const
 Returns true if warnings were discarded to enforce a resource limit.
std::size_t warningBytes () const
 Aggregate bytes retained for warning messages.
template<typename Mapper>
requires std::invocable<Mapper&, const SourceRange&> && std::same_as<std::invoke_result_t<Mapper&, const SourceRange&>, SourceRange>
void mapSourceRanges (Mapper &&mapper)
 Remap stored source ranges without changing warning text, severity, or admission accounting.
void markResourceLimitExceeded ()
 Record that warnings bound for this sink were discarded to enforce a resource limit elsewhere, for example by a sink whose warnings were filtered before being forwarded here, so that resourceLimitExceeded reports the collection as incomplete.
template<typename Read>
void mergeRecorded (std::size_t count, std::size_t attempts, bool exceeded, Read &&read)
 Append recorded diagnostics in order, preserving attempts even when the destination drops them.
void merge (ParseWarningSink &&other)
 Merge all warnings from another collecting sink into this one.
void mergeFromSubparser (ParseWarningSink &&other, FileOffset parentOffset)
 Merge warnings from a subparser, remapping source ranges using the given parent offset.

Static Public Member Functions

static ParseWarningSink Disabled ()
 Construct a disabled sink that discards all warnings (no-op).
static ParseWarningSink Bounded (std::size_t count, std::size_t bytes)
 Construct an enabled sink with a smaller count and reason-byte allowance.
static ParseWarningSink Streaming (std::size_t count, std::size_t bytes, void *context, bool(*consume)(void *, const ParseDiagnostic &))
 Deliver each admitted diagnostic to a synchronous consumer instead of retaining a vector. The consumer borrows its argument for this call and returns false if its own allowance is full.

Static Public Attributes

static constexpr std::size_t kMaximumWarnings = 1024
 Maximum warning objects retained by one sink.
static constexpr std::size_t kMaximumWarningBytes = 1024 * 1024
 Maximum aggregate warning-reason bytes retained by one sink.

Detailed Description

Collects parse warnings during parsing.

Always safe to call add() on—when disabled, warnings are silently dropped without invoking the factory callable, implicitly avoiding string formatting overhead.

Replaces the std::vector<ParseDiagnostic>* outWarnings pattern.

Usage:

// The lambda is only invoked if the sink is enabled---no formatting overhead when disabled.
sink.add([&] {
RcString::fromFormat("Unknown attribute '{}'", std::string_view(name)), range);
});
static RcString fromFormat(std::format_string< Args... > fmt, Args &&... args)
Constructs an RcString using std::format-style formatting.
Definition RcString.h:92
static ParseDiagnostic Warning(RcString reason, FileOffset location)
Create a warning diagnostic at a single offset.
Definition ParseDiagnostic.h:53
Examples
custom_css_parser.cc, geode_embed.cc, svg_animation.cc, svg_filter_interaction.cc, svg_quickstart.cc, svg_text_interaction.cc, svg_to_png.cc, and svg_tree_interaction.cc.

Member Function Documentation

◆ add()

template<typename Factory>
requires std::invocable<Factory> && std::same_as<std::invoke_result_t<Factory>, ParseDiagnostic>
void donner::ParseWarningSink::add ( Factory && factory)
inline

Add a warning via a factory callable.

The callable is only invoked when the sink is enabled and has not reached a resource limit, implicitly avoiding formatting overhead when warnings are disabled. Once kMaximumWarnings or kMaximumWarningBytes would be exceeded, this and every later warning is dropped and resourceLimitExceeded returns true.

Template Parameters
FactoryA callable returning ParseDiagnostic.
Parameters
factoryCallable that builds the warning.

◆ mapSourceRanges()

template<typename Mapper>
requires std::invocable<Mapper&, const SourceRange&> && std::same_as<std::invoke_result_t<Mapper&, const SourceRange&>, SourceRange>
void donner::ParseWarningSink::mapSourceRanges ( Mapper && mapper)
inline

Remap stored source ranges without changing warning text, severity, or admission accounting.

Parameters
mapperCallable taking a source range and returning its replacement range.

◆ markResourceLimitExceeded()

void donner::ParseWarningSink::markResourceLimitExceeded ( )
inline

Record that warnings bound for this sink were discarded to enforce a resource limit elsewhere, for example by a sink whose warnings were filtered before being forwarded here, so that resourceLimitExceeded reports the collection as incomplete.

As after any resource limit, later warnings are dropped. Does nothing when disabled.

◆ mergeFromSubparser()

void donner::ParseWarningSink::mergeFromSubparser ( ParseWarningSink && other,
FileOffset parentOffset )
inline

Merge warnings from a subparser, remapping source ranges using the given parent offset.

Each warning's range.start and range.end are remapped via addParentOffset(parentOffset), translating them from the subparser's local coordinate space to the parent's.

Parameters
otherSubparser's warning sink to merge from.
parentOffsetThe offset of the subparser's input within the parent input.

The documentation for this class was generated from the following file: