2.3.10. Controlling warning messages

The compiler issues warnings about potential portability problems and other hazards. The compiler options enable you to turn off specific warnings. For example, you can turn off warnings if you are in the early stages of porting a program written in old-style C. In general, it is better to check the code than to switch off warnings.

The options are on by default, unless specified otherwise.

See also Specifying additional checks for descriptions of additional warning messages.

The general form of the -W compiler option is:


-W[options][+][options]

where the options field contains zero or more characters.

If the + character is included in the characters following the -W, the warnings corresponding to any following letters are enabled rather than suppressed.

You can specify several options at the same time. For example:


-Wad+fg

turns off the warning messages specified by a and d, and turns on the warning messages specified by f and g.

The warning message options are as follows:

-W

This option suppresses all warnings. If one or more letters follow the option, only the warnings controlled by those letters are suppressed.

-Wa

This option suppresses the warning:

C2961W: Use of the assignment operator in a condition context

This warning is normally given when the compiler finds a statement such as:

if (a = b) {...

where it is possible that one of the following is intended:

if ((a = b) != 0) {... 
if (a == b) {...
-Wb

This option suppresses the warning messages that are issued for extensions to the ANSI standard. Examples include:

  • using an unwidened type in an ANSI C assignment

  • specifying bitfields with a type of char, short, long, or long long

  • specifying char, short, float, or enum arguments to variadic functions such as va_start().

-Wd

This option suppresses the warning message:

C2215W: Deprecated declaration foo() - give arg types

This warning is normally given when a declaration without argument types is encountered in ANSI C mode.

In ANSI C, declarations like this are deprecated. However, it is sometimes useful to suppress this warning when porting old code.

In C++, void foo(); means void foo(void); and no warning is generated.

-We

This option suppresses the warning messages given when using an extended initializer (see C language extensions) that other C compilers are not required by the standard to accept.

-Wf

This option suppresses the message:

Inventing extern int foo()

This is an error in C++ and cannot be suppressed. It is a warning in ANSI C and suppressing this message can be useful when compiling old-style C in ANSI C mode.

-Wg

This option suppresses the warning given when an unguarded header file is #included.


C2819W: Header file not guarded against multiple inclusion

This warning is off by default. It can be enabled with -W+g. An unguarded header file is a header file not wrapped in a declaration such as:

#ifndef foo_h
#define foo_h
/* body of include file */
#endif
-Wi

This option suppresses the implicit constructor warning (C++ only).


C2887W: implicit constructor ’struct X’()

It is issued when the code requires a constructor to be invoked implicitly. For example:

struct X { X(int); };
X x = 10;				// actually means, X x = X(10);
				// See the Annotated C++ 
				// Reference Manual p.272

This warning is switched off by default. It can be enabled with -W+i.

-Wk

This option turns off the warning:


C2621W: double constant automatically converted to float

These warnings are given when the default type of unqualified floating-point constants is changed by the -auto_float_constants option. This warning is switched on by default.

-Wl

This option turns off the warning:

C2951W: lower precision in wider context

when code like the following is found:

long x; int y, z; x = y*z

where the multiplication yields an int result that is then widened to long. This warning indicates a potential problem when either the destination is long long or where the code has been ported to a system that uses 16-bit integers or 64-bit longs. This option is off by default. It can be enabled with -W+l.

-Wm

This option suppresses warnings about multiple-character char constants.

-Wn

This option suppresses the warning message:

C2921W: implicit narrowing cast

This warning is issued when the compiler detects the implicit narrowing of a long expression in an int or char context, or detects the implicit narrowing of a floating-point expression in an integer or narrower floating-point context.

Such implicit narrowing casts are almost always a source of problems when moving code that has been developed on a 32-bit system to a system where int occupies 16 bits and long occupies 32 bits. This option is off by default.

-Wo

This option suppresses warnings for implicit conversion to signed long long constants.

-Wp

This option suppresses the warning message:

C2812W: Non-ANSI #include <…>

The ANSI C standard requires that you use #include <…> for ANSI C headers only. However, it is useful to disable this warning when compiling code not conforming to this aspect of the standard. This warning is suppressed by default unless you specify the -strict option.

-Wq

This option suppresses warnings in C++ constructor initialization order.

-Wr

This option suppresses the implicit virtual warning (C++ only) issued when a non-virtual member function of a derived class hides a virtual member of a parent class. For example:

struct Base { virtual void f(); };
struct Derived : Base { void f(); };	

generates the following warning:


C2997W: 'Derived::f()' inherits implicit virtual from 'Base::f()'

Adding the virtual keyword in the derived class prevents the warning.

-Ws

This option suppresses warnings generated when the compiler inserts padding in a struct. For example:


C2221W: padding inserted in struct 's'

This warning is off by default. It can be enabled with -W+s.

-Wt

This option suppresses the unused this warning. This warning is issued when the implicit this argument is not used in a non-static member function. It is applicable to C++ only. The warning can also be avoided by making the member function a static member function. The default is off. For example:

struct T {
        int f() { return 42; }
};

results in the following warning:


C2924W: 'this' unused in non-static member function

To avoid the warning, use static int f() ...

-Wu

For C code, -Wu suppresses warnings about future compatibility with C++. Warnings are suppressed by default. You can enable them with -W+u. For example:

int *new(void *p) { return p; }

results in the following warnings:


C2204W: C++ keyword used as identifier: 'new'
C2920W: implicit cast from (void *), C++ forbids

-Wv

This option suppresses warning messages of the type:

C2218W: implicit 'int' return type for 'f' - 'void' intended?

This is usually caused by a return from a function that was assumed to return int, because no other type was specified, but is being used as a void function. This is widespread in old-style C. Such action always results in an error in C++.

-Wx

This option suppresses unused declaration warnings such as:

C2870W: variable 'y' declared but not used

By default, unused declaration warnings are given for:

  • local (within a function) declarations of variables, typedefs, and functions

  • labels (always within a function)

  • top-level static functions and static variables.

-Wy

This option turns off warnings about deprecated features.

Copyright © 1999-2001 ARM Limited. All rights reserved.ARM DUI 0067D
Non-Confidential