SARIFDiagnosticPrinter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- SARIFDiagnosticPrinter.h - SARIF Diagnostic Client -------*- C++-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This is a concrete diagnostic client, which prints the diagnostics to
  15. // standard error in SARIF format.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
  19. #define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/LLVM.h"
  22. #include "clang/Basic/Sarif.h"
  23. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include <memory>
  26. namespace clang {
  27. class DiagnosticOptions;
  28. class LangOptions;
  29. class SARIFDiagnostic;
  30. class SarifDocumentWriter;
  31. class SARIFDiagnosticPrinter : public DiagnosticConsumer {
  32. public:
  33. SARIFDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions *Diags);
  34. ~SARIFDiagnosticPrinter() = default;
  35. SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &&) = delete;
  36. SARIFDiagnosticPrinter(SARIFDiagnosticPrinter &&) = delete;
  37. SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &) = delete;
  38. SARIFDiagnosticPrinter(const SARIFDiagnosticPrinter &) = delete;
  39. /// setPrefix - Set the diagnostic printer prefix string, which will be
  40. /// printed at the start of any diagnostics. If empty, no prefix string is
  41. /// used.
  42. void setPrefix(llvm::StringRef Value) { Prefix = Value; }
  43. bool hasSarifWriter() const { return Writer != nullptr; }
  44. SarifDocumentWriter &getSarifWriter() const {
  45. assert(Writer && "SarifWriter not set!");
  46. return *Writer;
  47. }
  48. void setSarifWriter(std::unique_ptr<SarifDocumentWriter> SarifWriter) {
  49. Writer = std::move(SarifWriter);
  50. }
  51. void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override;
  52. void EndSourceFile() override;
  53. void HandleDiagnostic(DiagnosticsEngine::Level Level,
  54. const Diagnostic &Info) override;
  55. private:
  56. raw_ostream &OS;
  57. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
  58. /// Handle to the currently active SARIF diagnostic emitter.
  59. std::unique_ptr<SARIFDiagnostic> SARIFDiag;
  60. /// A string to prefix to error messages.
  61. std::string Prefix;
  62. std::unique_ptr<SarifDocumentWriter> Writer;
  63. };
  64. } // end namespace clang
  65. #endif
  66. #ifdef __GNUC__
  67. #pragma GCC diagnostic pop
  68. #endif