clang  16.0.0git
SARIFDiagnosticPrinter.cpp
Go to the documentation of this file.
1 //===------- SARIFDiagnosticPrinter.cpp - Diagnostic Printer---------------===//
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 diagnostic client prints out their diagnostic messages in SARIF format.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 #include "clang/Basic/Sarif.h"
19 #include "clang/Lex/Lexer.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/JSON.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 
26 namespace clang {
27 
29  DiagnosticOptions *Diags)
30  : OS(OS), DiagOpts(Diags) {}
31 
33  const Preprocessor *PP) {
34  // Build the SARIFDiagnostic utility.
35  assert(hasSarifWriter() && "Writer not set!");
36  assert(!SARIFDiag && "SARIFDiagnostic already set.");
37  SARIFDiag = std::make_unique<SARIFDiagnostic>(OS, LO, &*DiagOpts, &*Writer);
38  // Initialize the SARIF object.
39  Writer->createRun("clang", Prefix);
40 }
41 
43  assert(SARIFDiag && "SARIFDiagnostic has not been set.");
44  Writer->endRun();
45  llvm::json::Value Value(Writer->createDocument());
46  OS << "\n" << Value << "\n\n";
47  OS.flush();
48  SARIFDiag.reset();
49 }
50 
52  const Diagnostic &Info) {
53  assert(SARIFDiag && "SARIFDiagnostic has not been set.");
54  // Default implementation (Warnings/errors count). Keeps track of the
55  // number of errors.
57 
58  // Render the diagnostic message into a temporary buffer eagerly. We'll use
59  // this later as we add the diagnostic to the SARIF object.
60  SmallString<100> OutStr;
61  Info.FormatDiagnostic(OutStr);
62 
63  llvm::raw_svector_ostream DiagMessageStream(OutStr);
64 
65  // Use a dedicated, simpler path for diagnostics without a valid location.
66  // This is important as if the location is missing, we may be emitting
67  // diagnostics in a context that lacks language options, a source manager, or
68  // other infrastructure necessary when emitting more rich diagnostics.
69  if (Info.getLocation().isInvalid()) {
70  // FIXME: Enable diagnostics without a source manager
71  return;
72  }
73 
74  // Assert that the rest of our infrastructure is setup properly.
75  assert(DiagOpts && "Unexpected diagnostic without options set");
76  assert(Info.hasSourceManager() &&
77  "Unexpected diagnostic with no source manager");
78 
79  SARIFDiag->emitDiagnostic(
81  DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints(), &Info);
82 }
83 } // namespace clang
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:195
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:368
SourceManager.h
SARIFDiagnostic.h
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1561
SARIFDiagnosticPrinter.h
clang::SARIFDiagnosticPrinter::BeginSourceFile
void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: SARIFDiagnosticPrinter.cpp:32
clang::DiagnosticConsumer::HandleDiagnostic
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:564
clang::SARIFDiagnosticPrinter::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info) override
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: SARIFDiagnosticPrinter.cpp:51
llvm::SmallString
Definition: LLVM.h:37
clang::SARIFDiagnosticPrinter::hasSarifWriter
bool hasSarifWriter() const
Definition: SARIFDiagnosticPrinter.h:45
clang::Diagnostic::getLocation
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1572
clang::SARIFDiagnosticPrinter::SARIFDiagnosticPrinter
SARIFDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions *Diags)
Definition: SARIFDiagnosticPrinter.cpp:28
Lexer.h
Value
Value
Definition: UninitializedValues.cpp:103
clang::Diagnostic::getFixItHints
ArrayRef< FixItHint > getFixItHints() const
Definition: Diagnostic.h:1665
clang::Diagnostic::getSourceManager
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1574
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:111
clang
Definition: CalledOnceCheck.h:17
clang::Diagnostic::getRanges
ArrayRef< CharSourceRange > getRanges() const
Return an array reference for this diagnostic's ranges.
Definition: Diagnostic.h:1652
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
Sarif.h
DiagnosticOptions.h
clang::Diagnostic::hasSourceManager
bool hasSourceManager() const
Definition: Diagnostic.h:1573
DiagnosticRenderer.h
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:129
clang::Diagnostic::FormatDiagnostic
void FormatDiagnostic(SmallVectorImpl< char > &OutStr) const
Format this diagnostic into a string, substituting the formal arguments into the %0 slots.
Definition: Diagnostic.cpp:795
clang::SARIFDiagnosticPrinter::EndSourceFile
void EndSourceFile() override
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: SARIFDiagnosticPrinter.cpp:42
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70