clang 23.0.0git
SARIFTransformationReportFormat.cpp
Go to the documentation of this file.
1//===- SARIFTransformationReportFormat.cpp --------------------------------===//
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
10#include "clang/Basic/Sarif.h"
11#include "clang/Basic/Version.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/FormatVariadic.h"
16#include "llvm/Support/JSON.h"
17#include "llvm/Support/raw_ostream.h"
18
19using namespace clang;
20using namespace ssaf;
21
23 llvm::StringRef Path) {
24 std::error_code EC;
25 llvm::raw_fd_ostream OS(Path, EC, llvm::sys::fs::OF_None);
26 if (EC)
27 return llvm::createStringError(EC, "failed to open '" + Path + "'");
28
30 std::string LongToolName =
31 "clang ScalableStaticAnalysisFramework source transformation (" +
32 Doc.TransformationName + ")";
33 Writer.createRun("clang-ssaf", LongToolName, CLANG_VERSION_STRING);
34
35 llvm::StringMap<size_t> RuleIndex;
36 for (const ReportResult &R : Doc.Results) {
37 if (RuleIndex.contains(R.RuleId))
38 continue;
39 RuleIndex[R.RuleId] =
40 Writer.createRule(clang::SarifRule::create().setRuleId(R.RuleId));
41 }
42
43 for (const ReportResult &R : Doc.Results) {
45 .setRuleId(R.RuleId)
46 .setDiagnosticMessage(R.Message)
47 .setDiagnosticLevel(R.Level);
48 if (R.Range.isValid())
49 Result = Result.addLocations({R.Range});
50 Writer.appendResult(Result);
51 }
52
53 llvm::json::Value Document(Writer.createDocument());
54 OS << llvm::formatv("{0:2}", Document) << "\n";
55 return llvm::Error::success();
56}
Defines clang::SarifDocumentWriter, clang::SarifRule, clang::SarifResult.
Defines version macros and version-related utility functions for Clang.
This class handles creating a valid SARIF document given various input attributes.
Definition Sarif.h:414
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.
Definition Sarif.cpp:345
size_t createRule(const SarifRule &Rule)
Associate the given rule with the current run.
Definition Sarif.cpp:381
llvm::json::Object createDocument()
Return the SARIF document in its current state.
Definition Sarif.cpp:437
void appendResult(const SarifResult &SarifResult)
Append a new result to the currently in-flight run.
Definition Sarif.cpp:387
A SARIF result (also called a "reporting item") is a unit of output produced when one of the tool's r...
Definition Sarif.h:322
SarifResult setDiagnosticMessage(llvm::StringRef Message)
Definition Sarif.h:355
SarifResult setRuleId(llvm::StringRef Id)
Definition Sarif.h:350
SarifResult setDiagnosticLevel(const SarifResultLevel &TheLevel)
Definition Sarif.h:393
static SarifResult create(uint32_t RuleIdx)
Definition Sarif.h:343
static SarifRule create()
Definition Sarif.h:270
llvm::Error writeSARIFTransformationReport(const ReportDocument &Doc, llvm::StringRef Path)
Writes Doc to Path as a SARIF JSON document.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Definition TypeBase.h:905