Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get gcc or clang to warn on explicit casts?

What I'm trying to do is find all explicit casts from type double or float to any other type in some source files I have. Is there a built-in gcc way to do this? Language is C. Thanks!

like image 793
Chris Avatar asked Apr 25 '12 15:04

Chris


People also ask

How do I enable warnings in GCC?

This warning is enabled by -Wall or -Wextra . Warn when a #pragma directive is encountered that is not understood by GCC. If this command-line option is used, warnings are even issued for unknown pragmas in system header files. This is not the case if the warnings are only enabled by the -Wall command-line option.

Which option of GCC inhibit all warning messages?

-Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error. Suppress warning messages emitted by #warning directives.

Which option can be used to display compiler warnings?

You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.

Which flags would you pass to your C++ compiler so it warns you about implicit conversions?

The C++ compiler comes with a lot of useful warnings that warn you about potential errors and issues in your code. You always want to compile with warnings. A warning flag starts with -W and then the name of the warning, for example -Wconversion which warns about implicit conversions.


1 Answers

If your C code can also be compiled in C++ mode, you can use g++'s -Wold-style-cast warning flag to trigger a warning on all such casts.

You can determine whether Clang has any warnings which will trigger for a particular coding pattern by using its -Weverything switch (but note that this is not useful for almost any other purpose -- clang has disabled-by-default warnings which trigger on various forms of legitimate code). However, in this case, clang does not have any warnings which trigger on such casts.

like image 180
Richard Smith Avatar answered Nov 07 '22 09:11

Richard Smith