Files
carbon-lang/toolchain/diagnostics/sorting_diagnostic_consumer_test.cpp
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

70 lines
2.5 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
#include "toolchain/diagnostics/sorting_diagnostic_consumer.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "llvm/ADT/StringRef.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/mocks.h"
namespace Carbon {
namespace {
using ::Carbon::Testing::IsSingleDiagnostic;
using ::testing::_;
using ::testing::InSequence;
CARBON_DIAGNOSTIC(TestDiagnostic, Error, "M{0}", int);
class FakeDiagnosticEmitter : public DiagnosticEmitter<int32_t> {
public:
using DiagnosticEmitter::DiagnosticEmitter;
protected:
auto ConvertLoc(int32_t last_byte_offset, ContextFnT /*context_fn*/) const
-> ConvertedDiagnosticLoc override {
return {.loc = {}, .last_byte_offset = last_byte_offset};
}
};
TEST(SortedDiagnosticEmitterTest, SortErrors) {
Testing::MockDiagnosticConsumer consumer;
SortingDiagnosticConsumer sorting_consumer(consumer);
FakeDiagnosticEmitter emitter(&sorting_consumer);
emitter.Emit(1, TestDiagnostic, 1);
emitter.Emit(-1, TestDiagnostic, 2);
emitter.Emit(0, TestDiagnostic, 3);
emitter.Emit(4, TestDiagnostic, 4);
emitter.Emit(3, TestDiagnostic, 5);
emitter.Emit(3, TestDiagnostic, 6);
InSequence s;
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M2")));
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M3")));
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M1")));
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M5")));
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M6")));
EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
DiagnosticKind::TestDiagnostic,
DiagnosticLevel::Error, _, _, "M4")));
sorting_consumer.Flush();
}
} // namespace
} // namespace Carbon