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

Meh*_*dad 177 c++ gcc warnings compiler-warnings gcc-warning

我想启用 - 字面上 - GCC 所有的警告.(你会觉得这很容易......)

  • 你认为-Wall可能会有所作为,但不是!仍然需要-Wextra.

  • 你认为-Wextra可能会有所作为,但不是!并非此处列出的所有警告(例如-Wshadow)都已启用.我仍然不知道这份清单是否全面.

我如何告诉海湾合作委员会启用(不是,不是,或者是!)所有警告?

Jon*_*ely 121

你不能.

GCC 4.4.0的手册仅适用于该版本,但它列出了4.4.0的所有可能警告.它们并不是您链接到的页面上的所有内容,例如,某些特定于语言的选项位于C++选项或Obj-C选项的页面上.要找到它们,您最好查看选项摘要

打开所有内容将包括-Wdouble-promotion哪些仅与具有32位单精度浮点单元的CPU相关,该单元float在硬件中实现,但double在软件中进行仿真.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提供了一个很好的无用警告示例-Waggregate-return,几乎可以肯定C++代码没有任何意义.

即你真的不想要所有警告,你只是认为你这样做.

阅读手册,阅读它们,确定您可能要启用哪些,尝试它们.无论如何,阅读编译器的手册是一件好事TM,采取捷径并启用你不理解的警告并不是一个好主意,特别是如果要避免使用RTFM.

任何只是打开一切的人都可能是这样做的,因为他们一无所知,或者一个尖头发的老板说"没有警告".

有些警告很重要,有些警告不重要.你必须要有辨别力,否则你就搞乱了你的计划.例如,考虑一下-Wdouble-promotion.如果你正在使用嵌入式系统,你可能会想要这个; 如果你正在使用桌面系统,你可能不会.你想要-Wtraditional吗?我对此表示怀疑.

编辑:另请参阅-Wall-all以启用作为WONTFIX关闭的所有警告.

编辑2:响应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))
Run Code Online (Sandbox Code Playgroud)

  • 我认为,`-Wethingthing`是比不提供这种选择的gcc策略更好的解决方案.我在clang中使用那个标志,因为我的理念是我希望默认启用所有警告(因为有人认为它有助于添加到编译器中),如果我不喜欢它,我会特别关闭该警告.关键是你不知道没有触发的警告,但你知道你不希望触发的警告,并且它们很容易被关闭. (57认同)
  • *"阅读手册,阅读它们,确定您可能要启用它们,尝试它们."*此处的问题是您遗漏的步骤:"重新访问每个编译器版本的手册并调整您的列表因为它们是一个变化的警告.让你的Makefile检查确切的编译器版本,并为每个版本使用*不同的警告列表." 我们有维护者维护的优化级别; 为什么他们不能为提供相同的警告服务而烦恼? (32认同)
  • @JonathanWakely:我有*我的*项目,GCC不在其中.我指出他们产品的弱点.无论是他们修复它还是他们接受了不这样做的责任,但是我不应该为他们修复它,OSS会被诅咒. (16认同)
  • @JonathanWakely是的,但他们很微不足道.查看警告*可能*与您的代码相关的最简单方法是查看*您的代码触发了哪些警告; 此时,您可以在决定是否禁用警告之前查看潜在危险代码的相关实际示例.这可以通过Clang的`-Weverything`选项轻松完成,但不可能与GCC一起使用. (15认同)
  • @JonathanWakely:*"如果你想要什么,请求它,不要讨厌它."* - 我没有义务参加GCC项目以批评它,特别是如果#31573被标记为WONTFIX已经.这使得这个主题从"询问它"变成了"关于它的婊子"球场. (12认同)
  • 除此之外,维护者已经__维持警告级别,他们决定`-Wall`应该是什么以及`-Wextra`应该是什么,所有缺少的是`-Wover9000`用于"其他所有".大多数新警告都包含在`-Wall`中,或者是对现有警告的更精细控制(例如,而不是`-Wunused`你可以使用`-Wunused-but-set-variable -Wunused-but-set-parameter`得到一些'-Wunused`警告.)你究竟在抱怨他们什么都不想做? (8认同)
  • 仅供参考,最后有一种方法可以找出给定版本GCC的完整警告列表:https://github.com/barro/compiler-warnings (8认同)
  • @DevSolar,AFAIK没有用户要求`-Wn`,它是由GCC开发人员(GCC)在GCC列表中讨论提出的(半).如果没有人要求,开发者如何知道人们想要它?我们的意思是通灵吗?搜索互联网上的每一篇文章,找出人们认为的弱点?如果你想要什么,请求它,不要惹恼它. (5认同)
  • 为了避免阅读本手册:我真的不相信没有很多,*很多*更有用的东西要阅读,例如Effective C++或几个有用的SO帖子,而且我的时间和记忆都有限.在某些时候,如果您的工具要求您吸收消防栓信息以便正确使用它们,那么他们就不再帮助您. (4认同)
  • 已经讨论过类似`-W1`,`-W2`的内容(例如,请参阅http://gcc.gnu.org/ml/gcc/2012-04/msg00087.html上的长线程),但他们"无法"不被打扰,因为没有人提出设计或完成工作,并且没有无限的开发人员资源来处理每个用户的宠物需求列表.这是开源的,你为什么不这样做?补丁欢迎. (3认同)
  • @DevSolar,我的回答已经解释了为什么`-Wall-all`并不是一个好主意.正如我说的两条评论,还有什么遗漏?批评是好的,但不要假设它是因为没有人可以为做某事而烦恼,特别是当你无法打开增强请求而不是抱怨时. (2认同)
  • @JonathanWakely:你的答案解释了为什么你和其他GCC维护者*认为*所有人都不是一个好主意.问题,功能请求你自己链接,我的评论解释了为什么其他人*做*认为这是一个好主意.我的评论还解释了为什么我不打算打开一个增强请求,该请求将与已经关闭的WONTFIX重复.就是这样. (2认同)
  • “任何只是打开一切的人都可能这样做是因为他们一无所知,因为或者一个尖头发的老板说“没有警告”。 grep/grep -v) 你可以找到一个你从未想过的相关警告,因为你自己不是 GCC/Clang 开发人员......我发现 C++ 代码问题甚至在 Stroustrup 自己的 C++ 文件中!例如,在他的 std_lib_facilities.h 上做一个 -Weverything 作为最简单的演示。 (2认同)

Haa*_*hii 63

我同意之前的答案,即启用字面上的所有警告可能没有益处,但是GCC确实提供了一种相当方便的方法来实现这一点.命令

gcc -Q --help=warning
Run Code Online (Sandbox Code Playgroud)

提供所有支持的警告选项的列表,以及有关它们是否处于活动状态的信息 顺便说一句,这可以用来找出(-Wall和)启用哪些选项(例如和)-Wextra

gcc -Wall -Wextra -Q --help=warning
Run Code Online (Sandbox Code Playgroud)

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

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

对于我目前的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 -Wignedated-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-missses- 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 -Wpadde d -Whatarentheses -Wpedantic -Wpmf-conversions -Wpointer-arith -Wpointer-sign -Wpointer-to-int-cast -Wpragmas -Wproperty-assign-default -Wprotocol -Wreal-q-constant -Wrealloc-lhs -Wallalloc-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 -Wditional -Wditional-conversion -Wtrampolines -Wtrigraphs -Wtype-limits -Wundeclared-selector -Wundef -Wunderflow -Wuninitialized -Wunknown-pragmas -Wuns afe-loop-optimizations -Wunnsixed-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-register-var -Wwrite-strings -Wzero-as-null-pointer-constant -Wzerotrip -frequire-return-statement

现在可以用它来调用GCC,即

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

但请注意,由于某些警告选项仅适用于某些语言(例如C++),因此会导致警告.通过使用一些更多的正则表达式来仅包括当前语言允许的选项可以避免这些.

  • @ValentinHeinitz正如我所说,我不认为启用字面上的所有警告是有益的,但这是OP所要求的.但是我认为通过明确删除其他答案中已经提到的一些有问题的警告(例如通过在调用结束时添加相应的-Wno-无论如何),这可以是实际使用的. (10认同)
  • 我担心这不实用.Gcc向我展示了std lib的警告. (5认同)
  • @ValentinHeinitz您可以通过对相关目录使用`-isystem`而不是`-I`来阻止gcc发出系统/标准/第三方标题的警告. (5认同)
  • 这应该是公认的答案,因为这实际上是直接回答问题。 (3认同)

Kon*_*ski 15

在启用所有警告的情况下编程是完全不可能的(除非您要忽略它们,但是,为什么要这么麻烦?).例如,假设您使用以下一组标志:-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) {
     ^
Run Code Online (Sandbox Code Playgroud)

您可能会认为"好吧,我将使用旧式原型".不,这不行.

/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)
     ^
Run Code Online (Sandbox Code Playgroud)

不,不指定任何原型也是错误的,因为编译器也会抱怨.

/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() {
     ^
Run Code Online (Sandbox Code Playgroud)

如果在程序中定义任何函数,则不能使用所有标志,因为编译器会抱怨任何可以想象的函数定义.

对于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
Run Code Online (Sandbox Code Playgroud)

  • 直到现在我都没有费心去检查这个,但实际上,这并非不可能......尝试`int main(int,char**); int main(argc,argv)int argc; char**argv; {(void)argc; (空隙)的argv; 返回0; }` (13认同)
  • 即使使用这个微不足道的程序,我仍然可以收到“警告:堆栈使用量为 16 字节 [-Wstack-usage=]”;-) (2认同)

Kyl*_*and 6

有人创建了一套工具来确定给定GCC或Clang版本的完整警告集.

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

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

存储库还包含文本文件,其中包含为大多数GCC和Clang版本生成的警告列表(目前为Clang 3.2至3.7和GCC 3.4至5.3).

https://github.com/barro/compiler-warnings


alf*_*lfC 6

编辑 2023:针对 GCC 12 进行了刷新。


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

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

gcc -Wall -Wextra -Wpedantic -Q --help=warning
Run Code Online (Sandbox Code Playgroud)

在所有这些警告中,有些警告不适用于 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)
        >
)
Run Code Online (Sandbox Code Playgroud)

  • 我写了一个小 cmake 项目,其中包含 clang/gcc 直至发布版本的内容:https://github.com/goldsteinn/weverything (2认同)

roc*_*oot 5

Gcc 4.3+现在有-Q --help =警告,你甚至可以指定--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 }'