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!
-
2Maybe I'm just being simple, but couldn't you just search for `(double)` or `(float)` in your source? – Robert Harvey Apr 25 '12 at 15:27
-
@RobertHarvey Sorry, I reworded it to make more sense. I want to catch all explicit casts that are from type float/double to anything else, so I can search for (int), etc.. but I wouldn't be able to tell if they are float or double. – Chris Apr 25 '12 at 15:29
-
2Just a thought (I don't know C++ or if it is even possible): override the cast operator for double and float to emit a compilation failure and compile as C++ :) – pmg Apr 25 '12 at 15:33
-
@pmg That sounds interesting, but I have very little knowledge in how to get started doing that. – Chris Apr 25 '12 at 15:37
-
1@user1071861: I asked if it's possible: http://stackoverflow.com/questions/10320282/are-casts-overridable-operations-if-so-how – pmg Apr 25 '12 at 16:55
-
@pmg - ...and learned that it isn't. – Jirka Hanika Apr 25 '12 at 17:04
5 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.

- 13,696
- 56
- 78
-Wconversion
warn for implicit conversions that may alter a value (double
are large types) , and -Wno-sign-conversion
disable warnings about conversions between signed and unsigned integers (so there would be less unnecessary warnings). Else I don't see any standard alternative...
Worst case you can looking for those keywords directly into your source files...

- 23,373
- 3
- 44
- 93
-
1From what I understand, explicit conversions won't be caught by -Wconversion, in fact they are a way to get rid of the warnings. I did a couple tests and found this to be true as well so I need to find some other way to do so. – Chris Apr 25 '12 at 15:31
-
Unluckily, they are shown (at least were when I tried a couple of months back). I had enabled `-Wconversion` to catch _accidential_ conversions, and the amount of noise generated from explicit (and intended) conversions was unbearable. – Damon Apr 25 '12 at 16:10
Since casts are explicitly legal, and the right way to perform weird conversions, it's highly unlikely that gcc would contain an option to warn about them
Instead, depending on how huge your source is, you might be able to get away with:
grep '\(double|float\) ' *
to give you all of the double or float variables. Since c isn't a regular language, it's not trivial (with shell tools) to parse this into a list of double or float variables, but if your source is small enough, doing this by hand is easy.
grep '([^()]*)[ ()]*\(your list of variable names\)' *
From there would show you many of your casts.

- 10,964
- 3
- 32
- 54
While the compilers I know don't have options for that, Gimpel's FlexeLint can do what you want:
$ cat tst.c
int main (void)
{
int i = 0, j = 0;
float f = 0.0;
double d = 0.0;
i = (int) f;
j = (int) d;
d = (double) f;
f = (float) d;
i = (int)j;
j = (unsigned) i;
return (int) j;
}
$ flexelint -w1 +e922 tst.c
FlexeLint for C/C++ (Unix) Vers. 9.00j, Copyright Gimpel Software 1985-2012
--- Module: tst.c (C)
_
i = (int) f;
tst.c 7 Note 922: cast from float to int
_
j = (int) d;
tst.c 8 Note 922: cast from double to int
_
d = (double) f;
tst.c 9 Note 922: cast from float to double
_
f = (float) d;
tst.c 10 Note 922: cast from double to float
shell returned 4

- 69,818
- 15
- 125
- 179
Well I think there is no such option. After all, a warning is issued by the compiler in order to warn you from things you might have done unintentionally. An explicit cast, however, is basically a way to tell your compiler "shut up, I know what I am doing".

- 7,072
- 2
- 26
- 22
-
It's not to float or doubles that I'm worried about, it's FROM float and doubles to anything else. I created a list for all casts, but I need to extract the variable names from that list that are only floats or doubles, which I'm finding very hard to do. – Chris Apr 25 '12 at 15:49
-
Which type of cast is problematic here at all? If they are problematic, why did they end up in the code the first place? – cli_hlt Apr 25 '12 at 15:53
-
2@cli_hlt: There exists bad code, you know, which needs to be fixed to the better :) – Niklas B. Apr 25 '12 at 16:04