Files
carbon-lang/toolchain/diagnostics/diagnostic_consumer.cpp
T
Richard Smith 92860c56b4 Include parse node being checked in crash backtrace. (#3926)
When we crash, include the source location of the parse node that we
were handling, as well as the name of the check function that we were
calling. Also include the source snippet, since it's easy to do so, and
may avoid the need to look at the input file in some cases.
2024-05-01 15:42:53 +00:00

36 lines
966 B
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/diagnostic_consumer.h"
#include <algorithm>
#include <cstdint>
namespace Carbon {
auto StreamDiagnosticConsumer::HandleDiagnostic(Diagnostic diagnostic) -> void {
if (printed_diagnostic_) {
*stream_ << "\n";
} else {
printed_diagnostic_ = true;
}
for (const auto& message : diagnostic.messages) {
message.loc.FormatLocation(*stream_);
*stream_ << ": ";
if (message.level == DiagnosticLevel::Error) {
*stream_ << "ERROR: ";
}
*stream_ << message.format_fn(message) << "\n";
message.loc.FormatSnippet(*stream_);
}
}
auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
return *consumer;
}
} // namespace Carbon