Skip to content

Commit e9eac07

Browse files
Merge pull request #5809 from carsonRadtke/suppression-justification
Updates to Warning Suppression Documentation
2 parents ed10309 + 7407042 commit e9eac07

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

docs/code-quality/c26401.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public:
6363
ref_count_--;
6464
if (ref_count_ == 0)
6565
{
66-
[[gsl::suppress(i.11)]]
66+
[[gsl::suppress("i.11")]]
6767
delete this;
6868
}
6969
}

docs/code-quality/c26409.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public:
4949
ref_count_--;
5050
if (ref_count_ == 0)
5151
{
52-
[[gsl::suppress(i.11)]]
52+
[[gsl::suppress("i.11")]]
5353
delete this;
5454
}
5555
}

docs/code-quality/using-the-cpp-core-guidelines-checkers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int main()
7474
int arr[10]; // warning C26494
7575
int* p = arr; // warning C26485
7676

77-
[[gsl::suppress(bounds.1)]] // This attribute suppresses Bounds rule #1
77+
[[gsl::suppress("bounds.1", justification : "This attribute suppresses Bounds rules #1")]]
7878
{
7979
int* q = p + 1; // warning C26481 (suppressed)
8080
p = q++; // warning C26481 (suppressed)
@@ -104,7 +104,7 @@ c:\users\username\documents\visual studio 2015\projects\corecheckexample\coreche
104104
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
105105
```
106106

107-
The C++ Core Guidelines are there to help you write better and safer code. However, you might find an instance where a rule or a profile shouldn't be applied. It's easy to suppress it directly in the code. You can use the `[[gsl::suppress]]` attribute to keep C++ Core Check from detecting and reporting any violation of a rule in the following code block. You can mark individual statements to suppress specific rules. You can even suppress the entire bounds profile by writing `[[gsl::suppress(bounds)]]` without including a specific rule number.
107+
The C++ Core Guidelines are there to help you write better and safer code. However, you might find an instance where a rule or a profile shouldn't be applied. It's easy to suppress it directly in the code. You can use the `[[gsl::suppress]]` attribute to keep C++ Core Check from detecting and reporting any violation of a rule in the following code block. You can mark individual statements to suppress specific rules. You can even suppress the entire bounds profile by writing `[[gsl::suppress("bounds")]]` without including a specific rule number.
108108

109109
## Supported rule sets
110110

@@ -197,10 +197,10 @@ The Microsoft C++ compiler has limited support for the `[[gsl::suppress]]` attri
197197

198198
```cpp
199199
// Suppress only warnings from the 'r.11' rule in expression.
200-
[[gsl::suppress(r.11)]] new int;
200+
[[gsl::suppress("r.11")]] new int;
201201

202202
// Suppress all warnings from the 'r' rule group (resource management) in block.
203-
[[gsl::suppress(r)]]
203+
[[gsl::suppress("r")]]
204204
{
205205
new int;
206206
}
@@ -209,7 +209,7 @@ The Microsoft C++ compiler has limited support for the `[[gsl::suppress]]` attri
209209
// For declarations, you might need to use the surrounding block.
210210
// Macros are not expanded inside of attributes.
211211
// Use plain numbers instead of macros from the warnings.h.
212-
[[gsl::suppress(26400)]]
212+
[[gsl::suppress("26400")]]
213213
{
214214
int *p = new int;
215215
}

docs/cpp/attributes.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,18 @@ The `[[noreturn]]` attribute specifies that a function never returns; in other w
7777

7878
## Microsoft-specific attributes
7979

80-
### `[[gsl::suppress(rules)]]`
80+
### `[[gsl::suppress(<tag> [, justification: <narrow-string-literal>])]]`
8181

82-
The Microsoft-specific `[[gsl::suppress(rules)]]` attribute is used to suppress warnings from checkers that enforce [Guidelines Support Library (GSL)](https://github.com/Microsoft/GSL) rules in code. For example, consider this code snippet:
82+
`<tag>` is a string that specifies the name of the rule to suppress. The optional `justification` field allows you to explain why a warning is being disabled or suppressed. This value will appear in the SARIF output when the `/analyze:log:includesuppressed` option is specified. Its value is a UTF-8 encoded narrow string literal. The `[[gsl::suppress]]` attribute is available in Visual Studio 2022 version 17.14 and later versions.
83+
84+
The Microsoft-specific `[[gsl::suppress]]` attribute is used to suppress warnings from checkers that enforce [Guidelines Support Library (GSL)](https://github.com/Microsoft/GSL) rules in code. For example, consider this code snippet:
8385

8486
```cpp
8587
int main()
8688
{
8789
int arr[10]; // GSL warning C26494 will be fired
8890
int* p = arr; // GSL warning C26485 will be fired
89-
[[gsl::suppress(bounds.1)]] // This attribute suppresses Bounds rule #1
91+
[[gsl::suppress("bounds.1", justification: "This attribute suppresses Bounds rule #1")]]
9092
{
9193
int* q = p + 1; // GSL warning C26481 suppressed
9294
p = q--; // GSL warning C26481 suppressed
@@ -102,7 +104,7 @@ The example raises these warnings:
102104

103105
- [C26481](../code-quality/c26481.md) (Bounds Rule 1: Don't use pointer arithmetic. Use span instead.)
104106

105-
The first two warnings fire when you compile this code with the CppCoreCheck code analysis tool installed and activated. But the third warning doesn't fire because of the attribute. You can suppress the entire bounds profile by writing `[[gsl::suppress(bounds)]]` without including a specific rule number. The C++ Core Guidelines are designed to help you write better and safer code. The suppress attribute makes it easy to turn off the warnings when they aren't wanted.
107+
The first two warnings fire when you compile this code with the CppCoreCheck code analysis tool installed and activated. But the third warning doesn't fire because of the attribute. You can suppress the entire bounds profile by writing `[[gsl::suppress("bounds")]]` without including a specific rule number. The C++ Core Guidelines are designed to help you write better and safer code. The suppress attribute makes it easy to turn off the warnings when they aren't wanted.
106108

107109
### `[[msvc::flatten]]`
108110

docs/preprocessor/warning.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Enables selective modification of the behavior of compiler warning messages.
1313
## Syntax
1414

1515
> **`#pragma warning(`**\
16-
> &emsp;*`warning-specifier`* **`:`** *`warning-number-list`*\
16+
> &emsp;*`warning-specifier`* **`:`** *`warning-number-list`* [ **`,`** **`justification`** **`:`** *`string-literal`*]\
1717
> &emsp;[**`;`** *`warning-specifier`* **`:`** *`warning-number-list`* ... ] **`)`**\
1818
> **`#pragma warning( push`** [ **`,`** *n* ] **`)`**\
1919
> **`#pragma warning( pop )`**
@@ -26,17 +26,26 @@ The following warning-specifier parameters are available.
2626
|--|--|
2727
| `1`, `2`, `3`, `4` | Apply the given level to the specified warnings. Also turns on a specified warning that is off by default. |
2828
| `default` | Reset warning behavior to its default value. Also turns on a specified warning that is off by default. The warning will be generated at its default, documented, level.<br /><br /> For more information, see [Compiler warnings that are off by default](../preprocessor/compiler-warnings-that-are-off-by-default.md). |
29-
| `disable` | Don't issue the specified warning messages. |
29+
| `disable` | Don't issue the specified warning messages. The optional **`justification`** property is allowed. |
3030
| `error` | Report the specified warnings as errors. |
3131
| `once` | Display the specified message(s) only one time. |
32-
| `suppress` | Pushes the current state of the pragma on the stack, disables the specified warning for the next line, and then pops the warning stack so that the pragma state is reset. |
32+
| `suppress` | Pushes the current state of the pragma on the stack, disables the specified warning for the next line, and then pops the warning stack so that the pragma state is reset. The optional **`justification`** property is allowed. |
3333

3434
The following code statement illustrates that a *`warning-number-list`* parameter can contain multiple warning numbers, and that multiple *`warning-specifier`* parameters can be specified in the same pragma directive.
3535

3636
```cpp
3737
#pragma warning( disable : 4507 34; once : 4385; error : 164 )
3838
```
3939

40+
However, when the **`justification`** field is present, only one warning number can be specified. The following code statement illustrates the use of the **`justification`** field.
41+
42+
```cpp
43+
#pragma warning( disable : 4507, justification : "This warning is disabled" )
44+
```
45+
46+
The **`justification`** fields allows you to explain why a warning is being disable or
47+
suppressed. The **`justification`** field is only supported for the **`disable`** and **`suppress`** *`warning-specifier`*. This value will appear in the SARIF output when the `/analyze:log:includesuppressed` option is specified. Its value is a UTF-8 encoded narrow string literal.
48+
4049
This directive is functionally equivalent to the following code:
4150

4251
```cpp

0 commit comments

Comments
 (0)