@@ -1132,7 +1132,9 @@ Controlling Diagnostics via Pragmas
11321132Clang can also control what diagnostics are enabled through the use of
11331133pragmas in the source code. This is useful for turning off specific
11341134warnings 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.
11361138
11371139The pragma may control any warning that can be used from the command
11381140line. Warnings may be set to ignored, warning, error, or fatal. The
@@ -1143,8 +1145,7 @@ warnings:
11431145
11441146 #pragma GCC diagnostic ignored "-Wall"
11451147
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
11481149particularly useful when writing a header file that will be compiled by
11491150other people, because you don't know what warning flags they build with.
11501151
@@ -1157,23 +1158,34 @@ existed.
11571158 #if foo
11581159 #endif foo // warning: extra tokens at end of #endif directive
11591160
1160- #pragma clang diagnostic push
1161- #pragma clang diagnostic ignored "-Wextra-tokens"
1161+ #pragma GCC diagnostic push
1162+ #pragma GCC diagnostic ignored "-Wextra-tokens"
11621163
11631164 #if foo
11641165 #endif foo // no warning
11651166
1166- #pragma clang diagnostic pop
1167+ #pragma GCC diagnostic pop
11671168
11681169 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
11731171supports the GCC pragma, Clang and GCC do not support the exact same set
11741172of warnings, so even when using GCC compatible #pragmas there is no
11751173guarantee that they will have identical behaviour on both compilers.
11761174
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+
11771189In addition to controlling warnings and errors generated by the compiler, it is
11781190possible to generate custom warning and error messages through the following
11791191pragmas:
0 commit comments