xref: /aosp_15_r20/external/clang/lib/Frontend/TextDiagnosticBuffer.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This is a concrete diagnostic client, which buffers the diagnostic messages.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li 
14*67e74705SXin Li #include "clang/Frontend/TextDiagnosticBuffer.h"
15*67e74705SXin Li #include "llvm/ADT/SmallString.h"
16*67e74705SXin Li #include "llvm/Support/ErrorHandling.h"
17*67e74705SXin Li using namespace clang;
18*67e74705SXin Li 
19*67e74705SXin Li /// HandleDiagnostic - Store the errors, warnings, and notes that are
20*67e74705SXin Li /// reported.
21*67e74705SXin Li ///
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)22*67e74705SXin Li void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
23*67e74705SXin Li                                             const Diagnostic &Info) {
24*67e74705SXin Li   // Default implementation (Warnings/errors count).
25*67e74705SXin Li   DiagnosticConsumer::HandleDiagnostic(Level, Info);
26*67e74705SXin Li 
27*67e74705SXin Li   SmallString<100> Buf;
28*67e74705SXin Li   Info.FormatDiagnostic(Buf);
29*67e74705SXin Li   switch (Level) {
30*67e74705SXin Li   default: llvm_unreachable(
31*67e74705SXin Li                          "Diagnostic not handled during diagnostic buffering!");
32*67e74705SXin Li   case DiagnosticsEngine::Note:
33*67e74705SXin Li     Notes.emplace_back(Info.getLocation(), Buf.str());
34*67e74705SXin Li     break;
35*67e74705SXin Li   case DiagnosticsEngine::Warning:
36*67e74705SXin Li     Warnings.emplace_back(Info.getLocation(), Buf.str());
37*67e74705SXin Li     break;
38*67e74705SXin Li   case DiagnosticsEngine::Remark:
39*67e74705SXin Li     Remarks.emplace_back(Info.getLocation(), Buf.str());
40*67e74705SXin Li     break;
41*67e74705SXin Li   case DiagnosticsEngine::Error:
42*67e74705SXin Li   case DiagnosticsEngine::Fatal:
43*67e74705SXin Li     Errors.emplace_back(Info.getLocation(), Buf.str());
44*67e74705SXin Li     break;
45*67e74705SXin Li   }
46*67e74705SXin Li }
47*67e74705SXin Li 
FlushDiagnostics(DiagnosticsEngine & Diags) const48*67e74705SXin Li void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
49*67e74705SXin Li   // FIXME: Flush the diagnostics in order.
50*67e74705SXin Li   for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
51*67e74705SXin Li     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
52*67e74705SXin Li         << it->second;
53*67e74705SXin Li   for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
54*67e74705SXin Li     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning, "%0"))
55*67e74705SXin Li         << it->second;
56*67e74705SXin Li   for (const_iterator it = remark_begin(), ie = remark_end(); it != ie; ++it)
57*67e74705SXin Li     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Remark, "%0"))
58*67e74705SXin Li         << it->second;
59*67e74705SXin Li   for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
60*67e74705SXin Li     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
61*67e74705SXin Li         << it->second;
62*67e74705SXin Li }
63*67e74705SXin Li 
64