From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/gcc~13/diagnostic-pragmas.html | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 devdocs/gcc~13/diagnostic-pragmas.html (limited to 'devdocs/gcc~13/diagnostic-pragmas.html') diff --git a/devdocs/gcc~13/diagnostic-pragmas.html b/devdocs/gcc~13/diagnostic-pragmas.html new file mode 100644 index 00000000..c5403c4b --- /dev/null +++ b/devdocs/gcc~13/diagnostic-pragmas.html @@ -0,0 +1,44 @@ +

6.62.12 Diagnostic Pragmas

GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project’s policy might require that all sources compile with -Werror but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

+#pragma GCC diagnostic kind option +

Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; at the moment only warnings (normally controlled by ‘-W…’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.

kind is ‘error’ to treat this diagnostic as an error, ‘warning’ to treat it like a warning (even if -Werror is in effect), or ‘ignored’ if the diagnostic is to be ignored. option is a double quoted string that matches the command-line option.

#pragma GCC diagnostic warning "-Wformat"
+#pragma GCC diagnostic error "-Wformat"
+#pragma GCC diagnostic ignored "-Wformat"
+

Note that these pragmas override any command-line options. GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, pragmas occurring after a line do not affect diagnostics caused by that line.

#pragma GCC diagnostic push
#pragma GCC diagnostic pop

Causes GCC to remember the state of the diagnostics as of each push, and restore to that point at each pop. If a pop has no matching push, the command-line options are restored.

#pragma GCC diagnostic error "-Wuninitialized"
+  foo(a);                       /* error is given for this one */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wuninitialized"
+  foo(b);                       /* no diagnostic for this one */
+#pragma GCC diagnostic pop
+  foo(c);                       /* error is given for this one */
+#pragma GCC diagnostic pop
+  foo(d);                       /* depends on command-line options */
+
#pragma GCC diagnostic ignored_attributes

Similarly to -Wno-attributes=, this pragma allows users to suppress warnings about unknown scoped attributes (in C++11 and C2X). For example, #pragma GCC diagnostic ignored_attributes "vendor::attr" disables warning about the following declaration:

[[vendor::attr]] void f();
+

whereas #pragma GCC diagnostic ignored_attributes "vendor::" prevents warning about both of these declarations:

[[vendor::safe]] void f();
+[[vendor::unsafe]] void f2();
+

GCC also offers a simple mechanism for printing messages during compilation.

+#pragma message string +

Prints string as a compiler message on compilation. The message is informational only, and is neither a compilation warning nor an error. Newlines can be included in the string by using the ‘\n’ escape sequence.

#pragma message "Compiling " __FILE__ "..."
+

string may be parenthesized, and is printed with location information. For example,

#define DO_PRAGMA(x) _Pragma (#x)
+#define TODO(x) DO_PRAGMA(message ("TODO - " #x))
+
+TODO(Remember to fix this)
+

prints ‘/tmp/file.c:4: note: #pragma message: TODO - Remember to fix this’.

+#pragma GCC error message +
+

Generates an error message. This pragma is considered to indicate an error in the compilation, and it will be treated as such.

Newlines can be included in the string by using the ‘\n’ escape sequence. They will be displayed as newlines even if the -fmessage-length option is set to zero.

The error is only generated if the pragma is present in the code after pre-processing has been completed. It does not matter however if the code containing the pragma is unreachable:

#if 0
+#pragma GCC error "this error is not seen"
+#endif
+void foo (void)
+{
+  return;
+#pragma GCC error "this error is seen"
+}
+
+#pragma GCC warning message +
+

This is just like ‘pragma GCC error’ except that a warning message is issued instead of an error message. Unless -Werror is in effect, in which case this pragma will generate an error as well.

+

+ © Free Software Foundation
Licensed under the GNU Free Documentation License, Version 1.3.
+ https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Diagnostic-Pragmas.html +

+
-- cgit v1.2.3