TextDiagnosticBuffer.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "clang/Frontend/TextDiagnosticBuffer.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Basic/LLVM.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. using namespace clang;
  18. /// HandleDiagnostic - Store the errors, warnings, and notes that are
  19. /// reported.
  20. void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
  21. const Diagnostic &Info) {
  22. // Default implementation (Warnings/errors count).
  23. DiagnosticConsumer::HandleDiagnostic(Level, Info);
  24. SmallString<100> Buf;
  25. Info.FormatDiagnostic(Buf);
  26. switch (Level) {
  27. default: llvm_unreachable(
  28. "Diagnostic not handled during diagnostic buffering!");
  29. case DiagnosticsEngine::Note:
  30. All.emplace_back(Level, Notes.size());
  31. Notes.emplace_back(Info.getLocation(), std::string(Buf.str()));
  32. break;
  33. case DiagnosticsEngine::Warning:
  34. All.emplace_back(Level, Warnings.size());
  35. Warnings.emplace_back(Info.getLocation(), std::string(Buf.str()));
  36. break;
  37. case DiagnosticsEngine::Remark:
  38. All.emplace_back(Level, Remarks.size());
  39. Remarks.emplace_back(Info.getLocation(), std::string(Buf.str()));
  40. break;
  41. case DiagnosticsEngine::Error:
  42. case DiagnosticsEngine::Fatal:
  43. All.emplace_back(Level, Errors.size());
  44. Errors.emplace_back(Info.getLocation(), std::string(Buf.str()));
  45. break;
  46. }
  47. }
  48. void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
  49. for (const auto &I : All) {
  50. auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
  51. switch (I.first) {
  52. default: llvm_unreachable(
  53. "Diagnostic not handled during diagnostic flushing!");
  54. case DiagnosticsEngine::Note:
  55. Diag << Notes[I.second].second;
  56. break;
  57. case DiagnosticsEngine::Warning:
  58. Diag << Warnings[I.second].second;
  59. break;
  60. case DiagnosticsEngine::Remark:
  61. Diag << Remarks[I.second].second;
  62. break;
  63. case DiagnosticsEngine::Error:
  64. case DiagnosticsEngine::Fatal:
  65. Diag << Errors[I.second].second;
  66. break;
  67. }
  68. }
  69. }