Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -Wformat=2 do?

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?
like image 375
Peter Hosey Avatar asked Mar 02 '13 05:03

Peter Hosey


1 Answers

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.

like image 180
justin Avatar answered Sep 23 '22 15:09

justin