6.3 Controlling compiler diagnostic messages with pragmas

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:

You can alternatively use command-line options to suppress or change the severity of messages, but the change applies for the entire compilation.

Examples

The following example shows three identical functions, foo1(), foo2(), and foo3(), all of which would normally provoke diagnostic message #177-D: variable "x" was declared but never referenced.

For foo1(), the current pragma state is pushed to the stack and #pragma diag_suppress suppresses the message. The message is re-enabled by #pragma pop before compiling foo2(). In foo3(), the message is not suppressed because the #pragma push and #pragma pop do not enclose the full scope responsible for the generation of the message:

#pragma push
#pragma diag_suppress 177
void foo1( void )
{
    /* Here we do not expect a diagnostic, because we suppressed it. */
    int x;
}
#pragma pop

void foo2( void )
{
    /* Here we do, because the suppression was inside push/pop. */
    int x;
}
      
void foo3( void )
{
    #pragma push
    #pragma diag_suppress 177
    /* Here, the suppression fails because the push/pop must enclose the whole function. */ 
    int x;
    #pragma pop
}      

Diagnostic messages use the pragma state in place at the time they are generated. If you use pragmas to control a message in your code, you must be aware of when that message is generated. For example, the following code is intended to suppress the diagnostic message #177-D: function "dummy" was declared but never referenced:

#include <stdio.h>
#pragma push
#pragma diag_suppress 177
static int dummy(void)
{
    printf("This function is never called.");
    return 1;
}
#pragma pop
main(void){
     printf("Hello world!\n");
}

However, message 177 is only generated after all functions have been processed. Therefore, the message is generated after pragma pop restores the pragma state, and message 177 is not suppressed.

Removing pragma push and pragma pop would correctly suppress message 177, but would suppress messages for all unreferenced functions rather than for only the dummy() function.

Non-ConfidentialPDF file icon PDF versionARM DUI0472M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.