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=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS 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 E3162C47404 for ; Fri, 11 Oct 2019 17:43:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B45CF20640 for ; Fri, 11 Oct 2019 17:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728716AbfJKRnK (ORCPT ); Fri, 11 Oct 2019 13:43:10 -0400 Received: from smtprelay0192.hostedemail.com ([216.40.44.192]:47098 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728416AbfJKRnK (ORCPT ); Fri, 11 Oct 2019 13:43:10 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay07.hostedemail.com (Postfix) with ESMTP id A41B7181D341A; Fri, 11 Oct 2019 17:43:08 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: cause95_7231ee8247c49 X-Filterd-Recvd-Size: 9632 Received: from XPS-9350.home (unknown [47.151.152.152]) (Authenticated sender: joe@perches.com) by omf10.hostedemail.com (Postfix) with ESMTPA; Fri, 11 Oct 2019 17:43:04 +0000 (UTC) Message-ID: <9fe980f7e28242c2835ffae34914c5f68e8268a7.camel@perches.com> Subject: Re: [PATCH 0/4] treewide: Add 'fallthrough' pseudo-keyword From: Joe Perches To: Linus Torvalds Cc: linux-sctp@vger.kernel.org, Miguel Ojeda , Kees Cook , Borislav Petkov , "H . Peter Anvin" , Thomas Gleixner , Pavel Machek , "Gustavo A . R . Silva" , Arnaldo Carvalho de Melo , Kan Liang , Namhyung Kim , Jiri Olsa , Alexander Shishkin , Shawn Landden , the arch/x86 maintainers , Linux Kernel Mailing List , Nathan Chancellor , Nick Desaulniers , Andrew Morton , David Miller , clang-built-linux , Jonathan Corbet , Vlad Yasevich , Neil Horman , Marcelo Ricardo Leitner , "open list:DOCUMENTATION" , Netdev Date: Fri, 11 Oct 2019 10:43:03 -0700 In-Reply-To: References: Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.32.1-2 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 Fri, 2019-10-11 at 09:29 -0700, Linus Torvalds wrote: > On Sat, Oct 5, 2019 at 9:46 AM Joe Perches wrote: > > Add 'fallthrough' pseudo-keyword to enable the removal of comments > > like '/* fallthrough */'. > > I applied patches 1-3 to my tree just to make it easier for people to > start doing this. Maybe some people want to do the conversion on their > own subsystem rather than with a global script, but even if not, this > looks better as a "prepare for the future" series and I feel that if > we're doing the "fallthrough" thing eventually, the sooner we do the > prepwork the better. > > I'm a tiny bit worried that the actual conversion is just going to > cause lots of pain for the stable people, but I'll not worry about it > _too_ much. If the stable people decide that it's too painful for them > to deal with the comment vs keyword model, they may want to backport > these three patches too. Shouldn't a conversion script be public somewhere? The old cvt_style script could be reduced to something like the below. From: Joe Perches Date: Fri, 11 Oct 2019 10:34:04 -0700 Subject: [PATCH] scripts:cvt_fallthrough.pl: Add script to convert /* fallthrough */ comments Convert /* fallthrough */ style comments to the pseudo-keyword fallthrough; to allow clang 10 and higher to work at finding missing fallthroughs too. Requires a git repository and this overwrites the input sources. Run with a path like: ./scripts/cvt_fallthrough.pl block and all files in the path will be converted or a specific file like: ./scripts/cvt_fallthrough.pl drivers/net/wireless/zydas/zd1201.c Signed-off-by: Joe Perches --- scripts/cvt_fallthrough.pl | 201 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100755 scripts/cvt_fallthrough.pl diff --git a/scripts/cvt_fallthrough.pl b/scripts/cvt_fallthrough.pl new file mode 100755 index 000000000000..013379464f86 --- /dev/null +++ b/scripts/cvt_fallthrough.pl @@ -0,0 +1,201 @@ +#!/usr/bin/perl -w + +# script to modify /* fallthrough */ style comments to fallthrough; +# use: perl cvt_fallthrough.pl +# e.g.: perl cvtfallthrough.pl drivers/net/ethernet/intel + +use strict; + +my $P = $0; +my $modified = 0; +my $quiet = 0; + +sub expand_tabs { + my ($str) = @_; + + my $res = ''; + my $n = 0; + for my $c (split(//, $str)) { + if ($c eq "\t") { + $res .= ' '; + $n++; + for (; ($n % 8) != 0; $n++) { + $res .= ' '; + } + next; + } + $res .= $c; + $n++; + } + + return $res; +} + +my $args = join(" ", @ARGV); +my $output = `git ls-files -- $args`; +my @files = split("\n", $output); + +foreach my $file (@files) { + my $f; + my $cvt = 0; + my $text; + +# read the file + + next if ((-d $file)); + + open($f, '<', $file) + or die "$P: Can't open $file for read\n"; + $text = do { local($/) ; <$f> }; + close($f); + + next if ($text eq ""); + + # for style: + + # /* */ + # case FOO: + + # while (comment has fallthrough and next non-blank, non-continuation line is (case or default:)) { + # remove newline, whitespace before, fallthrough comment and whitespace after + # insert newline, adjusted spacing and fallthrough; newline + # } + + my $count = 0; + my @fallthroughs = ( + 'fallthrough', + '@fallthrough@', + 'lint -fallthrough[ \t]*', + '[ \t.!]*(?:ELSE,? |INTENTIONAL(?:LY)? )?', + 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', + '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', + '[ \t.!]*(?:Else,? |Intentional(?:ly)? )?', + 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', + '[ \t.!]*(?:[Ee]lse,? |[Ii]ntentional(?:ly)? )?', + 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', + ); + do { + $count = 0; + pos($text) = 0; + foreach my $ft (@fallthroughs) { + my $regex = '(((?:[ \t]*\n)*[ \t]*)(/\*\s*(?!\*/)?\b' . "$ft" . '\s*(?!\*/)?\*/(?:[ \t]*\n)*)([ \t]*))(?:case\s+|default\s*:)'; + + while ($text =~ m{${regex}}i) { + my $all_but_case = $1; + my $space_before = $2; + my $fallthrough = $3; + my $space_after = $4; + my $pos_before = $-[1]; + my $pos_after = $+[3]; + + # try to maintain any additional comment on the same line + my $comment = ""; + if ($regex =~ /\\r/) { + $comment = $fallthrough; + $comment =~ s@^/\*\s*@@; + $comment =~ s@\s*\*/\s*$@@; + $comment =~ s@^\s*(?:intentional(?:ly)?\s+|else,?\s*)?fall[s\-]*\s*thr(?:ough|u|ew)[\s\.\-]*@@i; + $comment =~ s@\s+$@@; + $comment =~ s@\.*$@@; + $comment = "\t/* $comment */" if ($comment ne ""); + } + substr($text, $pos_before, $pos_after - $pos_before, ""); + substr($text, $pos_before, 0, "\n${space_after}\tfallthrough;${comment}\n"); + pos($text) = $pos_before; + $count++; + } + } + $cvt += $count; + } while ($count > 0); + + # Reset the fallthroughs types to a single regex + + @fallthroughs = ( + 'fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)' + ); + + # Convert the // style comments followed by case/default: + + do { + $count = 0; + pos($text) = 0; + foreach my $ft (@fallthroughs) { + my $regex = '(((?:[ \t]*\n)*[ \t]*)(//[ \t]*(?!\n)?\b' . "$ft" . '[ \t]*(?!\n)?\n(?:[ \t]*\n)*)([ \t]*))(?:case\s+|default\s*:)'; + while ($text =~ m{${regex}}i) { + my $all_but_case = $1; + my $space_before = $2; + my $fallthrough = $3; + my $space_after = $4; + my $pos_before = $-[1]; + my $pos_after = $+[3]; + + substr($text, $pos_before, $pos_after - $pos_before, ""); + substr($text, $pos_before, 0, "\n${space_after}\tfallthrough;\n"); + pos($text) = $pos_before; + $count++; + } + } + $cvt += $count; + } while ($count > 0); + + # Convert /* fallthrough */ comment macro lines with trailing \ + + do { + $count = 0; + pos($text) = 0; + foreach my $ft (@fallthroughs) { + my $regex = '((?:[ \t]*\\\\\n)*([ \t]*)(/\*[ \t]*\b' . "$ft" . '[ \t]*\*/)([ \t]*))\\\\\n*([ \t]*(?:case\s+|default\s*:))'; + + while ($text =~ m{${regex}}i) { + my $all_but_case = $1; + my $space_before = $2; + my $fallthrough = $3; + my $space_after = $4; + my $pos_before = $-[2]; + my $pos_after = $+[4]; + + my $oldline = "${space_before}${fallthrough}${space_after}"; + my $len = length(expand_tabs($oldline)); + + my $newline = "${space_before}fallthrough;${space_after}"; + $newline .= "\t" while (length(expand_tabs($newline)) < $len); + + substr($text, $pos_before, $pos_after - $pos_before, ""); + substr($text, $pos_before, 0, "$newline"); + pos($text) = $pos_before; + $count++; + } + } + $cvt += $count; + } while ($count > 0); + +# write the file if something was changed + + if ($cvt > 0) { + $modified = 1; + + open($f, '>', $file) + or die "$P: Can't open $file for write\n"; + print $f $text; + close($f); + + print "fallthroughs: $cvt $file\n" if (!$quiet); + } +} + +if ($modified && !$quiet) { + print <