Files
carbon-lang/toolchain/diagnostics/null_diagnostics.h
T
Jon Ross-Perkins e79d3be5bd Combine DiagnosticConverter into DiagnosticEmitter (#4878)
At present, we typically define a DiagnosticConverter, then store an
instance of it and a DiagnosticEmitter that wraps it. This is relatively
minor in general, but I've been trying to create more self-contained
DiagnosticEmitter classes (which hold their own DiagnosticConverter,
similar to NullDiagnosticEmitter), and there it just gets in the way.

Since we don't reuse DiagnosticConverter instances, this combines the
definition into DiagnosticEmitter. Mainly this means we don't have a
separate object in play, and less to carry around.

The most impact is probably to SemIRDiagnosticConverter, which was also
the most complex. Now `SemIRLocDiagnosticEmitter`, this gets some
different construction flow. Note in the PR I've split the file rename
to its own commit, to try to help delta views. However, the most
substantial parts of the refactoring are split into #4876, which this
depends upon.
2025-02-06 20:27:57 +00:00

44 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_NULL_DIAGNOSTICS_H_
#define CARBON_TOOLCHAIN_DIAGNOSTICS_NULL_DIAGNOSTICS_H_
#include "toolchain/diagnostics/diagnostic_emitter.h"
namespace Carbon {
// Returns a singleton consumer that doesn't print its diagnostics.
inline auto NullDiagnosticConsumer() -> DiagnosticConsumer& {
struct Consumer : DiagnosticConsumer {
auto HandleDiagnostic(Diagnostic /*d*/) -> void override {}
};
static auto* consumer = new Consumer;
return *consumer;
}
// Returns a singleton emitter that doesn't print its diagnostics.
template <typename LocT>
inline auto NullDiagnosticEmitter() -> DiagnosticEmitter<LocT>& {
class Emitter : public DiagnosticEmitter<LocT> {
public:
using DiagnosticEmitter<LocT>::DiagnosticEmitter;
protected:
// Converts a filename directly to the diagnostic location.
auto ConvertLoc(LocT /*loc*/,
DiagnosticEmitter<LocT>::ContextFnT /*context_fn*/) const
-> ConvertedDiagnosticLoc override {
return {.loc = {}, .last_byte_offset = -1};
}
};
static auto* emitter = new Emitter(&NullDiagnosticConsumer());
return *emitter;
}
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DIAGNOSTICS_NULL_DIAGNOSTICS_H_