如何开启(字面上)GCC的所有警告?

256

我希望能够启用GCC的所有警告(你会认为这很容易...)

  • 你也许会认为 -Wall 就可以实现,但实际上不行! 你仍然需要 -Wextra

  • 你也许会认为 -Wextra 就可以实现,但实际上并不是全部列在这里的警告都被启用了(例如-Wshadow)。而且我仍然不确定这个列表是否全面。

如何告诉GCC启用(没有其他条件!)所有它所拥有的警告?


46
@Arafangion: 我不明白问题哪里“不清楚”——是的,我想打开适用于我的代码的所有警告,无论它们有多么啰嗦。至于原因,非常简单:我发现一些-Wall或-Wextra没有打开的警告很有帮助,所以我想尝试其余部分,看看能否改进我的代码。就这么简单。 - user541686
21
你希望编译器警告什么?可能出现的错误和/或不良风格?我的意思是,如果我知道所有确切的警告,我就会手动打开它们,无需提问。如果答案确实是“你必须查看源代码才能找到它们”,那么请将其发布为答案! - user541686
70
Clang 3.1 提供了 -Weverything 参数。 - Alexandre Hamez
6
好的,这个问题被标记为C++,所以... :) - Some programmer dude
6
现在(终于)有一种自动提取源代码警告的方法:https://github.com/barro/compiler-warnings。请问您需要什么其他帮助吗? - Kyle Strand
显示剩余10条评论
8个回答

158

你无法这样做。

GCC 4.4.0的手册仅适用于该版本,但它确实列出了所有可能的警告信息。然而,并非所有警告都在你链接的页面上。例如,某些特定于语言的选项在C++选项或Objective-C选项页面上。要找到它们所有的内容,最好查看Options Summary

打开everything会包括-Wdouble-promotion,它仅适用于具有32位单精度浮点单元并以硬件形式实现float,但在软件中模拟double的CPU。使用double进行计算将使用软件仿真并变慢。这对于一些嵌入式CPU是相关的,但对于具有64位浮点硬件支持的现代桌面CPU完全不相关。

另一个通常没有用处的警告是-Wtraditional,它会警告关于完全格式正确但在传统C中具有不同含义(或无法工作)的代码,例如"string " "concatenation"或ISO C函数定义!您真的关心与30年前的编译器兼容性吗?您真的想要为编写int inc(int i) { return i+1; }而收到警告吗?
我认为 -Weffc++ 太吵了,没有用。它基于过时的第一版 Effective C++,并警告那些完全有效的 C++ 构造(对于这些构造,指南在书的后续版本中已更改)。我不想被警告我没有在我的构造函数中初始化 std::string 成员; 它有一个默认构造函数,恰好是我想要的。我为什么要写 m_str() 来调用它?-Weffc++ 警告会有所帮助,但太难以准确检测到(导致假阴性),而那些没有用的警告,比如显式初始化所有成员,只会产生太多噪音,导致假阳性。
Luc Danton 提供了一个 great example,展示了来自 -Waggregate-return 的无用警告,几乎肯定不适用于 C++ 代码。
也就是说,你并不真正想要 所有 的警告;你只是认为你需要。
阅读手册,了解它们,决定哪些你可能想启用,然后尝试它们。无论如何,阅读编译器的手册都是一件好事,采取捷径并启用你不理解的警告不是一个很好的主意,特别是如果这是为了避免 阅读文档

只有那些无知的人或者被自以为是的老板说“不要警告”的人才会打开所有的警告。

有些警告很重要,有些则不是。你必须有所区分,否则会破坏你的程序。例如考虑 -Wdouble-promotion。如果你正在处理嵌入式系统,你可能需要这个;如果你正在处理桌面系统,你可能不需要。而你是否需要 -Wtraditional?我怀疑。

参见-Wall-all以启用所有警告,该选项已关闭,标记为WONTFIX
针对DevSolar关于makefile需要根据编译器版本使用不同警告的抱怨,如果-Wall -Wextra不合适,则很容易使用特定于编译器和版本的CFLAGS。
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))

55
阅读手册,了解其内容,决定应该启用哪些功能,尝试它们。这里遗漏了一些步骤:“为每个编译器版本重新查看手册,并适应您的警告列表,因为它们正在不断变化。让您的Makefile检查确切的编译器版本,并为每个版本使用不同的警告列表。”我们有维护者维护的优化级别,为什么他们不能费心为警告提供相同的服务呢? - DevSolar
25
@JonathanWakely说:“我有我的项目,GCC不在其中。我指出了他们产品的一个缺陷。要么他们修复它,要么他们接受没有这样做的责备,但我不会为他们修复它,开源软件也无法改变这一点。” - DevSolar
27
@JonathanWakely说:"如果你想要什么,就要直接提出请求,不要抱怨。" 我并不必须参与GCC项目才能批评它,尤其是如果#31573已经被标记为"WONTFIX"。这把这个主题从“询问”转变为“抱怨”。 - DevSolar
78
我认为,“-Weverything”是比gcc策略更好的解决方案,后者不提供这样的选项。我在使用clang时会带上这个标志,因为我的理念是默认开启所有警告(因为有人认为将其添加到编译器中已经足够有用了),如果我不喜欢某个警告,我会特别关闭它。关键在于你无法知道未触发的警告,但是你会知道你不想要的警告已经触发了,而且它们很容易被关闭。 - David Stone
22
@JonathanWakely 说的没错,但那些警告太少了。最容易发现哪些警告可能与你的代码相关的方法是查看哪些警告被触发了;在那时,你可以看到一个相关的、真实的潜在危险代码的例子,然后再决定是否禁用该警告。使用Clang的-Weverything选项可以轻松完成这个任务,但使用GCC则不可能。 - Kyle Strand
显示剩余12条评论

96

我同意之前的答案,认为启用所有警告可能并不有益,但是GCC确实提供了一种相当方便的方法来实现这一点。该命令为:

gcc -Q --help=warning

列出所有支持的警告选项列表,提供它们是否被激活的信息。这可以用来查找哪些选项已经被启用或未启用,例如通过-Wall-Wextra

gcc -Wall -Wextra -Q --help=warning

为了启用所有警告,您可以使用一些正则表达式来提取命令行参数

gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n'

对于我的当前GCC,这会给出:

-Wabi -Wabi-tag -Waddress -Waggregate-return -Waggressive-loop-optimizations -Waliasing -Walign-commons -Wampersand -Warray-bounds -Warray-temporaries -Wassign-intercept -Wattributes -Wbad-function-cast -Wbool-compare -Wbuiltin-macro-redefined -Wc++-compat -Wc++0x-compat -Wc++14-compat -Wc-binding-type -Wc90-c99-compat -Wc99-c11-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wcharacter-truncation -Wchkp -Wclobbered -Wcomment -Wcompare-reals -Wconditionally-supported -Wconversion -Wconversion-extra -Wconversion-null -Wcoverage-mismatch -Wcpp -Wctor-dtor-privacy -Wdate-time -Wdeclaration-after-statement -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated -Wdeprecated-declarations -Wdesignated-init -Wdisabled-optimization -Wdiscarded-array-qualifiers -Wdiscarded-qualifiers -Wdiv-by-zero -Wdouble-promotion -Weffc++ -Wempty-body -Wendif-labels -Wenum-compare -Wextra -Wfloat-equal -Wformat-contains-nul -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-signedness -Wformat-y2k -Wformat-zero-length -Wfree-nonheap-object -Wfunction-elimination -Wignored-qualifiers -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wimplicit-interface -Wimplicit-procedure -Wincompatible-pointer-types -Winherited-variadic-ctor -Winit-self -Winline -Wint-conversion -Wint-to-pointer-cast -Wintrinsic-shadow -Wintrinsics-std -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch -Wjump-misses-init -Wline-truncation -Wliteral-suffix -Wlogical-not-parentheses -Wlogical-op -Wlong-long -Wmain -Wmaybe-uninitialized -Wmemset-transposed-args -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers -Wmissing-include-dirs -Wmissing-parameter-type -Wmissing-prototypes -Wmultichar -Wnarrowing -Wnested-externs -Wnoexcept -Wnon-template-friend -Wnon-virtual-dtor -Wnonnull -Wodr -Wold-style-cast -Wold-style-declaration -Wold-style-definition -Wopenmp-simd -Woverflow -Woverlength-strings -Woverloaded-virtual -Woverride-init -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpedantic -Wpmf-conversions -Wpointer-arith -Wpointer-sign -Wpointer-to-int-cast -Wpragmas -Wproperty-assign-default -Wprotocol -Wreal-q-constant -Wrealloc-lhs -Wrealloc-lhs-all -Wredundant-decls -Wreorder -Wreturn-local-addr -Wreturn-type -Wselector -Wsequence-point -Wshadow -Wshadow-ivar -Wshift-count-negative -Wshift-count-overflow -Wsign-compare -Wsign-promo -Wsized-deallocation -Wsizeof-array-argument -Wsizeof-pointer-memaccess -Wstack-protector -Wstrict-null-sentinel -Wstrict-prototypes -Wstrict-selector-match -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types -Wsuggest-override -Wsurprising -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum -Wsync-nand -Wsynth -Wsystem-headers -Wtabs -Wtarget-lifetime -Wtraditional -Wtraditional-conversion -Wtrampolines -Wtrigraphs -Wtype-limits -Wundeclared-selector -Wundef -Wunderflow -Wuninitialized -Wunknown-pragmas -Wunsafe-loop-optimizations -Wunsuffixed-float-constants -Wunused -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-dummy-argument -Wunused-function -Wunused-label -Wunused-local-typedefs -Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable -Wuse-without-only -Wuseless-cast -Wvarargs -Wvariadic-macros -Wvector-operation-performance -Wvirtual-move-assign -Wvla -Wvolatile

gcc $(gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n')

但要注意,这将导致出现警告,因为某些警告选项仅适用于特定语言(例如C++)。可以通过使用更多的正则表达式来仅包含当前语言允许的选项或在调用的末尾添加适当的-Wno-whatever来避免这些警告。


11
恐怕这不切实际。GCC已经向我显示了标准库的警告。 - Valentin H
19
正如我所说的,我不认为启用所有警告会有益,但这是OP要求的。然而,我认为通过明确删除其他答案中已经提到的一些问题警告(例如,通过在调用末尾添加相应的-Wno-whatever),这可以实际使用。 - Haatschii
6
您可以通过在相关目录中使用-isystem而不是-I来防止gcc对系统/ std /第三方头文件发出警告。 - Kyle Strand
5
这应该是被接受的答案,因为它确实并直接回答了问题。 - TFuto
有人能澄清一下吗:你可以启用所有GCC警告,但也可以添加其他标志来禁用单个标志/检查吗? - user997112
1
@user997112 是的,那是正确的。请注意参数的顺序很重要。例如,“gcc -Wabi -Wno-abi -Q --help=warning”将显示“-Wabi”已禁用,而“gcc -Wno-abi -Wabi -Q --help=warning”将显示“-Wabi”已启用。 - Haatschii

18

启用所有警告选项进行编程是不可能的(除非您打算忽略它们,但那样做又有何意义呢?)。例如,假设您使用以下一组标志:-Wstrict-prototypes -Wtraditional

即使启用了两个警告,下面的程序也会报错。

/tmp $ cat main.c 
int main(int argc, char **argv) {
    return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c 
main.c: In function ‘main’:
main.c:1:5: warning: traditional C rejects ISO C style function definitions [-Wtraditional]
 int main(int argc, char **argv) {
     ^

你可能会认为“好吧,那我来使用旧式原型”。不,这是行不通的。
/tmp $ cat main.c 
int main(argc, argv)
    int argc;
    char **argv;
{
    return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c 
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
 int main(argc, argv)
     ^

不指定任何原型也是错误的,因为编译器也会报错。

/tmp $ cat main.c 
int main() {
    return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c 
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
 int main() {
     ^

如果在程序中定义了任何函数,您就无法使用所有的标志,因为编译器会抱怨任何想象得到的函数定义。

对于C++,这是可能的(-Wtraditional标志不存在),并且非常简单的程序可以被编译。要启用所有警告,请使用以下警告列表(可能会出现一些重复的警告,因为我没有过滤由-Wall启用的警告)。

-Wabi -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Weffc++ -Wstrict-null-sentinel -Wno-non-template-friend -Wold-style-cast -Woverloaded-virtual -Wno-pmf-conversions -Wsign-promo -Wextra -Wall -Waddress -Waggregate-return -Warray-bounds -Wno-attributes -Wno-builtin-macro-redefined -Wc++0x-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wclobbered -Wcomment -Wconversion -Wcoverage-mismatch -Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization -Wno-div-by-zero -Wempty-body -Wenum-compare -Wno-endif-labels -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wignored-qualifiers -Winit-self -Winline -Wno-int-to-pointer-cast -Wno-invalid-offsetof -Winvalid-pch -Wunsafe-loop-optimizations -Wlogical-op -Wlong-long -Wmain -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wno-mudflap -Wno-multichar -Wnonnull -Wno-overflow -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wsign-conversion -Wstack-protector -Wstrict-aliasing=1 -Wstrict-overflow=5 -Wswitch -Wswitch-default -Wswitch-enum -Wsync-nand -Wsystem-headers -Wtrigraphs -Wtype-limits -Wundef -Wuninitialized -Wunknown-pragmas -Wno-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvla -Wvolatile-register-var -Wwrite-strings

15
直到现在我从未费心去检查,但实际上这并非不可能...... 请尝试使用int main(int, char **); int main(argc, argv) int argc; char **argv; { (void)argc; (void)argv; return 0; }代码。 (注:这是一段C++代码,用于定义一个main函数并返回0) - user541686
2
即使是这个微不足道的程序,我仍然可以得到“警告:堆栈使用量为16字节[-Wstack-usage=]”;-) - Marc Glisse
OP 在某处表示目的是学习风格规则,以更好地了解应激活哪些警告以及在代码中进行哪些更改。 - Chris Mountford

12

有人创建了一组工具,用于确定给定GCC或Clang版本的完整警告集。

对于GCC来说,从此工具提供的完整警告列表中复制您的编译器版本似乎是确保所有警告被打开的唯一方法,因为(与Clang不同)GCC不提供-Weverything

该工具似乎解析了GCC源代码中实际的c.opt文件,因此其结果应该是明确的。

该存储库还包含生成的大多数GCC和Clang版本的警告列表的文本文件(目前为止是Clang 3.2到3.7和GCC 3.4到5.3)。


这是答案。使用“顶层”列表,并添加所有在顶层(未缩进/嵌套)的参数。https://github.com/Barro/compiler-warnings/blob/master/gcc/warnings-gcc-top-level-8.txt - Jetski S-type

8
GCC 4.3+现在有-Q --help=warnings,你甚至可以指定--help=warnings,C只打印与C相关的警告。
我刚写了一个m4模块来利用它(它还支持Clang-Weverything);请参阅wget_manywarnings.m4
使用它非常简单。基本上,该模块会打开每个警告标志。您可以根据需要删除警告--有些警告确实非常冗长。
例如:configure.ac 如果您不使用autotools,您可以在m4模块中找到打开所有禁用警告的代码,这基本上是通过AWK管道传递的GCC调用: flags="-Wall -Wextra -Wformat=2 "$(gcc -Wall -Wextra -Wformat=2 -Q --help=warning,C|awk '{ if (($2 == "[disabled]" || $2 == "") && $1!~/=/ && $1~/^-W/&& $1!="-Wall") print $1 }'

4
这个页面可以看到: 请注意,并非所有警告标志都由-Wall隐含。其中一些警告是针对用户通常不认为有问题的构造,但偶尔您可能希望检查一下;其他警告是针对某些情况下必需或难以避免的构造,没有简单的方法来修改代码以抑制警告。其中一些被-Wextra启用,但许多必须逐个启用。
那么问题就是哪些是警告标志?也许你可以使用grep在该页面中查找所有以-W开头的行,并获得完整的警告标志列表。然后将其与-Wall-Wextra下的列表进行比较。还有一个-Wpedantic,虽然您显然想要更加严格 =)

“而且我仍然不知道这个列表是否全面”,是的,我当然可以grep那个页面,但问题是,它是否全面? - user541686
1
我不知道... 你可能需要仔细研究GCC源代码。你是想让作为程序员的生活变得异常困难,还是有一个很好的理由想要看到每一个可能的警告呢?=) - paddy
3
我希望能够查看GCC对我的代码的诊断结果,我发现这对我非常有帮助。但是显然,如果我已经知道所有警告以及哪些警告是有用的(哪些不是),那么就没有必要询问了。除非我去尝试它们,否则我无法确定哪些警告是有用的(例如,我发现阴影变量警告很有用,所以它们并不是无用的仅仅因为它们被关闭了)。 - user541686

3
我仍然不确定这个列表是否全面。它可能是全面的,但唯一真正全面的列表是编译器的实际源代码。然而,GCC非常庞大!我不知道所有命令行参数是否都集中在一个地方或分散在几个源文件中。此外,请注意,有些警告是针对预处理器的,有些是针对实际编译器的,还有一些是针对链接器的(这是一个完全独立的程序,并且包含在binutils软件包中),因此它们很可能分散在各处。

3
我的回答中提到了一个名为“Options Summary”的页面链接(https://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Option-Summary.html),该页面将所有选项分组在一个页面上。GCC代码审查政策要求所有新选项都必须有文档记录,因此文档应该是全面的。 - Jonathan Wakely

3

编辑于2023年:为GCC 12进行了更新。


我从其他帖子中收集了信息,并逐一测试了C++库测试中的警告。

使用Haatschii的列表和他/她获取GCC 11完整列表的方法:

gcc -Wall -Wextra -Wpedantic -Q --help=warning

在所有这些警告中,有些不适用于C++,因此这里是一个警告列表和一些最小的注释,适用于我的C++项目测试。

请注意:

  1. 其中一些警告默认情况下已经打开,无需添加任何选项。
  2. 我并不确定某些警告的实际含义。
  3. 我不建议使用或不使用任何特定的警告。
  4. 有些被注释了,这并不意味着什么。根据需要进行注释或取消注释。(我注释了那些对我的项目没有用的警告。)
  5. 有些在GCC 10上无法使用。
  6. 该列表是按字母顺序排列的,稍微分组以节省垂直空间。作为奖励,它格式化为可在CMake项目中使用。

现在是列表:

target_compile_options(
    target
    PRIVATE
        $<$<AND:$<CXX_COMPILER_ID:GNU>,$<NOT:$<CUDA_COMPILER_ID:NVIDIA>>,$<NOT:$<CUDA_COMPILER_ID:Clang>>>:
            -Werror
            -Wall
            -Wextra  # (activates -Wunknown-pragmas)
            -Wpedantic
            -WNSObject-attribute  # (gcc 12, not in 11)
            # -Wabi=13 -Wabi-tag (maybe important when linking with very old libraries)
            # -Wabsolute-value  # C/ObjC only (gcc 12, not in 11)
            -Waddress
            # -Waddress-of-packed-member (gcc 11, not in gcc 8)
            # -Waggregate-return (disallow return classes or structs, seems a C-compatibility warning)
            -Waggressive-loop-optimizations
            # -Waligned-new=all  (gcc 12, not in 11)
            # -Walloc-size-larger-than=<bytes>  (gcc 12, not in 11)
            -Walloc-zero  # -Walloc-size-larger-than=<bytes>
            -Walloca  # -Walloca-larger-than=<number>
            # -Wanalyzer-double-fclose -Wanalyzer-double-free -Wanalyzer-exposure-through-output-file -Wanalyzer-file-leak -Wanalyzer-free-of-non-heap -Wanalyzer-malloc-leak (gcc 11, not in gcc 9)
            # -Wanalyzer-mismatching-deallocation (gcc 11, not in gcc 10)
            # -Wanalyzer-null-argument -Wanalyzer-possible-null-argument -Wanalyzer-null-dereference (gcc 11, not in gcc 9)
            # -Wanalyzer-possible-null-dereference (gcc 11, not in gcc 9)
            # -Wanalyzer-shift-count-negative -Wanalyzer-shift-count-overflow (gcc 11, not in gcc 10)
            # -Wanalyzer-stale-setjmp-buffer
            # -Wanalyzer-tainted-allocation-size (gcc 12, not in 11)
            # -Wanalyzer-tainted-array-index (gcc 11, not in gcc 9)
            # -Wanalyzer-tainted-divisor -Wanalyzer-tainted-offset -Wanalyzer-tainted-size -Wanalyzer-too-complex -Wanalyzer-unsafe-call-within-signal-handler (gcc 12, not in 11)
            # -Wanalyzer-unsafe-call-within-signal-handler -Wanalyzer-use-after-free -Wanalyzer-use-of-pointer-in-stale-stack-frame
            # -Wanalyzer-write-to-const -Wanalyzer-write-to-string-literal (gcc 11, not in gcc 10)
            # -Warith-conversion (gcc 11, not in gcc 9)
            -Warray-bounds
            # -Warray-bounds=<0,2> -Warray-compare (gcc 12, not in gcc 9)
            # -Warray-parameter -Warray-parameter=<0,2>  (gcc 11, not in gcc 10)
            # -Wattribute-alias -Wattribute-alias=<0,2> (gcc 12, not in 11)
            # -Wattribute-warning (gcc 9, not in 8)
            -Wattributes
            # -Wbad-function-cast (gcc 12, not in 11)
            -Wbool-compare -Wbool-operation
            # -Wbidi-chars -Wbidi-chars=any (gcc 12, not in 11)
            -Wbuiltin-declaration-mismatch -Wbuiltin-macro-redefined
            #-Wc++-compat
            -Wc++0x-compat -Wc++11-compat -Wc++14-compat -Wc++17-compat -Wc++17-extensions -Wc++1z-compat
            # -Wc++20-compat -Wc++20-extensions -Wc++23-extensions -Wc++2a-compat (gcc 11, not in gcc 9)
            # -Wcannot-profile (gcc 9, not in gcc 8)
            -Wcast-align=strict -Wcast-function-type
            -Wcast-qual
            -Wcatch-value  #=<0, 3>
            -Wchar-subscripts
            # -Wchkp -Wclass-conversion -Wclass-memaccess (gcc 12, not in 11)
            # -Wclobbered
            # -Wcomma-subscript (gcc 12, not in 11)
            # -Wcomment  # (same as -Wcomments)
            # -Wcompare-reals (gcc 12, not in 11)
            -Wconditionally-supported
            -Wconversion -Wconversion-null
            -Wcoverage-mismatch -Wcpp
            # -Wctad-maybe-unsupported  # TODO(correaa) add ctad explicitly as necessary
            -Wctor-dtor-privacy
            -Wdangling-else
            # -Wdangling-pointer (gcc 12, not in 11)
            -Wdate-time
            # -Wdeclaration-after-statement (gcc 12, not in 11)
            -Wdelete-incomplete -Wdelete-non-virtual-dtor
            -Wdeprecated
            # -Wdeprecated-copy -Wdeprecated-copy-dtor (gcc 11, not in gcc 8)
            -Wdeprecated-declarations
            # -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion (gcc 11, not in gcc 10)
            # -Wdesignated-init (gcc 12, not in 11)
            -Wdisabled-optimization
            # -Wdiscarded-array-qualifiers (gcc 12, not in 11)
            -Wdiv-by-zero -Wdouble-promotion
            # -Wduplicate-decl-specifier (gcc 12, not in 11)
            -Wduplicated-branches -Wduplicated-cond
            # -Weffc++ (doesn't allow some advanced techniques, such as CRTP)
            -Wempty-body -Wendif-labels
            -Wenum-compare
            # -Wenum-conversion (gcc 11, not in gcc 10)
            -Wexpansion-to-defined
            # -Werror-implicit-function-declaration not for C++ (gcc 12, not in 11)
            # -Wexceptions  (gcc 11, not in gcc 10)
            # -Wextra
            -Wextra-semi
            -Wfloat-conversion # -Wfloat-equal (disallows float equality)
            -Wformat=2
            # -Wformat-contains-nul (gcc 12, not in 11)
            # -Wformat-diag (gcc 10, not in gcc 9)
            -Wformat-extra-args -Wformat-nonliteral
            # -Wformat-overflow=1
            -Wformat-security -Wformat-signedness -Wformat-truncation -Wformat-y2k -Wformat-zero-length
            -Wframe-address  # -Wframe-larger-than=<byte-size>
            -Wfree-nonheap-object -Whsa
            -Wif-not-aligned
            -Wignored-attributes -Wignored-qualifiers
            # -Wimplicit (gcc 12, not in 11)
            -Wimplicit-fallthrough#=3  # -Wimplicit-fallthrough=<0,5>
            # -Wimplicit-function-declaration -Wimplicit-int (gcc 12, not in 11)
            # -Winaccessible-base (gcc 12, not in 11)
            # -Wincompatible-pointer-types -Winfinite-recursion  -Winherited-variadic-ctor -Winit-list-lifetime (gcc 12, not in 11)
            -Winit-self
            # -Winline
            # -Wint-conversion (gcc 12, not in 11)
            -Wint-in-bool-context -Wint-to-pointer-cast
            # -Winterference-size (gcc 12, not in 11)
            # -Winvalid-imported-macros (gcc 11, not in gcc 10)
            -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch
            # -Wjump-misses-init (gcc 12, not in 11)
            # -Wlarger-than=<byte-size>  # (disallow large objects types? in executable)
            -Wliteral-suffix
            -Wlogical-not-parentheses -Wlogical-op
            # -Wlong-long (C++98 warning)
            -Wlto-type-mismatch -Wmain -Wmaybe-uninitialized
            -Wmemset-elt-size -Wmemset-transposed-args
            -Wmisleading-indentation
            # -Wmismatched-dealloc -Wmismatched-new-delete (gcc 11, not in gcc 10)
            # -Wmismatched-tags (gcc 11, not in gcc 9)
            -Wmissing-attributes
            -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn
            # -Wmissing-parameter-type (gcc 12, not in  11)
            # -Wmissing-profile (gcc 11, not in gcc 8)
            # -Wmissing-prototypes -Wmissing-requires -Wmissing-template-keyword (gcc 12, not in 11)
            -Wmultichar
            # -Wmultiple-inheritance (disallows composition by inheritance)
            -Wmultistatement-macros
            # -Wnamespaces (disallows use of namespaces, seems a C-tool)
            -Wnarrowing
            # -Wnested-externs (gcc 12, not in 11)
            # -Wno-alloc-size-larger-than=<bytes> -Wframe-larger-than=<bytes> -Wno-larger-than<bytes> -Wstack-usage=<bytes> (gcc 112, not in 11)
            -Wnoexcept -Wnoexcept-type
            -Wnon-template-friend -Wnon-virtual-dtor
            -Wnonnull -Wnonnull-compare
            -Wnormalized  #=nfc -Wnormalized=[none|id|nfc|nfkc]
            -Wnull-dereference
            -Wodr -Wold-style-cast
            # -Wold-style-declaration -Wold-style-definition (gcc 12, not in 11)
            # -Wopenacc-parallelism (gcc 12, not in 11)
            -Wopenmp-simd -Woverflow
            -Woverlength-strings -Woverloaded-virtual
            # -Woverride-init -Woverride-init-side-effects (gcc 12, not in 11)
            -Wpacked -Wpacked-bitfield-compat -Wpacked-not-aligned
            # -Wpadded (disallows structs that need padding for alignment)
            -Wparentheses
            # -Wpedantic (see above)
            # -Wpessimizing-move (gcc 11, not in gcc 8)
            -Wplacement-new  #=1  -Wplacement-new=<0,2>
            -Wpmf-conversions
            -Wpointer-arith -Wpointer-compare
            # -Wpointer-sign -Wpointer-to-int-cast (gcc 12, not in 11)
            -Wpragmas
            # -Wprio-ctor-dtor (gcc 11, not in gcc 8)
            -Wpsabi
            # -Wrange-loop-construct (gcc 11, not in gcc 10)
            -Wredundant-decls
            # -Wredundant-move (gcc 11, not in gcc 8)
            # -Wredundant-tags (gcc 11, not in gcc 9)
            -Wregister
            # -Wreorder (gcc 12, not in 11)
            -Wreturn-local-addr -Wreturn-type
            -Wrestrict -Wreorder
            -Wscalar-storage-order -Wsequence-point
            -Wshadow -Wshadow-compatible-local -Wshadow-local -Wshadow=compatible-local -Wshadow=local
            -Wshift-count-negative -Wshift-count-overflow -Wshift-negative-value -Wshift-overflow  #=1 -Wshift-overflow=<0,2>
            -Wsign-compare -Wsign-conversion -Wsign-promo
            -Wsized-deallocation
            -Wsizeof-array-argument
            # -Wsizeof-array-div (gcc 11, not in gcc 10)
            -Wsizeof-pointer-div
            -Wsizeof-pointer-memaccess
            -Wstack-protector  # -Wstack-usage=<byte-size>
            -Wstrict-aliasing  #=3  -Wstrict-aliasing=<0,3>
            -Wstrict-null-sentinel  #=1  -Wstrict-overflow=<0,5>
            -Wstrict-overflow  #=1  -Wstrict-overflow=<0,5>
            # -Wstrict-prototypes (gcc 12, not in gcc 11)
            # -Wstring-compare (gcc 11, not in gcc 9)
            -Wstringop-overflow  #=2  -Wstringop-overflow=<0,4>
            # -Wstringop-overread (gcc 11, not in gcc 10)
            -Wstringop-truncation
            -Wsubobject-linkage
            # -Wsuggest-attribute=cold (gcc 12, not in 11)
            -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=malloc -Wsuggest-attribute=noreturn
            # -Wsuggest-attribute=pure
            -Wsuggest-final-methods -Wsuggest-final-types
            # -Wsuggest-override (gcc 12, not in gcc 11)
            -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum
            # -Wswitch-outside-range (gcc 11, not in gcc 9)
            -Wswitch-unreachable
            -Wsync-nand -Wsynth
            # -Wsystem-headers (expects system headers to be warning-compliant which they are not)
            -Wtautological-compare
            # -Wtemplates (disallows templates, C-tool)
            # -Wterminate -Wtraditional -Wtraditional-conversion (gcc 12, not in 11)
            -Wtrampolines -Wtrigraphs
            # -Wtrivial-auto-var-init (gcc 12, not in 11)
            # -Wtsan (gcc 11, not in 10)
            -Wtype-limits -Wundef -Wuninitialized
            -Wno-unknown-pragmas  # (see above) -Wunknown-pragmas (other compilers need their own pragmas for their warnings)
            -Wunreachable-code -Wunsafe-loop-optimizations
            # -Wunsuffixed-float-constants (gcc 12, not in 11)
            -Wunused -Wunused-but-set-parameter -Wunused-but-set-variable
            # -Wunused-const-variable  #=2 TODO(correaa) add [[maybe_unused]] to niebloids
            -Wunused-function -Wunused-label -Wunused-local-typedefs -Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable
            # -Wuse-after-free  # =<0,3> (gcc 12, not in 11)
            -Wuseless-cast
            -Wvarargs -Wvariadic-macros -Wvector-operation-performance
            # -Wvexing-parse (gcc 11, not in gcc 10)
            -Wvirtual-inheritance -Wvirtual-move-assign
            -Wvla
            # -Wvla-larger-than=<number>  (gcc 12, not in 11)
            # -Wvla-parameter (gcc 11, not in gcc 10)
            # -Wvolatile (gcc 11, not in gcc 9)
            -Wvolatile-register-var
            -Wwrite-strings
            -Wzero-as-null-pointer-constant
            # -Wzero-length-bounds (gcc 12, not in 11)
        >
)

2
我写了一个小的cmake项目,其中包含了针对clang/gcc的发布版本的一些内容:https://github.com/goldsteinn/weverything - Noah
@Noah,太酷了,而且看起来你可以根据版本选择性地激活标志,这是我硬编码列表的一个很大限制。当切换到旧版本时,我必须注释掉标志。 - alfC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接