1341825Sdim//===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
2193326Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193326Sed//
7193326Sed//===----------------------------------------------------------------------===//
8193326Sed//
9193326Sed// This is a concrete diagnostic client, which buffers the diagnostic messages.
10193326Sed//
11193326Sed//===----------------------------------------------------------------------===//
12193326Sed
13193326Sed#include "clang/Frontend/TextDiagnosticBuffer.h"
14341825Sdim#include "clang/Basic/Diagnostic.h"
15341825Sdim#include "clang/Basic/LLVM.h"
16193326Sed#include "llvm/ADT/SmallString.h"
17226633Sdim#include "llvm/Support/ErrorHandling.h"
18341825Sdim
19193326Sedusing namespace clang;
20193326Sed
21193326Sed/// HandleDiagnostic - Store the errors, warnings, and notes that are
22193326Sed/// reported.
23226633Sdimvoid TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
24226633Sdim                                            const Diagnostic &Info) {
25218893Sdim  // Default implementation (Warnings/errors count).
26226633Sdim  DiagnosticConsumer::HandleDiagnostic(Level, Info);
27218893Sdim
28234353Sdim  SmallString<100> Buf;
29199990Srdivacky  Info.FormatDiagnostic(Buf);
30193326Sed  switch (Level) {
31226633Sdim  default: llvm_unreachable(
32226633Sdim                         "Diagnostic not handled during diagnostic buffering!");
33226633Sdim  case DiagnosticsEngine::Note:
34327952Sdim    All.emplace_back(Level, Notes.size());
35288943Sdim    Notes.emplace_back(Info.getLocation(), Buf.str());
36193326Sed    break;
37226633Sdim  case DiagnosticsEngine::Warning:
38327952Sdim    All.emplace_back(Level, Warnings.size());
39288943Sdim    Warnings.emplace_back(Info.getLocation(), Buf.str());
40193326Sed    break;
41276479Sdim  case DiagnosticsEngine::Remark:
42327952Sdim    All.emplace_back(Level, Remarks.size());
43288943Sdim    Remarks.emplace_back(Info.getLocation(), Buf.str());
44276479Sdim    break;
45226633Sdim  case DiagnosticsEngine::Error:
46226633Sdim  case DiagnosticsEngine::Fatal:
47327952Sdim    All.emplace_back(Level, Errors.size());
48288943Sdim    Errors.emplace_back(Info.getLocation(), Buf.str());
49193326Sed    break;
50193326Sed  }
51193326Sed}
52199990Srdivacky
53226633Sdimvoid TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
54341825Sdim  for (const auto &I : All) {
55341825Sdim    auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
56341825Sdim    switch (I.first) {
57327952Sdim    default: llvm_unreachable(
58327952Sdim                           "Diagnostic not handled during diagnostic flushing!");
59327952Sdim    case DiagnosticsEngine::Note:
60341825Sdim      Diag << Notes[I.second].second;
61327952Sdim      break;
62327952Sdim    case DiagnosticsEngine::Warning:
63341825Sdim      Diag << Warnings[I.second].second;
64327952Sdim      break;
65327952Sdim    case DiagnosticsEngine::Remark:
66341825Sdim      Diag << Remarks[I.second].second;
67327952Sdim      break;
68327952Sdim    case DiagnosticsEngine::Error:
69327952Sdim    case DiagnosticsEngine::Fatal:
70341825Sdim      Diag << Errors[I.second].second;
71327952Sdim      break;
72327952Sdim    }
73327952Sdim  }
74199990Srdivacky}
75