From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87609 invoked by alias); 16 Jun 2017 11:43:07 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 87594 invoked by uid 89); 16 Jun 2017 11:43:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,KAM_STOCKGEN,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 16 Jun 2017 11:43:05 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8E8AAC04B941; Fri, 16 Jun 2017 11:43:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8E8AAC04B941 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8E8AAC04B941 Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69B8F8E7BB; Fri, 16 Jun 2017 11:43:03 +0000 (UTC) Subject: Re: [RFC v4 3/9] Add basic Linux kernel support To: Philipp Rudo , Yao Qi References: <20170612170836.25174-1-prudo@linux.vnet.ibm.com> <20170612170836.25174-4-prudo@linux.vnet.ibm.com> <86vanxtguc.fsf@gmail.com> <20170616121026.024f664b@ThinkPad> Cc: gdb-patches@sourceware.org, omair.javaid@linaro.org, yao.qi@linaro.org, peter.griffin@linaro.org, arnez@linux.vnet.ibm.com From: Pedro Alves Message-ID: Date: Fri, 16 Jun 2017 11:43:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20170616121026.024f664b@ThinkPad> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-06/txt/msg00457.txt.bz2 On 06/16/2017 11:10 AM, Philipp Rudo wrote: > True, but that would lead to code duplication. That's why I chose the way > using the two goto. > > Without goto the function reads like this > > struct lk_private_data * > lk_init_struct (const char *name, const char *alias, int silent) > { > { > /* Chek for "typedef struct { ... } name;"-like definitions. */ > sym = lookup_symbol (name, global, VAR_DOMAIN, NULL).symbol; > if (sym == NULL) > { > if (!silent) > error (_("Could not find %s. Aborting."), alias); > > return NULL; > } > > type = check_typedef (SYMBOL_TYPE (sym)); > if (TYPE_CODE (type) != TYPE_CODE_STRUCT) > { > if (!silent) > error (_("Could not find %s. Aborting."), alias); > > return NULL; > } > } You could add a helper function or lambda that handles the error return. E.g., with a lambda it'd look something like: lk_init_struct (const char *name, const char *alias, bool silent) { ... auto not_found = [=] () { if (!silent) error (_("Could not find %s. Aborting."), alias); return NULL; }; ... sym = lookup_symbol (name, global, VAR_DOMAIN, NULL).symbol; if (sym == NULL) return not_found (); type = check_typedef (SYMBOL_TYPE (sym)); if (TYPE_CODE (type) != TYPE_CODE_STRUCT) return not_found (); Alternatively, since NULL return is always an error indication, split into two functions, one that returns NULL if not found, and another that throws on error. The latter calls the former. I.e., something like this: /* Returns NULL if not found. */ lk_private_data * lk_init_struct_nothrow (const char *name, const char *alias) { ... sym = lookup_symbol (name, global, VAR_DOMAIN, NULL).symbol; if (sym == NULL) return NULL; ... return data; } /* Either returns non-NULL, or throws an error. */ lk_private_data * lk_init_struct (const char *name, const char *alias) { lk_private_data *data = lk_init_struct_nothrow (name, alias); if (data == NULL) error (_("Could not find %s. Aborting."), alias); return data; } Thanks, Pedro Alves