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

倖福魔咒の 提交于 2019-12-13 17:53:22

问题


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


回答1:


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);
}



回答2:


As the message indicates, its message identifier is 1540-2826, therefore change the #pragma report to:

#pragma report(disable, "1540-2826")




回答3:


You can tell the compiler not to generate this warning (for any code, not just the line in question) by passing the flag -qsuppress=1540-2826.

To suppress multiple warnings, separate the codes with a semicolon e.g. -qsuppress=1540-2826:1540-0809.




回答4:


The error message IDs produced by IBM XL C++ depend on the operating system. On z/OS the message IDs look like CCN8826, but elsewhere they look like 1540-2826.

On z/OS UNIX System Services, compiling your code using xlc++ tmp.cpp yields no warnings or errors. In any case, adding

-qsuppress=CCN8826

to the xlc++ command line ought to do the trick there if that error does show up. Based on Josh Milthorpe's answer,

-qsuppress=1540-2826

should work on operating systems where that message ID format is used.

Note that not all messages can be suppressed, so if you get a complaint about the message you're trying to suppress then that may be because that particular message cannot be suppressed, even if you are using the correct format for specifying a message to suppress.

The z/OS V2R2 XL C/C++ Messages document suggests that only message numbers 3000 through 4399 (for C), and 5001 through 6999 and 7500 through 8999 (for C++) can be suppressed. If I specify -qsuppress=CCN1142 on z/OS UNIX System Services for a source file that yields message CCN1142 then I get a complaint about 'Unrecognized value "CCN1142" specified with option "SUPPRESS".' and message CCN1142 continues to be reported. -qsuppress=CCN8826 produces no complaint.



来源:https://stackoverflow.com/questions/13863815/how-to-disable-a-specific-ibm-xl-c-compiler-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!