简体   繁体   English

如何打开 icc/icpc 警告?

[英]how to turn on icc/icpc warnings?

I installed Intel Compiler composer_xe_2013_sp1.3.174 on Linux.我在 Linux 上安装了 Intel Compiler composer_xe_2013_sp1.3.174。 I am confused about the icc warnings.我对ICC警告感到困惑。 Feed icc with a simple program main.c as below:用一个简单的程序 main.c 喂 icc,如下所示:

int main(int argc, char **argv) {
  int a = 1;
  unsigned int b = -22;
  if (b = a) {

  }
}

I compiled the file with the command: icc -Wall main.c .我使用以下命令编译了文件: icc -Wall main.c Surprisingly, the command works silently without any warnings.令人惊讶的是,该命令无提示运行,没有任何警告。 Do I have to turn on the warnings switch on icc?我必须打开icc上的警告开关吗? Thanks谢谢

The Intel compiler doesn't really have good presets for warnings the way that gcc does (at least on Linux).英特尔编译器并没有像 gcc 那样有很好的警告预设(至少在 Linux 上)。 The main warning option is -wn where n can be 0 to 5. The default is 1, and 4 & 5 have no meaning on Linux.主要的警告选项是-wn ,其中 n 可以是 0 到 5。默认值为 1,4 和 5 在 Linux 上没有意义。 It also supports some gcc options like -Wall and -Wextra .它还支持一些 gcc 选项,例如-Wall-Wextra However:然而:

  • -Wall is very minimalistic compared to gcc, as you found -Wall与 gcc 相比非常简约,正如您所发现的
  • -w2 and -w3 enable some useful diagnostics, but also a lot of spam remarks -w2-w3启用一些有用的诊断,但也有很多垃圾评论
  • -diag-disable:remark removes that spam but also a lot of useful diagnostics -diag-disable:remark删除垃圾邮件,但也有很多有用的诊断

In the end -w3 -diag-disable:remark is the best preset that icc has but it is still more minimalistic than gcc -Wall .最后-w3 -diag-disable:remark是 icc 最好的预设,但它仍然比gcc -Wall更简约。 Instead you need to either start with a minimal set of warnings and build up your own, or start with maximum diagnostics and disable any that get annoying using -wd .相反,您需要从最少的警告集开始并建立自己的警告,或者从最大的诊断开始并使用-wd禁用任何令人讨厌的警告。

Build Up积聚

The main downside to the first approach is that Intel doesn't really document most of its warnings, so it is hard to know what is available to enable.第一种方法的主要缺点是英特尔并未真正记录其大部分警告,因此很难知道可以启用什么。 However, it does support many of the GCC command line flags, so the GCC documentation is a good place to start.但是,它确实支持许多 GCC 命令行标志,因此 GCC 文档是一个很好的起点。 For example, here are settings that are relatively close to g++ -Wall , which is convenient if you want to develop with one and have a good chance of a clean build with the other:例如,这里有一些比较接近g++ -Wall的设置,如果你想用一个开发并且很有可能用另一个干净构建,这很方便:

icpc -Wall -Warray-bounds -Wchar-subscripts -Wcomment -Wenum-compare -Wformat -Wuninitialized -Wmaybe-uninitialized -Wmain -Wnarrowing -Wnonnull -Wparentheses -Wpointer-sign -Wreorder -Wreturn-type -Wsign-compare -Wsequence-point -Wtrigraphs -Wunused-function -Wunused-but-set-variable -Wunused-variable -Wwrite-strings

This isn't an exact match for gcc -Wall .这与gcc -Wall不完全匹配。 There are differences between GCC's and ICC's implementation of the above warnings. GCC 和 ICC 对上述警告的实施存在差异。 I was also unable to find ICC options to match these GCC warnings:我也找不到匹配这些 GCC 警告的 ICC 选项:

-Wformat-contains-nul
-Wunused-label
-Wstrict-overflow
-Wvolatile-register-var

And I intentionally left these out, because the ICC version was much more spammy than GCC:我故意忽略了这些,因为 ICC 版本比 GCC 更垃圾:

-Wstrict-aliasing   So broad that any use of polymophism will cause this warning
-Wswitch            Requires a default even if you have cases for all enumeration values

Trim Down裁减

If GCC parity isn't a concern then the easiest way to learn what warnings ICC has is to enable them all, and then decide whether you like them or not.如果 GCC 奇偶校验不是问题,那么了解 ICC 警告的最简单方法是启用它们,然后决定您是否喜欢它们。 If you don't like a warning, you can disable it using it's diagnostics number, which often has more granularity that GCC's options do.如果您不喜欢警告,您可以使用它的诊断号禁用它,它通常比 GCC 的选项更精细。

icpc -w3 -wd1418,2259

Here are some diagnostics that I have seen disabled in the past:以下是我过去曾看到禁用的一些诊断:

  • 383: value copied to temporary, reference to temporary used 383:值复制到临时,引用临时使用
  • 869: parameter "*" was never referenced 869:参数“*”从未被引用
  • 981: operands are evaluated in unspecified order 981:以未指定的顺序评估操作数
  • 1418: external function definition with no prior declaration 1418:没有事先声明的外部函数定义
  • 1572: floating-point equality and inequality comparisons are unreliable 1572:浮点等式和不等式比较不可靠
  • 2259: non-pointer conversion may loose significant bits 2259:非指针转换可能会丢失有效位
  • 11074: Inlining inhibited by limit max-size (or max-total-size) 11074:限制最大大小(或最大总大小)禁止内联
  • 11076: To get full report use -qopt-report=4 -qopt-report-phase ipo 11076:要获得完整报告,请使用 -qopt-report=4 -qopt-report-phase ipo
  • 161: disable warning for unrecognized pragmas 161:禁用无法识别的编译指示的警告

But I encourage you to start with them all on and pare down just the ones that are problematic for your code base.但我鼓励您从所有这些开始,并减少对您的代码库有问题的那些。

Generally speaking, the best compilation options for a small program your developing is -Wall -Wextra -std=c11 -pedantic一般来说,您开发的小程序的最佳编译选项是 -Wall -Wextra -std=c11 -pedantic

Contrary to the warning switch's name, Wall does not actually activate all warnings;与警告开关的名称相反,Wall 实际上并没有激活所有警告; you use both Wall and Wextra to get the majority of the important warnings.您同时使用 Wall 和 Wextra 来获取大部分重要警告。

-std switch sets the standard that the code uses; -std 开关设置代码使用的标准; the most recent one is C11 therefore std=c11.最新的是 C11,因此 std=c11。 Pedantic is a way to signal to the compiler that you want to write a program that doesn't use compiler-specific extensions. Pedantic 是一种向编译器发出信号的方法,即您要编写一个不使用编译器特定扩展的程序。 Pedantic requires the std switch and will emit warnings for any syntax, ect. Pedantic 需要 std 开关,并且会针对任何语法等发出警告。 that does not conform to the standard specified by std.不符合标准规定的标准。 Finally, if you want errors instead of warnings for usage of compiler-extension, use -pedantic-errors instead.*最后,如果您希望使用编译器扩展时出现错误而不是警告,请改用 -pedantic-errors。*

(* - pedantic does not warn about the usage of non-standard libraries like conio.h) (* - pedantic 不会对使用非标准库(如 conio.h)发出警告)

Now if you compile the program with Wall Wextra std=c11 pedantic, you should get 1 warnings:现在,如果您使用 Wall Wextra std=c11 pedantic 编译程序,您应该会收到 1 个警告:

Warning: Line 4 - Suggest Parenthesis around truthy value (9 out of 10, this means you used = instead == in a comparison context).警告:第 4 行 - 建议围绕真值使用括号(十分之九,这意味着您在比较上下文中使用 = 而不是 ==)。

If you fix that warning, you'll receive another warning: Warning: Line 4 - Comparison between Signed and Unsigned Integer without a cast.如果您修复该警告,您将收到另一个警告:警告:第 4 行 - 有符号和无符号整数之间的比较,无需强制转换。

Adding an explicit cast or changing b to a normal int will solve this warning.添加显式强制转换或将 b 更改为普通 int 将解决此警告。

These days I am pretty happy using this target_options in CMake with icpc 2021.6.0 20220226 , I collected them from several sources.这些天我很高兴在 CMake 中使用这个target_optionsicpc 2021.6.0 20220226 ,我从几个来源收集它们。 Any additions are welcome.欢迎任何补充。

        target_compile_options(
            ${TEST_EXE}
            PRIVATE
                $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:
                    ...
                >
                $<$<AND:$<CXX_COMPILER_ID:GNU>,$<NOT:$<CUDA_COMPILER_ID:NVIDIA>>,$<NOT:$<CUDA_COMPILER_ID:Clang>>>:
                    ...
                >
                $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CUDA_COMPILER_ID:Clang>>:
                    ...
                >
                $<$<CXX_COMPILER_ID:Intel>:  # also IntelLLVM, XL (ibm), XLClang (ibm)
                    -Werror
                    -Wall
                    -Wextra
                    -diag-disable=remark
                    -diag-error:3846
                    -diag-disable=1011  # disables warning missing return at the end of non-void function
                    -wd161
                    -Wabi
                    -Warray-bounds
                    -Wcast-qual
                    -Wchar-subscripts
                    -Wcomment
                    -Wdeprecated
                    -Wenum-compare
                    -Wextra-tokens
                    -Wformat
                    -Wformat=2
                    -Wformat-security
                    -Wic-pointer
                    -Wnarrowing
                    -Wno-return-type
                    -Wnon-virtual-dtor
                    -Wnonnull
                    -Wmaybe-uninitialized
                    -Wmain
                    -Wmissing-declarations
                    -Wmissing-prototypes
                    -Wmultichar
                    -Woverloaded-virtual
                    -Woverflow
                    -Wparentheses
                    -Wpointer-arith
                    -Wpointer-sign
                    -Wreorder
                    -Wreturn-type
                    -Wsequence-point
                    -Wshadow
                    -Wsign-compare
                    -Wshorten-64-to-32
                    -Wno-strict-aliasing
                    -Wstrict-prototypes
                    -Wtrigraphs
                    -Wtype-limits
                    -Wuninitialized
                    -Wunused
                    -Wunused-but-set-variable
                    -Wunused-function
                    -Wunused-parameter
                    -Wunused-variable
                    -Wwrite-strings
                >
                $<$<OR:$<CXX_COMPILER_ID:PGI>,$<CXX_COMPILER_ID:NVHPC>>:
                ...
                >
                $<$<CXX_COMPILER_ID:MSVC>:
                ...
                >
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM