mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +01:00
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.
44 lines
1.4 KiB
C++
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_
|