Is there a way to get gcc or clang to warn about explicit translations?

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

+6
source share
5 answers

Since throws are clearly legal and a surefire way to perform strange transformations, it is unlikely that gcc would contain an option to warn them

Instead, depending on how huge your source is, you can get away with:

grep '\(double|float\) ' * 

to provide you with all double or floating variables. Since c is not a common language, it is not trivial (with shell tools) to parse it into a list of double or floating variables, but if your source is small enough, doing it manually is easy.

 grep '([^()]*)[ ()]*\(your list of variable names\)' * 

From there you will see many of your prizes.

+2
source

If your C code can also be compiled in C ++ mode, you can use the g ++ -Wold-style-cast flag to trigger a warning about all such garbage.

You can determine if the Clan has any warnings that will be triggered for a specific encoding template using its -Weverything switch (but note that this is not useful for almost any other purpose - clang has default shutdown warnings, which trigger on various forms of legal code). However, in this case, clang has no warnings that cause such drops.

+6
source

-Wconversion warn about implicit conversions that can change the value ( double are large types), and -Wno-sign-conversion disable -Wno-sign-conversion warnings between unsigned integers (so there will be less unnecessary warnings). Otherwise, I do not see any standard alternative ...

In the worst case scenario, you can search for these keywords directly in your source files ...

+2
source

Well, I think there is no such option. In the end, the compiler issues a warning to warn you of what you might have done unintentionally. An explicit cast, however, is basically a way of telling your compiler to "shut up, I know what I'm doing."

0
source

While the compilers I know don't have options for this, Gimpel 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 
0
source

Source: https://habr.com/ru/post/914142/


All Articles