how to turn on icc/icpc warnings?
Asked Answered
E

3

11

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(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. Surprisingly, the command works silently without any warnings. Do I have to turn on the warnings switch on icc? Thanks

Eckmann answered 7/12, 2014 at 13:56 Comment(8)
b = a might b == a ?Hayfork
@Hayfork You should read the question before posting ...Boulware
But right as an expression.Hayfork
@Hayfork I wrote it in purpose of testing the warningEckmann
When you add -Wcheck do you see the emitted warning regarding b=a assignment within the if statement ?Farleigh
Warning is not believed necessary. gcc and clang is kind, but I seem to superfluous. E.g warning: suggest parentheses around assignment used as truth value Parentheses totally unnecessary.Hayfork
ICC contains lot of GCC compatibility, so have you tried some of the warning options for GCC, like e.g. -Wextra?Memphis
It is shocking that -Wall -Werror -Wextra do not work at all with icc.Frimaire
H
14

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.

Heffner answered 22/3, 2016 at 22:33 Comment(1)
Just to be clear, it doesn't even implement all of the warnings GCC does for the version of GCC it claims to support. For example, GCC has supported -Wvariadic-macros since 4.0, but if you write #pragma GCC diagnostic ignored "-Wvariadic-macros" (even inside of an #if __GNUC__ >= 4) ICC will emit a diagnostic (#2282: unrecognized GCC pragma) at -w3.Wherewithal
L
1

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.

Leafstalk answered 3/1, 2015 at 1:39 Comment(3)
Which version of icc are you using? None of them that I've tested list -Wextra or -pedantic in their options, and seem to generate the same warnings with or without those flags.Heffner
This answer is applicable to gcc; not icc.Elemi
@pavon: -Wall -Wextra works on ICC as early as ICC13. Godbolt doesn't have any earlier versions. So does -pedantic. -Wextra does enable the extra warnings for uninitialized objects, for example. Return statement does not get executed in c. Google didn't find official docs for ICC warning options, but it's not surprising that ICC supports gcc options for compatibility even if it doesn't document them.Paediatrics
C
0

These days I am pretty happy using this target_options in CMake with icpc 2021.6.0 20220226, I collected them from several sources. 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>:
                ...
                >
        )
Chryso answered 24/6, 2022 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.