-Wformat = 2做什么? [英] What does -Wformat=2 do?
问题描述
我在网络上看到Clang的 -Wformat = 2
选项的无数提及,有时还有众所周知的 -Wformat
(Xcode列出为对 printf
/ scanf
的Typecheck调用
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).
- 这是否有作用吗?
- 如果它会做什么,如果有的话,它与
-Wformat
有什么不同吗? - 同时拥有这两个功能是否有用?
- 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?
推荐答案
,它与-Wformat有什么不同?
If it does, what, if anything, does it do differently from -Wformat?
它是从GCC借来的,因为它很适合减少更换。 GCC的描述:
It's borrowed from GCC, as it is designed to be a suitable drop in replacement. GCC's description:
-Wformat = 2
启用-Wformat加格式检查功能包含在-Wformat中。当前
相当于`-Wformat -Wformat-nonliteral -Wformat-security
-Wformat-y2k'。
那回答了您的大部分问题。
That answers most of your questions.
这是否有作用?
Does this do anything at all?
是。以下程序根据 -Wformat = 2
的存在(或不存在)发出不同的警告:
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;
}
注意:似乎Clang滚动了 -Wformat-security
转换为 -Wformat
。
Note: It appears Clang rolls -Wformat-security
into -Wformat
.
最后,我尚未测试 -Wformat-y2k
,但由GCC定义为:
Finally, I've not tested -Wformat-y2k
, but it is defined by GCC as:
-Wformat- y2k
如果指定了-Wformat,还警告strftime格式可能只产生两位数的年份。
这篇关于-Wformat = 2做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!