how to turn on icc/icpc warnings?

前端 未结 2 1766
故里飘歌
故里飘歌 2021-02-06 00:26

I installed Intel Compiler composer_xe_2013_sp1.3.174 on Linux. I am confused about the icc warnings. Feed icc with a simple program main.c as below:

int main(in         


        
相关标签:
2条回答
  • 2021-02-06 00:52

    The Intel compiler doesn't really have good presets for warnings the way that gcc does (at least on 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. It also supports some gcc options like -Wall and -Wextra. However:

    • -Wall is very minimalistic compared to gcc, as you found
    • -w2 and -w3 enable some useful diagnostics, but also a lot of spam remarks
    • -diag-disable:remark removes that spam but also a lot of useful diagnostics

    In the end -w3 -diag-disable:remark is the best preset that icc has but it is still more minimalistic than 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.

    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. 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:

    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. There are differences between GCC's and ICC's implementation of the above warnings. I was also unable to find ICC options to match these GCC warnings:

    -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:

    -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. 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.

    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
    • 869: parameter "*" was never referenced
    • 981: operands are evaluated in unspecified order
    • 1418: external function definition with no prior declaration
    • 1572: floating-point equality and inequality comparisons are unreliable
    • 2259: non-pointer conversion may loose significant bits
    • 11074: Inlining inhibited by limit max-size (or max-total-size)
    • 11076: To get full report use -qopt-report=4 -qopt-report-phase ipo
    • 161: disable warning for unrecognized pragmas

    But I encourage you to start with them all on and pare down just the ones that are problematic for your code base.

    0 讨论(0)
  • 2021-02-06 01:06

    Generally speaking, the best compilation options for a small program your developing is -Wall -Wextra -std=c11 -pedantic

    Contrary to the warning switch's name, Wall does not actually activate all warnings; you use both Wall and Wextra to get the majority of the important warnings.

    -std switch sets the standard that the code uses; the most recent one is C11 therefore 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 requires the std switch and will emit warnings for any syntax, ect. 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 does not warn about the usage of non-standard libraries like conio.h)

    Now if you compile the program with Wall Wextra std=c11 pedantic, you should get 1 warnings:

    Warning: Line 4 - Suggest Parenthesis around truthy value (9 out of 10, this means you used = instead == in a comparison context).

    If you fix that warning, you'll receive another warning: Warning: Line 4 - Comparison between Signed and Unsigned Integer without a cast.

    Adding an explicit cast or changing b to a normal int will solve this warning.

    0 讨论(0)
提交回复
热议问题