| 1 | //===-- SARIFDiagnosticPrinter.h - SARIF Diagnostic Client -------*- C++-*-===// |
|---|---|
| 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 prints the diagnostics to |
| 10 | // standard error in SARIF format. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H |
| 15 | #define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H |
| 16 | |
| 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "clang/Basic/Sarif.h" |
| 20 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include <memory> |
| 23 | |
| 24 | namespace clang { |
| 25 | class DiagnosticOptions; |
| 26 | class LangOptions; |
| 27 | class SARIFDiagnostic; |
| 28 | class SarifDocumentWriter; |
| 29 | |
| 30 | class SARIFDiagnosticPrinter : public DiagnosticConsumer { |
| 31 | public: |
| 32 | SARIFDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions &DiagOpts); |
| 33 | ~SARIFDiagnosticPrinter() = default; |
| 34 | |
| 35 | SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &&) = delete; |
| 36 | SARIFDiagnosticPrinter(SARIFDiagnosticPrinter &&) = delete; |
| 37 | SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &) = delete; |
| 38 | SARIFDiagnosticPrinter(const SARIFDiagnosticPrinter &) = delete; |
| 39 | |
| 40 | /// setPrefix - Set the diagnostic printer prefix string, which will be |
| 41 | /// printed at the start of any diagnostics. If empty, no prefix string is |
| 42 | /// used. |
| 43 | void setPrefix(llvm::StringRef Value) { Prefix = Value; } |
| 44 | |
| 45 | bool hasSarifWriter() const { return Writer != nullptr; } |
| 46 | |
| 47 | SarifDocumentWriter &getSarifWriter() const { |
| 48 | assert(Writer && "SarifWriter not set!"); |
| 49 | return *Writer; |
| 50 | } |
| 51 | |
| 52 | void setSarifWriter(std::unique_ptr<SarifDocumentWriter> SarifWriter) { |
| 53 | Writer = std::move(SarifWriter); |
| 54 | } |
| 55 | |
| 56 | void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override; |
| 57 | void EndSourceFile() override; |
| 58 | void HandleDiagnostic(DiagnosticsEngine::Level Level, |
| 59 | const Diagnostic &Info) override; |
| 60 | |
| 61 | private: |
| 62 | raw_ostream &OS; |
| 63 | DiagnosticOptions &DiagOpts; |
| 64 | |
| 65 | /// Handle to the currently active SARIF diagnostic emitter. |
| 66 | std::unique_ptr<SARIFDiagnostic> SARIFDiag; |
| 67 | |
| 68 | /// A string to prefix to error messages. |
| 69 | std::string Prefix; |
| 70 | |
| 71 | std::unique_ptr<SarifDocumentWriter> Writer; |
| 72 | }; |
| 73 | |
| 74 | } // end namespace clang |
| 75 | |
| 76 | #endif |
| 77 |