|
| 1 | +--- |
| 2 | +description: "How to make the compiler output diagnostics as structured SARIF" |
| 3 | +title: "Structured SARIF Diagnostics" |
| 4 | +ms.date: "10/26/2023" |
| 5 | +author: tartanllama |
| 6 | +ms.author: sybrand |
| 7 | +manager: mluparu |
| 8 | +helpviewer_keywords: ["SARIF", "structured diagnostics"] |
| 9 | +--- |
| 10 | + |
| 11 | +# Structured SARIF Diagnostics |
| 12 | + |
| 13 | +The MSVC compiler can be made to output diagnostics as [SARIF](https://sarifweb.azurewebsites.net/) (Static Analysis Results Interchange Format). SARIF is a machine-readable JSON-based format. |
| 14 | + |
| 15 | +There are two ways to make the MSVC compiler produce SARIF diagnostics: |
| 16 | + |
| 17 | +- Pass the `/experimental:log` switch on the command line. See the [documentation for `/experimental:log`](experimental-log.md) for details. |
| 18 | +- Launch `cl.exe` programatically and set the `SARIF_OUTPUT_PIPE` environment variable to retrieve SARIF blocks through a pipe. |
| 19 | + |
| 20 | +## Retrieving SARIF through a pipe |
| 21 | + |
| 22 | +Tools that consume SARIF from the MSVC compiler while a compilation is in progress use a pipe. See the documentation for [`CreatePipe`](/windows/win32/api/namedpipeapi/nf-namedpipeapi-createpipe) for details about creating Windows pipes. |
| 23 | + |
| 24 | +To retrieve SARIF through a pipe, set the `SARIF_OUTPUT_PIPE` environment variable to be the UTF-16-encoded integer representation of the `HANDLE` to the write end of the pipe, then launch `cl.exe`. SARIF is sent along the pipe as follows: |
| 25 | + |
| 26 | +- When a new diagnostic is available, it is written to this pipe. |
| 27 | +- Diagnostics are written to the pipe one-at-a-time rather than as an entire SARIF object. |
| 28 | +- Each diagnostic is represented by a [JSON-RPC 2.0](https://www.jsonrpc.org/) message of type [Notification](https://www.jsonrpc.org/specification#notification:~:text=as%20binary%20fractions.-,4.1%20Notification,-A%20Notification%20is). |
| 29 | +- The JSON-RPC message is prefixed with a `Content-Length` header with the form `Content-Length: <N>` followed by two newlines, where `<N>` is the length of the following JSON-RPC message in bytes. |
| 30 | +- The JSON-RPC message and header are both encoded in UTF-8. |
| 31 | +- This JSON-RPC-with-header format is compatible with [vs-streamjsonrpc](https://github.com/microsoft/vs-streamjsonrpc). |
| 32 | +- The method name for the JSON-RPC call is `OnSarifResult`. |
| 33 | +- The call has a single parameter that is encoded [by-name](https://www.jsonrpc.org/specification#parameter_structures) with the parameter name `result`. |
| 34 | +- The value of the argument is a single `result` object as specified by the [SARIF Version 2.1 standard](https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790888). |
| 35 | + |
| 36 | +### Example |
| 37 | + |
| 38 | +Here's an example of a JSON-RPC SARIF result produced by `cl.exe`: |
| 39 | + |
| 40 | +```json |
| 41 | +Content-Length: 334 |
| 42 | + |
| 43 | +{"jsonrpc":"2.0","method":"OnSarifResult","params":{"result":{"ruleId":"C1034","level":"fatal","message":{"text":"iostream: no include path set"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///C:/Users/sybrand/source/repos/cppcon-diag/cppcon-diag/cppcon-diag.cpp"},"region":{"startLine":1,"startColumn":10}}}]}}}{"jsonrpc":"2.0","method":"OnSarifResult","params":{"result":{"ruleId":"C1034","level":"fatal","message":{"text":"iostream: no include path set"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///C:/Users/sybrand/source/repos/cppcon-diag/cppcon-diag/cppcon-diag.cpp"},"region":{"startLine":1,"startColumn":10}}}]}}} |
| 44 | +``` |
| 45 | + |
| 46 | +## SARIF result data |
| 47 | + |
| 48 | +The compiler outputs SARIF that may include additional information to represent the nested structure of some diagnostics. A diagnostic (represented by a `result` SARIF object) may contain a "diagnostic tree" of additional information in its `relatedLocations` field. This tree is encoded using a SARIF [property bag](https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790698) as follows: |
| 49 | + |
| 50 | +A `location` object's `properties` field may contain a `nestingLevel` property whose value is the depth of this location in the diagnostic tree. If a location doesn't have a `nestingLevel` specified, the depth is considered to be `0` and this location is a child of the root diagnostic represented by the `result` object containing it. Otherwise, if the value is greater than the depth of the location immediately preceding this location in the `relatedLocations` field, this location is a child of that location. Otherwise, this location is a sibling of the closest preceding `location` in the `relatedLocations` field with the same depth. |
| 51 | + |
| 52 | +### Example |
| 53 | + |
| 54 | +Consider the following code: |
| 55 | + |
| 56 | +```cpp |
| 57 | +struct dog {}; |
| 58 | +struct cat {}; |
| 59 | + |
| 60 | +void pet(dog); |
| 61 | +void pet(cat); |
| 62 | + |
| 63 | +struct lizard {}; |
| 64 | + |
| 65 | +int main() { |
| 66 | + pet(lizard{}); |
| 67 | +} |
| 68 | +``` |
| 69 | +
|
| 70 | +When this code is compiled, the compiler produces the following `result` object (`physicalLocation` properties have been removed for brevity): |
| 71 | +
|
| 72 | +```json |
| 73 | +{ |
| 74 | + "ruleId": "C2665", |
| 75 | + "level": "error", |
| 76 | + "message": { |
| 77 | + "text": "'pet': no overloaded function could convert all the argument types" |
| 78 | + }, |
| 79 | + "relatedLocations": [ |
| 80 | + { |
| 81 | + "id": 0, |
| 82 | + "message": { |
| 83 | + "text": "could be 'void pet(cat)'" |
| 84 | + } |
| 85 | + }, |
| 86 | + { |
| 87 | + "id": 1, |
| 88 | + "message": { |
| 89 | + "text": "'void pet(cat)': cannot convert argument 1 from 'lizard' to 'cat'" |
| 90 | + }, |
| 91 | + "properties": { |
| 92 | + "nestingLevel": 1 |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + "id": 2, |
| 97 | + "message": { |
| 98 | + "text": "No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called" |
| 99 | + }, |
| 100 | + "properties": { |
| 101 | + "nestingLevel": 2 |
| 102 | + } |
| 103 | + }, |
| 104 | + { |
| 105 | + "id": 3, |
| 106 | + "message": { |
| 107 | + "text": "or 'void pet(dog)'" |
| 108 | + } |
| 109 | + }, |
| 110 | + { |
| 111 | + "id": 4, |
| 112 | + "message": { |
| 113 | + "text": "'void pet(dog)': cannot convert argument 1 from 'lizard' to 'dog'" |
| 114 | + }, |
| 115 | + "properties": { |
| 116 | + "nestingLevel": 1 |
| 117 | + } |
| 118 | + }, |
| 119 | + { |
| 120 | + "id": 5, |
| 121 | + "message": { |
| 122 | + "text": "No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called" |
| 123 | + }, |
| 124 | + "properties": { |
| 125 | + "nestingLevel": 2 |
| 126 | + } |
| 127 | + }, |
| 128 | + { |
| 129 | + "id": 6, |
| 130 | + "message": { |
| 131 | + "text": "while trying to match the argument list '(lizard)'" |
| 132 | + } |
| 133 | + } |
| 134 | + ] |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +The logical diagnostics tree produced from the messages in this `result` object is: |
| 139 | + |
| 140 | +- 'pet': no overloaded function could convert all the argument types |
| 141 | + - could be 'void pet(cat)' |
| 142 | + - 'void pet(cat)': cannot convert argument 1 from 'lizard' to 'cat |
| 143 | + - No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called |
| 144 | + - or 'void pet(dog)' |
| 145 | + - 'void pet(dog)': cannot convert argument 1 from 'lizard' to 'dog' |
| 146 | + - No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called |
| 147 | + - while trying to match the argument list '(lizard)' |
| 148 | + |
| 149 | +## See also |
| 150 | + |
| 151 | +[`/experimental:log` (Enable structured SARIF diagnostics)](experimental-log.md) |
0 commit comments