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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
15 //===----------------------------------------------------------------------===//
17 #include "flang/Frontend/TextDiagnosticBuffer.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/ErrorHandling.h"
22 using namespace Fortran::frontend
;
24 /// HandleDiagnostic - Store the errors, warnings, and notes that are
26 void TextDiagnosticBuffer::HandleDiagnostic(
27 clang::DiagnosticsEngine::Level level
, const clang::Diagnostic
&info
) {
28 // Default implementation (warnings_/errors count).
29 DiagnosticConsumer::HandleDiagnostic(level
, info
);
31 llvm::SmallString
<100> buf
;
32 info
.FormatDiagnostic(buf
);
35 llvm_unreachable("Diagnostic not handled during diagnostic buffering!");
36 case clang::DiagnosticsEngine::Note
:
37 all
.emplace_back(level
, notes
.size());
38 notes
.emplace_back(info
.getLocation(), std::string(buf
));
40 case clang::DiagnosticsEngine::Warning
:
41 all
.emplace_back(level
, warnings
.size());
42 warnings
.emplace_back(info
.getLocation(), std::string(buf
));
44 case clang::DiagnosticsEngine::Remark
:
45 all
.emplace_back(level
, remarks
.size());
46 remarks
.emplace_back(info
.getLocation(), std::string(buf
));
48 case clang::DiagnosticsEngine::Error
:
49 case clang::DiagnosticsEngine::Fatal
:
50 all
.emplace_back(level
, errors
.size());
51 errors
.emplace_back(info
.getLocation(), std::string(buf
));
56 void TextDiagnosticBuffer::flushDiagnostics(
57 clang::DiagnosticsEngine
&diags
) const {
58 for (const auto &i
: all
) {
59 auto diag
= diags
.Report(diags
.getCustomDiagID(i
.first
, "%0"));
62 llvm_unreachable("Diagnostic not handled during diagnostic flushing!");
63 case clang::DiagnosticsEngine::Note
:
64 diag
<< notes
[i
.second
].second
;
66 case clang::DiagnosticsEngine::Warning
:
67 diag
<< warnings
[i
.second
].second
;
69 case clang::DiagnosticsEngine::Remark
:
70 diag
<< remarks
[i
.second
].second
;
72 case clang::DiagnosticsEngine::Error
:
73 case clang::DiagnosticsEngine::Fatal
:
74 diag
<< errors
[i
.second
].second
;