diff --git a/CMakeLists.txt b/CMakeLists.txt index 0294463..098c145 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ endif() add_subdirectory(core) add_subdirectory(coro) add_subdirectory(graph) +add_subdirectory(sarif) option(GAP_ENABLE_MLIR "Enable MLIR support" OFF) @@ -167,6 +168,7 @@ if (GAP_INSTALL) install_gap_target(gap-settings core) install_gap_target(gap-coro coro) install_gap_target(gap-graph graph) + install_gap_target(gap-sarif sarif) if (${GAP_ENABLE_MLIR}) install_gap_target(gap-mlir mlir) diff --git a/sarif/CMakeLists.txt b/sarif/CMakeLists.txt new file mode 100644 index 0000000..00ec981 --- /dev/null +++ b/sarif/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2024-present, Trail of Bits, Inc. All rights reserved. + +find_package(nlohmann_json CONFIG REQUIRED) + +add_headers(sarif GAP_SARIF_HEADERS + sarif.hpp +) + +add_sources(sarif GAP_SARIF_SOURCES + sarif.cpp +) + +add_gap_static_library(gap-sarif "${GAP_SARIF_HEADERS}" "${GAP_SARIF_SOURCES}") +target_link_libraries(gap-sarif PUBLIC nlohmann_json::nlohmann_json) \ No newline at end of file diff --git a/sarif/include/gap/sarif/sarif.hpp b/sarif/include/gap/sarif/sarif.hpp new file mode 100644 index 0000000..1cc37ea --- /dev/null +++ b/sarif/include/gap/sarif/sarif.hpp @@ -0,0 +1,2699 @@ +// Copyright 2024-present, Trail of Bits, Inc. + +#pragma once + +// +// These definitions were generated from a JSON Schema description of SARIF +// found at https://github.com/microsoft/sarif-python-om/blob/7a84e8c2b2b9d8b9a8d25b1d376f039a0bf92a7c/sarif-schema-2.1.0.json +// +// The naming convention used is to convert all of the definition names from camelCase to snake_case, +// property names are kept as-is. +// +// Enum value names are converted from camelCase to PascalCase and prefixed with `k`. +// + +#include +#include +#include +#include +#include + +#include + +namespace gap::sarif +{ + using json = nlohmann::json; + + enum class version { + k2_1_0, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(version, { + { version::k2_1_0, "2.1.0" }, + }) + + // + // Key/value pairs that provide additional information about the object. + // + struct property_bag { + // + // A set of distinct strings that provide additional information. + // + std::vector< std::string > tags = {}; + + json additional_properties; + }; + + void to_json(json &, const property_bag &); + void from_json(const json &, property_bag &); + + // + // A message string or message format string rendered in multiple formats. + // + struct multiformat_message_string { + // + // A plain text message string or format string. + // + std::string text; + + // + // A Markdown message string or format string. + // + std::optional< std::string > markdown = std::nullopt; + + // + // Key/value pairs that provide additional information about the message. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const multiformat_message_string &); + void from_json(const json &, multiformat_message_string &); + + enum class level { + kNone, + kNote, + kWarning, + kError, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(level, { + { level::kNone, "none" }, + { level::kNote, "note" }, + { level::kWarning, "warning" }, + { level::kError, "error" }, + }) + + // + // Information about a rule or notification that can be configured at runtime. + // + struct reporting_configuration { + // + // Specifies whether the report may be produced during the scan. + // + bool enabled = true; + + // + // Specifies the failure level for the report. + // + ::gap::sarif::level level = ::gap::sarif::level::kWarning; + + // + // Specifies the relative priority of the report. Used for analysis output only. + // + double rank = -1.0; + + // + // Contains configuration information specific to a report. + // + std::optional< property_bag > parameters = std::nullopt; + + // + // Key/value pairs that provide additional information about the reporting configuration. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const reporting_configuration &); + void from_json(const json &, reporting_configuration &); + + // + // Identifies a particular toolComponent object, either the driver or an extension. + // + struct tool_component_reference { + // + // The 'name' property of the referenced toolComponent. + // + std::optional< std::string > name = std::nullopt; + + // + // An index into the referenced toolComponent in tool.extensions. + // + int64_t index = -1; + + // + // The 'guid' property of the referenced toolComponent. + // + std::optional< std::string > guid = std::nullopt; + + // + // Key/value pairs that provide additional information about the toolComponentReference. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const tool_component_reference &); + void from_json(const json &, tool_component_reference &); + + // + // Information about how to locate a relevant reporting descriptor. + // + struct reporting_descriptor_reference { + // + // The id of the descriptor. + // + std::optional< std::string > id = std::nullopt; + + // + // The index into an array of descriptors in toolComponent.ruleDescriptors, toolComponent.notificationDescriptors, or toolComponent.taxonomyDescriptors, depending on context. + // + int64_t index = -1; + + // + // A guid that uniquely identifies the descriptor. + // + std::optional< std::string > guid = std::nullopt; + + // + // A reference used to locate the toolComponent associated with the descriptor. + // + std::optional< tool_component_reference > toolComponent = std::nullopt; + + // + // Key/value pairs that provide additional information about the reporting descriptor reference. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const reporting_descriptor_reference &); + void from_json(const json &, reporting_descriptor_reference &); + + // + // Encapsulates a message intended to be read by the end user. + // + struct message { + // + // A plain text message string. + // + std::optional< std::string > text = std::nullopt; + + // + // A Markdown message string. + // + std::optional< std::string > markdown = std::nullopt; + + // + // The identifier for this message. + // + std::optional< std::string > id = std::nullopt; + + // + // An array of strings to substitute into the message string. + // + std::vector< std::string > arguments = {}; + + // + // Key/value pairs that provide additional information about the message. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const message &); + void from_json(const json &, message &); + + // + // Information about the relation of one reporting descriptor to another. + // + struct reporting_descriptor_relationship { + // + // A reference to the related reporting descriptor. + // + reporting_descriptor_reference target; + + // + // A set of distinct strings that categorize the relationship. Well-known kinds include 'canPrecede', 'canFollow', 'willPrecede', 'willFollow', 'superset', 'subset', 'equal', 'disjoint', 'relevant', and 'incomparable'. + // + std::vector< std::string > kinds = { "relevant" }; + + // + // A description of the reporting descriptor relationship. + // + std::optional< message > description = std::nullopt; + + // + // Key/value pairs that provide additional information about the reporting descriptor reference. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const reporting_descriptor_relationship &); + void from_json(const json &, reporting_descriptor_relationship &); + + // + // Metadata that describes a specific report produced by the tool, as part of the analysis it provides or its runtime reporting. + // + struct reporting_descriptor { + // + // A stable, opaque identifier for the report. + // + std::string id; + + // + // An array of stable, opaque identifiers by which this report was known in some previous version of the analysis tool. + // + std::vector< std::string > deprecatedIds = {}; + + // + // A unique identifer for the reporting descriptor in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // An array of unique identifies in the form of a GUID by which this report was known in some previous version of the analysis tool. + // + std::vector< std::string > deprecatedGuids = {}; + + // + // A report identifier that is understandable to an end user. + // + std::optional< std::string > name = std::nullopt; + + // + // An array of readable identifiers by which this report was known in some previous version of the analysis tool. + // + std::vector< std::string > deprecatedNames = {}; + + // + // A concise description of the report. Should be a single sentence that is understandable when visible space is limited to a single line of text. + // + std::optional< multiformat_message_string > shortDescription = std::nullopt; + + // + // A description of the report. Should, as far as possible, provide details sufficient to enable resolution of any problem indicated by the result. + // + std::optional< multiformat_message_string > fullDescription = std::nullopt; + + // + // A set of name/value pairs with arbitrary names. Each value is a multiformatMessageString object, which holds message strings in plain text and (optionally) Markdown format. The strings can include placeholders, which can be used to construct a message in combination with an arbitrary number of additional string arguments. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > messageStrings = std::nullopt; + + // + // Default reporting configuration information. + // + std::optional< reporting_configuration > defaultConfiguration = std::nullopt; + + // + // A URI where the primary documentation for the report can be found. + // + std::optional< std::string > helpUri = std::nullopt; + + // + // Provides the primary documentation for the report, useful when there is no online documentation. + // + std::optional< multiformat_message_string > help = std::nullopt; + + // + // An array of objects that describe relationships between this reporting descriptor and others. + // + std::vector< reporting_descriptor_relationship > relationships = {}; + + // + // Key/value pairs that provide additional information about the report. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const reporting_descriptor &); + void from_json(const json &, reporting_descriptor &); + + // + // Specifies the location of an artifact. + // + struct artifact_location { + // + // A string containing a valid relative or absolute URI. + // + std::optional< std::string > uri = std::nullopt; + + // + // A string which indirectly specifies the absolute URI with respect to which a relative URI in the "uri" property is interpreted. + // + std::optional< std::string > uriBaseId = std::nullopt; + + // + // The index within the run artifacts array of the artifact object associated with the artifact location. + // + int64_t index = -1; + + // + // A short description of the artifact location. + // + std::optional< message > description = std::nullopt; + + // + // Key/value pairs that provide additional information about the artifact location. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const artifact_location &); + void from_json(const json &, artifact_location &); + + enum class contents { + kLocalizedData, + kNonLocalizedData, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(contents, { + { contents::kLocalizedData, "localizedData" }, + { contents::kNonLocalizedData, "nonLocalizedData" }, + }) + + // + // Provides additional metadata related to translation. + // + struct translation_metadata { + // + // The name associated with the translation metadata. + // + std::string name; + + // + // The full name associated with the translation metadata. + // + std::optional< std::string > fullName = std::nullopt; + + // + // A brief description of the translation metadata. + // + std::optional< multiformat_message_string > shortDescription = std::nullopt; + + // + // A comprehensive description of the translation metadata. + // + std::optional< multiformat_message_string > fullDescription = std::nullopt; + + // + // The absolute URI from which the translation metadata can be downloaded. + // + std::optional< std::string > downloadUri = std::nullopt; + + // + // The absolute URI from which information related to the translation metadata can be downloaded. + // + std::optional< std::string > informationUri = std::nullopt; + + // + // Key/value pairs that provide additional information about the translation metadata. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const translation_metadata &); + void from_json(const json &, translation_metadata &); + + // + // A component, such as a plug-in or the driver, of the analysis tool that was run. + // + struct tool_component { + // + // A unique identifer for the tool component in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // The name of the tool component. + // + std::string name; + + // + // The organization or company that produced the tool component. + // + std::optional< std::string > organization = std::nullopt; + + // + // A product suite to which the tool component belongs. + // + std::optional< std::string > product = std::nullopt; + + // + // A localizable string containing the name of the suite of products to which the tool component belongs. + // + std::optional< std::string > productSuite = std::nullopt; + + // + // A brief description of the tool component. + // + std::optional< multiformat_message_string > shortDescription = std::nullopt; + + // + // A comprehensive description of the tool component. + // + std::optional< multiformat_message_string > fullDescription = std::nullopt; + + // + // The name of the tool component along with its version and any other useful identifying information, such as its locale. + // + std::optional< std::string > fullName = std::nullopt; + + // + // The tool component version, in whatever format the component natively provides. + // + std::optional< std::string > version = std::nullopt; + + // + // The tool component version in the format specified by Semantic Versioning 2.0. + // + std::optional< std::string > semanticVersion = std::nullopt; + + // + // The binary version of the tool component's primary executable file expressed as four non-negative integers separated by a period (for operating systems that express file versions in this way). + // + std::optional< std::string > dottedQuadFileVersion = std::nullopt; + + // + // A string specifying the UTC date (and optionally, the time) of the component's release. + // + std::optional< std::string > releaseDateUtc = std::nullopt; + + // + // The absolute URI from which the tool component can be downloaded. + // + std::optional< std::string > downloadUri = std::nullopt; + + // + // The absolute URI at which information about this version of the tool component can be found. + // + std::optional< std::string > informationUri = std::nullopt; + + // + // A dictionary, each of whose keys is a resource identifier and each of whose values is a multiformatMessageString object, which holds message strings in plain text and (optionally) Markdown format. The strings can include placeholders, which can be used to construct a message in combination with an arbitrary number of additional string arguments. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > globalMessageStrings = std::nullopt; + + // + // An array of reportingDescriptor objects relevant to the notifications related to the configuration and runtime execution of the tool component. + // + std::vector< reporting_descriptor > notifications = {}; + + // + // An array of reportingDescriptor objects relevant to the analysis performed by the tool component. + // + std::vector< reporting_descriptor > rules = {}; + + // + // An array of reportingDescriptor objects relevant to the definitions of both standalone and tool-defined taxonomies. + // + std::vector< reporting_descriptor > taxa = {}; + + // + // An array of the artifactLocation objects associated with the tool component. + // + std::vector< artifact_location > locations = {}; + + // + // The language of the messages emitted into the log file during this run (expressed as an ISO 639-1 two-letter lowercase language code) and an optional region (expressed as an ISO 3166-1 two-letter uppercase subculture code associated with a country or region). The casing is recommended but not required (in order for this data to conform to RFC5646). + // + std::string language = "en-US"; + + // + // The kinds of data contained in this object. + // + std::vector< ::gap::sarif::contents > contents = { ::gap::sarif::contents::kLocalizedData, ::gap::sarif::contents::kNonLocalizedData }; + + // + // Specifies whether this object contains a complete definition of the localizable and/or non-localizable data for this component, as opposed to including only data that is relevant to the results persisted to this log file. + // + bool isComprehensive = false; + + // + // The semantic version of the localized strings defined in this component; maintained by components that provide translations. + // + std::optional< std::string > localizedDataSemanticVersion = std::nullopt; + + // + // The minimum value of localizedDataSemanticVersion required in translations consumed by this component; used by components that consume translations. + // + std::optional< std::string > minimumRequiredLocalizedDataSemanticVersion = std::nullopt; + + // + // The component which is strongly associated with this component. For a translation, this refers to the component which has been translated. For an extension, this is the driver that provides the extension's plugin model. + // + std::optional< tool_component_reference > associatedComponent = std::nullopt; + + // + // Translation metadata, required for a translation, not populated by other component types. + // + std::optional< translation_metadata > translationMetadata = std::nullopt; + + // + // An array of toolComponentReference objects to declare the taxonomies supported by the tool component. + // + std::vector< tool_component_reference > supportedTaxonomies = {}; + + // + // Key/value pairs that provide additional information about the tool component. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const tool_component &); + void from_json(const json &, tool_component &); + + // + // The analysis tool that was run. + // + struct tool { + // + // The analysis tool that was run. + // + tool_component driver; + + // + // Tool extensions that contributed to or reconfigured the analysis tool that was run. + // + std::vector< tool_component > extensions = {}; + + // + // Key/value pairs that provide additional information about the tool. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const tool &); + void from_json(const json &, tool &); + + // + // Information about how a specific rule or notification was reconfigured at runtime. + // + struct configuration_override { + // + // Specifies how the rule or notification was configured during the scan. + // + reporting_configuration configuration; + + // + // A reference used to locate the descriptor whose configuration was overridden. + // + reporting_descriptor_reference descriptor; + + // + // Key/value pairs that provide additional information about the configuration override. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const configuration_override &); + void from_json(const json &, configuration_override &); + + // + // A physical or virtual address, or a range of addresses, in an 'addressable region' (memory or a binary file). + // + struct address { + // + // The address expressed as a byte offset from the start of the addressable region. + // + int64_t absoluteAddress = -1; + + // + // The address expressed as a byte offset from the absolute address of the top-most parent object. + // + std::optional< int64_t > relativeAddress = std::nullopt; + + // + // The number of bytes in this range of addresses. + // + std::optional< int64_t > length = std::nullopt; + + // + // An open-ended string that identifies the address kind. 'data', 'function', 'header','instruction', 'module', 'page', 'section', 'segment', 'stack', 'stackFrame', 'table' are well-known values. + // + std::optional< std::string > kind = std::nullopt; + + // + // A name that is associated with the address, e.g., '.text'. + // + std::optional< std::string > name = std::nullopt; + + // + // A human-readable fully qualified name that is associated with the address. + // + std::optional< std::string > fullyQualifiedName = std::nullopt; + + // + // The byte offset of this address from the absolute or relative address of the parent object. + // + std::optional< int64_t > offsetFromParent = std::nullopt; + + // + // The index within run.addresses of the cached object for this address. + // + int64_t index = -1; + + // + // The index within run.addresses of the parent object. + // + int64_t parentIndex = -1; + + // + // Key/value pairs that provide additional information about the address. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const address &); + void from_json(const json &, address &); + + // + // Represents the contents of an artifact. + // + struct artifact_content { + // + // UTF-8-encoded content from a text artifact. + // + std::optional< std::string > text = std::nullopt; + + // + // MIME Base64-encoded content from a binary artifact, or from a text artifact in its original encoding. + // + std::optional< std::string > binary = std::nullopt; + + // + // An alternate rendered representation of the artifact (e.g., a decompiled representation of a binary region). + // + std::optional< multiformat_message_string > rendered = std::nullopt; + + // + // Key/value pairs that provide additional information about the artifact content. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const artifact_content &); + void from_json(const json &, artifact_content &); + + // + // A region within an artifact where a result was detected. + // + struct region { + // + // The line number of the first character in the region. + // + std::optional< int64_t > startLine = std::nullopt; + + // + // The column number of the first character in the region. + // + std::optional< int64_t > startColumn = std::nullopt; + + // + // The line number of the last character in the region. + // + std::optional< int64_t > endLine = std::nullopt; + + // + // The column number of the character following the end of the region. + // + std::optional< int64_t > endColumn = std::nullopt; + + // + // The zero-based offset from the beginning of the artifact of the first character in the region. + // + int64_t charOffset = -1; + + // + // The length of the region in characters. + // + std::optional< int64_t > charLength = std::nullopt; + + // + // The zero-based offset from the beginning of the artifact of the first byte in the region. + // + int64_t byteOffset = -1; + + // + // The length of the region in bytes. + // + std::optional< int64_t > byteLength = std::nullopt; + + // + // The portion of the artifact contents within the specified region. + // + std::optional< artifact_content > snippet = std::nullopt; + + // + // A message relevant to the region. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // Specifies the source language, if any, of the portion of the artifact specified by the region object. + // + std::optional< std::string > sourceLanguage = std::nullopt; + + // + // Key/value pairs that provide additional information about the region. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const region &); + void from_json(const json &, region &); + + // + // A physical location relevant to a result. Specifies a reference to a programming artifact together with a range of bytes or characters within that artifact. + // + struct physical_location { + // + // The address of the location. + // + std::optional< ::gap::sarif::address > address = std::nullopt; + + // + // The location of the artifact. + // + std::optional< artifact_location > artifactLocation = std::nullopt; + + // + // Specifies a portion of the artifact. + // + std::optional< ::gap::sarif::region > region = std::nullopt; + + // + // Specifies a portion of the artifact that encloses the region. Allows a viewer to display additional context around the region. + // + std::optional< ::gap::sarif::region > contextRegion = std::nullopt; + + // + // Key/value pairs that provide additional information about the physical location. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const physical_location &); + void from_json(const json &, physical_location &); + + // + // A logical location of a construct that produced a result. + // + struct logical_location { + // + // Identifies the construct in which the result occurred. For example, this property might contain the name of a class or a method. + // + std::optional< std::string > name = std::nullopt; + + // + // The index within the logical locations array. + // + int64_t index = -1; + + // + // The human-readable fully qualified name of the logical location. + // + std::optional< std::string > fullyQualifiedName = std::nullopt; + + // + // The machine-readable name for the logical location, such as a mangled function name provided by a C++ compiler that encodes calling convention, return type and other details along with the function name. + // + std::optional< std::string > decoratedName = std::nullopt; + + // + // Identifies the index of the immediate parent of the construct in which the result was detected. For example, this property might point to a logical location that represents the namespace that holds a type. + // + int64_t parentIndex = -1; + + // + // The type of construct this logical location component refers to. Should be one of 'function', 'member', 'module', 'namespace', 'parameter', 'resource', 'returnType', 'type', 'variable', 'object', 'array', 'property', 'value', 'element', 'text', 'attribute', 'comment', 'declaration', 'dtd' or 'processingInstruction', if any of those accurately describe the construct. + // + std::optional< std::string > kind = std::nullopt; + + // + // Key/value pairs that provide additional information about the logical location. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const logical_location &); + void from_json(const json &, logical_location &); + + // + // Information about the relation of one location to another. + // + struct location_relationship { + // + // A reference to the related location. + // + int64_t target; + + // + // A set of distinct strings that categorize the relationship. Well-known kinds include 'includes', 'isIncludedBy' and 'relevant'. + // + std::vector< std::string > kinds = { "relevant" }; + + // + // A description of the location relationship. + // + std::optional< message > description = std::nullopt; + + // + // Key/value pairs that provide additional information about the location relationship. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const location_relationship &); + void from_json(const json &, location_relationship &); + + // + // A location within a programming artifact. + // + struct location { + // + // Value that distinguishes this location from all other locations within a single result object. + // + int64_t id = -1; + + // + // Identifies the artifact and region. + // + std::optional< physical_location > physicalLocation = std::nullopt; + + // + // The logical locations associated with the result. + // + std::vector< logical_location > logicalLocations = {}; + + // + // A message relevant to the location. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // A set of regions relevant to the location. + // + std::vector< region > annotations = {}; + + // + // An array of objects that describe relationships between this location and others. + // + std::vector< location_relationship > relationships = {}; + + // + // Key/value pairs that provide additional information about the location. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const location &); + void from_json(const json &, location &); + + // + // A function call within a stack trace. + // + struct stack_frame { + // + // The location to which this stack frame refers. + // + std::optional< ::gap::sarif::location > location = std::nullopt; + + // + // The name of the module that contains the code of this stack frame. + // + std::optional< std::string > module = std::nullopt; + + // + // The thread identifier of the stack frame. + // + std::optional< int64_t > threadId = std::nullopt; + + // + // The parameters of the call that is executing. + // + std::vector< std::string > parameters = {}; + + // + // Key/value pairs that provide additional information about the stack frame. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const stack_frame &); + void from_json(const json &, stack_frame &); + + // + // A call stack that is relevant to a result. + // + struct stack { + // + // A message relevant to this call stack. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // An array of stack frames that represents a sequence of calls, rendered in reverse chronological order, that comprise the call stack. + // + std::vector< stack_frame > frames; + + // + // Key/value pairs that provide additional information about the stack. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const stack &); + void from_json(const json &, stack &); + struct exception; + + // + // Describes a runtime exception encountered during the execution of an analysis tool. + // + struct exception { + // + // A string that identifies the kind of exception, for example, the fully qualified type name of an object that was thrown, or the symbolic name of a signal. + // + std::optional< std::string > kind = std::nullopt; + + // + // A message that describes the exception. + // + std::optional< std::string > message = std::nullopt; + + // + // The sequence of function calls leading to the exception. + // + std::optional< ::gap::sarif::stack > stack = std::nullopt; + + // + // An array of exception objects each of which is considered a cause of this exception. + // + std::vector< exception > innerExceptions = {}; + + // + // Key/value pairs that provide additional information about the exception. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const exception &); + void from_json(const json &, exception &); + + // + // Describes a condition relevant to the tool itself, as opposed to being relevant to a target being analyzed by the tool. + // + struct notification { + // + // The locations relevant to this notification. + // + std::vector< location > locations = {}; + + // + // A message that describes the condition that was encountered. + // + ::gap::sarif::message message; + + // + // A value specifying the severity level of the notification. + // + ::gap::sarif::level level = ::gap::sarif::level::kWarning; + + // + // The thread identifier of the code that generated the notification. + // + std::optional< int64_t > threadId = std::nullopt; + + // + // The Coordinated Universal Time (UTC) date and time at which the analysis tool generated the notification. + // + std::optional< std::string > timeUtc = std::nullopt; + + // + // The runtime exception, if any, relevant to this notification. + // + std::optional< ::gap::sarif::exception > exception = std::nullopt; + + // + // A reference used to locate the descriptor relevant to this notification. + // + std::optional< reporting_descriptor_reference > descriptor = std::nullopt; + + // + // A reference used to locate the rule descriptor associated with this notification. + // + std::optional< reporting_descriptor_reference > associatedRule = std::nullopt; + + // + // Key/value pairs that provide additional information about the notification. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const notification &); + void from_json(const json &, notification &); + + // + // The runtime environment of the analysis tool run. + // + struct invocation { + // + // The command line used to invoke the tool. + // + std::optional< std::string > commandLine = std::nullopt; + + // + // An array of strings, containing in order the command line arguments passed to the tool from the operating system. + // + std::vector< std::string > arguments = {}; + + // + // The locations of any response files specified on the tool's command line. + // + std::vector< artifact_location > responseFiles = {}; + + // + // The Coordinated Universal Time (UTC) date and time at which the run started. See "Date/time properties" in the SARIF spec for the required format. + // + std::optional< std::string > startTimeUtc = std::nullopt; + + // + // The Coordinated Universal Time (UTC) date and time at which the run ended. See "Date/time properties" in the SARIF spec for the required format. + // + std::optional< std::string > endTimeUtc = std::nullopt; + + // + // The process exit code. + // + std::optional< int64_t > exitCode = std::nullopt; + + // + // An array of configurationOverride objects that describe rules related runtime overrides. + // + std::vector< configuration_override > ruleConfigurationOverrides = {}; + + // + // An array of configurationOverride objects that describe notifications related runtime overrides. + // + std::vector< configuration_override > notificationConfigurationOverrides = {}; + + // + // A list of runtime conditions detected by the tool during the analysis. + // + std::vector< notification > toolExecutionNotifications = {}; + + // + // A list of conditions detected by the tool that are relevant to the tool's configuration. + // + std::vector< notification > toolConfigurationNotifications = {}; + + // + // The reason for the process exit. + // + std::optional< std::string > exitCodeDescription = std::nullopt; + + // + // The name of the signal that caused the process to exit. + // + std::optional< std::string > exitSignalName = std::nullopt; + + // + // The numeric value of the signal that caused the process to exit. + // + std::optional< int64_t > exitSignalNumber = std::nullopt; + + // + // The reason given by the operating system that the process failed to start. + // + std::optional< std::string > processStartFailureMessage = std::nullopt; + + // + // Specifies whether the tool's execution completed successfully. + // + bool executionSuccessful; + + // + // The machine that hosted the analysis tool run. + // + std::optional< std::string > machine = std::nullopt; + + // + // The account that ran the analysis tool. + // + std::optional< std::string > account = std::nullopt; + + // + // The process id for the analysis tool run. + // + std::optional< int64_t > processId = std::nullopt; + + // + // An absolute URI specifying the location of the analysis tool's executable. + // + std::optional< artifact_location > executableLocation = std::nullopt; + + // + // The working directory for the analysis tool run. + // + std::optional< artifact_location > workingDirectory = std::nullopt; + + // + // The environment variables associated with the analysis tool process, expressed as key/value pairs. + // + std::optional< std::unordered_map< std::string, std::string > > environmentVariables = std::nullopt; + + // + // A file containing the standard input stream to the process that was invoked. + // + std::optional< artifact_location > stdin = std::nullopt; + + // + // A file containing the standard output stream from the process that was invoked. + // + std::optional< artifact_location > stdout = std::nullopt; + + // + // A file containing the standard error stream from the process that was invoked. + // + std::optional< artifact_location > stderr = std::nullopt; + + // + // A file containing the interleaved standard output and standard error stream from the process that was invoked. + // + std::optional< artifact_location > stdoutStderr = std::nullopt; + + // + // Key/value pairs that provide additional information about the invocation. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const invocation &); + void from_json(const json &, invocation &); + + // + // Describes how a converter transformed the output of a static analysis tool from the analysis tool's native output format into the SARIF format. + // + struct conversion { + // + // A tool object that describes the converter. + // + ::gap::sarif::tool tool; + + // + // An invocation object that describes the invocation of the converter. + // + std::optional< ::gap::sarif::invocation > invocation = std::nullopt; + + // + // The locations of the analysis tool's per-run log files. + // + std::vector< artifact_location > analysisToolLogFiles = {}; + + // + // Key/value pairs that provide additional information about the conversion. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const conversion &); + void from_json(const json &, conversion &); + + // + // Specifies the information necessary to retrieve a desired revision from a version control system. + // + struct version_control_details { + // + // The absolute URI of the repository. + // + std::string repositoryUri; + + // + // A string that uniquely and permanently identifies the revision within the repository. + // + std::optional< std::string > revisionId = std::nullopt; + + // + // The name of a branch containing the revision. + // + std::optional< std::string > branch = std::nullopt; + + // + // A tag that has been applied to the revision. + // + std::optional< std::string > revisionTag = std::nullopt; + + // + // A Coordinated Universal Time (UTC) date and time that can be used to synchronize an enlistment to the state of the repository at that time. + // + std::optional< std::string > asOfTimeUtc = std::nullopt; + + // + // The location in the local file system to which the root of the repository was mapped at the time of the analysis. + // + std::optional< artifact_location > mappedTo = std::nullopt; + + // + // Key/value pairs that provide additional information about the version control details. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const version_control_details &); + void from_json(const json &, version_control_details &); + + enum class roles { + kAnalysisTarget, + kAttachment, + kResponseFile, + kResultFile, + kStandardStream, + kTracedFile, + kUnmodified, + kModified, + kAdded, + kDeleted, + kRenamed, + kUncontrolled, + kDriver, + kExtension, + kTranslation, + kTaxonomy, + kPolicy, + kReferencedOnCommandLine, + kMemoryContents, + kDirectory, + kUserSpecifiedConfiguration, + kToolSpecifiedConfiguration, + kDebugOutputFile, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(roles, { + { roles::kAnalysisTarget, "analysisTarget" }, + { roles::kAttachment, "attachment" }, + { roles::kResponseFile, "responseFile" }, + { roles::kResultFile, "resultFile" }, + { roles::kStandardStream, "standardStream" }, + { roles::kTracedFile, "tracedFile" }, + { roles::kUnmodified, "unmodified" }, + { roles::kModified, "modified" }, + { roles::kAdded, "added" }, + { roles::kDeleted, "deleted" }, + { roles::kRenamed, "renamed" }, + { roles::kUncontrolled, "uncontrolled" }, + { roles::kDriver, "driver" }, + { roles::kExtension, "extension" }, + { roles::kTranslation, "translation" }, + { roles::kTaxonomy, "taxonomy" }, + { roles::kPolicy, "policy" }, + { roles::kReferencedOnCommandLine, "referencedOnCommandLine" }, + { roles::kMemoryContents, "memoryContents" }, + { roles::kDirectory, "directory" }, + { roles::kUserSpecifiedConfiguration, "userSpecifiedConfiguration" }, + { roles::kToolSpecifiedConfiguration, "toolSpecifiedConfiguration" }, + { roles::kDebugOutputFile, "debugOutputFile" }, + }) + + // + // A single artifact. In some cases, this artifact might be nested within another artifact. + // + struct artifact { + // + // A short description of the artifact. + // + std::optional< message > description = std::nullopt; + + // + // The location of the artifact. + // + std::optional< artifact_location > location = std::nullopt; + + // + // Identifies the index of the immediate parent of the artifact, if this artifact is nested. + // + int64_t parentIndex = -1; + + // + // The offset in bytes of the artifact within its containing artifact. + // + std::optional< int64_t > offset = std::nullopt; + + // + // The length of the artifact in bytes. + // + int64_t length = -1; + + // + // The role or roles played by the artifact in the analysis. + // + std::vector< ::gap::sarif::roles > roles = {}; + + // + // The MIME type (RFC 2045) of the artifact. + // + std::optional< std::string > mimeType = std::nullopt; + + // + // The contents of the artifact. + // + std::optional< artifact_content > contents = std::nullopt; + + // + // Specifies the encoding for an artifact object that refers to a text file. + // + std::optional< std::string > encoding = std::nullopt; + + // + // Specifies the source language for any artifact object that refers to a text file that contains source code. + // + std::optional< std::string > sourceLanguage = std::nullopt; + + // + // A dictionary, each of whose keys is the name of a hash function and each of whose values is the hashed value of the artifact produced by the specified hash function. + // + std::optional< std::unordered_map< std::string, std::string > > hashes = std::nullopt; + + // + // The Coordinated Universal Time (UTC) date and time at which the artifact was most recently modified. See "Date/time properties" in the SARIF spec for the required format. + // + std::optional< std::string > lastModifiedTimeUtc = std::nullopt; + + // + // Key/value pairs that provide additional information about the artifact. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const artifact &); + void from_json(const json &, artifact &); + struct node; + + // + // Represents a node in a graph. + // + struct node { + // + // A string that uniquely identifies the node within its graph. + // + std::string id; + + // + // A short description of the node. + // + std::optional< message > label = std::nullopt; + + // + // A code location associated with the node. + // + std::optional< ::gap::sarif::location > location = std::nullopt; + + // + // Array of child nodes. + // + std::vector< node > children = {}; + + // + // Key/value pairs that provide additional information about the node. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const node &); + void from_json(const json &, node &); + + // + // Represents a directed edge in a graph. + // + struct edge { + // + // A string that uniquely identifies the edge within its graph. + // + std::string id; + + // + // A short description of the edge. + // + std::optional< message > label = std::nullopt; + + // + // Identifies the source node (the node at which the edge starts). + // + std::string sourceNodeId; + + // + // Identifies the target node (the node at which the edge ends). + // + std::string targetNodeId; + + // + // Key/value pairs that provide additional information about the edge. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const edge &); + void from_json(const json &, edge &); + + // + // A network of nodes and directed edges that describes some aspect of the structure of the code (for example, a call graph). + // + struct graph { + // + // A description of the graph. + // + std::optional< message > description = std::nullopt; + + // + // An array of node objects representing the nodes of the graph. + // + std::vector< node > nodes = {}; + + // + // An array of edge objects representing the edges of the graph. + // + std::vector< edge > edges = {}; + + // + // Key/value pairs that provide additional information about the graph. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const graph &); + void from_json(const json &, graph &); + + enum class kind { + kNotApplicable, + kPass, + kFail, + kReview, + kOpen, + kInformational, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(kind, { + { kind::kNotApplicable, "notApplicable" }, + { kind::kPass, "pass" }, + { kind::kFail, "fail" }, + { kind::kReview, "review" }, + { kind::kOpen, "open" }, + { kind::kInformational, "informational" }, + }) + + enum class importance { + kImportant, + kEssential, + kUnimportant, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(importance, { + { importance::kImportant, "important" }, + { importance::kEssential, "essential" }, + { importance::kUnimportant, "unimportant" }, + }) + + // + // Describes an HTTP request. + // + struct web_request { + // + // The index within the run.webRequests array of the request object associated with this result. + // + int64_t index = -1; + + // + // The request protocol. Example: 'http'. + // + std::optional< std::string > protocol = std::nullopt; + + // + // The request version. Example: '1.1'. + // + std::optional< std::string > version = std::nullopt; + + // + // The target of the request. + // + std::optional< std::string > target = std::nullopt; + + // + // The HTTP method. Well-known values are 'GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'. + // + std::optional< std::string > method = std::nullopt; + + // + // The request headers. + // + std::optional< std::unordered_map< std::string, std::string > > headers = std::nullopt; + + // + // The request parameters. + // + std::optional< std::unordered_map< std::string, std::string > > parameters = std::nullopt; + + // + // The body of the request. + // + std::optional< artifact_content > body = std::nullopt; + + // + // Key/value pairs that provide additional information about the request. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const web_request &); + void from_json(const json &, web_request &); + + // + // Describes the response to an HTTP request. + // + struct web_response { + // + // The index within the run.webResponses array of the response object associated with this result. + // + int64_t index = -1; + + // + // The response protocol. Example: 'http'. + // + std::optional< std::string > protocol = std::nullopt; + + // + // The response version. Example: '1.1'. + // + std::optional< std::string > version = std::nullopt; + + // + // The response status code. Example: 451. + // + std::optional< int64_t > statusCode = std::nullopt; + + // + // The response reason. Example: 'Not found'. + // + std::optional< std::string > reasonPhrase = std::nullopt; + + // + // The response headers. + // + std::optional< std::unordered_map< std::string, std::string > > headers = std::nullopt; + + // + // The body of the response. + // + std::optional< artifact_content > body = std::nullopt; + + // + // Specifies whether a response was received from the server. + // + bool noResponseReceived = false; + + // + // Key/value pairs that provide additional information about the response. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const web_response &); + void from_json(const json &, web_response &); + + // + // A location visited by an analysis tool while simulating or monitoring the execution of a program. + // + struct thread_flow_location { + // + // The index within the run threadFlowLocations array. + // + int64_t index = -1; + + // + // The code location. + // + std::optional< ::gap::sarif::location > location = std::nullopt; + + // + // The call stack leading to this location. + // + std::optional< ::gap::sarif::stack > stack = std::nullopt; + + // + // A set of distinct strings that categorize the thread flow location. Well-known kinds include 'acquire', 'release', 'enter', 'exit', 'call', 'return', 'branch', 'implicit', 'false', 'true', 'caution', 'danger', 'unknown', 'unreachable', 'taint', 'function', 'handler', 'lock', 'memory', 'resource', 'scope' and 'value'. + // + std::vector< std::string > kinds = {}; + + // + // An array of references to rule or taxonomy reporting descriptors that are applicable to the thread flow location. + // + std::vector< reporting_descriptor_reference > taxa = {}; + + // + // The name of the module that contains the code that is executing. + // + std::optional< std::string > module = std::nullopt; + + // + // A dictionary, each of whose keys specifies a variable or expression, the associated value of which represents the variable or expression value. For an annotation of kind 'continuation', for example, this dictionary might hold the current assumed values of a set of global variables. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > state = std::nullopt; + + // + // An integer representing a containment hierarchy within the thread flow. + // + std::optional< int64_t > nestingLevel = std::nullopt; + + // + // An integer representing the temporal order in which execution reached this location. + // + int64_t executionOrder = -1; + + // + // The Coordinated Universal Time (UTC) date and time at which this location was executed. + // + std::optional< std::string > executionTimeUtc = std::nullopt; + + // + // Specifies the importance of this location in understanding the code flow in which it occurs. The order from most to least important is "essential", "important", "unimportant". Default: "important". + // + ::gap::sarif::importance importance = ::gap::sarif::importance::kImportant; + + // + // A web request associated with this thread flow location. + // + std::optional< web_request > webRequest = std::nullopt; + + // + // A web response associated with this thread flow location. + // + std::optional< web_response > webResponse = std::nullopt; + + // + // Key/value pairs that provide additional information about the threadflow location. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const thread_flow_location &); + void from_json(const json &, thread_flow_location &); + + // + // Describes a sequence of code locations that specify a path through a single thread of execution such as an operating system or fiber. + // + struct thread_flow { + // + // An string that uniquely identifies the threadFlow within the codeFlow in which it occurs. + // + std::optional< std::string > id = std::nullopt; + + // + // A message relevant to the thread flow. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // Values of relevant expressions at the start of the thread flow that may change during thread flow execution. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > initialState = std::nullopt; + + // + // Values of relevant expressions at the start of the thread flow that remain constant. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > immutableState = std::nullopt; + + // + // A temporally ordered array of 'threadFlowLocation' objects, each of which describes a location visited by the tool while producing the result. + // + std::vector< thread_flow_location > locations; + + // + // Key/value pairs that provide additional information about the thread flow. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const thread_flow &); + void from_json(const json &, thread_flow &); + + // + // A set of threadFlows which together describe a pattern of code execution relevant to detecting a result. + // + struct code_flow { + // + // A message relevant to the code flow. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // An array of one or more unique threadFlow objects, each of which describes the progress of a program through a thread of execution. + // + std::vector< thread_flow > threadFlows; + + // + // Key/value pairs that provide additional information about the code flow. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const code_flow &); + void from_json(const json &, code_flow &); + + // + // Represents the traversal of a single edge during a graph traversal. + // + struct edge_traversal { + // + // Identifies the edge being traversed. + // + std::string edgeId; + + // + // A message to display to the user as the edge is traversed. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // The values of relevant expressions after the edge has been traversed. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > finalState = std::nullopt; + + // + // The number of edge traversals necessary to return from a nested graph. + // + std::optional< int64_t > stepOverEdgeCount = std::nullopt; + + // + // Key/value pairs that provide additional information about the edge traversal. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const edge_traversal &); + void from_json(const json &, edge_traversal &); + + // + // Represents a path through a graph. + // + struct graph_traversal { + // + // The index within the run.graphs to be associated with the result. + // + int64_t runGraphIndex = -1; + + // + // The index within the result.graphs to be associated with the result. + // + int64_t resultGraphIndex = -1; + + // + // A description of this graph traversal. + // + std::optional< message > description = std::nullopt; + + // + // Values of relevant expressions at the start of the graph traversal that may change during graph traversal. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > initialState = std::nullopt; + + // + // Values of relevant expressions at the start of the graph traversal that remain constant for the graph traversal. + // + std::optional< std::unordered_map< std::string, multiformat_message_string > > immutableState = std::nullopt; + + // + // The sequences of edges traversed by this graph traversal. + // + std::vector< edge_traversal > edgeTraversals = {}; + + // + // Key/value pairs that provide additional information about the graph traversal. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const graph_traversal &); + void from_json(const json &, graph_traversal &); + + enum class state { + kAccepted, + kUnderReview, + kRejected, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(state, { + { state::kAccepted, "accepted" }, + { state::kUnderReview, "underReview" }, + { state::kRejected, "rejected" }, + }) + + // + // A suppression that is relevant to a result. + // + struct suppression { + // + // A stable, unique identifer for the suprression in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // A string that indicates where the suppression is persisted. + // + ::gap::sarif::kind kind; + + // + // A string that indicates the state of the suppression. + // + std::optional< ::gap::sarif::state > state = std::nullopt; + + // + // A string representing the justification for the suppression. + // + std::optional< std::string > justification = std::nullopt; + + // + // Identifies the location associated with the suppression. + // + std::optional< ::gap::sarif::location > location = std::nullopt; + + // + // Key/value pairs that provide additional information about the suppression. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const suppression &); + void from_json(const json &, suppression &); + + enum class baseline_state { + kNew, + kUnchanged, + kUpdated, + kAbsent, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(baseline_state, { + { baseline_state::kNew, "new" }, + { baseline_state::kUnchanged, "unchanged" }, + { baseline_state::kUpdated, "updated" }, + { baseline_state::kAbsent, "absent" }, + }) + + // + // An area within an image. + // + struct rectangle { + // + // The Y coordinate of the top edge of the rectangle, measured in the image's natural units. + // + std::optional< double > top = std::nullopt; + + // + // The X coordinate of the left edge of the rectangle, measured in the image's natural units. + // + std::optional< double > left = std::nullopt; + + // + // The Y coordinate of the bottom edge of the rectangle, measured in the image's natural units. + // + std::optional< double > bottom = std::nullopt; + + // + // The X coordinate of the right edge of the rectangle, measured in the image's natural units. + // + std::optional< double > right = std::nullopt; + + // + // A message relevant to the rectangle. + // + std::optional< ::gap::sarif::message > message = std::nullopt; + + // + // Key/value pairs that provide additional information about the rectangle. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const rectangle &); + void from_json(const json &, rectangle &); + + // + // An artifact relevant to a result. + // + struct attachment { + // + // A message describing the role played by the attachment. + // + std::optional< message > description = std::nullopt; + + // + // The location of the attachment. + // + artifact_location artifactLocation; + + // + // An array of regions of interest within the attachment. + // + std::vector< region > regions = {}; + + // + // An array of rectangles specifying areas of interest within the image. + // + std::vector< rectangle > rectangles = {}; + + // + // Key/value pairs that provide additional information about the attachment. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const attachment &); + void from_json(const json &, attachment &); + + // + // Contains information about how and when a result was detected. + // + struct result_provenance { + // + // The Coordinated Universal Time (UTC) date and time at which the result was first detected. See "Date/time properties" in the SARIF spec for the required format. + // + std::optional< std::string > firstDetectionTimeUtc = std::nullopt; + + // + // The Coordinated Universal Time (UTC) date and time at which the result was most recently detected. See "Date/time properties" in the SARIF spec for the required format. + // + std::optional< std::string > lastDetectionTimeUtc = std::nullopt; + + // + // A GUID-valued string equal to the automationDetails.guid property of the run in which the result was first detected. + // + std::optional< std::string > firstDetectionRunGuid = std::nullopt; + + // + // A GUID-valued string equal to the automationDetails.guid property of the run in which the result was most recently detected. + // + std::optional< std::string > lastDetectionRunGuid = std::nullopt; + + // + // The index within the run.invocations array of the invocation object which describes the tool invocation that detected the result. + // + int64_t invocationIndex = -1; + + // + // An array of physicalLocation objects which specify the portions of an analysis tool's output that a converter transformed into the result. + // + std::vector< physical_location > conversionSources = {}; + + // + // Key/value pairs that provide additional information about the result. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const result_provenance &); + void from_json(const json &, result_provenance &); + + // + // The replacement of a single region of an artifact. + // + struct replacement { + // + // The region of the artifact to delete. + // + region deletedRegion; + + // + // The content to insert at the location specified by the 'deletedRegion' property. + // + std::optional< artifact_content > insertedContent = std::nullopt; + + // + // Key/value pairs that provide additional information about the replacement. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const replacement &); + void from_json(const json &, replacement &); + + // + // A change to a single artifact. + // + struct artifact_change { + // + // The location of the artifact to change. + // + artifact_location artifactLocation; + + // + // An array of replacement objects, each of which represents the replacement of a single region in a single artifact specified by 'artifactLocation'. + // + std::vector< replacement > replacements; + + // + // Key/value pairs that provide additional information about the change. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const artifact_change &); + void from_json(const json &, artifact_change &); + + // + // A proposed fix for the problem represented by a result object. A fix specifies a set of artifacts to modify. For each artifact, it specifies a set of bytes to remove, and provides a set of new bytes to replace them. + // + struct fix { + // + // A message that describes the proposed fix, enabling viewers to present the proposed change to an end user. + // + std::optional< message > description = std::nullopt; + + // + // One or more artifact changes that comprise a fix for a result. + // + std::vector< artifact_change > artifactChanges; + + // + // Key/value pairs that provide additional information about the fix. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const fix &); + void from_json(const json &, fix &); + + // + // A result produced by an analysis tool. + // + struct result { + // + // The stable, unique identifier of the rule, if any, to which this notification is relevant. This member can be used to retrieve rule metadata from the rules dictionary, if it exists. + // + std::optional< std::string > ruleId = std::nullopt; + + // + // The index within the tool component rules array of the rule object associated with this result. + // + int64_t ruleIndex = -1; + + // + // A reference used to locate the rule descriptor relevant to this result. + // + std::optional< reporting_descriptor_reference > rule = std::nullopt; + + // + // A value that categorizes results by evaluation state. + // + ::gap::sarif::kind kind = ::gap::sarif::kind::kFail; + + // + // A value specifying the severity level of the result. + // + ::gap::sarif::level level = ::gap::sarif::level::kWarning; + + // + // A message that describes the result. The first sentence of the message only will be displayed when visible space is limited. + // + ::gap::sarif::message message; + + // + // Identifies the artifact that the analysis tool was instructed to scan. This need not be the same as the artifact where the result actually occurred. + // + std::optional< artifact_location > analysisTarget = std::nullopt; + + // + // The set of locations where the result was detected. Specify only one location unless the problem indicated by the result can only be corrected by making a change at every specified location. + // + std::vector< location > locations = {}; + + // + // A stable, unique identifer for the result in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // A stable, unique identifier for the equivalence class of logically identical results to which this result belongs, in the form of a GUID. + // + std::optional< std::string > correlationGuid = std::nullopt; + + // + // A positive integer specifying the number of times this logically unique result was observed in this run. + // + std::optional< int64_t > occurrenceCount = std::nullopt; + + // + // A set of strings that contribute to the stable, unique identity of the result. + // + std::optional< std::unordered_map< std::string, std::string > > partialFingerprints = std::nullopt; + + // + // A set of strings each of which individually defines a stable, unique identity for the result. + // + std::optional< std::unordered_map< std::string, std::string > > fingerprints = std::nullopt; + + // + // An array of 'stack' objects relevant to the result. + // + std::vector< stack > stacks = {}; + + // + // An array of 'codeFlow' objects relevant to the result. + // + std::vector< code_flow > codeFlows = {}; + + // + // An array of zero or more unique graph objects associated with the result. + // + std::vector< graph > graphs = {}; + + // + // An array of one or more unique 'graphTraversal' objects. + // + std::vector< graph_traversal > graphTraversals = {}; + + // + // A set of locations relevant to this result. + // + std::vector< location > relatedLocations = {}; + + // + // A set of suppressions relevant to this result. + // + std::vector< suppression > suppressions = {}; + + // + // The state of a result relative to a baseline of a previous run. + // + std::optional< baseline_state > baselineState = std::nullopt; + + // + // A number representing the priority or importance of the result. + // + double rank = -1.0; + + // + // A set of artifacts relevant to the result. + // + std::vector< attachment > attachments = {}; + + // + // An absolute URI at which the result can be viewed. + // + std::optional< std::string > hostedViewerUri = std::nullopt; + + // + // The URIs of the work items associated with this result. + // + std::vector< std::string > workItemUris = {}; + + // + // Information about how and when the result was detected. + // + std::optional< result_provenance > provenance = std::nullopt; + + // + // An array of 'fix' objects, each of which represents a proposed fix to the problem indicated by the result. + // + std::vector< fix > fixes = {}; + + // + // An array of references to taxonomy reporting descriptors that are applicable to the result. + // + std::vector< reporting_descriptor_reference > taxa = {}; + + // + // A web request associated with this result. + // + std::optional< web_request > webRequest = std::nullopt; + + // + // A web response associated with this result. + // + std::optional< web_response > webResponse = std::nullopt; + + // + // Key/value pairs that provide additional information about the result. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const result &); + void from_json(const json &, result &); + + // + // Information that describes a run's identity and role within an engineering system process. + // + struct run_automation_details { + // + // A description of the identity and role played within the engineering system by this object's containing run object. + // + std::optional< message > description = std::nullopt; + + // + // A hierarchical string that uniquely identifies this object's containing run object. + // + std::optional< std::string > id = std::nullopt; + + // + // A stable, unique identifer for this object's containing run object in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // A stable, unique identifier for the equivalence class of runs to which this object's containing run object belongs in the form of a GUID. + // + std::optional< std::string > correlationGuid = std::nullopt; + + // + // Key/value pairs that provide additional information about the run automation details. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const run_automation_details &); + void from_json(const json &, run_automation_details &); + + enum class column_kind { + kUtf16CodeUnits, + kUnicodeCodePoints, + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(column_kind, { + { column_kind::kUtf16CodeUnits, "utf16CodeUnits" }, + { column_kind::kUnicodeCodePoints, "unicodeCodePoints" }, + }) + + // + // Contains information that enables a SARIF consumer to locate the external property file that contains the value of an externalized property associated with the run. + // + struct external_property_file_reference { + // + // The location of the external property file. + // + std::optional< artifact_location > location = std::nullopt; + + // + // A stable, unique identifer for the external property file in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // A non-negative integer specifying the number of items contained in the external property file. + // + int64_t itemCount = -1; + + // + // Key/value pairs that provide additional information about the external property file. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const external_property_file_reference &); + void from_json(const json &, external_property_file_reference &); + + // + // References to external property files that should be inlined with the content of a root log file. + // + struct external_property_file_references { + // + // An external property file containing a run.conversion object to be merged with the root log file. + // + std::optional< external_property_file_reference > conversion = std::nullopt; + + // + // An array of external property files containing a run.graphs object to be merged with the root log file. + // + std::vector< external_property_file_reference > graphs = {}; + + // + // An external property file containing a run.properties object to be merged with the root log file. + // + std::optional< external_property_file_reference > externalizedProperties = std::nullopt; + + // + // An array of external property files containing run.artifacts arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > artifacts = {}; + + // + // An array of external property files containing run.invocations arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > invocations = {}; + + // + // An array of external property files containing run.logicalLocations arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > logicalLocations = {}; + + // + // An array of external property files containing run.threadFlowLocations arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > threadFlowLocations = {}; + + // + // An array of external property files containing run.results arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > results = {}; + + // + // An array of external property files containing run.taxonomies arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > taxonomies = {}; + + // + // An array of external property files containing run.addresses arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > addresses = {}; + + // + // An external property file containing a run.driver object to be merged with the root log file. + // + std::optional< external_property_file_reference > driver = std::nullopt; + + // + // An array of external property files containing run.extensions arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > extensions = {}; + + // + // An array of external property files containing run.policies arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > policies = {}; + + // + // An array of external property files containing run.translations arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > translations = {}; + + // + // An array of external property files containing run.requests arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > webRequests = {}; + + // + // An array of external property files containing run.responses arrays to be merged with the root log file. + // + std::vector< external_property_file_reference > webResponses = {}; + + // + // Key/value pairs that provide additional information about the external property files. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const external_property_file_references &); + void from_json(const json &, external_property_file_references &); + + // + // Defines locations of special significance to SARIF consumers. + // + struct special_locations { + // + // Provides a suggestion to SARIF consumers to display file paths relative to the specified location. + // + std::optional< artifact_location > displayBase = std::nullopt; + + // + // Key/value pairs that provide additional information about the special locations. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const special_locations &); + void from_json(const json &, special_locations &); + + // + // Describes a single run of an analysis tool, and contains the reported output of that run. + // + struct run { + // + // Information about the tool or tool pipeline that generated the results in this run. A run can only contain results produced by a single tool or tool pipeline. A run can aggregate results from multiple log files, as long as context around the tool run (tool command-line arguments and the like) is identical for all aggregated files. + // + ::gap::sarif::tool tool; + + // + // Describes the invocation of the analysis tool. + // + std::vector< invocation > invocations = {}; + + // + // A conversion object that describes how a converter transformed an analysis tool's native reporting format into the SARIF format. + // + std::optional< ::gap::sarif::conversion > conversion = std::nullopt; + + // + // The language of the messages emitted into the log file during this run (expressed as an ISO 639-1 two-letter lowercase culture code) and an optional region (expressed as an ISO 3166-1 two-letter uppercase subculture code associated with a country or region). The casing is recommended but not required (in order for this data to conform to RFC5646). + // + std::string language = "en-US"; + + // + // Specifies the revision in version control of the artifacts that were scanned. + // + std::vector< version_control_details > versionControlProvenance = {}; + + // + // The artifact location specified by each uriBaseId symbol on the machine where the tool originally ran. + // + std::optional< std::unordered_map< std::string, artifact_location > > originalUriBaseIds = std::nullopt; + + // + // An array of artifact objects relevant to the run. + // + std::vector< artifact > artifacts = {}; + + // + // An array of logical locations such as namespaces, types or functions. + // + std::vector< logical_location > logicalLocations = {}; + + // + // An array of zero or more unique graph objects associated with the run. + // + std::vector< graph > graphs = {}; + + // + // The set of results contained in an SARIF log. The results array can be omitted when a run is solely exporting rules metadata. It must be present (but may be empty) if a log file represents an actual scan. + // + std::vector< result > results = {}; + + // + // Automation details that describe this run. + // + std::optional< run_automation_details > automationDetails = std::nullopt; + + // + // Automation details that describe the aggregate of runs to which this run belongs. + // + std::vector< run_automation_details > runAggregates = {}; + + // + // The 'guid' property of a previous SARIF 'run' that comprises the baseline that was used to compute result 'baselineState' properties for the run. + // + std::optional< std::string > baselineGuid = std::nullopt; + + // + // An array of strings used to replace sensitive information in a redaction-aware property. + // + std::vector< std::string > redactionTokens = {}; + + // + // Specifies the default encoding for any artifact object that refers to a text file. + // + std::optional< std::string > defaultEncoding = std::nullopt; + + // + // Specifies the default source language for any artifact object that refers to a text file that contains source code. + // + std::optional< std::string > defaultSourceLanguage = std::nullopt; + + // + // An ordered list of character sequences that were treated as line breaks when computing region information for the run. + // + std::vector< std::string > newlineSequences = { "\r\n", "\n" }; + + // + // Specifies the unit in which the tool measures columns. + // + std::optional< column_kind > columnKind = std::nullopt; + + // + // References to external property files that should be inlined with the content of a root log file. + // + std::optional< external_property_file_references > externalPropertyFileReferences = std::nullopt; + + // + // An array of threadFlowLocation objects cached at run level. + // + std::vector< thread_flow_location > threadFlowLocations = {}; + + // + // An array of toolComponent objects relevant to a taxonomy in which results are categorized. + // + std::vector< tool_component > taxonomies = {}; + + // + // Addresses associated with this run instance, if any. + // + std::vector< address > addresses = {}; + + // + // The set of available translations of the localized data provided by the tool. + // + std::vector< tool_component > translations = {}; + + // + // Contains configurations that may potentially override both reportingDescriptor.defaultConfiguration (the tool's default severities) and invocation.configurationOverrides (severities established at run-time from the command line). + // + std::vector< tool_component > policies = {}; + + // + // An array of request objects cached at run level. + // + std::vector< web_request > webRequests = {}; + + // + // An array of response objects cached at run level. + // + std::vector< web_response > webResponses = {}; + + // + // A specialLocations object that defines locations of special significance to SARIF consumers. + // + std::optional< special_locations > specialLocations = std::nullopt; + + // + // Key/value pairs that provide additional information about the run. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const run &); + void from_json(const json &, run &); + + // + // The top-level element of an external property file. + // + struct external_properties { + // + // The URI of the JSON schema corresponding to the version of the external property file format. + // + std::optional< std::string > schema = std::nullopt; + + // + // The SARIF format version of this external properties object. + // + std::optional< ::gap::sarif::version > version = std::nullopt; + + // + // A stable, unique identifer for this external properties object, in the form of a GUID. + // + std::optional< std::string > guid = std::nullopt; + + // + // A stable, unique identifer for the run associated with this external properties object, in the form of a GUID. + // + std::optional< std::string > runGuid = std::nullopt; + + // + // A conversion object that will be merged with a separate run. + // + std::optional< ::gap::sarif::conversion > conversion = std::nullopt; + + // + // An array of graph objects that will be merged with a separate run. + // + std::vector< graph > graphs = {}; + + // + // Key/value pairs that provide additional information that will be merged with a separate run. + // + std::optional< property_bag > externalizedProperties = std::nullopt; + + // + // An array of artifact objects that will be merged with a separate run. + // + std::vector< artifact > artifacts = {}; + + // + // Describes the invocation of the analysis tool that will be merged with a separate run. + // + std::vector< invocation > invocations = {}; + + // + // An array of logical locations such as namespaces, types or functions that will be merged with a separate run. + // + std::vector< logical_location > logicalLocations = {}; + + // + // An array of threadFlowLocation objects that will be merged with a separate run. + // + std::vector< thread_flow_location > threadFlowLocations = {}; + + // + // An array of result objects that will be merged with a separate run. + // + std::vector< result > results = {}; + + // + // Tool taxonomies that will be merged with a separate run. + // + std::vector< tool_component > taxonomies = {}; + + // + // The analysis tool object that will be merged with a separate run. + // + std::optional< tool_component > driver = std::nullopt; + + // + // Tool extensions that will be merged with a separate run. + // + std::vector< tool_component > extensions = {}; + + // + // Tool policies that will be merged with a separate run. + // + std::vector< tool_component > policies = {}; + + // + // Tool translations that will be merged with a separate run. + // + std::vector< tool_component > translations = {}; + + // + // Addresses that will be merged with a separate run. + // + std::vector< address > addresses = {}; + + // + // Requests that will be merged with a separate run. + // + std::vector< web_request > webRequests = {}; + + // + // Responses that will be merged with a separate run. + // + std::vector< web_response > webResponses = {}; + + // + // Key/value pairs that provide additional information about the external properties. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const external_properties &); + void from_json(const json &, external_properties &); + + // + // Static Analysis Results Format (SARIF) Version 2.1.0 JSON Schema: a standard format for the output of static analysis tools. + // + struct root { + // + // The SARIF format version of this log file. + // + ::gap::sarif::version version; + + // + // The set of runs contained in this log file. + // + std::vector< run > runs; + + // + // References to external property files that share data between runs. + // + std::vector< external_properties > inlineExternalProperties = {}; + + // + // Key/value pairs that provide additional information about the log file. + // + std::optional< property_bag > properties = std::nullopt; + }; + + void to_json(json &, const root &); + void from_json(const json &, root &); +} // namespace gap::sarif diff --git a/sarif/scripts/sarif-headergen/.gitignore b/sarif/scripts/sarif-headergen/.gitignore new file mode 100644 index 0000000..3a8816c --- /dev/null +++ b/sarif/scripts/sarif-headergen/.gitignore @@ -0,0 +1,162 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm-project.org/#use-with-ide +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/sarif/scripts/sarif-headergen/README.md b/sarif/scripts/sarif-headergen/README.md new file mode 100644 index 0000000..e4eb624 --- /dev/null +++ b/sarif/scripts/sarif-headergen/README.md @@ -0,0 +1,7 @@ +# sarif-headergen + +Generates nlohmann::json (de)serialization files from the JSON schema for SARIF files. + +## Usage + + $ python3 -m src.sarif_headergen.__cli__ sarif.json out.hpp out.cpp diff --git a/sarif/scripts/sarif-headergen/pyproject.toml b/sarif/scripts/sarif-headergen/pyproject.toml new file mode 100644 index 0000000..74bf50d --- /dev/null +++ b/sarif/scripts/sarif-headergen/pyproject.toml @@ -0,0 +1,19 @@ +[project] +name = "sarif-headergen" +version = "0.1.0" +description = "Generates nlohmann::json (de)serialization files from the JSON schema for SARIF files" +authors = [ + {name = "Francesco Bertolaccini", email = "francesco.bertolaccini@trailofbits.com"}, +] +dependencies = [] +requires-python = ">=3.12" +readme = "README.md" +license = {text = "MIT"} + +[build-system] +requires = ["pdm-backend"] +build-backend = "pdm.backend" + + +[tool.pdm] +distribution = true diff --git a/sarif/scripts/sarif-headergen/src/sarif_headergen/__cli__.py b/sarif/scripts/sarif-headergen/src/sarif_headergen/__cli__.py new file mode 100644 index 0000000..959cbe2 --- /dev/null +++ b/sarif/scripts/sarif-headergen/src/sarif_headergen/__cli__.py @@ -0,0 +1,257 @@ +import json +from .output import get_structs, Enum, Struct, ForwardRef, Array, FieldType, Map, Field +from .schema import Schema + + +def get_type_name(type: FieldType, field_names: set[str] = set()): + def _get_type_name(type: FieldType): + if isinstance(type, Enum): + return to_snake_case(f"{type.name}") + if isinstance(type, Struct): + return to_snake_case(f"{type.name}") + if isinstance(type, ForwardRef): + return to_snake_case(f"{type.name}") + if isinstance(type, Array): + return f"std::vector< {get_type_name(type.subtype, field_names)} >" + if isinstance(type, Map): + return f"std::unordered_map< std::string, {get_type_name(type.subtype, field_names)} >" + if type == "boolean": + return "bool" + if type == "number": + return "double" + if type == "integer": + return "int64_t" + if type == "string": + return "std::string" + if type == "json": + return "json" + name = _get_type_name(type) + if name in field_names: + return f"::gap::sarif::{name}" + return name + + +def sanitize(name: str) -> str: + return name.translate(str.maketrans({ + "-": "_", + ".": "_", + " ": "_", + })) + + +def to_snake_case(name: str) -> str: + tr = str.maketrans({ + letter: f"_{letter.lower()}" for letter in "QWERTYUIOPASDFGHJKLZXCVBNM" + }) + return name.translate(tr) + + +def to_pascal_case(name: str) -> str: + return name[0].upper() + name[1:] + + +def get_default_value(field: Field) -> str: + def stringify(val): + type = field.type + if isinstance(type, Array): + type = type.subtype + + if type == "string": + val = val.translate(str.maketrans({ + '"': '\\"', + "\r": "\\r", + "\n": "\\n", + "\\": "\\\\", + })) + return f'"{val}"' + if isinstance(type, Enum): + return f"{get_type_name(type, field.name)}::k{sanitize(to_pascal_case(val))}" + elif isinstance(val, bool): + return str(val).lower() + else: + return str(val) + if isinstance(field.default, list): + if len(field.default) == 0: + return "{}" + else: + return f"{{ {', '.join(map(stringify, field.default))} }}" + else: + return stringify(field.default) + + +def print_struct(elem: Struct, file): + elem_name = get_type_name(elem) + + print(file=file) + print(" //", file=file) + print(f" // {elem.description}", file=file) + print(" //", file=file) + print(f" struct {elem_name} {{", end="", file=file) + field_names = set(field.name for field in elem.fields) + for field in elem.fields: + print(file=file) + if field.description is not None: + print(" //", file=file) + print(f" // {field.description}", file=file) + print(" //", file=file) + type_name = get_type_name(field.type, field_names) + if field.required: + print(f" {type_name} {field.name};", file=file) + elif field.default is not None: + print(f" {type_name} {field.name} = {get_default_value(field)};", file=file) + elif isinstance(field.type, Array): + print(f" {type_name} {field.name} = {{}};", file=file) + else: + print(f" std::optional< {type_name} > {field.name} = std::nullopt;", file=file) + if elem.additional_props is not None: + print(file=file) + if elem.additional_props == "json": + print(" json additional_properties;", file=file) + else: + print(f" std::unordered_map< std::string, {get_type_name(elem.additional_props)} > additional_properties;", file=file) + + print(" };", file=file) + print(file=file) + print(f" void to_json(json &, const {elem_name} &);", file=file) + print(f" void from_json(const json &, {elem_name} &);", file=file) + + +def print_enum(elem: Enum, file): + elem_name = get_type_name(elem) + + print(file=file) + print(f" enum class {elem_name} {{", file=file) + for value in elem.values: + value_name = sanitize(to_pascal_case(value)) + print(f" k{value_name},", file=file) + print(" };", file=file) + print(file=file) + print(f" NLOHMANN_JSON_SERIALIZE_ENUM({elem_name}, {{", file=file) + for value in elem.values: + value_name = sanitize(to_pascal_case(value)) + print(f" {{ {elem_name}::k{value_name}, \"{value}\" }},", file=file) + print(" })", file=file) + + +def print_from_json(elem: Struct, file): + print(f" void from_json(const json &j, {get_type_name(elem)} &o) {{", file=file) + print(" for( const auto &[key, val] : j.items() ) {", file=file) + is_first = True + for field in elem.fields: + if is_first: + print(f" if ( key == \"{field.name}\" ) {{", file=file) + is_first = False + else: + print(f" else if ( key == \"{field.name}\" ) {{", file=file) + print(f" val.get_to(o.{field.name});", file=file) + print(" }", end="", file=file) + if elem.additional_props is not None: + print(" else {", file=file) + print(" val.get_to(o.additional_properties[key]);", file=file) + print(" }", end="", file=file) + print(file=file) + print(" }", file=file) + print(" }", file=file) + + +def print_to_json(elem: Struct, file): + print(f" void to_json(json &j, const {get_type_name(elem)} &o) {{", file=file) + for field in elem.fields: + if field.required: + print(f" j[\"{field.name}\"] = o.{field.name};", file=file) + elif field.default is not None: + if isinstance(field.default, list): + if len(field.default) == 0: + print(f" if ( !o.{field.name}.empty() ) {{", file=file) + else: + print(f" if ( o.{field.name} != decltype(o.{field.name}){get_default_value(field)} ) {{", file=file) + else: + print(f" if ( o.{field.name} != {get_default_value(field)} ) {{", file=file) + print(f" j[\"{field.name}\"] = o.{field.name};", file=file) + print(" }", file=file) + elif isinstance(field.type, Array): + print(f" if ( !o.{field.name}.empty() ) {{", file=file) + print(f" j[\"{field.name}\"] = o.{field.name};", file=file) + print(" }", file=file) + else: + print(f" if ( o.{field.name}.has_value() ) {{", file=file) + print(f" j[\"{field.name}\"] = *o.{field.name};", file=file) + print(" }", file=file) + if elem.additional_props is not None: + if elem.additional_props == "json": + print(" for ( auto &[key, val] : o.additional_properties.items() ) {", file=file) + else: + print(" for ( auto &[key, val] : o.additional_properties ) {", file=file) + print(" j[key] = val;", file=file) + print(" }", file=file) + print(" }", file=file) + + +if __name__ == "__main__": + import argparse + parser = argparse.ArgumentParser() + parser.add_argument("input_schema") + parser.add_argument("output_header") + parser.add_argument("output_source") + + args = parser.parse_args() + + with open(args.input_schema) as sarif: + sarif: Schema = json.load(sarif) + output = get_structs(sarif) + with open(args.output_header, mode="w") as output_header: + print("""// Copyright 2024-present, Trail of Bits, Inc. + +#pragma once + +// +// These definitions were generated from a JSON Schema description of SARIF +// found at https://github.com/microsoft/sarif-python-om/blob/7a84e8c2b2b9d8b9a8d25b1d376f039a0bf92a7c/sarif-schema-2.1.0.json +// +// The naming convention used is to convert all of the definition names from camelCase to snake_case, +// property names are kept as-is. +// +// Enum value names are converted from camelCase to PascalCase and prefixed with `k`. +// + +#include +#include +#include +#include +#include + +#include + +namespace gap::sarif +{ + using json = nlohmann::json;""", file=output_header) + for elem in output: + if isinstance(elem, ForwardRef): + print(f" struct {get_type_name(elem)};", file=output_header) + if isinstance(elem, Struct): + print_struct(elem, output_header) + if isinstance(elem, Enum): + print_enum(elem, output_header) + print("} // namespace gap::sarif", file=output_header) + + with open(args.output_source, mode="w") as output_source: + print("""// Copyright (c) 2024-present, Trail of Bits, Inc. + +#include + +namespace nlohmann { + template + struct adl_serializer< std::optional > { + static void from_json(const json& j, std::optional& opt) { + opt = std::optional{ j.get() }; + } + }; +} + +namespace gap::sarif {""", file=output_source) + for elem in output: + if not isinstance(elem, Struct): + continue + print_from_json(elem, output_source) + print_to_json(elem, output_source) + print("} // namespace gap::sarif", file=output_source) diff --git a/sarif/scripts/sarif-headergen/src/sarif_headergen/__init__.py b/sarif/scripts/sarif-headergen/src/sarif_headergen/__init__.py new file mode 100644 index 0000000..923b5d0 --- /dev/null +++ b/sarif/scripts/sarif-headergen/src/sarif_headergen/__init__.py @@ -0,0 +1,3 @@ +from .schema import Schema + +__all__ = [Schema] diff --git a/sarif/scripts/sarif-headergen/src/sarif_headergen/output.py b/sarif/scripts/sarif-headergen/src/sarif_headergen/output.py new file mode 100644 index 0000000..f1d141e --- /dev/null +++ b/sarif/scripts/sarif-headergen/src/sarif_headergen/output.py @@ -0,0 +1,143 @@ +from typing import Literal +from functools import cache +from dataclasses import dataclass + +from .schema import Schema + +type FieldType = ( + Literal["string", "number", "boolean", "integer", "json"] | "Array" | "Struct" | "Enum" | "ForwardRef" | "Map" +) + +@dataclass(frozen=True) +class Array: + subtype: FieldType + + +@dataclass(frozen=True) +class Field: + name: str + type: FieldType + description: str | None + required: bool + default: str | int | float | bool | list | None + + +@dataclass(frozen=True) +class Enum: + name: str + values: list[str] + + +@dataclass(frozen=True) +class Struct: + name: str + fields: list[Field] + description: str | None + additional_props: FieldType | None + +@dataclass(frozen=True) +class ForwardRef: + name: str + +@dataclass(frozen=True) +class Map: + subtype: FieldType + + +def get_structs(schema: Schema) -> list[Struct | Enum | ForwardRef]: + assert "definitions" in schema + + res: list[Struct | Enum | ForwardRef] = [] + defs: dict[str, FieldType] = {} + state: dict[str, Literal["visiting", "visited"]] = {} + + definitions = schema["definitions"] + + def get_field(name: str, field: Schema) -> FieldType: + if "$ref" in field: + return get_ref(field["$ref"]) + + if "type" in field: + if field["type"] in ("boolean", "number", "string", "integer"): + return field["type"] + if field["type"] == "array": + items = field["items"] + return Array(subtype=get_field(name, items)) + if field["type"] == "object": + if "properties" not in field and "additionalProperties" in field and field["additionalProperties"] != False: + if field["additionalProperties"] != True: + return Map(subtype=get_field(name, field["additionalProperties"])) + else: + return "json" + return make_struct(name, field) + raise Exception("Invalid type") + + if "enum" in field: + if name in defs: + return defs[name] + enum = Enum(name, field["enum"]) + res.append(enum) + defs[name] = enum + return enum + + raise Exception("Unknown field type") + + @cache + def get_ref(ref: str) -> FieldType: + assert ref.startswith("#/definitions/") + name = ref[len("#/definitions/") :] + if name in state: + if state[name] == "visiting": + if name in defs: + return defs[name] + else: + fwd = ForwardRef(name) + res.append(fwd) + defs[name] = fwd + return fwd + else: + return defs[name] + + state[name] = "visiting" + result = get_field(name, definitions[name]) + defs[name] = result + state[name] = "visited" + return result + + def make_struct(name: str, type: Schema): + assert type["type"] == "object" + additional_props: FieldType | None = None + if "additionalProperties" in type: + if type["additionalProperties"] == True: + additional_props = "json" + elif type["additionalProperties"] != False: + additional_props = get_field("additional_properties", type["additionalProperties"]) + fields: list[Field] = [] + if "properties" in type: + fields = [ + Field( + name=field_name, + type=get_field(field_name, field_type), + description=field_type.get("description", None), + required="required" in type and field_name in type["required"], + default=field_type.get("default", None), + ) + for field_name, field_type in type["properties"].items() + if field_name != "$schema" + ] + struct = Struct( + name, + fields=fields, + description=type.get("description", None), + additional_props=additional_props, + ) + res.append(struct) + defs[name] = struct + return struct + + make_struct("root", schema) + + for name in schema["definitions"]: + get_ref(f"#/definitions/{name}") + + return res diff --git a/sarif/scripts/sarif-headergen/src/sarif_headergen/schema.py b/sarif/scripts/sarif-headergen/src/sarif_headergen/schema.py new file mode 100644 index 0000000..c3d02dc --- /dev/null +++ b/sarif/scripts/sarif-headergen/src/sarif_headergen/schema.py @@ -0,0 +1,30 @@ +from typing import TypedDict, NotRequired, Union +from enum import Enum + + +class Type(str, Enum): + null = "null" + boolean = "boolean" + object = "object" + array = "array" + number = "number" + string = "string" + integer = "integer" + + +Schema = TypedDict( + "Schema", + { + "title": NotRequired[str], + "description": NotRequired[str], + "type": NotRequired[Type], + "$ref": NotRequired[str], + "items": NotRequired["Schema"], + "properties": NotRequired[dict[str, "Schema"]], + "additionalProperties": NotRequired[Union["Schema", bool]], + "definitions": NotRequired[dict[str, "Schema"]], + "enum": NotRequired[list[str]], + "required": NotRequired[list[str]], + "default": NotRequired[str | int | float | bool | list], + }, +) diff --git a/sarif/src/sarif.cpp b/sarif/src/sarif.cpp new file mode 100644 index 0000000..d93d9fb --- /dev/null +++ b/sarif/src/sarif.cpp @@ -0,0 +1,2424 @@ +// Copyright (c) 2024-present, Trail of Bits, Inc. + +#include + +namespace nlohmann { + template + struct adl_serializer< std::optional > { + static void from_json(const json& j, std::optional& opt) { + opt = std::optional{ j.get() }; + } + }; +} + +namespace gap::sarif { + void from_json(const json &j, property_bag &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "tags" ) { + val.get_to(o.tags); + } else { + val.get_to(o.additional_properties[key]); + } + } + } + void to_json(json &j, const property_bag &o) { + if ( !o.tags.empty() ) { + j["tags"] = o.tags; + } + for ( auto &[key, val] : o.additional_properties.items() ) { + j[key] = val; + } + } + void from_json(const json &j, multiformat_message_string &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "text" ) { + val.get_to(o.text); + } else if ( key == "markdown" ) { + val.get_to(o.markdown); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const multiformat_message_string &o) { + j["text"] = o.text; + if ( o.markdown.has_value() ) { + j["markdown"] = *o.markdown; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, reporting_configuration &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "enabled" ) { + val.get_to(o.enabled); + } else if ( key == "level" ) { + val.get_to(o.level); + } else if ( key == "rank" ) { + val.get_to(o.rank); + } else if ( key == "parameters" ) { + val.get_to(o.parameters); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const reporting_configuration &o) { + if ( o.enabled != true ) { + j["enabled"] = o.enabled; + } + if ( o.level != ::gap::sarif::level::kWarning ) { + j["level"] = o.level; + } + if ( o.rank != -1.0 ) { + j["rank"] = o.rank; + } + if ( o.parameters.has_value() ) { + j["parameters"] = *o.parameters; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, tool_component_reference &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const tool_component_reference &o) { + if ( o.name.has_value() ) { + j["name"] = *o.name; + } + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, reporting_descriptor_reference &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "toolComponent" ) { + val.get_to(o.toolComponent); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const reporting_descriptor_reference &o) { + if ( o.id.has_value() ) { + j["id"] = *o.id; + } + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.toolComponent.has_value() ) { + j["toolComponent"] = *o.toolComponent; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, message &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "text" ) { + val.get_to(o.text); + } else if ( key == "markdown" ) { + val.get_to(o.markdown); + } else if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "arguments" ) { + val.get_to(o.arguments); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const message &o) { + if ( o.text.has_value() ) { + j["text"] = *o.text; + } + if ( o.markdown.has_value() ) { + j["markdown"] = *o.markdown; + } + if ( o.id.has_value() ) { + j["id"] = *o.id; + } + if ( !o.arguments.empty() ) { + j["arguments"] = o.arguments; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, reporting_descriptor_relationship &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "target" ) { + val.get_to(o.target); + } else if ( key == "kinds" ) { + val.get_to(o.kinds); + } else if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const reporting_descriptor_relationship &o) { + j["target"] = o.target; + if ( o.kinds != decltype(o.kinds){ "relevant" } ) { + j["kinds"] = o.kinds; + } + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, reporting_descriptor &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "deprecatedIds" ) { + val.get_to(o.deprecatedIds); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "deprecatedGuids" ) { + val.get_to(o.deprecatedGuids); + } else if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "deprecatedNames" ) { + val.get_to(o.deprecatedNames); + } else if ( key == "shortDescription" ) { + val.get_to(o.shortDescription); + } else if ( key == "fullDescription" ) { + val.get_to(o.fullDescription); + } else if ( key == "messageStrings" ) { + val.get_to(o.messageStrings); + } else if ( key == "defaultConfiguration" ) { + val.get_to(o.defaultConfiguration); + } else if ( key == "helpUri" ) { + val.get_to(o.helpUri); + } else if ( key == "help" ) { + val.get_to(o.help); + } else if ( key == "relationships" ) { + val.get_to(o.relationships); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const reporting_descriptor &o) { + j["id"] = o.id; + if ( !o.deprecatedIds.empty() ) { + j["deprecatedIds"] = o.deprecatedIds; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( !o.deprecatedGuids.empty() ) { + j["deprecatedGuids"] = o.deprecatedGuids; + } + if ( o.name.has_value() ) { + j["name"] = *o.name; + } + if ( !o.deprecatedNames.empty() ) { + j["deprecatedNames"] = o.deprecatedNames; + } + if ( o.shortDescription.has_value() ) { + j["shortDescription"] = *o.shortDescription; + } + if ( o.fullDescription.has_value() ) { + j["fullDescription"] = *o.fullDescription; + } + if ( o.messageStrings.has_value() ) { + j["messageStrings"] = *o.messageStrings; + } + if ( o.defaultConfiguration.has_value() ) { + j["defaultConfiguration"] = *o.defaultConfiguration; + } + if ( o.helpUri.has_value() ) { + j["helpUri"] = *o.helpUri; + } + if ( o.help.has_value() ) { + j["help"] = *o.help; + } + if ( !o.relationships.empty() ) { + j["relationships"] = o.relationships; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, artifact_location &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "uri" ) { + val.get_to(o.uri); + } else if ( key == "uriBaseId" ) { + val.get_to(o.uriBaseId); + } else if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const artifact_location &o) { + if ( o.uri.has_value() ) { + j["uri"] = *o.uri; + } + if ( o.uriBaseId.has_value() ) { + j["uriBaseId"] = *o.uriBaseId; + } + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, translation_metadata &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "fullName" ) { + val.get_to(o.fullName); + } else if ( key == "shortDescription" ) { + val.get_to(o.shortDescription); + } else if ( key == "fullDescription" ) { + val.get_to(o.fullDescription); + } else if ( key == "downloadUri" ) { + val.get_to(o.downloadUri); + } else if ( key == "informationUri" ) { + val.get_to(o.informationUri); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const translation_metadata &o) { + j["name"] = o.name; + if ( o.fullName.has_value() ) { + j["fullName"] = *o.fullName; + } + if ( o.shortDescription.has_value() ) { + j["shortDescription"] = *o.shortDescription; + } + if ( o.fullDescription.has_value() ) { + j["fullDescription"] = *o.fullDescription; + } + if ( o.downloadUri.has_value() ) { + j["downloadUri"] = *o.downloadUri; + } + if ( o.informationUri.has_value() ) { + j["informationUri"] = *o.informationUri; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, tool_component &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "organization" ) { + val.get_to(o.organization); + } else if ( key == "product" ) { + val.get_to(o.product); + } else if ( key == "productSuite" ) { + val.get_to(o.productSuite); + } else if ( key == "shortDescription" ) { + val.get_to(o.shortDescription); + } else if ( key == "fullDescription" ) { + val.get_to(o.fullDescription); + } else if ( key == "fullName" ) { + val.get_to(o.fullName); + } else if ( key == "version" ) { + val.get_to(o.version); + } else if ( key == "semanticVersion" ) { + val.get_to(o.semanticVersion); + } else if ( key == "dottedQuadFileVersion" ) { + val.get_to(o.dottedQuadFileVersion); + } else if ( key == "releaseDateUtc" ) { + val.get_to(o.releaseDateUtc); + } else if ( key == "downloadUri" ) { + val.get_to(o.downloadUri); + } else if ( key == "informationUri" ) { + val.get_to(o.informationUri); + } else if ( key == "globalMessageStrings" ) { + val.get_to(o.globalMessageStrings); + } else if ( key == "notifications" ) { + val.get_to(o.notifications); + } else if ( key == "rules" ) { + val.get_to(o.rules); + } else if ( key == "taxa" ) { + val.get_to(o.taxa); + } else if ( key == "locations" ) { + val.get_to(o.locations); + } else if ( key == "language" ) { + val.get_to(o.language); + } else if ( key == "contents" ) { + val.get_to(o.contents); + } else if ( key == "isComprehensive" ) { + val.get_to(o.isComprehensive); + } else if ( key == "localizedDataSemanticVersion" ) { + val.get_to(o.localizedDataSemanticVersion); + } else if ( key == "minimumRequiredLocalizedDataSemanticVersion" ) { + val.get_to(o.minimumRequiredLocalizedDataSemanticVersion); + } else if ( key == "associatedComponent" ) { + val.get_to(o.associatedComponent); + } else if ( key == "translationMetadata" ) { + val.get_to(o.translationMetadata); + } else if ( key == "supportedTaxonomies" ) { + val.get_to(o.supportedTaxonomies); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const tool_component &o) { + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + j["name"] = o.name; + if ( o.organization.has_value() ) { + j["organization"] = *o.organization; + } + if ( o.product.has_value() ) { + j["product"] = *o.product; + } + if ( o.productSuite.has_value() ) { + j["productSuite"] = *o.productSuite; + } + if ( o.shortDescription.has_value() ) { + j["shortDescription"] = *o.shortDescription; + } + if ( o.fullDescription.has_value() ) { + j["fullDescription"] = *o.fullDescription; + } + if ( o.fullName.has_value() ) { + j["fullName"] = *o.fullName; + } + if ( o.version.has_value() ) { + j["version"] = *o.version; + } + if ( o.semanticVersion.has_value() ) { + j["semanticVersion"] = *o.semanticVersion; + } + if ( o.dottedQuadFileVersion.has_value() ) { + j["dottedQuadFileVersion"] = *o.dottedQuadFileVersion; + } + if ( o.releaseDateUtc.has_value() ) { + j["releaseDateUtc"] = *o.releaseDateUtc; + } + if ( o.downloadUri.has_value() ) { + j["downloadUri"] = *o.downloadUri; + } + if ( o.informationUri.has_value() ) { + j["informationUri"] = *o.informationUri; + } + if ( o.globalMessageStrings.has_value() ) { + j["globalMessageStrings"] = *o.globalMessageStrings; + } + if ( !o.notifications.empty() ) { + j["notifications"] = o.notifications; + } + if ( !o.rules.empty() ) { + j["rules"] = o.rules; + } + if ( !o.taxa.empty() ) { + j["taxa"] = o.taxa; + } + if ( !o.locations.empty() ) { + j["locations"] = o.locations; + } + if ( o.language != "en-US" ) { + j["language"] = o.language; + } + if ( o.contents != decltype(o.contents){ ::gap::sarif::contents::kLocalizedData, ::gap::sarif::contents::kNonLocalizedData } ) { + j["contents"] = o.contents; + } + if ( o.isComprehensive != false ) { + j["isComprehensive"] = o.isComprehensive; + } + if ( o.localizedDataSemanticVersion.has_value() ) { + j["localizedDataSemanticVersion"] = *o.localizedDataSemanticVersion; + } + if ( o.minimumRequiredLocalizedDataSemanticVersion.has_value() ) { + j["minimumRequiredLocalizedDataSemanticVersion"] = *o.minimumRequiredLocalizedDataSemanticVersion; + } + if ( o.associatedComponent.has_value() ) { + j["associatedComponent"] = *o.associatedComponent; + } + if ( o.translationMetadata.has_value() ) { + j["translationMetadata"] = *o.translationMetadata; + } + if ( !o.supportedTaxonomies.empty() ) { + j["supportedTaxonomies"] = o.supportedTaxonomies; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, tool &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "driver" ) { + val.get_to(o.driver); + } else if ( key == "extensions" ) { + val.get_to(o.extensions); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const tool &o) { + j["driver"] = o.driver; + if ( !o.extensions.empty() ) { + j["extensions"] = o.extensions; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, configuration_override &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "configuration" ) { + val.get_to(o.configuration); + } else if ( key == "descriptor" ) { + val.get_to(o.descriptor); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const configuration_override &o) { + j["configuration"] = o.configuration; + j["descriptor"] = o.descriptor; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, address &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "absoluteAddress" ) { + val.get_to(o.absoluteAddress); + } else if ( key == "relativeAddress" ) { + val.get_to(o.relativeAddress); + } else if ( key == "length" ) { + val.get_to(o.length); + } else if ( key == "kind" ) { + val.get_to(o.kind); + } else if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "fullyQualifiedName" ) { + val.get_to(o.fullyQualifiedName); + } else if ( key == "offsetFromParent" ) { + val.get_to(o.offsetFromParent); + } else if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "parentIndex" ) { + val.get_to(o.parentIndex); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const address &o) { + if ( o.absoluteAddress != -1 ) { + j["absoluteAddress"] = o.absoluteAddress; + } + if ( o.relativeAddress.has_value() ) { + j["relativeAddress"] = *o.relativeAddress; + } + if ( o.length.has_value() ) { + j["length"] = *o.length; + } + if ( o.kind.has_value() ) { + j["kind"] = *o.kind; + } + if ( o.name.has_value() ) { + j["name"] = *o.name; + } + if ( o.fullyQualifiedName.has_value() ) { + j["fullyQualifiedName"] = *o.fullyQualifiedName; + } + if ( o.offsetFromParent.has_value() ) { + j["offsetFromParent"] = *o.offsetFromParent; + } + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.parentIndex != -1 ) { + j["parentIndex"] = o.parentIndex; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, artifact_content &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "text" ) { + val.get_to(o.text); + } else if ( key == "binary" ) { + val.get_to(o.binary); + } else if ( key == "rendered" ) { + val.get_to(o.rendered); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const artifact_content &o) { + if ( o.text.has_value() ) { + j["text"] = *o.text; + } + if ( o.binary.has_value() ) { + j["binary"] = *o.binary; + } + if ( o.rendered.has_value() ) { + j["rendered"] = *o.rendered; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, region &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "startLine" ) { + val.get_to(o.startLine); + } else if ( key == "startColumn" ) { + val.get_to(o.startColumn); + } else if ( key == "endLine" ) { + val.get_to(o.endLine); + } else if ( key == "endColumn" ) { + val.get_to(o.endColumn); + } else if ( key == "charOffset" ) { + val.get_to(o.charOffset); + } else if ( key == "charLength" ) { + val.get_to(o.charLength); + } else if ( key == "byteOffset" ) { + val.get_to(o.byteOffset); + } else if ( key == "byteLength" ) { + val.get_to(o.byteLength); + } else if ( key == "snippet" ) { + val.get_to(o.snippet); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "sourceLanguage" ) { + val.get_to(o.sourceLanguage); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const region &o) { + if ( o.startLine.has_value() ) { + j["startLine"] = *o.startLine; + } + if ( o.startColumn.has_value() ) { + j["startColumn"] = *o.startColumn; + } + if ( o.endLine.has_value() ) { + j["endLine"] = *o.endLine; + } + if ( o.endColumn.has_value() ) { + j["endColumn"] = *o.endColumn; + } + if ( o.charOffset != -1 ) { + j["charOffset"] = o.charOffset; + } + if ( o.charLength.has_value() ) { + j["charLength"] = *o.charLength; + } + if ( o.byteOffset != -1 ) { + j["byteOffset"] = o.byteOffset; + } + if ( o.byteLength.has_value() ) { + j["byteLength"] = *o.byteLength; + } + if ( o.snippet.has_value() ) { + j["snippet"] = *o.snippet; + } + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( o.sourceLanguage.has_value() ) { + j["sourceLanguage"] = *o.sourceLanguage; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, physical_location &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "address" ) { + val.get_to(o.address); + } else if ( key == "artifactLocation" ) { + val.get_to(o.artifactLocation); + } else if ( key == "region" ) { + val.get_to(o.region); + } else if ( key == "contextRegion" ) { + val.get_to(o.contextRegion); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const physical_location &o) { + if ( o.address.has_value() ) { + j["address"] = *o.address; + } + if ( o.artifactLocation.has_value() ) { + j["artifactLocation"] = *o.artifactLocation; + } + if ( o.region.has_value() ) { + j["region"] = *o.region; + } + if ( o.contextRegion.has_value() ) { + j["contextRegion"] = *o.contextRegion; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, logical_location &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "name" ) { + val.get_to(o.name); + } else if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "fullyQualifiedName" ) { + val.get_to(o.fullyQualifiedName); + } else if ( key == "decoratedName" ) { + val.get_to(o.decoratedName); + } else if ( key == "parentIndex" ) { + val.get_to(o.parentIndex); + } else if ( key == "kind" ) { + val.get_to(o.kind); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const logical_location &o) { + if ( o.name.has_value() ) { + j["name"] = *o.name; + } + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.fullyQualifiedName.has_value() ) { + j["fullyQualifiedName"] = *o.fullyQualifiedName; + } + if ( o.decoratedName.has_value() ) { + j["decoratedName"] = *o.decoratedName; + } + if ( o.parentIndex != -1 ) { + j["parentIndex"] = o.parentIndex; + } + if ( o.kind.has_value() ) { + j["kind"] = *o.kind; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, location_relationship &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "target" ) { + val.get_to(o.target); + } else if ( key == "kinds" ) { + val.get_to(o.kinds); + } else if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const location_relationship &o) { + j["target"] = o.target; + if ( o.kinds != decltype(o.kinds){ "relevant" } ) { + j["kinds"] = o.kinds; + } + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, location &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "physicalLocation" ) { + val.get_to(o.physicalLocation); + } else if ( key == "logicalLocations" ) { + val.get_to(o.logicalLocations); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "annotations" ) { + val.get_to(o.annotations); + } else if ( key == "relationships" ) { + val.get_to(o.relationships); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const location &o) { + if ( o.id != -1 ) { + j["id"] = o.id; + } + if ( o.physicalLocation.has_value() ) { + j["physicalLocation"] = *o.physicalLocation; + } + if ( !o.logicalLocations.empty() ) { + j["logicalLocations"] = o.logicalLocations; + } + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( !o.annotations.empty() ) { + j["annotations"] = o.annotations; + } + if ( !o.relationships.empty() ) { + j["relationships"] = o.relationships; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, stack_frame &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "module" ) { + val.get_to(o.module); + } else if ( key == "threadId" ) { + val.get_to(o.threadId); + } else if ( key == "parameters" ) { + val.get_to(o.parameters); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const stack_frame &o) { + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( o.module.has_value() ) { + j["module"] = *o.module; + } + if ( o.threadId.has_value() ) { + j["threadId"] = *o.threadId; + } + if ( !o.parameters.empty() ) { + j["parameters"] = o.parameters; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, stack &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "frames" ) { + val.get_to(o.frames); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const stack &o) { + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + j["frames"] = o.frames; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, exception &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "kind" ) { + val.get_to(o.kind); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "stack" ) { + val.get_to(o.stack); + } else if ( key == "innerExceptions" ) { + val.get_to(o.innerExceptions); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const exception &o) { + if ( o.kind.has_value() ) { + j["kind"] = *o.kind; + } + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( o.stack.has_value() ) { + j["stack"] = *o.stack; + } + if ( !o.innerExceptions.empty() ) { + j["innerExceptions"] = o.innerExceptions; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, notification &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "locations" ) { + val.get_to(o.locations); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "level" ) { + val.get_to(o.level); + } else if ( key == "threadId" ) { + val.get_to(o.threadId); + } else if ( key == "timeUtc" ) { + val.get_to(o.timeUtc); + } else if ( key == "exception" ) { + val.get_to(o.exception); + } else if ( key == "descriptor" ) { + val.get_to(o.descriptor); + } else if ( key == "associatedRule" ) { + val.get_to(o.associatedRule); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const notification &o) { + if ( !o.locations.empty() ) { + j["locations"] = o.locations; + } + j["message"] = o.message; + if ( o.level != ::gap::sarif::level::kWarning ) { + j["level"] = o.level; + } + if ( o.threadId.has_value() ) { + j["threadId"] = *o.threadId; + } + if ( o.timeUtc.has_value() ) { + j["timeUtc"] = *o.timeUtc; + } + if ( o.exception.has_value() ) { + j["exception"] = *o.exception; + } + if ( o.descriptor.has_value() ) { + j["descriptor"] = *o.descriptor; + } + if ( o.associatedRule.has_value() ) { + j["associatedRule"] = *o.associatedRule; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, invocation &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "commandLine" ) { + val.get_to(o.commandLine); + } else if ( key == "arguments" ) { + val.get_to(o.arguments); + } else if ( key == "responseFiles" ) { + val.get_to(o.responseFiles); + } else if ( key == "startTimeUtc" ) { + val.get_to(o.startTimeUtc); + } else if ( key == "endTimeUtc" ) { + val.get_to(o.endTimeUtc); + } else if ( key == "exitCode" ) { + val.get_to(o.exitCode); + } else if ( key == "ruleConfigurationOverrides" ) { + val.get_to(o.ruleConfigurationOverrides); + } else if ( key == "notificationConfigurationOverrides" ) { + val.get_to(o.notificationConfigurationOverrides); + } else if ( key == "toolExecutionNotifications" ) { + val.get_to(o.toolExecutionNotifications); + } else if ( key == "toolConfigurationNotifications" ) { + val.get_to(o.toolConfigurationNotifications); + } else if ( key == "exitCodeDescription" ) { + val.get_to(o.exitCodeDescription); + } else if ( key == "exitSignalName" ) { + val.get_to(o.exitSignalName); + } else if ( key == "exitSignalNumber" ) { + val.get_to(o.exitSignalNumber); + } else if ( key == "processStartFailureMessage" ) { + val.get_to(o.processStartFailureMessage); + } else if ( key == "executionSuccessful" ) { + val.get_to(o.executionSuccessful); + } else if ( key == "machine" ) { + val.get_to(o.machine); + } else if ( key == "account" ) { + val.get_to(o.account); + } else if ( key == "processId" ) { + val.get_to(o.processId); + } else if ( key == "executableLocation" ) { + val.get_to(o.executableLocation); + } else if ( key == "workingDirectory" ) { + val.get_to(o.workingDirectory); + } else if ( key == "environmentVariables" ) { + val.get_to(o.environmentVariables); + } else if ( key == "stdin" ) { + val.get_to(o.stdin); + } else if ( key == "stdout" ) { + val.get_to(o.stdout); + } else if ( key == "stderr" ) { + val.get_to(o.stderr); + } else if ( key == "stdoutStderr" ) { + val.get_to(o.stdoutStderr); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const invocation &o) { + if ( o.commandLine.has_value() ) { + j["commandLine"] = *o.commandLine; + } + if ( !o.arguments.empty() ) { + j["arguments"] = o.arguments; + } + if ( !o.responseFiles.empty() ) { + j["responseFiles"] = o.responseFiles; + } + if ( o.startTimeUtc.has_value() ) { + j["startTimeUtc"] = *o.startTimeUtc; + } + if ( o.endTimeUtc.has_value() ) { + j["endTimeUtc"] = *o.endTimeUtc; + } + if ( o.exitCode.has_value() ) { + j["exitCode"] = *o.exitCode; + } + if ( !o.ruleConfigurationOverrides.empty() ) { + j["ruleConfigurationOverrides"] = o.ruleConfigurationOverrides; + } + if ( !o.notificationConfigurationOverrides.empty() ) { + j["notificationConfigurationOverrides"] = o.notificationConfigurationOverrides; + } + if ( !o.toolExecutionNotifications.empty() ) { + j["toolExecutionNotifications"] = o.toolExecutionNotifications; + } + if ( !o.toolConfigurationNotifications.empty() ) { + j["toolConfigurationNotifications"] = o.toolConfigurationNotifications; + } + if ( o.exitCodeDescription.has_value() ) { + j["exitCodeDescription"] = *o.exitCodeDescription; + } + if ( o.exitSignalName.has_value() ) { + j["exitSignalName"] = *o.exitSignalName; + } + if ( o.exitSignalNumber.has_value() ) { + j["exitSignalNumber"] = *o.exitSignalNumber; + } + if ( o.processStartFailureMessage.has_value() ) { + j["processStartFailureMessage"] = *o.processStartFailureMessage; + } + j["executionSuccessful"] = o.executionSuccessful; + if ( o.machine.has_value() ) { + j["machine"] = *o.machine; + } + if ( o.account.has_value() ) { + j["account"] = *o.account; + } + if ( o.processId.has_value() ) { + j["processId"] = *o.processId; + } + if ( o.executableLocation.has_value() ) { + j["executableLocation"] = *o.executableLocation; + } + if ( o.workingDirectory.has_value() ) { + j["workingDirectory"] = *o.workingDirectory; + } + if ( o.environmentVariables.has_value() ) { + j["environmentVariables"] = *o.environmentVariables; + } + if ( o.stdin.has_value() ) { + j["stdin"] = *o.stdin; + } + if ( o.stdout.has_value() ) { + j["stdout"] = *o.stdout; + } + if ( o.stderr.has_value() ) { + j["stderr"] = *o.stderr; + } + if ( o.stdoutStderr.has_value() ) { + j["stdoutStderr"] = *o.stdoutStderr; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, conversion &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "tool" ) { + val.get_to(o.tool); + } else if ( key == "invocation" ) { + val.get_to(o.invocation); + } else if ( key == "analysisToolLogFiles" ) { + val.get_to(o.analysisToolLogFiles); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const conversion &o) { + j["tool"] = o.tool; + if ( o.invocation.has_value() ) { + j["invocation"] = *o.invocation; + } + if ( !o.analysisToolLogFiles.empty() ) { + j["analysisToolLogFiles"] = o.analysisToolLogFiles; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, version_control_details &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "repositoryUri" ) { + val.get_to(o.repositoryUri); + } else if ( key == "revisionId" ) { + val.get_to(o.revisionId); + } else if ( key == "branch" ) { + val.get_to(o.branch); + } else if ( key == "revisionTag" ) { + val.get_to(o.revisionTag); + } else if ( key == "asOfTimeUtc" ) { + val.get_to(o.asOfTimeUtc); + } else if ( key == "mappedTo" ) { + val.get_to(o.mappedTo); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const version_control_details &o) { + j["repositoryUri"] = o.repositoryUri; + if ( o.revisionId.has_value() ) { + j["revisionId"] = *o.revisionId; + } + if ( o.branch.has_value() ) { + j["branch"] = *o.branch; + } + if ( o.revisionTag.has_value() ) { + j["revisionTag"] = *o.revisionTag; + } + if ( o.asOfTimeUtc.has_value() ) { + j["asOfTimeUtc"] = *o.asOfTimeUtc; + } + if ( o.mappedTo.has_value() ) { + j["mappedTo"] = *o.mappedTo; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, artifact &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "parentIndex" ) { + val.get_to(o.parentIndex); + } else if ( key == "offset" ) { + val.get_to(o.offset); + } else if ( key == "length" ) { + val.get_to(o.length); + } else if ( key == "roles" ) { + val.get_to(o.roles); + } else if ( key == "mimeType" ) { + val.get_to(o.mimeType); + } else if ( key == "contents" ) { + val.get_to(o.contents); + } else if ( key == "encoding" ) { + val.get_to(o.encoding); + } else if ( key == "sourceLanguage" ) { + val.get_to(o.sourceLanguage); + } else if ( key == "hashes" ) { + val.get_to(o.hashes); + } else if ( key == "lastModifiedTimeUtc" ) { + val.get_to(o.lastModifiedTimeUtc); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const artifact &o) { + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( o.parentIndex != -1 ) { + j["parentIndex"] = o.parentIndex; + } + if ( o.offset.has_value() ) { + j["offset"] = *o.offset; + } + if ( o.length != -1 ) { + j["length"] = o.length; + } + if ( !o.roles.empty() ) { + j["roles"] = o.roles; + } + if ( o.mimeType.has_value() ) { + j["mimeType"] = *o.mimeType; + } + if ( o.contents.has_value() ) { + j["contents"] = *o.contents; + } + if ( o.encoding.has_value() ) { + j["encoding"] = *o.encoding; + } + if ( o.sourceLanguage.has_value() ) { + j["sourceLanguage"] = *o.sourceLanguage; + } + if ( o.hashes.has_value() ) { + j["hashes"] = *o.hashes; + } + if ( o.lastModifiedTimeUtc.has_value() ) { + j["lastModifiedTimeUtc"] = *o.lastModifiedTimeUtc; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, node &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "label" ) { + val.get_to(o.label); + } else if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "children" ) { + val.get_to(o.children); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const node &o) { + j["id"] = o.id; + if ( o.label.has_value() ) { + j["label"] = *o.label; + } + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( !o.children.empty() ) { + j["children"] = o.children; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, edge &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "label" ) { + val.get_to(o.label); + } else if ( key == "sourceNodeId" ) { + val.get_to(o.sourceNodeId); + } else if ( key == "targetNodeId" ) { + val.get_to(o.targetNodeId); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const edge &o) { + j["id"] = o.id; + if ( o.label.has_value() ) { + j["label"] = *o.label; + } + j["sourceNodeId"] = o.sourceNodeId; + j["targetNodeId"] = o.targetNodeId; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, graph &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "nodes" ) { + val.get_to(o.nodes); + } else if ( key == "edges" ) { + val.get_to(o.edges); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const graph &o) { + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( !o.nodes.empty() ) { + j["nodes"] = o.nodes; + } + if ( !o.edges.empty() ) { + j["edges"] = o.edges; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, web_request &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "protocol" ) { + val.get_to(o.protocol); + } else if ( key == "version" ) { + val.get_to(o.version); + } else if ( key == "target" ) { + val.get_to(o.target); + } else if ( key == "method" ) { + val.get_to(o.method); + } else if ( key == "headers" ) { + val.get_to(o.headers); + } else if ( key == "parameters" ) { + val.get_to(o.parameters); + } else if ( key == "body" ) { + val.get_to(o.body); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const web_request &o) { + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.protocol.has_value() ) { + j["protocol"] = *o.protocol; + } + if ( o.version.has_value() ) { + j["version"] = *o.version; + } + if ( o.target.has_value() ) { + j["target"] = *o.target; + } + if ( o.method.has_value() ) { + j["method"] = *o.method; + } + if ( o.headers.has_value() ) { + j["headers"] = *o.headers; + } + if ( o.parameters.has_value() ) { + j["parameters"] = *o.parameters; + } + if ( o.body.has_value() ) { + j["body"] = *o.body; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, web_response &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "protocol" ) { + val.get_to(o.protocol); + } else if ( key == "version" ) { + val.get_to(o.version); + } else if ( key == "statusCode" ) { + val.get_to(o.statusCode); + } else if ( key == "reasonPhrase" ) { + val.get_to(o.reasonPhrase); + } else if ( key == "headers" ) { + val.get_to(o.headers); + } else if ( key == "body" ) { + val.get_to(o.body); + } else if ( key == "noResponseReceived" ) { + val.get_to(o.noResponseReceived); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const web_response &o) { + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.protocol.has_value() ) { + j["protocol"] = *o.protocol; + } + if ( o.version.has_value() ) { + j["version"] = *o.version; + } + if ( o.statusCode.has_value() ) { + j["statusCode"] = *o.statusCode; + } + if ( o.reasonPhrase.has_value() ) { + j["reasonPhrase"] = *o.reasonPhrase; + } + if ( o.headers.has_value() ) { + j["headers"] = *o.headers; + } + if ( o.body.has_value() ) { + j["body"] = *o.body; + } + if ( o.noResponseReceived != false ) { + j["noResponseReceived"] = o.noResponseReceived; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, thread_flow_location &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "index" ) { + val.get_to(o.index); + } else if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "stack" ) { + val.get_to(o.stack); + } else if ( key == "kinds" ) { + val.get_to(o.kinds); + } else if ( key == "taxa" ) { + val.get_to(o.taxa); + } else if ( key == "module" ) { + val.get_to(o.module); + } else if ( key == "state" ) { + val.get_to(o.state); + } else if ( key == "nestingLevel" ) { + val.get_to(o.nestingLevel); + } else if ( key == "executionOrder" ) { + val.get_to(o.executionOrder); + } else if ( key == "executionTimeUtc" ) { + val.get_to(o.executionTimeUtc); + } else if ( key == "importance" ) { + val.get_to(o.importance); + } else if ( key == "webRequest" ) { + val.get_to(o.webRequest); + } else if ( key == "webResponse" ) { + val.get_to(o.webResponse); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const thread_flow_location &o) { + if ( o.index != -1 ) { + j["index"] = o.index; + } + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( o.stack.has_value() ) { + j["stack"] = *o.stack; + } + if ( !o.kinds.empty() ) { + j["kinds"] = o.kinds; + } + if ( !o.taxa.empty() ) { + j["taxa"] = o.taxa; + } + if ( o.module.has_value() ) { + j["module"] = *o.module; + } + if ( o.state.has_value() ) { + j["state"] = *o.state; + } + if ( o.nestingLevel.has_value() ) { + j["nestingLevel"] = *o.nestingLevel; + } + if ( o.executionOrder != -1 ) { + j["executionOrder"] = o.executionOrder; + } + if ( o.executionTimeUtc.has_value() ) { + j["executionTimeUtc"] = *o.executionTimeUtc; + } + if ( o.importance != ::gap::sarif::importance::kImportant ) { + j["importance"] = o.importance; + } + if ( o.webRequest.has_value() ) { + j["webRequest"] = *o.webRequest; + } + if ( o.webResponse.has_value() ) { + j["webResponse"] = *o.webResponse; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, thread_flow &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "initialState" ) { + val.get_to(o.initialState); + } else if ( key == "immutableState" ) { + val.get_to(o.immutableState); + } else if ( key == "locations" ) { + val.get_to(o.locations); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const thread_flow &o) { + if ( o.id.has_value() ) { + j["id"] = *o.id; + } + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( o.initialState.has_value() ) { + j["initialState"] = *o.initialState; + } + if ( o.immutableState.has_value() ) { + j["immutableState"] = *o.immutableState; + } + j["locations"] = o.locations; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, code_flow &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "threadFlows" ) { + val.get_to(o.threadFlows); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const code_flow &o) { + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + j["threadFlows"] = o.threadFlows; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, edge_traversal &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "edgeId" ) { + val.get_to(o.edgeId); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "finalState" ) { + val.get_to(o.finalState); + } else if ( key == "stepOverEdgeCount" ) { + val.get_to(o.stepOverEdgeCount); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const edge_traversal &o) { + j["edgeId"] = o.edgeId; + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( o.finalState.has_value() ) { + j["finalState"] = *o.finalState; + } + if ( o.stepOverEdgeCount.has_value() ) { + j["stepOverEdgeCount"] = *o.stepOverEdgeCount; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, graph_traversal &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "runGraphIndex" ) { + val.get_to(o.runGraphIndex); + } else if ( key == "resultGraphIndex" ) { + val.get_to(o.resultGraphIndex); + } else if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "initialState" ) { + val.get_to(o.initialState); + } else if ( key == "immutableState" ) { + val.get_to(o.immutableState); + } else if ( key == "edgeTraversals" ) { + val.get_to(o.edgeTraversals); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const graph_traversal &o) { + if ( o.runGraphIndex != -1 ) { + j["runGraphIndex"] = o.runGraphIndex; + } + if ( o.resultGraphIndex != -1 ) { + j["resultGraphIndex"] = o.resultGraphIndex; + } + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.initialState.has_value() ) { + j["initialState"] = *o.initialState; + } + if ( o.immutableState.has_value() ) { + j["immutableState"] = *o.immutableState; + } + if ( !o.edgeTraversals.empty() ) { + j["edgeTraversals"] = o.edgeTraversals; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, suppression &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "kind" ) { + val.get_to(o.kind); + } else if ( key == "state" ) { + val.get_to(o.state); + } else if ( key == "justification" ) { + val.get_to(o.justification); + } else if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const suppression &o) { + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + j["kind"] = o.kind; + if ( o.state.has_value() ) { + j["state"] = *o.state; + } + if ( o.justification.has_value() ) { + j["justification"] = *o.justification; + } + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, rectangle &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "top" ) { + val.get_to(o.top); + } else if ( key == "left" ) { + val.get_to(o.left); + } else if ( key == "bottom" ) { + val.get_to(o.bottom); + } else if ( key == "right" ) { + val.get_to(o.right); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const rectangle &o) { + if ( o.top.has_value() ) { + j["top"] = *o.top; + } + if ( o.left.has_value() ) { + j["left"] = *o.left; + } + if ( o.bottom.has_value() ) { + j["bottom"] = *o.bottom; + } + if ( o.right.has_value() ) { + j["right"] = *o.right; + } + if ( o.message.has_value() ) { + j["message"] = *o.message; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, attachment &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "artifactLocation" ) { + val.get_to(o.artifactLocation); + } else if ( key == "regions" ) { + val.get_to(o.regions); + } else if ( key == "rectangles" ) { + val.get_to(o.rectangles); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const attachment &o) { + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + j["artifactLocation"] = o.artifactLocation; + if ( !o.regions.empty() ) { + j["regions"] = o.regions; + } + if ( !o.rectangles.empty() ) { + j["rectangles"] = o.rectangles; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, result_provenance &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "firstDetectionTimeUtc" ) { + val.get_to(o.firstDetectionTimeUtc); + } else if ( key == "lastDetectionTimeUtc" ) { + val.get_to(o.lastDetectionTimeUtc); + } else if ( key == "firstDetectionRunGuid" ) { + val.get_to(o.firstDetectionRunGuid); + } else if ( key == "lastDetectionRunGuid" ) { + val.get_to(o.lastDetectionRunGuid); + } else if ( key == "invocationIndex" ) { + val.get_to(o.invocationIndex); + } else if ( key == "conversionSources" ) { + val.get_to(o.conversionSources); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const result_provenance &o) { + if ( o.firstDetectionTimeUtc.has_value() ) { + j["firstDetectionTimeUtc"] = *o.firstDetectionTimeUtc; + } + if ( o.lastDetectionTimeUtc.has_value() ) { + j["lastDetectionTimeUtc"] = *o.lastDetectionTimeUtc; + } + if ( o.firstDetectionRunGuid.has_value() ) { + j["firstDetectionRunGuid"] = *o.firstDetectionRunGuid; + } + if ( o.lastDetectionRunGuid.has_value() ) { + j["lastDetectionRunGuid"] = *o.lastDetectionRunGuid; + } + if ( o.invocationIndex != -1 ) { + j["invocationIndex"] = o.invocationIndex; + } + if ( !o.conversionSources.empty() ) { + j["conversionSources"] = o.conversionSources; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, replacement &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "deletedRegion" ) { + val.get_to(o.deletedRegion); + } else if ( key == "insertedContent" ) { + val.get_to(o.insertedContent); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const replacement &o) { + j["deletedRegion"] = o.deletedRegion; + if ( o.insertedContent.has_value() ) { + j["insertedContent"] = *o.insertedContent; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, artifact_change &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "artifactLocation" ) { + val.get_to(o.artifactLocation); + } else if ( key == "replacements" ) { + val.get_to(o.replacements); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const artifact_change &o) { + j["artifactLocation"] = o.artifactLocation; + j["replacements"] = o.replacements; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, fix &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "artifactChanges" ) { + val.get_to(o.artifactChanges); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const fix &o) { + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + j["artifactChanges"] = o.artifactChanges; + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, result &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "ruleId" ) { + val.get_to(o.ruleId); + } else if ( key == "ruleIndex" ) { + val.get_to(o.ruleIndex); + } else if ( key == "rule" ) { + val.get_to(o.rule); + } else if ( key == "kind" ) { + val.get_to(o.kind); + } else if ( key == "level" ) { + val.get_to(o.level); + } else if ( key == "message" ) { + val.get_to(o.message); + } else if ( key == "analysisTarget" ) { + val.get_to(o.analysisTarget); + } else if ( key == "locations" ) { + val.get_to(o.locations); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "correlationGuid" ) { + val.get_to(o.correlationGuid); + } else if ( key == "occurrenceCount" ) { + val.get_to(o.occurrenceCount); + } else if ( key == "partialFingerprints" ) { + val.get_to(o.partialFingerprints); + } else if ( key == "fingerprints" ) { + val.get_to(o.fingerprints); + } else if ( key == "stacks" ) { + val.get_to(o.stacks); + } else if ( key == "codeFlows" ) { + val.get_to(o.codeFlows); + } else if ( key == "graphs" ) { + val.get_to(o.graphs); + } else if ( key == "graphTraversals" ) { + val.get_to(o.graphTraversals); + } else if ( key == "relatedLocations" ) { + val.get_to(o.relatedLocations); + } else if ( key == "suppressions" ) { + val.get_to(o.suppressions); + } else if ( key == "baselineState" ) { + val.get_to(o.baselineState); + } else if ( key == "rank" ) { + val.get_to(o.rank); + } else if ( key == "attachments" ) { + val.get_to(o.attachments); + } else if ( key == "hostedViewerUri" ) { + val.get_to(o.hostedViewerUri); + } else if ( key == "workItemUris" ) { + val.get_to(o.workItemUris); + } else if ( key == "provenance" ) { + val.get_to(o.provenance); + } else if ( key == "fixes" ) { + val.get_to(o.fixes); + } else if ( key == "taxa" ) { + val.get_to(o.taxa); + } else if ( key == "webRequest" ) { + val.get_to(o.webRequest); + } else if ( key == "webResponse" ) { + val.get_to(o.webResponse); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const result &o) { + if ( o.ruleId.has_value() ) { + j["ruleId"] = *o.ruleId; + } + if ( o.ruleIndex != -1 ) { + j["ruleIndex"] = o.ruleIndex; + } + if ( o.rule.has_value() ) { + j["rule"] = *o.rule; + } + if ( o.kind != ::gap::sarif::kind::kFail ) { + j["kind"] = o.kind; + } + if ( o.level != ::gap::sarif::level::kWarning ) { + j["level"] = o.level; + } + j["message"] = o.message; + if ( o.analysisTarget.has_value() ) { + j["analysisTarget"] = *o.analysisTarget; + } + if ( !o.locations.empty() ) { + j["locations"] = o.locations; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.correlationGuid.has_value() ) { + j["correlationGuid"] = *o.correlationGuid; + } + if ( o.occurrenceCount.has_value() ) { + j["occurrenceCount"] = *o.occurrenceCount; + } + if ( o.partialFingerprints.has_value() ) { + j["partialFingerprints"] = *o.partialFingerprints; + } + if ( o.fingerprints.has_value() ) { + j["fingerprints"] = *o.fingerprints; + } + if ( !o.stacks.empty() ) { + j["stacks"] = o.stacks; + } + if ( !o.codeFlows.empty() ) { + j["codeFlows"] = o.codeFlows; + } + if ( !o.graphs.empty() ) { + j["graphs"] = o.graphs; + } + if ( !o.graphTraversals.empty() ) { + j["graphTraversals"] = o.graphTraversals; + } + if ( !o.relatedLocations.empty() ) { + j["relatedLocations"] = o.relatedLocations; + } + if ( !o.suppressions.empty() ) { + j["suppressions"] = o.suppressions; + } + if ( o.baselineState.has_value() ) { + j["baselineState"] = *o.baselineState; + } + if ( o.rank != -1.0 ) { + j["rank"] = o.rank; + } + if ( !o.attachments.empty() ) { + j["attachments"] = o.attachments; + } + if ( o.hostedViewerUri.has_value() ) { + j["hostedViewerUri"] = *o.hostedViewerUri; + } + if ( !o.workItemUris.empty() ) { + j["workItemUris"] = o.workItemUris; + } + if ( o.provenance.has_value() ) { + j["provenance"] = *o.provenance; + } + if ( !o.fixes.empty() ) { + j["fixes"] = o.fixes; + } + if ( !o.taxa.empty() ) { + j["taxa"] = o.taxa; + } + if ( o.webRequest.has_value() ) { + j["webRequest"] = *o.webRequest; + } + if ( o.webResponse.has_value() ) { + j["webResponse"] = *o.webResponse; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, run_automation_details &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "description" ) { + val.get_to(o.description); + } else if ( key == "id" ) { + val.get_to(o.id); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "correlationGuid" ) { + val.get_to(o.correlationGuid); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const run_automation_details &o) { + if ( o.description.has_value() ) { + j["description"] = *o.description; + } + if ( o.id.has_value() ) { + j["id"] = *o.id; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.correlationGuid.has_value() ) { + j["correlationGuid"] = *o.correlationGuid; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, external_property_file_reference &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "location" ) { + val.get_to(o.location); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "itemCount" ) { + val.get_to(o.itemCount); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const external_property_file_reference &o) { + if ( o.location.has_value() ) { + j["location"] = *o.location; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.itemCount != -1 ) { + j["itemCount"] = o.itemCount; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, external_property_file_references &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "conversion" ) { + val.get_to(o.conversion); + } else if ( key == "graphs" ) { + val.get_to(o.graphs); + } else if ( key == "externalizedProperties" ) { + val.get_to(o.externalizedProperties); + } else if ( key == "artifacts" ) { + val.get_to(o.artifacts); + } else if ( key == "invocations" ) { + val.get_to(o.invocations); + } else if ( key == "logicalLocations" ) { + val.get_to(o.logicalLocations); + } else if ( key == "threadFlowLocations" ) { + val.get_to(o.threadFlowLocations); + } else if ( key == "results" ) { + val.get_to(o.results); + } else if ( key == "taxonomies" ) { + val.get_to(o.taxonomies); + } else if ( key == "addresses" ) { + val.get_to(o.addresses); + } else if ( key == "driver" ) { + val.get_to(o.driver); + } else if ( key == "extensions" ) { + val.get_to(o.extensions); + } else if ( key == "policies" ) { + val.get_to(o.policies); + } else if ( key == "translations" ) { + val.get_to(o.translations); + } else if ( key == "webRequests" ) { + val.get_to(o.webRequests); + } else if ( key == "webResponses" ) { + val.get_to(o.webResponses); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const external_property_file_references &o) { + if ( o.conversion.has_value() ) { + j["conversion"] = *o.conversion; + } + if ( !o.graphs.empty() ) { + j["graphs"] = o.graphs; + } + if ( o.externalizedProperties.has_value() ) { + j["externalizedProperties"] = *o.externalizedProperties; + } + if ( !o.artifacts.empty() ) { + j["artifacts"] = o.artifacts; + } + if ( !o.invocations.empty() ) { + j["invocations"] = o.invocations; + } + if ( !o.logicalLocations.empty() ) { + j["logicalLocations"] = o.logicalLocations; + } + if ( !o.threadFlowLocations.empty() ) { + j["threadFlowLocations"] = o.threadFlowLocations; + } + if ( !o.results.empty() ) { + j["results"] = o.results; + } + if ( !o.taxonomies.empty() ) { + j["taxonomies"] = o.taxonomies; + } + if ( !o.addresses.empty() ) { + j["addresses"] = o.addresses; + } + if ( o.driver.has_value() ) { + j["driver"] = *o.driver; + } + if ( !o.extensions.empty() ) { + j["extensions"] = o.extensions; + } + if ( !o.policies.empty() ) { + j["policies"] = o.policies; + } + if ( !o.translations.empty() ) { + j["translations"] = o.translations; + } + if ( !o.webRequests.empty() ) { + j["webRequests"] = o.webRequests; + } + if ( !o.webResponses.empty() ) { + j["webResponses"] = o.webResponses; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, special_locations &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "displayBase" ) { + val.get_to(o.displayBase); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const special_locations &o) { + if ( o.displayBase.has_value() ) { + j["displayBase"] = *o.displayBase; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, run &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "tool" ) { + val.get_to(o.tool); + } else if ( key == "invocations" ) { + val.get_to(o.invocations); + } else if ( key == "conversion" ) { + val.get_to(o.conversion); + } else if ( key == "language" ) { + val.get_to(o.language); + } else if ( key == "versionControlProvenance" ) { + val.get_to(o.versionControlProvenance); + } else if ( key == "originalUriBaseIds" ) { + val.get_to(o.originalUriBaseIds); + } else if ( key == "artifacts" ) { + val.get_to(o.artifacts); + } else if ( key == "logicalLocations" ) { + val.get_to(o.logicalLocations); + } else if ( key == "graphs" ) { + val.get_to(o.graphs); + } else if ( key == "results" ) { + val.get_to(o.results); + } else if ( key == "automationDetails" ) { + val.get_to(o.automationDetails); + } else if ( key == "runAggregates" ) { + val.get_to(o.runAggregates); + } else if ( key == "baselineGuid" ) { + val.get_to(o.baselineGuid); + } else if ( key == "redactionTokens" ) { + val.get_to(o.redactionTokens); + } else if ( key == "defaultEncoding" ) { + val.get_to(o.defaultEncoding); + } else if ( key == "defaultSourceLanguage" ) { + val.get_to(o.defaultSourceLanguage); + } else if ( key == "newlineSequences" ) { + val.get_to(o.newlineSequences); + } else if ( key == "columnKind" ) { + val.get_to(o.columnKind); + } else if ( key == "externalPropertyFileReferences" ) { + val.get_to(o.externalPropertyFileReferences); + } else if ( key == "threadFlowLocations" ) { + val.get_to(o.threadFlowLocations); + } else if ( key == "taxonomies" ) { + val.get_to(o.taxonomies); + } else if ( key == "addresses" ) { + val.get_to(o.addresses); + } else if ( key == "translations" ) { + val.get_to(o.translations); + } else if ( key == "policies" ) { + val.get_to(o.policies); + } else if ( key == "webRequests" ) { + val.get_to(o.webRequests); + } else if ( key == "webResponses" ) { + val.get_to(o.webResponses); + } else if ( key == "specialLocations" ) { + val.get_to(o.specialLocations); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const run &o) { + j["tool"] = o.tool; + if ( !o.invocations.empty() ) { + j["invocations"] = o.invocations; + } + if ( o.conversion.has_value() ) { + j["conversion"] = *o.conversion; + } + if ( o.language != "en-US" ) { + j["language"] = o.language; + } + if ( !o.versionControlProvenance.empty() ) { + j["versionControlProvenance"] = o.versionControlProvenance; + } + if ( o.originalUriBaseIds.has_value() ) { + j["originalUriBaseIds"] = *o.originalUriBaseIds; + } + if ( !o.artifacts.empty() ) { + j["artifacts"] = o.artifacts; + } + if ( !o.logicalLocations.empty() ) { + j["logicalLocations"] = o.logicalLocations; + } + if ( !o.graphs.empty() ) { + j["graphs"] = o.graphs; + } + if ( !o.results.empty() ) { + j["results"] = o.results; + } + if ( o.automationDetails.has_value() ) { + j["automationDetails"] = *o.automationDetails; + } + if ( !o.runAggregates.empty() ) { + j["runAggregates"] = o.runAggregates; + } + if ( o.baselineGuid.has_value() ) { + j["baselineGuid"] = *o.baselineGuid; + } + if ( !o.redactionTokens.empty() ) { + j["redactionTokens"] = o.redactionTokens; + } + if ( o.defaultEncoding.has_value() ) { + j["defaultEncoding"] = *o.defaultEncoding; + } + if ( o.defaultSourceLanguage.has_value() ) { + j["defaultSourceLanguage"] = *o.defaultSourceLanguage; + } + if ( o.newlineSequences != decltype(o.newlineSequences){ "\r\n", "\n" } ) { + j["newlineSequences"] = o.newlineSequences; + } + if ( o.columnKind.has_value() ) { + j["columnKind"] = *o.columnKind; + } + if ( o.externalPropertyFileReferences.has_value() ) { + j["externalPropertyFileReferences"] = *o.externalPropertyFileReferences; + } + if ( !o.threadFlowLocations.empty() ) { + j["threadFlowLocations"] = o.threadFlowLocations; + } + if ( !o.taxonomies.empty() ) { + j["taxonomies"] = o.taxonomies; + } + if ( !o.addresses.empty() ) { + j["addresses"] = o.addresses; + } + if ( !o.translations.empty() ) { + j["translations"] = o.translations; + } + if ( !o.policies.empty() ) { + j["policies"] = o.policies; + } + if ( !o.webRequests.empty() ) { + j["webRequests"] = o.webRequests; + } + if ( !o.webResponses.empty() ) { + j["webResponses"] = o.webResponses; + } + if ( o.specialLocations.has_value() ) { + j["specialLocations"] = *o.specialLocations; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, external_properties &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "schema" ) { + val.get_to(o.schema); + } else if ( key == "version" ) { + val.get_to(o.version); + } else if ( key == "guid" ) { + val.get_to(o.guid); + } else if ( key == "runGuid" ) { + val.get_to(o.runGuid); + } else if ( key == "conversion" ) { + val.get_to(o.conversion); + } else if ( key == "graphs" ) { + val.get_to(o.graphs); + } else if ( key == "externalizedProperties" ) { + val.get_to(o.externalizedProperties); + } else if ( key == "artifacts" ) { + val.get_to(o.artifacts); + } else if ( key == "invocations" ) { + val.get_to(o.invocations); + } else if ( key == "logicalLocations" ) { + val.get_to(o.logicalLocations); + } else if ( key == "threadFlowLocations" ) { + val.get_to(o.threadFlowLocations); + } else if ( key == "results" ) { + val.get_to(o.results); + } else if ( key == "taxonomies" ) { + val.get_to(o.taxonomies); + } else if ( key == "driver" ) { + val.get_to(o.driver); + } else if ( key == "extensions" ) { + val.get_to(o.extensions); + } else if ( key == "policies" ) { + val.get_to(o.policies); + } else if ( key == "translations" ) { + val.get_to(o.translations); + } else if ( key == "addresses" ) { + val.get_to(o.addresses); + } else if ( key == "webRequests" ) { + val.get_to(o.webRequests); + } else if ( key == "webResponses" ) { + val.get_to(o.webResponses); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const external_properties &o) { + if ( o.schema.has_value() ) { + j["schema"] = *o.schema; + } + if ( o.version.has_value() ) { + j["version"] = *o.version; + } + if ( o.guid.has_value() ) { + j["guid"] = *o.guid; + } + if ( o.runGuid.has_value() ) { + j["runGuid"] = *o.runGuid; + } + if ( o.conversion.has_value() ) { + j["conversion"] = *o.conversion; + } + if ( !o.graphs.empty() ) { + j["graphs"] = o.graphs; + } + if ( o.externalizedProperties.has_value() ) { + j["externalizedProperties"] = *o.externalizedProperties; + } + if ( !o.artifacts.empty() ) { + j["artifacts"] = o.artifacts; + } + if ( !o.invocations.empty() ) { + j["invocations"] = o.invocations; + } + if ( !o.logicalLocations.empty() ) { + j["logicalLocations"] = o.logicalLocations; + } + if ( !o.threadFlowLocations.empty() ) { + j["threadFlowLocations"] = o.threadFlowLocations; + } + if ( !o.results.empty() ) { + j["results"] = o.results; + } + if ( !o.taxonomies.empty() ) { + j["taxonomies"] = o.taxonomies; + } + if ( o.driver.has_value() ) { + j["driver"] = *o.driver; + } + if ( !o.extensions.empty() ) { + j["extensions"] = o.extensions; + } + if ( !o.policies.empty() ) { + j["policies"] = o.policies; + } + if ( !o.translations.empty() ) { + j["translations"] = o.translations; + } + if ( !o.addresses.empty() ) { + j["addresses"] = o.addresses; + } + if ( !o.webRequests.empty() ) { + j["webRequests"] = o.webRequests; + } + if ( !o.webResponses.empty() ) { + j["webResponses"] = o.webResponses; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } + void from_json(const json &j, root &o) { + for( const auto &[key, val] : j.items() ) { + if ( key == "version" ) { + val.get_to(o.version); + } else if ( key == "runs" ) { + val.get_to(o.runs); + } else if ( key == "inlineExternalProperties" ) { + val.get_to(o.inlineExternalProperties); + } else if ( key == "properties" ) { + val.get_to(o.properties); + } + } + } + void to_json(json &j, const root &o) { + j["version"] = o.version; + j["runs"] = o.runs; + if ( !o.inlineExternalProperties.empty() ) { + j["inlineExternalProperties"] = o.inlineExternalProperties; + } + if ( o.properties.has_value() ) { + j["properties"] = *o.properties; + } + } +} // namespace gap::sarif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4c3aebe..ed69d20 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,6 +34,7 @@ endfunction() add_subdirectory(core) add_subdirectory(coro) add_subdirectory(graph) +add_subdirectory(sarif) if (${GAP_ENABLE_MLIR}) add_subdirectory(mlir) diff --git a/test/sarif/CMakeLists.txt b/test/sarif/CMakeLists.txt new file mode 100644 index 0000000..fe0b48c --- /dev/null +++ b/test/sarif/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright 2024, Trail of Bits, Inc. All rights reserved. + +add_gap_test(test-gap-sarif + sarif.cpp +) + +target_link_libraries(test-gap-sarif + PUBLIC + gap-sarif +) \ No newline at end of file diff --git a/test/sarif/sarif.cpp b/test/sarif/sarif.cpp new file mode 100644 index 0000000..96cbda4 --- /dev/null +++ b/test/sarif/sarif.cpp @@ -0,0 +1,143 @@ +// Copyright (c) 2024-present, Trail of Bits, Inc. + +#include +#include + +namespace gap::sarif { + TEST_CASE("Struct to JSON roundtrip") { + // Sample SARIF output from https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md + root obj{ + .version = version::k2_1_0, + .runs = { + { + .tool = { + .driver = { + .name = "ESLint", + .informationUri = "https://eslint.org", + .rules = { + { + .id = "no-unused-vars", + .shortDescription = {{ + .text = "disallow unused variables", + }}, + .helpUri = "https://eslint.org/docs/rules/no-unused-vars", + .properties = {{ + .additional_properties = { + {"category", "Variable"}, + }, + }}, + } + }, + }, + }, + .artifacts = { + { + .location = {{ + .uri = "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js", + }}, + }, + }, + .results = {{ + { + .ruleId = "no-unused-vars", + .ruleIndex = 0, + .level = level::kError, + .message = { + .text = "'x' is assigned a value but never used.", + }, + .locations = {{ + { + .physicalLocation = {{ + .artifactLocation = {{ + .uri = "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js", + .index = 0, + }}, + }}, + .properties = {{ + .additional_properties = { + {"region", { + {"startLine", 1}, + {"startColumn", 5}, + }}, + }, + }}, + }, + }}, + }, + }}, + } + }, + }; + + json obj_json = obj; + root deser = obj; + json deser_json = deser; + + CHECK_EQ(obj_json, deser_json); + } + + TEST_CASE("JSON to struct roundtrip") { + auto j = json::parse(R"({ + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "ESLint", + "informationUri": "https://eslint.org", + "rules": [ + { + "id": "no-unused-vars", + "shortDescription": { + "text": "disallow unused variables" + }, + "helpUri": "https://eslint.org/docs/rules/no-unused-vars", + "properties": { + "category": "Variables" + } + } + ] + } + }, + "artifacts": [ + { + "location": { + "uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js" + } + } + ], + "results": [ + { + "level": "error", + "message": { + "text": "'x' is assigned a value but never used." + }, + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js", + "index": 0 + }, + "region": { + "startLine": 1, + "startColumn": 5 + } + } + } + ], + "ruleId": "no-unused-vars", + "ruleIndex": 0 + } + ] + } + ] +})"); + + root root = j; + + json j2 = root; + + CHECK_EQ(j, j2); + } +} \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json index 18336ac..9777497 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,7 +2,8 @@ "name": "main", "version-string": "latest", "dependencies": [ - "spdlog" + "spdlog", + "nlohmann-json" ], "features": { "with-doctest": {