From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C93D0C56202 for ; Thu, 26 Nov 2020 09:28:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E90A2173E for ; Thu, 26 Nov 2020 09:28:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389252AbgKZJ2V (ORCPT ); Thu, 26 Nov 2020 04:28:21 -0500 Received: from smtprelay0125.hostedemail.com ([216.40.44.125]:52964 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726721AbgKZJ2T (ORCPT ); Thu, 26 Nov 2020 04:28:19 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay08.hostedemail.com (Postfix) with ESMTP id EF36F182CF666; Thu, 26 Nov 2020 09:28:17 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: snake52_100d3822737e X-Filterd-Recvd-Size: 3167 Received: from XPS-9350.home (unknown [47.151.128.180]) (Authenticated sender: joe@perches.com) by omf17.hostedemail.com (Postfix) with ESMTPA; Thu, 26 Nov 2020 09:28:16 +0000 (UTC) Message-ID: Subject: Re: [PATCH v2] checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi] From: Joe Perches To: Dwaipayan Ray Cc: linux-kernel-mentees@lists.linuxfoundation.org, linux-kernel@vger.kernel.org, lukas.bulwahn@gmail.com Date: Thu, 26 Nov 2020 01:28:15 -0800 In-Reply-To: <20201126084623.39178-1-dwaipayanray1@gmail.com> References: <20201126084623.39178-1-dwaipayanray1@gmail.com> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.38.1-1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2020-11-26 at 14:16 +0530, Dwaipayan Ray wrote: > Modifiers %h and %hh should never be used. > > Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use > of unnecessary %h[xudi] and %hh[xudi]") specifies that: > > "Standard integer promotion is already done and %hx and %hhx is useless > so do not encourage the use of %hh[xudi] or %h[xudi]." > > "The "h" and "hh" things should never be used. The only reason for them > being used if you have an "int", but you want to print it out as a > "char" (and honestly, that is a really bad reason, you'd be better off > just using a proper cast to make the code more obvious)." > > Add a new check to emit a warning on finding an unneeded use of %h or > %hh modifier. > > Link: https://lore.kernel.org/lkml/4910042649a4f3ab22fac93191b8c1fa0a2e17c3.camel@perches.com/ > > Suggested-by: Lukas Bulwahn > Signed-off-by: Dwaipayan Ray > --- > Changes in v2: > - Use $logFunctions instead of the manual list. > - Relocate the check to after logging continuations check. > - Remove perl_version_ok condition which is unneeded here. [] > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl [] > @@ -6027,6 +6027,18 @@ sub process { >   "Avoid logging continuation uses where feasible\n" . $herecurr); >   } > > +# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions > + if (defined $stat && > + $line =~ /\b$logFunctions\s*\(/) { > + my $lc = $stat =~ tr@\n@@; > + $lc = $lc + $linenr; > + my $stat_real = get_stat_real($linenr, $lc); > + if ($stat_real =~ /\"[^\"]*%[\d\.\*\-]*h+[idux].*\"/i) { > + WARN("UNNECESSARY_MODIFIER", > + "Unnecessary use of modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n"); o Why not capture the group and show the actual integer format portion here? (%[\d\.\*\-]*h+[idux]) o This might also show every unnecessary use of %h. (using while (.../g) o The .*\" isn't useful. o The /i is probably wrong. o Perhaps this could use a --fix line if the format is on the same line as $logFunctions o Perhaps the message should say something about integer promotion. Maybe something like: "Integer promotion: using 'h' in '$1' is unnecessary\n"