You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -16,15 +16,55 @@ Treat the rest of the file as external for diagnostics reports.
16
16
17
17
## Remarks
18
18
19
-
Starting in Visual Studio 2017 version 15.6, the compiler lets you set two different default diagnostic warning levels on the command line. Normally, you use a [`/W0`, `/W1`, `/W2`, `/W3`, or `/W4`](../build/reference/compiler-option-warning-level.md) compiler option to specify a single diagnostic level for all code in a project. However, your project might include system header files or files from external libraries that generate warnings at the specified level. When you can't or don't want to edit these files, you can specify them as *external*. Files specified as external can have a separate compiler diagnostic level applied to them as a group. For more information on how to specify external files and the external warning level to the compiler, see [`/external`](../build/reference/external-external-headers-diagnostics.md).
19
+
The **`system_header`** pragma tells the compilerto show diagnostics at the level specified by the **`/external:Wn`** option for the rest of the current source file. For more information on how to specify external files and the external warning level to the compiler, see [`/external`](../build/reference/external-external-headers-diagnostics.md).
20
20
21
-
For example, a common scenario uses the **`/external:W1`**option to apply a **`/W1`** warning levelto external library header files, while you use **`/W4 /WX`**on your own code. Then you don't see minor diagnostics for the code that isn't yours.
21
+
The **`system_header`** pragma doesn't apply past the end of the current source file. In other words, it doesn't apply to files that include this file. The **`system_header`**pragma applies even if no other files are specified as external to the compiler. However, if no **`/external:Wn`**option level is specified, the compiler may issue a diagnostic and uses the same [warning level](../build/reference/compiler-option-warning-level.md) it applies to non-external files. Other pragma directives that affect warning behavior still apply after a **`system_header`**pragma. The effect of `#pragma system_header` is similar to the [`warning pragma`](warning.md):
22
22
23
-
The **`system_header`** pragma tells the compiler to show diagnostics at the **`/external:Wn`** level for the rest of the source file. The **`system_header`** pragma applies even if no other files are specified as external to the compiler. However, if no **`/external:Wn`** option level is specified, the compiler issues a diagnostic and uses the same warning level it applies to non-external files. Other pragma directives that affect warning behavior still apply after a **`system_header`** pragma.
23
+
```cpp
24
+
// If n represents the warning level specified by /external:Wn,
25
+
// #pragma system_header is roughly equivalent to:
26
+
#pragma warning( push, n )
27
+
28
+
// . . .
29
+
30
+
// At the end of the file:
31
+
#pragma warning( pop )
32
+
```
24
33
25
34
The **`system_header`** pragma is available starting in Visual Studio 2019 version 16.10.
26
35
36
+
## Example
37
+
38
+
This sample header shows how to mark the contents of a file as external:
39
+
40
+
```cpp
41
+
// library.h
42
+
// Use /external:Wn to set the compiler diagnostics level for this file's contents
43
+
44
+
#pragma once
45
+
#ifndef _LIBRARY_H // include guard for 3rd party interop
46
+
#define _LIBRARY_H
47
+
#pragma system_header
48
+
// The compiler applies the /external:Wn diagnostic level from here to the end of this file.
49
+
50
+
// . . .
51
+
52
+
// You can still override the external diagnostic level for warnings locally:
0 commit comments