What does -Wformat=2 do?

守給你的承諾、 提交于 2019-12-23 11:01:13

问题


I'm seeing numerous mentions on the web of a -Wformat=2 option to Clang, sometimes alongside the already-known -Wformat (the one that Xcode lists as “Typecheck calls to printf/scanf, although it covers many more string-formatting APIs now).

  • Does this do anything at all?
  • If it does, what, if anything, does it do differently from -Wformat?
  • Is it useful to have both, or is either one a superset of the other?

回答1:


If it does, what, if anything, does it do differently from -Wformat?

It's borrowed from GCC, as it is designed to be a suitable drop in replacement. GCC's description:

-Wformat=2

Enable -Wformat plus format checks not included in -Wformat. Currently equivalent to `-Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k'.

That answers most of your questions.

Does this do anything at all?

Yes. The following program emits different warnings, depending on the presence (or absence) of -Wformat=2:

__attribute__((__format__ (__printf__, 1, 2)))
static void F(const char* const pString, ...) {
}

int main(int argc, const char* argv[]) {
    F("", 0);
    F(argv[0], 0);
    const char str[] = "aaa";
    F(str, 0);
    F("%i", 0);
    F("%i");
    return 0;
}

Note: It appears Clang rolls -Wformat-security into -Wformat.

Finally, I've not tested -Wformat-y2k, but it is defined by GCC as:

-Wformat-y2k

If -Wformat is specified, also warn about strftime formats which may yield only a two-digit year.



来源:https://stackoverflow.com/questions/15171017/what-does-wformat-2-do

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