Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===-- SARIFDiagnosticPrinter.h - SARIF Diagnostic Client -------*- C++-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This is a concrete diagnostic client, which prints the diagnostics to
0010 // standard error in SARIF format.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
0015 #define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
0016 
0017 #include "clang/Basic/Diagnostic.h"
0018 #include "clang/Basic/LLVM.h"
0019 #include "clang/Basic/Sarif.h"
0020 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0021 #include "llvm/ADT/StringRef.h"
0022 #include <memory>
0023 
0024 namespace clang {
0025 class DiagnosticOptions;
0026 class LangOptions;
0027 class SARIFDiagnostic;
0028 class SarifDocumentWriter;
0029 
0030 class SARIFDiagnosticPrinter : public DiagnosticConsumer {
0031 public:
0032   SARIFDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions *Diags);
0033   ~SARIFDiagnosticPrinter() = default;
0034 
0035   SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &&) = delete;
0036   SARIFDiagnosticPrinter(SARIFDiagnosticPrinter &&) = delete;
0037   SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &) = delete;
0038   SARIFDiagnosticPrinter(const SARIFDiagnosticPrinter &) = delete;
0039 
0040   /// setPrefix - Set the diagnostic printer prefix string, which will be
0041   /// printed at the start of any diagnostics. If empty, no prefix string is
0042   /// used.
0043   void setPrefix(llvm::StringRef Value) { Prefix = Value; }
0044 
0045   bool hasSarifWriter() const { return Writer != nullptr; }
0046 
0047   SarifDocumentWriter &getSarifWriter() const {
0048     assert(Writer && "SarifWriter not set!");
0049     return *Writer;
0050   }
0051 
0052   void setSarifWriter(std::unique_ptr<SarifDocumentWriter> SarifWriter) {
0053     Writer = std::move(SarifWriter);
0054   }
0055 
0056   void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override;
0057   void EndSourceFile() override;
0058   void HandleDiagnostic(DiagnosticsEngine::Level Level,
0059                         const Diagnostic &Info) override;
0060 
0061 private:
0062   raw_ostream &OS;
0063   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
0064 
0065   /// Handle to the currently active SARIF diagnostic emitter.
0066   std::unique_ptr<SARIFDiagnostic> SARIFDiag;
0067 
0068   /// A string to prefix to error messages.
0069   std::string Prefix;
0070 
0071   std::unique_ptr<SarifDocumentWriter> Writer;
0072 };
0073 
0074 } // end namespace clang
0075 
0076 #endif