4
void foo(uint32_t* p);

...
float32_t* x;
foo( (uint32_t*)x );

A static code analysis tool reports a “casting from float* to integer*" problem.

How do I force GCC compiler to report the same warning?

Compiler optins are: -pedantic -Wall -Wextra -Wconversion

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • No, `-Wall`doesn't do it... added this info to question, thanks. – Danijel Oct 09 '19 at 11:59
  • I don't think there is a compiler option to save the programmer from using wild nonsense casts. – Lundin Oct 09 '19 at 12:02
  • 1
    Why should you get a warning for doing an explicit cast? With the explicit cast, you tell the compiler that you *want* your type to change. There's no need to issue a warning when the user makes it clear that this is his intent. Leave the cast away and you will get a warning (`Wincompatible-pointer-types`). – Blaze Oct 09 '19 at 12:02
  • Part of the purpose of an explicit type conversion (aka a "cast") is to force the compiler to permit a conversion that would not normally be permitted, and not issue a diagnostic. Practically, few compilers even have an option to issue a warning in such a case, since programmers often use the cast to stop the compiler complaining. If you want a problem to be reported, continue using your static analysis tool as well as the compiler. – Peter Oct 09 '19 at 12:04
  • 2
    Related, if not an actual dupe: https://stackoverflow.com/questions/21214875/gcc-accuracy-of-strict-aliasing-warnings – Andrew Henle Oct 09 '19 at 12:04
  • 2
    @Blaze Can I get an explicit cast from the automated ATM machine, if I put in my personal PIN number? ;) – Lightness Races in Orbit Oct 09 '19 at 12:06
  • @LightnessRacesinOrbit I'm not sure, I hope there's a public PSA announcement on this topic soon! – Blaze Oct 09 '19 at 12:08
  • @Blaze I look forward to it! Ideally we'll get an estimated ETA in short order – Lightness Races in Orbit Oct 09 '19 at 12:10
  • There does not seem to be an option rto enable such a warning. There is an option to warn about casting a pointer to type1 to a pointer to type2 where type2 has greater required alignment than type1 (`-Wcast-align`) but that probably doesn't apply in this case since `uint32_t` and `float` usually have identical required alignment. – Ian Abbott Oct 09 '19 at 12:10

2 Answers2

4

By using an explicit conversion (a "cast"), you have told the compiler: "I definitely want to do this". That's what a cast means. It makes little sense for the compiler to then warn about it.

Instead, rely on implicit conversions, and if you try to use one that is not valid, the compilation of your program will fail until you fix it.

Now, that only helps you while you're writing your program. It doesn't help you statically analyse poorly-written code that already exists, for the purpose of improving the code. Well, that's okay! That's what your, er, static analyser is for, as you've already discovered.

All that being said, if you have strict aliasing enabled, the compiler will perform a little static analysis of its own and may kick out a warning here, if there's a risk that your program won't work as you wanted. But strict aliasing is often disabled in GCC builds and, even when it's not, detecting aliasing violations is complicated and cannot be guaranteed.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks. I don't have this static analysis tool on my machine, it's a repository-wide nightly test. Not sure how to go about fixing this. `-Wstrict-aliasing=1`doesn't help. – Danijel Oct 09 '19 at 12:13
  • 1
    I'm not sure I understand the problem. Your nightly static analysis run has identified the problem. Now you can fix the problem by rewriting the code. What sort of "help" are you after? – Lightness Races in Orbit Oct 09 '19 at 12:16
  • @Danijel Note that `-Wstrict-aliasing` alone won't do anything, it requires that `-fstrict-aliasing` is enabled too. But this may break any code that uses non-standard aliasing tricks. – user694733 Oct 09 '19 at 12:49
  • The strict aliasing options of gcc aren't very reliable and are prone to give incorrect diagnostics. – Lundin Oct 09 '19 at 12:51
  • @user694733 Thanks, I've used `-fstrict-aliasing`. – Danijel Oct 09 '19 at 13:13
  • 1
    @Danijel Note that this doesn't just turn warnings on. You should make yourself fully aware of the ramifications of turning on this mode. In particular, I'd say that _any_ example of casts like you've used in the question should now be replaced with `memcpy` or similar; there may be more subtle examples too. – Lightness Races in Orbit Oct 09 '19 at 13:39
0

I doubt you will find the compiler option you're looking for. (In my opinion, the compiler option you're looking for should not exist.)

The usual question is:

I'm doing something weird. I know it's okay, but the compiler keeps warning about it. How do I disable the warning?"

Now, if the "something weird" you're doing is mixing and matching incompatible pointers in potentially unsafe ways, the way to disable the warning is often: use an explicit cast.

So what you're asking, in effect, is a way to re-enable a warning which you (or someone) has already disabled by using an explicit cast, a cast which you now believe is wrong, after all.

We're on the cusp of an infinite regress here. If there were a way to warn about questionable conversions requested by explicit casts, next week we'd be getting this question:

I'm using a special conversion between nominally-incompatible pointers. I know it's okay, but the compiler keeps warning about it, even though I'm using an explicit cast. How do I disable the warning? Do I need to use two casts?"

It's kind of like warning messages. Suppose you try to delete a vital system file. Suppose your computer pops up a warning dialog saying "About to delete vital file. Confirm?" Suppose you decide you don't want to delete it, but you accidentally click the "OK" button, and the file goes away. Do you find yourself wishing that the warning dialog had been followed by a second dialog asking, "Are you really sure?" You might, but most people will probably agree that having to click "OK" twice whenever they delete a file would be an unacceptable nuisance...

Steve Summit
  • 45,437
  • 7
  • 70
  • 103