From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 78219 invoked by alias); 19 Jun 2017 07:56:01 -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 78203 invoked by uid 89); 19 Jun 2017 07:56:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,KAM_STOCKGEN,KHOP_DYNAMIC,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=tip X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 19 Jun 2017 07:56:00 +0000 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v5J7rbrl073603 for ; Mon, 19 Jun 2017 03:56:03 -0400 Received: from e06smtp12.uk.ibm.com (e06smtp12.uk.ibm.com [195.75.94.108]) by mx0a-001b2d01.pphosted.com with ESMTP id 2b64rs4v5d-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 19 Jun 2017 03:56:03 -0400 Received: from localhost by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 19 Jun 2017 08:56:00 +0100 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 19 Jun 2017 08:55:57 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v5J7tu9m37159012; Mon, 19 Jun 2017 07:55:56 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6CC8011C11C; Mon, 19 Jun 2017 08:53:54 +0100 (BST) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2E94011C11F; Mon, 19 Jun 2017 08:53:54 +0100 (BST) Received: from ThinkPad (unknown [9.152.212.148]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Mon, 19 Jun 2017 08:53:54 +0100 (BST) Date: Mon, 19 Jun 2017 07:56:00 -0000 From: Philipp Rudo To: Pedro Alves Cc: Yao Qi , gdb-patches@sourceware.org, omair.javaid@linaro.org, yao.qi@linaro.org, peter.griffin@linaro.org, arnez@linux.vnet.ibm.com Subject: Re: [RFC v4 3/9] Add basic Linux kernel support In-Reply-To: References: <20170612170836.25174-1-prudo@linux.vnet.ibm.com> <20170612170836.25174-4-prudo@linux.vnet.ibm.com> <86vanxtguc.fsf@gmail.com> <20170616121026.024f664b@ThinkPad> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 17061907-0008-0000-0000-0000046A06CA X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17061907-0009-0000-0000-00001DF3796D Message-Id: <20170619095554.0f7426da@ThinkPad> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-06-19_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1706190134 X-IsSubscribed: yes X-SW-Source: 2017-06/txt/msg00495.txt.bz2 Hi Pedro On Fri, 16 Jun 2017 12:43:02 +0100 Pedro Alves wrote: > 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 for the tip. As I still live in a C world, I prefer your second approach. The code now look like this /* Helper function for lk_init_struct. Returns NULL if symbol could not be found. Doesn't throw an error. */ struct lk_private_data * lk_init_struct_silent (const char *name, const char *alias) { struct lk_private_data *data; const struct block *global; const struct symbol *sym; struct type *type; void **new_slot; void *old_slot; if ((old_slot = lk_find (alias)) != NULL) return (struct lk_private_data *) old_slot; global = block_global_block(get_selected_block (0)); sym = lookup_symbol (name, global, STRUCT_DOMAIN, NULL).symbol; if (sym != NULL) { type = SYMBOL_TYPE (sym); } else { /* Chek for "typedef struct { ... } name;"-like definitions. */ sym = lookup_symbol (name, global, VAR_DOMAIN, NULL).symbol; if (sym == NULL) return NULL; type = check_typedef (SYMBOL_TYPE (sym)); if (TYPE_CODE (type) != TYPE_CODE_STRUCT) return NULL; } data = XCNEW (struct lk_private_data); data->alias = alias; data->data.type = type; new_slot = lk_find_slot (alias); *new_slot = data; return data; } /* Same as lk_init_addr but for structs. */ struct lk_private_data * lk_init_struct (const char *name, const char *alias, bool silent) { struct lk_private_data *data; data = lk_init_struct_silent (name, alias); if (data == NULL && !silent) error (_("Could not find %s. Aborting."), alias); return data; } Philipp