public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonny Grant <jg@jguk.org>
To: Xi Ruoyao <xry111@mengyan1223.wang>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: gcc warn unreached else {}
Date: Thu, 9 Jul 2020 16:15:46 +0100	[thread overview]
Message-ID: <140d47ab-fe04-7e76-e59b-d0fddd75d2b0@jguk.org> (raw)
In-Reply-To: <6e02fef204a84b1015f8e1973f1236ff17721e4d.camel@mengyan1223.wang>



On 09/07/2020 16:00, Xi Ruoyao wrote:
> On 2020-07-09 10:45 +0100, Jonny Grant wrote:
>> Hello!
>>
>> There is an example below, (my code is not like this example below). I'm
>> reporting a possible issue, and asking if there is a way to detect it in code
>> bases. If it's a real issue I can file a bug report on Bugzilla.
>>
>> Can gcc warn where code will not get to the 'return 3;' below?
>>
>> Cheers, Jonny
>>
>>
>> int main(void)
>> {
>>     const int i = 1;
>>     if(1 == i)
>>     {
>>         return 1;
>>     }
>>     else if(1 != i)
>>     {
>>         return 2;
>>     }
>>     else
>>     {
>>         return 3;
>>     }
>> }
> 
> Generally finding all the branches in a program impossible to be executed is
> unsolvable.  Even in a program without any loop it's still NP-hard.
> 
> For some "simple" cases maybe we can implement some heuristics, or use a brute
> force approach (as a part of -fanalyzer).  But I doubt if these simple cases are
> really useful.

Thank you for your reply.
Yes, it would be useful to see more warnings output.

I see this situation every few weeks in code bases upon manual review. The simple case is where it is boolean as above, and is just values, and the else if() the inverse.

The compiler actually generated assembler that returns '1', so it did strip out the unused branches.

As two side questions:
-- Is there anyway to see which branches are removed?
-- Is there anyway to force the compile to keep all branches? (even those it believes will never be executed).

Cheers, Jonny

  reply	other threads:[~2020-07-09 15:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09  9:45 Jonny Grant
2020-07-09 15:00 ` Xi Ruoyao
2020-07-09 15:15   ` Jonny Grant [this message]
     [not found]     ` <e20bf113efcb7fe3e533ac694aa4bd62519b3654.camel@mengyan1223.wang>
2020-07-09 15:46       ` Xi Ruoyao
2020-07-09 15:46       ` Xi Ruoyao
     [not found]       ` <6d61e32f-3d69-9ac9-0a7e-88daa154c578@jguk.org>
2020-07-10  8:20         ` Xi Ruoyao
2020-07-10 21:50           ` Jonny Grant
2020-07-09 16:15 ` Martin Sebor
2020-07-16  0:20   ` Jonny Grant

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140d47ab-fe04-7e76-e59b-d0fddd75d2b0@jguk.org \
    --to=jg@jguk.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=xry111@mengyan1223.wang \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).