From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4084 invoked by alias); 13 Aug 2014 10:01:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 4068 invoked by uid 89); 13 Aug 2014 10:01:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 X-HELO: mx2.suse.de Received: from cantor2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Wed, 13 Aug 2014 10:01:29 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id BDF3CABF3; Wed, 13 Aug 2014 10:01:26 +0000 (UTC) Date: Wed, 13 Aug 2014 10:01:00 -0000 From: Martin Jambor To: David Malcolm Cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH 005/236] Introduce as_a_nullable Message-ID: <20140813100124.GA1916@virgil.suse> Mail-Followup-To: David Malcolm , gcc-patches@gcc.gnu.org References: <1407345815-14551-1-git-send-email-dmalcolm@redhat.com> <1407345815-14551-6-git-send-email-dmalcolm@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1407345815-14551-6-git-send-email-dmalcolm@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes X-SW-Source: 2014-08/txt/msg01271.txt.bz2 Hi, On Wed, Aug 06, 2014 at 01:19:44PM -0400, David Malcolm wrote: > In many circumstances, is_a_helper ::test assumes that the pointer is > non-NULL, but sometimes you have a pointer of type T that can be NULL. > > Earlier versions of this patch kit made numerous uses of the ternary > operator to handle nullable pointers e.g.: > > return insn ? as_a (insn) : NULL; > > but this was ugly. Instead, introduce an as_a_nullable variant that > adds a check for NULL, so the above can be written simply as: > > return as_a_nullable (insn); A tiny bikeshedding, disregard it if you don't like it: however, the "nullable" part of the name suggests the pointer can be NULLed in the process. I think that variants of functions that survive NULL arguments are traditionally called "safe" in gcc, so what about "safe_as" (or safely_as)? Martin > > gcc/ > * is-a.h (template as_a_nullable ) New function. > --- > gcc/is-a.h | 24 ++++++++++++++++++++++++ > 1 file changed, 24 insertions(+) > > diff --git a/gcc/is-a.h b/gcc/is-a.h > index a14e344..f50e62c 100644 > --- a/gcc/is-a.h > +++ b/gcc/is-a.h > @@ -46,6 +46,14 @@ TYPE as_a (pointer) > > do_something_with (as_a *ptr); > > +TYPE as_a_nullable (pointer) > + > + Like as_a (pointer), but where pointer could be NULL. This > + adds a check against NULL where the regular is_a_helper hook for TYPE > + assumes non-NULL. > + > + do_something_with (as_a_nullable *ptr); > + > TYPE dyn_cast (pointer) > > Converts pointer to TYPE if and only if "is_a pointer". Otherwise, > @@ -185,6 +193,22 @@ as_a (U *p) > return is_a_helper ::cast (p); > } > > +/* Similar to as_a<>, but where the pointer can be NULL, even if > + is_a_helper doesn't check for NULL. */ > + > +template > +inline T > +as_a_nullable (U *p) > +{ > + if (p) > + { > + gcc_checking_assert (is_a (p)); > + return is_a_helper ::cast (p); > + } > + else > + return NULL; > +} > + > /* A generic checked conversion from a base type U to a derived type T. See > the discussion above for when to use this function. */ > > -- > 1.8.5.3 >