Files
carbon-lang/toolchain/diagnostics/diagnostic_consumer.cpp
T
Jon Ross-Perkins 422639685b Add a marker for warnings. (#3972)
Since notes can be above, it's unclear without.
2024-05-21 20:34:30 +00:00

43 lines
1.1 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) {
message.loc.FormatLocation(*stream_);
*stream_ << ": ";
switch (message.level) {
case DiagnosticLevel::Error:
*stream_ << "ERROR: ";
break;
case DiagnosticLevel::Warning:
*stream_ << "WARNING: ";
break;
case DiagnosticLevel::Note:
break;
}
*stream_ << message.format_fn(message) << "\n";
message.loc.FormatSnippet(*stream_);
}
}
auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
return *consumer;
}
} // namespace Carbon