clang 20.0.0git
Public Member Functions | List of all members
clang::SarifDocumentWriter Class Reference

This class handles creating a valid SARIF document given various input attributes. More...

#include "clang/Basic/Sarif.h"

Public Member Functions

 SarifDocumentWriter ()=delete
 
 SarifDocumentWriter (const SourceManager &SourceMgr)
 Create a new empty SARIF document with the given source manager.
 
 ~SarifDocumentWriter ()=default
 Release resources held by this SARIF document.
 
void createRun (const llvm::StringRef ShortToolName, const llvm::StringRef LongToolName, const llvm::StringRef ToolVersion=CLANG_VERSION_STRING)
 Create a new run with which any upcoming analysis will be associated.
 
void endRun ()
 If there is a current run, end it.
 
size_t createRule (const SarifRule &Rule)
 Associate the given rule with the current run.
 
void appendResult (const SarifResult &SarifResult)
 Append a new result to the currently in-flight run.
 
llvm::json::Object createDocument ()
 Return the SARIF document in its current state.
 

Detailed Description

This class handles creating a valid SARIF document given various input attributes.

However, it requires an ordering among certain method calls:

  1. Because every SARIF document must contain at least 1 run, callers must ensure that SarifDocumentWriter::createRun is called before any other methods.
  2. If SarifDocumentWriter::endRun is called, callers MUST call SarifDocumentWriter::createRun, before invoking any of the result aggregation methods such as SarifDocumentWriter::appendResult etc.

Definition at line 380 of file Sarif.h.

Constructor & Destructor Documentation

◆ SarifDocumentWriter() [1/2]

clang::SarifDocumentWriter::SarifDocumentWriter ( )
delete

◆ SarifDocumentWriter() [2/2]

clang::SarifDocumentWriter::SarifDocumentWriter ( const SourceManager SourceMgr)
inline

Create a new empty SARIF document with the given source manager.

Definition at line 429 of file Sarif.h.

◆ ~SarifDocumentWriter()

clang::SarifDocumentWriter::~SarifDocumentWriter ( )
default

Release resources held by this SARIF document.

Member Function Documentation

◆ appendResult()

void SarifDocumentWriter::appendResult ( const SarifResult SarifResult)

Append a new result to the currently in-flight run.

Precondition
There must be a run associated with the document, failing to do so will cause undefined behaviour.
RuleIdx used to create the result must correspond to a rule known by the SARIF document. It must be the value returned by a previous call to createRule.

Definition at line 385 of file Sarif.cpp.

References createLocation(), createMessage(), Range, clang::Result, and resultLevelToStr().

Referenced by clang::SARIFDiagnostic::emitDiagnosticMessage().

◆ createDocument()

json::Object SarifDocumentWriter::createDocument ( )

Return the SARIF document in its current state.

Calling this will trigger a copy of the internal state including all reported diagnostics, resulting in an expensive call.

Definition at line 413 of file Sarif.cpp.

References endRun().

◆ createRule()

size_t SarifDocumentWriter::createRule ( const SarifRule Rule)

Associate the given rule with the current run.

Returns an integer rule index for the created rule that is unique within the current run, which can then be used to create a SarifResult to add to the current run. Note that a rule must exist before being referenced by a result.

Precondition
There must be a run associated with the document, failing to do so will cause undefined behaviour.

Definition at line 379 of file Sarif.cpp.

Referenced by createRuleMapping(), and clang::SARIFDiagnostic::emitDiagnosticMessage().

◆ createRun()

void SarifDocumentWriter::createRun ( const llvm::StringRef  ShortToolName,
const llvm::StringRef  LongToolName,
const llvm::StringRef  ToolVersion = CLANG_VERSION_STRING 
)

Create a new run with which any upcoming analysis will be associated.

Each run requires specifying the tool that is generating reporting items.

Definition at line 343 of file Sarif.cpp.

References endRun().

◆ endRun()

void SarifDocumentWriter::endRun ( )

If there is a current run, end it.

This method collects various book-keeping required to clear and close resources associated with the current run, but may also allocate some for the next run.

Calling endRun before associating a run through createRun leads to undefined behaviour.

Definition at line 261 of file Sarif.cpp.

References clang::if(), Loc, resultLevelToStr(), and V.

Referenced by createDocument(), and createRun().


The documentation for this class was generated from the following files: