Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1650992ybm; Sat, 30 May 2020 16:12:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwatQ0kI9wZCm1ap2zdgsl1cltizaXGnfw6cqQ04YTPsRo5pSSYvCCMXroFjp1Ih/t9Aji0 X-Received: by 2002:a17:906:410a:: with SMTP id j10mr13307376ejk.378.1590880371285; Sat, 30 May 2020 16:12:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590880371; cv=none; d=google.com; s=arc-20160816; b=1F0V3MavV9wk3mAzKJXwbTux/d3hnR9Na1FkeZwBQeVC+oqJWHP8Y9v8iYrWq+s7f5 ywsjShnuWZPb45VmhTO5g0/C3A+mmM+blRjUxulnrZ69mBJG4PwjnWOcgzaURyNMcUrM daLRIEkAclBiAaDzPzLenlrdJUpi+9DNjZ7yqtsvX1RkEOOe7cLM8T18l+2MOhFuYQm9 RU1gdNbPGosyMAeRkA8jz0I/K9H3Gh4xPAbV/v0H0Xnaz3gtVVN0mjN4yVBNr06e9UBO MLXf7jTq09wkWhwAoVOXirvtaVDVqqneba5fJtbpERYEkmDN6JTwzGaTXqNy6q3n9vcb 6PnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=GHRj0EP1Yv0YHak6GbxMkcEpePO1IozLGjotTbM3ZhA=; b=MN7ncTdkdRG89Y5gGtMLaAtAriBQt72iCEOBcmkD8helYvdaGAk+R2Jukxi2PjfPt3 /pJLEr3ihaEj3Dxyw/oGj4mCIGgmL8cYaeZhMvOmDfUYJAYfyo8bLFauYYa1O70KXFAC QFIevgD78I/plFHWSBdS/+km/PU6X4PQR3Xfwvo4EgjSWjZTR9NVOGCOr4UdV+r2r4aN Vx8qoiY60J3xrwW2HDhgy9zB3P9/1TMdVXOKKAB2pXPn8KvwfGpArQKYbJv99j6bBLb4 35JL7rruyNTdx2woqleC3syGSstYypgpsNFoExs4LBGly8OOkm2MKr8PkSud4U1diU5N ZvXg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i24si7189040edb.577.2020.05.30.16.12.27; Sat, 30 May 2020 16:12:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729538AbgE3XKk (ORCPT + 99 others); Sat, 30 May 2020 19:10:40 -0400 Received: from smtprelay0197.hostedemail.com ([216.40.44.197]:39632 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729520AbgE3XKk (ORCPT ); Sat, 30 May 2020 19:10:40 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay07.hostedemail.com (Postfix) with ESMTP id F30E9181D3026; Sat, 30 May 2020 23:10:38 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,,RULES_HIT:41:355:379:599:982:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2197:2199:2393:2559:2562:2828:3138:3139:3140:3141:3142:3353:3622:3653:3865:3866:3867:3868:3870:3871:3872:3873:4321:5007:6119:6120:6691:7903:8957:10004:10400:10848:11026:11232:11658:11914:12043:12297:12555:12740:12760:12895:12986:13069:13161:13229:13311:13357:13439:14181:14659:14721:21080:21221:21451:21627:30012:30054:30070:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: game74_0c0153226d70 X-Filterd-Recvd-Size: 2434 Received: from XPS-9350.home (unknown [47.151.136.130]) (Authenticated sender: joe@perches.com) by omf09.hostedemail.com (Postfix) with ESMTPA; Sat, 30 May 2020 23:10:38 +0000 (UTC) Message-ID: Subject: Re: [RFC PATCH] checkpatch: check for trivial sizeofs From: Joe Perches To: Denis Efremov , Andy Whitcroft Cc: linux-kernel@vger.kernel.org Date: Sat, 30 May 2020 16:10:37 -0700 In-Reply-To: <20200530212129.7498-1-efremov@linux.com> References: <20200530212129.7498-1-efremov@linux.com> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.36.2-0ubuntu1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 2020-05-31 at 00:21 +0300, Denis Efremov wrote: > sizeof(char) and its variations in most cases doesn't make code more clear. > It only makes code wordy. There are about 1000 of these uses in the kernel. Not sure I like this though as many/most of the uses seem _less_ readable with a 1 in their place. $ git grep -P 'sizeof\s*\(\s*(?:(?:unsigned\s+)?char\s*|(?:__)?u8|u?int8_t|[us]?byte(?:_t)?)\s*\)' | wc -l 970 Try the grep without the wc and see if you agree. And if this is actually going to be used, I'd prefer using a separate $typeChar for the search. Maybe something like: --- scripts/checkpatch.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index dd750241958b..de2e9242350b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -466,6 +466,13 @@ our $typeTypedefs = qr{(?x: $typeKernelTypedefs\b )}; +our $typeChar = qr{(?x: + (?:(?:un)?signed\s+)?char | + (?:__)?u8 | + (?:u_|u)?int8_t | + [us]?byte(?:_t)? +)}; + our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; our $logFunctions = qr{(?x: @@ -6106,6 +6113,16 @@ sub process { } } +# check for trivial sizeof(char) == 1 + if ($line =~ /\bsizeof\s*\(\s*($typeChar)\s*\)/) { + my $byte = $1; + if (CHK("SIZEOF_CHAR", + "sizeof($byte) could be the constant 1 instead\n" . $herecurr) && + $fix) { + $fixed[$fixlinenr] =~ s/sizeof\s*\(\s*\Q$byte\E\s*\)/1/; + } + } + # check for struct spinlock declarations if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { WARN("USE_SPINLOCK_T",