Search code examples
c++pragmasuppress-warningsxlc

How to disable a specific IBM XL C++ compiler warning?


Given tmp.cpp:

#include <stdio.h>

#pragma report(disable, CCN8826)

int main(int argc, const char *argv[])
{
    const char * hi = "hi\n";
    printf(hi);

    return 0;
}

Despite I use #pragma report that is supposed to suppress the warning, I still get:

bash-3.1$ xlC -qformat=all tmp.cpp
"tmp.cpp", line 8.12: 1540-2826 (W) The format string is not a string literal 
and format arguments are not given.

How do I get rid of that warning?

The error message numbers are here and the #pragma report description is here. My compiler is IBM XL C/C++ Advanced Edition for Blue Gene/P, V9.0


Solution

  • I know it doesn't directly answer your question but you could presumably avoid the warning by changing your code to

    printf("%s", hi);
    

    In case you have:

    void f(char * s) { printf(s); }
    

    you can modify it as:

    void f(char * s) { printf("%s", s); }
    

    to get rid of the warning.

    EDIT: An easy, slightly limited, probably nasty way of dealing with your new issue would be

    char buf[1024];
    snprintf(buf, sizeof(buf), "%s %s", "bloody", "warning");
    fprintf(stderr, "%s", buf);
    

    It may be possible to generalise this to something like the following (untested!)

    my_printf(const char* fmt, ...)
    {
        va_list ap;
        char buf[1024];
        vsnprintf(buf, sizeof(buf), fmt, ap);
        fprintf(stderr, "%s", buf);
    }