1 //===--- SARIFDiagnostic.h - SARIF Diagnostic Formatting -----*- 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 utility class that provides support for constructing a SARIF object
10 // containing diagnostics.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTIC_H
15 #define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTIC_H
16 
17 #include "clang/Basic/Sarif.h"
18 #include "clang/Frontend/DiagnosticRenderer.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace clang {
22 
23 class SARIFDiagnostic : public DiagnosticRenderer {
24 public:
25   SARIFDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
26                   DiagnosticOptions *DiagOpts, SarifDocumentWriter *Writer);
27 
28   ~SARIFDiagnostic() = default;
29 
30   SARIFDiagnostic &operator=(const SARIFDiagnostic &&) = delete;
31   SARIFDiagnostic(SARIFDiagnostic &&) = delete;
32   SARIFDiagnostic &operator=(const SARIFDiagnostic &) = delete;
33   SARIFDiagnostic(const SARIFDiagnostic &) = delete;
34 
35 protected:
36   void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
37                              DiagnosticsEngine::Level Level, StringRef Message,
38                              ArrayRef<CharSourceRange> Ranges,
39                              DiagOrStoredDiag D) override;
40 
41   void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
42                          DiagnosticsEngine::Level Level,
43                          ArrayRef<CharSourceRange> Ranges) override;
44 
45   void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
46                        SmallVectorImpl<CharSourceRange> &Ranges,
47                        ArrayRef<FixItHint> Hints) override {}
48 
49   void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
50 
51   void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
52                           StringRef ModuleName) override;
53 
54   void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
55                                   StringRef ModuleName) override;
56 
57 private:
58   // Shared between SARIFDiagnosticPrinter and this renderer.
59   SarifDocumentWriter *Writer;
60 
61   SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
62                                   PresumedLoc PLoc,
63                                   ArrayRef<CharSourceRange> Ranges,
64                                   const Diagnostic &Diag);
65 
66   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
67                                      DiagnosticsEngine::Level Level);
68 
69   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
70 };
71 
72 } // end namespace clang
73 
74 #endif
75