@@ -1132,7 +1132,9 @@ Controlling Diagnostics via Pragmas
1132
1132
Clang can also control what diagnostics are enabled through the use of
1133
1133
pragmas in the source code. This is useful for turning off specific
1134
1134
warnings in a section of source code. Clang supports GCC's pragma for
1135
- compatibility with existing source code, as well as several extensions.
1135
+ compatibility with existing source code, so ``#pragma GCC diagnostic ``
1136
+ and ``#pragma clang diagnostic `` are synonyms for Clang. GCC will ignore
1137
+ ``#pragma clang diagnostic ``, though.
1136
1138
1137
1139
The pragma may control any warning that can be used from the command
1138
1140
line. Warnings may be set to ignored, warning, error, or fatal. The
@@ -1143,8 +1145,7 @@ warnings:
1143
1145
1144
1146
#pragma GCC diagnostic ignored "-Wall"
1145
1147
1146
- In addition to all of the functionality provided by GCC's pragma, Clang
1147
- also allows you to push and pop the current warning state. This is
1148
+ Clang also allows you to push and pop the current warning state. This is
1148
1149
particularly useful when writing a header file that will be compiled by
1149
1150
other people, because you don't know what warning flags they build with.
1150
1151
@@ -1157,23 +1158,34 @@ existed.
1157
1158
#if foo
1158
1159
#endif foo // warning: extra tokens at end of #endif directive
1159
1160
1160
- #pragma clang diagnostic push
1161
- #pragma clang diagnostic ignored "-Wextra-tokens"
1161
+ #pragma GCC diagnostic push
1162
+ #pragma GCC diagnostic ignored "-Wextra-tokens"
1162
1163
1163
1164
#if foo
1164
1165
#endif foo // no warning
1165
1166
1166
- #pragma clang diagnostic pop
1167
+ #pragma GCC diagnostic pop
1167
1168
1168
1169
The push and pop pragmas will save and restore the full diagnostic state
1169
- of the compiler, regardless of how it was set. That means that it is
1170
- possible to use push and pop around GCC compatible diagnostics and Clang
1171
- will push and pop them appropriately, while GCC will ignore the pushes
1172
- and pops as unknown pragmas. It should be noted that while Clang
1170
+ of the compiler, regardless of how it was set. It should be noted that while Clang
1173
1171
supports the GCC pragma, Clang and GCC do not support the exact same set
1174
1172
of warnings, so even when using GCC compatible #pragmas there is no
1175
1173
guarantee that they will have identical behaviour on both compilers.
1176
1174
1175
+ Clang also doesn't yet support GCC behavior for ``#pragma diagnostic pop ``
1176
+ that doesn't have a corresponding ``#pragma diagnostic push ``. In this case
1177
+ GCC pretends that there is a ``#pragma diagnostic push `` at the very beginning
1178
+ of the source file, so "unpaired" ``#pragma diagnostic pop `` matches that
1179
+ implicit push. This makes a difference for ``#pragma GCC diagnostic ignored ``
1180
+ which are not guarded by push and pop. Refer to
1181
+ `GCC documentation <https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html >`_
1182
+ for details.
1183
+
1184
+ Like GCC, Clang accepts ``ignored ``, ``warning ``, ``error ``, and ``fatal ``
1185
+ severity levels. They can be used to change severity of a particular diagnostic
1186
+ for a region of source file. A notable difference from GCC is that diagnostic
1187
+ not enabled via command line arguments can't be enabled this way yet.
1188
+
1177
1189
In addition to controlling warnings and errors generated by the compiler, it is
1178
1190
possible to generate custom warning and error messages through the following
1179
1191
pragmas:
0 commit comments