Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Frontend/TextDiagnosticBuffer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This is a concrete diagnostic client, which buffers the diagnostic messages.
10
//
11
//===----------------------------------------------------------------------===//
12
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"
18
19
using namespace clang;
20
21
/// HandleDiagnostic - Store the errors, warnings, and notes that are
22
/// reported.
23
void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
24
0
                                            const Diagnostic &Info) {
25
  // Default implementation (Warnings/errors count).
26
0
  DiagnosticConsumer::HandleDiagnostic(Level, Info);
27
28
0
  SmallString<100> Buf;
29
0
  Info.FormatDiagnostic(Buf);
30
0
  switch (Level) {
31
0
  default: llvm_unreachable(
32
0
                         "Diagnostic not handled during diagnostic buffering!");
33
0
  case DiagnosticsEngine::Note:
34
0
    All.emplace_back(Level, Notes.size());
35
0
    Notes.emplace_back(Info.getLocation(), std::string(Buf.str()));
36
0
    break;
37
0
  case DiagnosticsEngine::Warning:
38
0
    All.emplace_back(Level, Warnings.size());
39
0
    Warnings.emplace_back(Info.getLocation(), std::string(Buf.str()));
40
0
    break;
41
0
  case DiagnosticsEngine::Remark:
42
0
    All.emplace_back(Level, Remarks.size());
43
0
    Remarks.emplace_back(Info.getLocation(), std::string(Buf.str()));
44
0
    break;
45
0
  case DiagnosticsEngine::Error:
46
0
  case DiagnosticsEngine::Fatal:
47
0
    All.emplace_back(Level, Errors.size());
48
0
    Errors.emplace_back(Info.getLocation(), std::string(Buf.str()));
49
0
    break;
50
0
  }
51
0
}
52
53
0
void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
54
0
  for (const auto &I : All) {
55
0
    auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
56
0
    switch (I.first) {
57
0
    default: llvm_unreachable(
58
0
                           "Diagnostic not handled during diagnostic flushing!");
59
0
    case DiagnosticsEngine::Note:
60
0
      Diag << Notes[I.second].second;
61
0
      break;
62
0
    case DiagnosticsEngine::Warning:
63
0
      Diag << Warnings[I.second].second;
64
0
      break;
65
0
    case DiagnosticsEngine::Remark:
66
0
      Diag << Remarks[I.second].second;
67
0
      break;
68
0
    case DiagnosticsEngine::Error:
69
0
    case DiagnosticsEngine::Fatal:
70
0
      Diag << Errors[I.second].second;
71
0
      break;
72
0
    }
73
0
  }
74
0
}