Diagnostic Control Pragmas

This is slighty abbreviated and reformatted copy-paste excerpts from the gcc docs for diagnostics pragmas and function attributes.

#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

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/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 message string

GCC offers a simple mechanism for printing messages during compilation.

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

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.

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/warning message

Generates an error or warning message. This pragma is considered to indicate an error/warning 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 warning/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"
}

The warning version 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.

Interesting Function Attributes

const and pure

const is for functions whose return value is not affected by changes to the observable state of the program and is pure.

pure is for functions that have no observable effects on the state of the program other than to return a value.

int square (int) __attribute__ ((const));
int hash (char *) __attribute__ ((pure));//official example, not sure, why this can't be const

The const attribute prohibits a function from reading objects that affect its return value between successive invocations. The function will thus always return the same output on the same input and has no side-effects (as I understand it, its result is cachable).

For pure, it is only cachable if the “observable state” does not change.

used and unused

The attribute unused, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function.

The attribute used, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.

When applied to a member function of a C++ class template, the attribute used also means that the function is instantiated if the class itself is instantiated.



Home