NO

Author Topic: wrong Unreachable code warning  (Read 3623 times)

jullien

  • Guest
wrong Unreachable code warning
« on: August 13, 2014, 06:02:34 PM »
If I have a header file (simplified sample) that contains:

inline   static void *
ollistitem(char *fname, int item, void *data)
{
   if (fname != 0 && item)
      return data;
   else   return 0;
}


Which is used in different source files with item passed as constant, compiler complains that code is unreachable because part of function is never used in one translation unit (but may be used in another translation unit).

Here is an example:

#include <stdio.h>
#include "foo.h"

void* olfirst(void* data);

void*
olfirst(void* data)
{
   return( ollistitem( "foo", 0, data) );
}



In this translation unit, index is passed as 0 which results to "else return 0;" which can be deduced at compile time.
Another translation unit may use the same inline function with a different index value resulting to true condition.

This annoying because using -W0 may hide other precious warnings.

Christian

neo313

  • Guest
Re: wrong Unreachable code warning
« Reply #1 on: August 13, 2014, 06:29:53 PM »
Find one more and you get a free bug. ;)

Use pragma warn to disable and/or enable certain warnings.


Code: [Select]
#pragma warn( disable: 2154 )

void* olfirst(void* data)
{
return( ollistitem( "foo", 0, data) );
}

#pragma warn( enable: 2154 )

jullien

  • Guest
Re: wrong Unreachable code warning
« Reply #2 on: August 13, 2014, 09:05:48 PM »
For sure I can disable this warning.
However, as -W1 is the default, I expect with default setting to get warnings only about suspicious code.
Here my inline function does not contain suspicious code and, depending on argument values all lines can be executed.
It is only because this function is inline that compiler sees the only call in a translation unit with constant value has some dead code.

To me, this warning should appear with -W2 or above but not with default -W1.


WDYT?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: wrong Unreachable code warning
« Reply #3 on: August 13, 2014, 10:01:42 PM »
Jullien,
your function is not only inlined, but also static so not intended for use in a different translation unit.
Do you get the same warning removing the static qualifier?
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

jullien

  • Guest
Re: wrong Unreachable code warning
« Reply #4 on: August 14, 2014, 07:54:36 AM »
Yes same issue if I remove static.

(My first name is Christian)

Here is another sample which is even more confusing. Both calls use a constant that result to one branch or the other.
Warning is only displayed with constant 0, not with constant 1.

foo.c(11): warning #2154: Unreachable code.
foo.c(23): warning #2154: Unreachable code.

Code: [Select]
#include <stdio.h>

inline void *
f(int item, void *data)
{
if (item)
return data;
else return 0;
}

void*
call0(void* data)
{
return f(0, data);
}

void*
call1(void* data)
{
return f(1, data);
}

void*
call0_alt(void* data)
{
return f(0, data);
}

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: wrong Unreachable code warning
« Reply #5 on: August 14, 2014, 02:21:08 PM »
From what I see seems that the compiler first inline the code, then, in the optimizing phase, forgetting what was the source of that code, emits the warning as if it was in the original function body.
It is a bug, but IMHO sopportable for now ...  ;)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

jullien

  • Guest
Re: wrong Unreachable code warning
« Reply #6 on: August 14, 2014, 03:30:14 PM »
I agree that it is not a blocker bug.
I'm very strict with my own code which generally compiles without complaining with all warnings turned on (-Wall + many -Wxxx on gcc, -W4 -WX -analysis on Windows, and most splint flags - splint is a very nice tool if you want a really strict coding style).

With Pelles C, I do the opposite, I use -W0 and that's it.
Not a big issue for me, I just try to help to improve Pelles C which is one of my uncounted compilers I use to qualify my code.

Christian

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: wrong Unreachable code warning
« Reply #7 on: August 14, 2014, 05:09:01 PM »
Not a big issue for me, I just try to help to improve Pelles C which is one of my uncounted compilers I use to qualify my code.

Christian
That's good  Christian :D
This is what Pelle requires in change of the compiler...  ;)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide