Skip to content

Commit e8029d2

Browse files
Learn Build Service GitHub AppLearn Build Service GitHub App
authored andcommitted
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents 190e2ee + a3b25b7 commit e8029d2

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

docs/build/reference/advanced-property-page.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Specifies the full version of the MSVC toolset that's used to build the project.
9797

9898
Specifies the full version of the LLVM toolset that's used to build the project. This property is available when **LLVM (clang-cl)** is selected as the platform toolset, starting in Visual Studio 2019 version 16.9. For more information, see [Set a custom LLVM toolset version](..\clang-support-msbuild.md#custom_llvm_toolset).
9999

100+
### Enable MSVC Structured Output
101+
102+
Specifies whether to enable [structured SARIF output](sarif-output.md), which enables the [**Problem Details** window](/visualstudio/ide/reference/problem-details-window) and hierarchical output in the [**Output** window](/visualstudio/ide/reference/output-window) in Visual Studio.
103+
100104
## C++/CLI Properties
101105

102106
### Common Language Runtime support

docs/build/reference/compiler-options-listed-alphabetically.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This table contains an alphabetical list of compiler options. For a list of comp
4949
| [`/EP`](ep-preprocess-to-stdout-without-hash-line-directives.md) | Copies preprocessor output to standard output. |
5050
| [`/errorReport`](errorreport-report-internal-compiler-errors.md) | Deprecated. [Windows Error Reporting (WER)](/windows/win32/wer/windows-error-reporting) settings control error reporting. |
5151
| [`/execution-charset`](execution-charset-set-execution-character-set.md) | Set execution character set. |
52+
| [`/experimental:log`](experimental-log.md) | Enables experimental structured SARIF output. |
5253
| [`/experimental:module`](experimental-module.md) | Enables experimental module support. |
5354
| [`/exportHeader`](module-exportheader.md) | Create the header units files (*`.ifc`*) specified by the input arguments. |
5455
| [`/external:anglebrackets`](external-external-headers-diagnostics.md) | Treat all headers included via `<>` as external. |

docs/build/reference/compiler-options-listed-by-category.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ Experimental options may only be supported by certain versions of the compiler.
299299

300300
| Option | Purpose |
301301
|--|--|
302+
| [`/experimental:log`](experimental-log.md) | Enables experimental structured SARIF output. |
302303
| [`/experimental:module`](experimental-module.md) | Enables experimental module support. |
303304

304305
## Deprecated and removed compiler options
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: "/experimental:log (Structured SARIF diagnostics)"
3+
description: "Use the /experimental:log compiler option to output experimental structured SARIF output for diagnostics."
4+
ms.date: 10/26/2023
5+
f1_keywords: ["/experimental:log"]
6+
helpviewer_keywords: ["/experimental:log", "SARIF", "structured diagnostics"]
7+
---
8+
# `/experimental:log` (Structured SARIF diagnostics)
9+
10+
Output [SARIF](https://sarifweb.azurewebsites.net/) diagnostics to the specified file. For more information, see [Structured SARIF Diagnostics](sarif-output.md).
11+
12+
## Syntax
13+
14+
> **`/experimental:log`** *filename*
15+
16+
## Arguments
17+
18+
*filename*
19+
20+
Where to output SARIF diagnostics. The `.sarif` suffix is added to *filename* to produce the final filename at which to store the resulting SARIF diagnostics. The space between `/experimental:log` and *filename* is optional. Paths that include spaces must be enclosed in double quotes. *filename* may name a relative or absolute path.
21+
22+
## Remarks
23+
24+
This option is available starting in Visual Studio 2022 version 17.8.
25+
26+
Diagnostics are also output as text to the console as usual.
27+
28+
### To set this compiler option in the Visual Studio development environment
29+
30+
1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).
31+
32+
1. Select the specific project **Configuration** and **Platform** for which you want to change the property. You can also choose **"All Configurations"** and **"All Platforms"**.
33+
34+
1. Select the **Configuration Properties** > **C/C++** > **Command Line** property page.
35+
36+
1. Modify the **Additional Options** property, and then choose **OK**.
37+
38+
## Example
39+
40+
The following command produces SARIF information for the entire compilation in the `diags.sarif` file in the current directory:
41+
42+
```cmd
43+
CL /experimental:logdiags main.cpp other.cpp
44+
```
45+
46+
## See also
47+
48+
[Structured SARIF Diagnostics](sarif-output.md)

docs/build/reference/sarif-output.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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)

docs/build/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ items:
515515
href: ../build/reference/errorreport-report-internal-compiler-errors.md
516516
- name: /execution-charset (Set execution character set)
517517
href: ../build/reference/execution-charset-set-execution-character-set.md
518+
- name: /experimental:log
519+
href: ../build/reference/experimental-log.md
518520
- name: /experimental:module
519521
href: ../build/reference/experimental-module.md
520522
- name: /experimental:preprocessor
@@ -859,6 +861,8 @@ items:
859861
href: ../build/reference/zs-syntax-check-only.md
860862
- name: /ZW (Windows Runtime compilation)
861863
href: ../build/reference/zw-windows-runtime-compilation.md
864+
- name: Structured SARIF output
865+
href: ../build/reference/sarif-output.md
862866
- name: Unicode support in the compiler and linker
863867
href: ../build/reference/unicode-support-in-the-compiler-and-linker.md
864868
- name: MSVC linker reference

0 commit comments

Comments
 (0)