From patchwork Wed Apr 11 20:30:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Randy Dunlap X-Patchwork-Id: 10337237 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7A50C6020F for ; Wed, 11 Apr 2018 20:31:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67B5527F97 for ; Wed, 11 Apr 2018 20:31:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5AD5027FA3; Wed, 11 Apr 2018 20:31:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AE99B27F97 for ; Wed, 11 Apr 2018 20:31:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753151AbeDKUa5 (ORCPT ); Wed, 11 Apr 2018 16:30:57 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:51836 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752894AbeDKUaz (ORCPT ); Wed, 11 Apr 2018 16:30:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: Content-Type:MIME-Version:Date:Message-ID:Subject:From:To:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=9Q3GOQCVdTLsSGZeWqRXXAc4mmKi8tW48ItOt4wLDHc=; b=MWg8vI7kqeF16R6SXKmyeiML9 h8NTn3bgoXDyy3j3GkL6FYkiVm/12aaRLiK2B5XJzojSmQI8aHKeUAbHfNBR9uiZ0K7CgZHAjbogS xG8DhIx9TJIYcBcUBq1QLgxOLweuWIfS6oma7oYfUv6mrYR/WB1tpOH9G40b1vDPH14TuNmbMXGmh NypuL29LcTJ8LA+86RfuxwsKAOk5bmT2miDuU6ViasRa/RZEFZpNZP9/8tbAJNntVuEC77Ok9VuYW wJirvQ4+/tZHsJgK220H62r1tXMcups1gvpElsAC5s5qRInTG3bo9yHhZw5i8cNxnCVOxmMF64L7b 1crw27g5w==; Received: from static-50-53-52-16.bvtn.or.frontiernet.net ([50.53.52.16] helo=midway.dunlab) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1f6MOM-00017E-B9; Wed, 11 Apr 2018 20:30:54 +0000 To: Linux-Sparse , Christopher Li , Luc Van Oostenryck From: Randy Dunlap Subject: [PATCH v3] sparse: add option to print compound global data symbol info Message-ID: <79efd80a-f418-1bb2-f459-803107dca981@infradead.org> Date: Wed, 11 Apr 2018 13:30:53 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 Content-Language: en-US Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Randy Dunlap with help from Linus. (many moons ago) sparse addition to print all compound/composite global data symbols with their sizes and alignment. usage: -vcompound Example: $ sparse -vcompound symbol-sizes.c compound-sizes.c:39:17: union un static [toplevel] un: compound size 192, alignment 8 compound-sizes.c:42:25: struct inventory static [toplevel] inven[100]: compound size 19200, alignment 8 compound-sizes.c:51:33: struct inventory static [toplevel] [usertype] invent[10]: compound size 1920, alignment 8 compound-sizes.c:58:25: float static [toplevel] floats[42]: compound size 168, alignment 4 compound-sizes.c:59:25: double static [toplevel] doubles[84]: compound size 672, alignment 8 and validation: $ ./test-suite single compound-sizes.c TEST compound-sizes (compound-sizes.c) compound-sizes.c passed ! Signed-off-by: Randy Dunlap --- v3: incorporate Luc's valuable help lib.c | 2 lib.h | 1 sparse.c | 44 ++++++++++++++++- validation/compound-sizes.c | 87 ++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) -- To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- spars-052rc1.orig/sparse.c +++ spars-052rc1/sparse.c @@ -292,6 +292,43 @@ static void check_symbols(struct symbol_ exit(1); } +static void list_compound_symbols(struct symbol_list *list) +{ + struct symbol *sym, *base; + + /* Only show arrays, structures, unions */ + FOR_EACH_PTR(list, sym) { + /* Only show symbols that have a non-zero size */ + if (!sym->bit_size) + continue; + if (!sym->ctype.base_type) + continue; + if (is_func_type(sym)) + continue; + if (is_float_type(sym) || is_scalar_type(sym)) + continue; + /* Don't show unnamed types */ + if (!sym->ident) + continue; + + if (sym->type == SYM_NODE) + base = sym->ctype.base_type; + else + base = sym; + switch (base->type) { + case SYM_STRUCT: case SYM_UNION: case SYM_ARRAY: + break; + default: + continue; + } + + info(sym->pos, "%s: compound size %u, alignment %lu", + show_typename(sym), + bits_to_bytes(sym->bit_size), + sym->ctype.alignment); + } END_FOR_EACH_PTR(sym); +} + int main(int argc, char **argv) { struct string_list *filelist = NULL; @@ -300,7 +337,12 @@ int main(int argc, char **argv) // Expand, linearize and show it. check_symbols(sparse_initialize(argc, argv, &filelist)); FOR_EACH_PTR_NOTAG(filelist, file) { - check_symbols(sparse(file)); + struct symbol_list *res = sparse(file); + + check_symbols(res); + + if (dbg_compound) + list_compound_symbols(res); } END_FOR_EACH_PTR_NOTAG(file); report_stats(); --- spars-052rc1.orig/lib.c +++ spars-052rc1/lib.c @@ -257,6 +257,7 @@ int dump_macro_defs = 0; int dbg_entry = 0; int dbg_dead = 0; +int dbg_compound = 0; int fmem_report = 0; int fdump_linearize; @@ -598,6 +599,7 @@ static char **handle_switch_W(char *arg, static struct warning debugs[] = { { "entry", &dbg_entry}, { "dead", &dbg_dead}, + { "compound", &dbg_compound}, }; --- spars-052rc1.orig/lib.h +++ spars-052rc1/lib.h @@ -150,6 +150,7 @@ extern int dump_macro_defs; extern int dbg_entry; extern int dbg_dead; +extern int dbg_compound; extern int fmem_report; extern int fdump_linearize; --- /dev/null +++ spars-052rc1/validation/compound-sizes.c @@ -0,0 +1,87 @@ +// This tests sparse "-vcompound" output. + +#include +#include + +// Do not list functions. +static int do_nothing(void) +{} + +// no: +static inline int zero(void) +{ + return 0 / 1; +} + +// no: +struct inventory { + unsigned char description[64]; + unsigned char department[64]; + uint32_t dept_number; + uint32_t item_cost; + uint64_t stock_number; + uint32_t tally[12]; // per month +}; + +// no +static struct inventory *get_inv(uint64_t stocknum) +{ + return NULL; +} + +// no +union un { + struct inventory inv; + unsigned char bytes[0]; +}; + +// yes +static union un un; + +// yes +static struct inventory inven[100]; + +// no +typedef struct inventory inventory_t; + +// no +static struct inventory *invptr; + +// yes +static inventory_t invent[10]; + +// no +static float floater; +static double double_float; + +// yes +static float floats[42]; +static double doubles[84]; + +// no +int main(void) +{ + // no, these are not global. + struct inventory inv[10]; + inventory_t invt[10]; + // what about statics? + static struct inventory invtop; + static inventory_t inv_top; + static uint64_t stocknums[100]; + + invptr = get_inv(42000); + return 0; +} + +/* + * check-name: compound-sizes + * check-command: sparse -vcompound $file + * + * check-error-start +compound-sizes.c:39:17: union un static [toplevel] un: compound size 192, alignment 8 +compound-sizes.c:42:25: struct inventory static [toplevel] inven[100]: compound size 19200, alignment 8 +compound-sizes.c:51:33: struct inventory static [toplevel] [usertype] invent[10]: compound size 1920, alignment 8 +compound-sizes.c:58:25: float static [toplevel] floats[42]: compound size 168, alignment 4 +compound-sizes.c:59:25: double static [toplevel] doubles[84]: compound size 672, alignment 8 + * check-error-end + */