Linux Perf Users
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ian Rogers <irogers@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1] perf build: Fix build for clang's -Wunreachable-code
Date: Sat, 31 May 2025 19:22:26 +0100	[thread overview]
Message-ID: <20250531192226.6af9bbde@pumpkin> (raw)
In-Reply-To: <aDoyPbQ38vZTCaNn@google.com>

On Fri, 30 May 2025 15:33:33 -0700
Namhyung Kim <namhyung@kernel.org> wrote:

...
> > > No, I meant we may not need the __builtin_unreachable() at the callsites.
> > >
> > > Would it complain this code?
> > >
> > >   if (some_bad_option_use)
> > >     usage_with_options(...);
> > >
> > >   /* normal code path */  
> > 
> > Right that would fix -Wunreachable, but the existing code would be:
> > 
> >   if (some_bad_option_use) {
> >     usage_with_options(...);
> >     exit(..);
> >   }
> >   /* normal code path */
> > 
> > Letting you know that "normal code path" couldn't be fallen into after
> > usage_with_options(...). To make the behavior more obvious we could
> > rename usage_with_options():
> > 
> >   if (some_bad_option_use)
> >     usage_with_options_and_exit(...);
> > 
> >   /* normal code path */
> > 
> > What I've done is:
> > 
> >   if (some_bad_option_use) {
> >     usage_with_options(...);
> >     __builtin_unreachable();
> >   }
> >   /* normal code path */

Isn't that just wrong?
Doesn't __builtin_unreachable() tell the compiler the code can't be executed.
In this case you want the opposite - something that the compiler will
whinge about if it finds a path that does execute the code.

In terms of source code readability it is a also horrid.
It is pretty common for an application to have a lot of 'usage' errors
and you want them to take as little vertical code space as possible.
That is the whole reason why it is common for a usage() function to
call exit() rather than return.

	David

> > 
> > My reasoning is that usage_with_options() doesn't obviously on the
> > face of it call exit and never return. To make that clear we could add
> > a comment:
> > 
> >   if (some_bad_option_use) {
> >     usage_with_options(...);
> >     /* usage_with_options never returns as it always calls exit */
> >   }
> >   /* normal code path */
> > 
> > But my preference is to use __builtin_unreachable as that is the same
> > as the comment but is also something the compiler can trap on were it
> > not true.  
> 
> I see, thanks for the explanation.  That part looks ok then.
> 
> Thanks,
> Namhyung
> 
> 


      reply	other threads:[~2025-05-31 18:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10 20:26 [RFC PATCH v1] perf build: Fix build for clang's -Wunreachable-code Ian Rogers
2025-04-11 21:34 ` Namhyung Kim
2025-04-11 22:14   ` Ian Rogers
2025-05-27 20:53     ` Ian Rogers
2025-05-28 18:24       ` Namhyung Kim
2025-05-28 18:35         ` Ian Rogers
2025-05-28 19:56           ` Namhyung Kim
2025-05-28 20:32             ` Ian Rogers
2025-05-28 21:59               ` Namhyung Kim
2025-05-28 23:18                 ` Ian Rogers
2025-05-30 22:33                   ` Namhyung Kim
2025-05-31 18:22                     ` David Laight [this message]

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=20250531192226.6af9bbde@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /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