1 //===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a concrete diagnostic client, which buffers the diagnostic messages.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Frontend/TextDiagnosticBuffer.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/ErrorHandling.h"
19
20 using namespace clang;
21
22 /// HandleDiagnostic - Store the errors, warnings, and notes that are
23 /// reported.
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)24 void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
25 const Diagnostic &Info) {
26 // Default implementation (Warnings/errors count).
27 DiagnosticConsumer::HandleDiagnostic(Level, Info);
28
29 SmallString<100> Buf;
30 Info.FormatDiagnostic(Buf);
31 switch (Level) {
32 default: llvm_unreachable(
33 "Diagnostic not handled during diagnostic buffering!");
34 case DiagnosticsEngine::Note:
35 All.emplace_back(Level, Notes.size());
36 Notes.emplace_back(Info.getLocation(), Buf.str());
37 break;
38 case DiagnosticsEngine::Warning:
39 All.emplace_back(Level, Warnings.size());
40 Warnings.emplace_back(Info.getLocation(), Buf.str());
41 break;
42 case DiagnosticsEngine::Remark:
43 All.emplace_back(Level, Remarks.size());
44 Remarks.emplace_back(Info.getLocation(), Buf.str());
45 break;
46 case DiagnosticsEngine::Error:
47 case DiagnosticsEngine::Fatal:
48 All.emplace_back(Level, Errors.size());
49 Errors.emplace_back(Info.getLocation(), Buf.str());
50 break;
51 }
52 }
53
FlushDiagnostics(DiagnosticsEngine & Diags) const54 void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
55 for (const auto &I : All) {
56 auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
57 switch (I.first) {
58 default: llvm_unreachable(
59 "Diagnostic not handled during diagnostic flushing!");
60 case DiagnosticsEngine::Note:
61 Diag << Notes[I.second].second;
62 break;
63 case DiagnosticsEngine::Warning:
64 Diag << Warnings[I.second].second;
65 break;
66 case DiagnosticsEngine::Remark:
67 Diag << Remarks[I.second].second;
68 break;
69 case DiagnosticsEngine::Error:
70 case DiagnosticsEngine::Fatal:
71 Diag << Errors[I.second].second;
72 break;
73 }
74 }
75 }
76