This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Implicit Type Conversions Warning


I used to use the CodeWarrior compiler on Mac OS 9 and X and then switched
to the GCC compiler.  One set of warnings that I sorely miss is that of
implicit type conversions.  GCC does offer a ³-Wconversion² warning, but
this warns against type conversions between types in a function prototype
vs. types that would be used if the prototype is missing.  This is clearly a
warning for historical purposes before C++ required function prototypes (and
for C one can use ­Wstrict-prototypes, ­Wmissing-prototypes and
­Wmissing-declarations).  What I desire is a warning for implicit type
conversions between the passed parameter and the function prototype for that
parameter.  The current ³-Wconversion² implicit parameter types for missing
function prototypes warning is irrelevant if one requires function
prototypes (as C++ does or as one can catch from the ³C² warnings just
mentioned).

The ³-Wconversion² also warns against signed constant to unsigned variable
conversion (assignment).  This is incomplete.  What I desire is a warning
for implicit type conversions in an assignment and would apply to assignment
from a variable or expression, not just from a constant.

Though there are many ways one can categorize implicit type conversions, the
following are useful and would apply to both function arguments vs. function
prototype parameters and to assignments:

- Signed/Unsigned conversions.  These are from a signed type to an unsigned
type and vice versa.  Unspecified types, such as ³int², are treated by their
implied signed type (³char² is treated as signed or unsigned based on the
machine unless ­funsigned-char or ­fsigned-char is specified).

- Shortened conversions.  These are from a larger type to a shorter type.
For example, from ³long² to ³short² or from ³double² to ³float² or from
³float² to ³int².  The size of an ³int² is based on the machine.  Since
³signed/unsigned conversions² is a separate warning, this ³shortened
conversions² warning would not warn about signed/unsigned conversions that
did not go from a larger type to a shorter type.  So, ³short² to ³unsigned
short² or ³short² to ³unsigned long² would not generate a warning from
³shortened conversions² (but would for ³signed/unsigned conversions² if that
warning was enabled).

- Any conversion.  This would add lengthened and equivalent conversions to
those above.  For example, from ³short² to ³long² or from ³int² to ³long²
(even if ³int² is the size of a ³long² on the machine).  This category also
catches conversions from ³char² to ³signed char² or ³unsigned char² or vice
versa since ³char² is machine-dependent.  However, ³int² and ³signed int²,
etc., are not considered to have any implicit conversion possibility since
the language definition requires these to be exactly equivalent (so no
warning is generated when missing a ³signed² except for the ³char² case).

The primary way to avoid these warnings is to use typecasts, when necessary,
for parameters in function calls and for expressions or constants in
assignments.  This forces the programmer to explicitly declare that they
know what they are doing ? that type conversions are occurring and are
intended.  With proper planning, most typecasts are avoided as parameters
and variables are designed with consistent types initially, but when
mismatches occur, they are at least made explicitly clear.  These warnings
have caught many potential problems in the past.

I didn¹t see this sort of enhancement request mentioned in the archives so
that¹s why I¹m adding this now.  Does anyone know why implicit type
conversion warnings aren¹t in GCC already?  Since CodeWarrior had them, I
would assume that they aren¹t that difficult to implement.

Thanks,
Richard Falk



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]