1 //===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is a concrete diagnostic client, which buffers the diagnostic messages.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Frontend/TextDiagnosticBuffer.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/LLVM.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/ErrorHandling.h"
19 using namespace clang
;
21 /// HandleDiagnostic - Store the errors, warnings, and notes that are
23 void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level
,
24 const Diagnostic
&Info
) {
25 // Default implementation (Warnings/errors count).
26 DiagnosticConsumer::HandleDiagnostic(Level
, Info
);
29 Info
.FormatDiagnostic(Buf
);
31 default: llvm_unreachable(
32 "Diagnostic not handled during diagnostic buffering!");
33 case DiagnosticsEngine::Note
:
34 All
.emplace_back(Level
, Notes
.size());
35 Notes
.emplace_back(Info
.getLocation(), std::string(Buf
.str()));
37 case DiagnosticsEngine::Warning
:
38 All
.emplace_back(Level
, Warnings
.size());
39 Warnings
.emplace_back(Info
.getLocation(), std::string(Buf
.str()));
41 case DiagnosticsEngine::Remark
:
42 All
.emplace_back(Level
, Remarks
.size());
43 Remarks
.emplace_back(Info
.getLocation(), std::string(Buf
.str()));
45 case DiagnosticsEngine::Error
:
46 case DiagnosticsEngine::Fatal
:
47 All
.emplace_back(Level
, Errors
.size());
48 Errors
.emplace_back(Info
.getLocation(), std::string(Buf
.str()));
53 void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine
&Diags
) const {
54 for (const auto &I
: All
) {
55 auto Diag
= Diags
.Report(Diags
.getCustomDiagID(I
.first
, "%0"));
57 default: llvm_unreachable(
58 "Diagnostic not handled during diagnostic flushing!");
59 case DiagnosticsEngine::Note
:
60 Diag
<< Notes
[I
.second
].second
;
62 case DiagnosticsEngine::Warning
:
63 Diag
<< Warnings
[I
.second
].second
;
65 case DiagnosticsEngine::Remark
:
66 Diag
<< Remarks
[I
.second
].second
;
68 case DiagnosticsEngine::Error
:
69 case DiagnosticsEngine::Fatal
:
70 Diag
<< Errors
[I
.second
].second
;