Files
carbon-lang/toolchain/diagnostics/diagnostic_consumer.cpp
T
Jon Ross-Perkins b8ceb8dd8b Print a blank line after a diagnostic. (#3806)
The purpose of the newline is to make it clearer where a given
diagnostic begins and ends, particularly as the first message of a
diagnostic may not be the error.

This is a trivial code change, but ripples edits through test files.
2024-03-22 18:10:49 +00:00

58 lines
1.9 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/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) {
*stream_ << message.location.filename;
if (message.location.line_number > 0) {
*stream_ << ":" << message.location.line_number;
if (message.location.column_number > 0) {
*stream_ << ":" << message.location.column_number;
}
}
*stream_ << ": ";
if (message.level == DiagnosticLevel::Error) {
*stream_ << "ERROR: ";
}
*stream_ << message.format_fn(message) << "\n";
if (message.location.column_number > 0) {
*stream_ << message.location.line << "\n";
stream_->indent(message.location.column_number - 1);
*stream_ << "^";
int underline_length = std::max(0, message.location.length - 1);
// We want to ensure that we don't underline past the end of the line in
// case of a multiline token.
// TODO: revisit this once we can reference multiple ranges on multiple
// lines in a single diagnostic message.
underline_length = std::min(
underline_length, static_cast<int32_t>(message.location.line.size()) -
message.location.column_number);
for (int i = 0; i < underline_length; ++i) {
*stream_ << "~";
}
*stream_ << "\n";
}
}
}
auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
return *consumer;
}
} // namespace Carbon