问题
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