You copied the Doc URL to your clipboard.

#pragma clang diagnostic

Pragmas let you suppress, enable, or change the severity of specific diagnostic messages from within your code.

For example, you can suppress a particular diagnostic message when compiling one specific function.

Note

Alternatively, you can use the command-line option, -Wname, to suppress or change the severity of messages, but the change applies for the entire compilation.

#pragma clang diagnostic ignored

#pragma clang diagnostic ignored "-Wname"

This pragma disables the diagnostic message specified by name.

#pragma clang diagnostic warning

#pragma clang diagnostic warning "-Wname"

This pragma sets the diagnostic message specified by name to warning severity.

#pragma clang diagnostic error

#pragma clang diagnostic error "-Wname"

This pragma sets the diagnostic message specified by name to error severity.

#pragma clang diagnostic fatal

#pragma clang diagnostic fatal "-Wname"

This pragma sets the diagnostic message specified by name to fatal error severity. Fatal error causes compilation to fail without processing the rest of the file.

#pragma clang diagnostic push, #pragma clang diagnostic pop

#pragma clang diagnostic push
#pragma clang diagnostic pop

#pragma clang diagnostic push saves the current pragma diagnostic state so that it can restored later.

#pragma clang diagnostic pop restores the diagnostic state that was previously saved using #pragma clang diagnostic push.

Examples of using pragmas to control diagnostics

The following example shows four identical functions, foo1(), foo2(), foo3(), and foo4(). All these functions would normally provoke diagnostic message warning: multi-character character constant [-Wmultichar] on the source lines char c = (char) 'ab';

Using pragmas, you can suppress or change the severity of these diagnostic messages for individual functions.

For foo1(), the current pragma diagnostic state is pushed to the stack and #pragma clang diagnostic ignored suppresses the message. The diagnostic message is then re-enabled by #pragma clang diagnostic pop.

For foo2(), the diagnostic message is not suppressed because the original pragma diagnostic state has been restored.

For foo3(), the message is initially suppressed by the preceding #pragma clang diagnostic ignored "-Wmultichar", however, the message is then re-enabled as an error, using #pragma clang diagnostic error "-Wmultichar". The compiler therefore reports an error in foo3().

For foo4(), the pragma diagnostic state is restored to the state saved by the preceding #pragma clang diagnostic push. This state therefore includes #pragma clang diagnostic ignored "-Wmultichar" and therefore the compiler does not report a warning in foo4().

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmultichar"
void foo1( void )
{
    /* Here we do not expect a diagnostic message, because it is suppressed by #pragma clang diagnostic ignored "-Wmultichar". */
    char c = (char) 'ab';
}
#pragma clang diagnostic pop

void foo2( void )
{
    /* Here we expect a warning, because the suppression was inside push and then the diagnostic message was restored by pop. */
    char c = (char) 'ab';
}
      
#pragma clang diagnostic ignored "-Wmultichar"
#pragma clang diagnostic push
void foo3( void )
{
    #pragma clang diagnostic error "-Wmultichar"
    /* Here, the diagnostic message is elevated to error severity. */ 
    char c = (char) 'ab';
}     
#pragma clang diagnostic pop

void foo4( void )
{
    /* Here, there is no diagnostic message because the restored diagnostic state only includes the #pragma clang diagnostic ignored "-Wmultichar". 
       It does not include the #pragma clang diagnostic error "-Wmultichar" that is within the push and pop pragmas. */ 
    char c = (char) 'ab';
}     

Diagnostic messages use the pragma state that is present at the time they are generated. If you use pragmas to control a diagnostic message in your code, you must be aware of when, in the compilation process, that diagnostic message is generated.

If a diagnostic message for a function, functionA, is only generated after all the functions have been processed, then the compiler controls this diagnostic message using the pragma diagnostic state that is present after processing all the functions. This diagnostic state might be different from the diagnostic state immediately before or within the definition of functionA.

Related reference
Was this page helpful? Yes No