From dd6e91f3536381f97cffc233fbafc65077b7f323 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 8 Apr 2024 18:16:44 -0700 Subject: [PATCH] switch control -wmat=s to -wma for simplicity --- CHANGES.md | 13 +++--- bin/perltidy | 96 +++++++++++++++++++++++--------------- lib/Perl/Tidy.pm | 1 + lib/Perl/Tidy/Formatter.pm | 61 +++++++++++++----------- 4 files changed, 100 insertions(+), 71 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8abcdea9..f990a656 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,7 +3,7 @@ ## 2024 02 02.04 - Add options --dump-mismatched-args (or -dma) and - --warn-mismatched-arg-types=s (or -wmat=s). These options look + --warn-mismatched-arg (or -wma). These options look for and report instances where the number of args expected by a sub appear to differ from the number passed to the sub. The -dump version writes the results for a single file to standard output @@ -14,12 +14,13 @@ The -warn version formats as normal but reports any issues as warnings in the error file: - perltidy -wmat=1 somefile.pl + perltidy -wma somefile.pl - It takes a string parameter which is 1 or '*' to activate all checks. - It may be customized with two additional parameters if necessary to - avoid needless warnings, --warn-mismatched-arg-exclusion-list=s and - --warn-mismatched-arg-undercount-cutoff=n. These are explained in the manual. + It may be customized with three additional parameters if necessary to + avoid needless warnings, --warn-mismatched-arg-types=s, + --warn-mismatched-arg-exclusion-list=s and + --warn-mismatched-arg-undercount-cutoff=n. + These are explained in the manual. - Add option --valign-wide-equals, or -vwe, for issue git #135. Setting this parameter causes the following assignment operators diff --git a/bin/perltidy b/bin/perltidy index a08c51ff..b486b7e5 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -6097,7 +6097,7 @@ so it is worth checking. =back -B +B =over 4 @@ -6105,28 +6105,42 @@ B The only subs which these checks are made are those for which the call args are unpacked in an orderly manner at the beginning of the sub from C<@_>, directly and/or with C operations. =item * -Subs which appear to have no args are not checked. This is necessary to avoid false warnings when a sub actually uses args in a complex way. +Subs which appear to have no args are not checked. This restriction is +currently necessary to avoid false warnings when a sub actually uses args in a +complex way. =item * -Only calls which appear to be to subs defined within the file are checked. +Only calls which appear to be to subs defined within the file being +processed are checked. The file may contain any number of packages. =item * Sub calls made without parentheses around the args are not checked. =item * -Anonymous subs and lexical subs (introduced with 'my') are not currently +Anonymous subs and lexical subs (introduced with C) are not currently checked. =back -=item B +Some of these restrictions may be removed in the future. -The parameter B<--warn-mismatched-arg-types=s>, or B<-wmat=s>, can be used to -produce a warning message when perltidy formats a file and certain of the above -mismatchs is detected during formatting. Both checks may be requested with -B<-wmat='*'> or B<-wmat=1>. +=item B. -The default is not to do any of these checks, which can also be indicated with B<-wmat=0>. +This is similar to the B<-dump> parameter described above +except that any mismatches are reported in the error file and +otherwise formatting continues normally. Thus + + perltidy -wma somefile.pl + +means format F and report any mismatched arg errors found. +Several companion controls are available to avoid unwanted error messages: + +=over 4 + +=item * +B<--warn-mismatched-arg-types=s>, or B<-wmat=s>, can be used to +select specific tests, either type B (arrow test) or B (mismatched counts). Both checks may be requested with B<-wmat='*'> or B<-wmat=1>. This is the default. To restrict the check to a specific warning type, set the string equal to the letter of that warning, either B or B. For example @@ -6135,11 +6149,14 @@ To restrict the check to a specific warning type, set the string equal to the le will format F and report any call arg count mismatches found but will skip checking for arrow-type mismatches. -A companion control parameter B<--warn-mismatched-arg-exclusion-list>, or -B<-wmaxl=string>, can be given to skip the warning checks for a list of -subroutine names. +=item * +B<--warn-mismatched-arg-exclusion-list>, or B<-wmaxl=string>, can be given to +skip the warning checks for a list of subroutine names, entered as a quoted +string of space- or comma-separated names. All subs with those names will be +skipped, regardless of package. -Another control parameter B<--warn-mismatched-arg-undercount-cutoff=n>, or +=item * +B<--warn-mismatched-arg-undercount-cutoff=n>, or B<-wmauc=n>, can be used to avoid undercount warnings when the number of args expected is B or less. Please note that this number B is the number of args from the point of view of the sub definition, so an object like @@ -6147,18 +6164,21 @@ C<$self> passed with an arrow operator counts as one arg. The default value is B. This has been found to allow most programs to pass without warnings, but it should be reduced if possible for better error -checking. The minimum possible value of B for a program can be determined -by running with -wmauc=0, or by running the -dump version. The output shows, -for each mismatch, the number of args expected by a sub plus the range of the -number of args actually passed to it. +checking. The minimum possible value of B needed to avoid triggering an +error for a program can be determined by running with B<-wmauc=0>, or by +running with B<--dump-mismatched-args>. The output shows, for each mismatch, +the number of args expected by a sub plus the range of the number of args +actually passed to it. -For example +To illustrate these controls, - perltidy -wmat='*' -wmaxl='new' -wmauc=2 somefile.pl + perltidy -wma -wmat='c' -wmaxl='new old' -wmauc=2 somefile.pl -means format F as usual and check for all mismatch types. But skip -checking for any sub named C, and only warn of undercounts for subs -expecting more than 2 args. +means format F as usual and check for mismatched counts but not +arrows. Skip checking for any sub named C or C, and only warn of +undercounts for subs expecting more than 2 args. + +=back =back @@ -6471,21 +6491,21 @@ The following list shows all short parameter names which allow a prefix aws b baa baao bar bbao bbb bbc bbs bl bli boa boc bok bol bom bos bot cblx ce conv cpb cs csc cscb cscw dac dbc dbs dcbl - dcsc ddf dia dior dln dmcp dnl dop dp dpro - drc dsc dsm dsn dtc dtt duv dwic dwls dwrs - dws eos f fpva frm fs fso gcs hbc hbcm - hbco hbh hbhh hbi hbj hbk hbm hbn hbp hbpd - hbpu hbq hbs hbsc hbv hbw hent hic hicm hico - hih hihh hii hij hik him hin hip hipd hipu - hiq his hisc hiv hiw hsc html ibc icb icp - iob ipc isbc iscl kgb kgbd kgbi kis lal log - lop lp lsl mem nib ohbr okw ola olc oll - olq opr opt osbc osbr otr ple pod pvl q - sac sbc sbl scbb schb scp scsb sct se sfp - sfs skp sob sobb sohb sop sosb sot ssc st - sts t tac tbc toc tp tqw trp ts tsc - tso vbc vc viu vmll vsc vsn w wfc wia - wme wn x xbt xci xlp xs + dcsc ddf dia dior dln dma dmcp dnl dop dp + dpro drc dsc dsm dsn dtc dtt duv dwic dwls + dwrs dws eos f fpva frm fs fso gcs hbc + hbcm hbco hbh hbhh hbi hbj hbk hbm hbn hbp + hbpd hbpu hbq hbs hbsc hbv hbw hent hic hicm + hico hih hihh hii hij hik him hin hip hipd + hipu hiq his hisc hiv hiw hsc html ibc icb + icp iob ipc isbc iscl kgb kgbd kgbi kis lal + log lop lp lsl mem nib ohbr okw ola olc + oll olq opr opt osbc osbr otr ple pod pvl + q sac sbc sbl scbb schb scp scsb sct se + sfp sfs skp sob sobb sohb sop sosb sot ssc + st sts t tac tbc toc tp tqw trp ts + tsc tso vbc vc viu vmll vsc vsn vwe w + wfc wia wma wme wn x xbt xci xlp xs Equivalently, the prefix 'no' or 'no-' on the corresponding long names may be used. diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 1227bf92..45c036d8 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3717,6 +3717,7 @@ sub generate_options { $add_option->( 'want-call-parens', 'wcp', '=s' ); $add_option->( 'nowant-call-parens', 'nwcp', '=s' ); + $add_option->( 'warn-mismatched-args', 'wma', '!' ); $add_option->( 'warn-mismatched-arg-types', 'wmat', '=s' ); $add_option->( 'warn-mismatched-arg-undercount-cutoff', 'wmauc', '=i' ); $add_option->( 'warn-mismatched-arg-exclusion-list', 'wmaxl', '=s' ); diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 1159e2a6..7be5f95f 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -389,7 +389,7 @@ my ( %warn_variable_types, %is_warn_variable_excluded_name, - # INITIALIZER: sub initialize_warn_mismatched_arg_types + # INITIALIZER: sub initialize_warn_mismatched_args %warn_mismatched_arg_types, %is_warn_mismatched_arg_excluded_name, @@ -1014,7 +1014,7 @@ sub new { $self->[_ris_asub_block_] = {}; $self->[_ris_sub_block_] = {}; - # Variables for --warn-mismatched-arg-types and + # Variables for --warn-mismatched-args and # --dump-mismatched-args $self->[_rK_package_list_] = []; $self->[_rsub_call_paren_info_by_seqno_] = {}; @@ -1472,7 +1472,7 @@ sub check_options { initialize_warn_variable_types(); - initialize_warn_mismatched_arg_types(); + initialize_warn_mismatched_args(); make_bli_pattern(); @@ -1491,6 +1491,21 @@ sub check_options { Exit(0); } + # --indent-only skips the call to sub respace_tokens, which defines + # some essential data structures needed by some dump routines, + # or might be in the future. Since there is an immediate exit after a + # dump, we can turn off indent-only to get these structures for a -dump. + if ( $rOpts->{'indent-only'} ) { + + if ( $rOpts->{'dump-mismatched-args'} ) { + $rOpts->{'indent-only'} = 0; + } + + if ( $rOpts->{'dump-block-summary'} ) { + $rOpts->{'indent-only'} = 0; + } + } + initialize_line_up_parentheses(); check_tabs(); @@ -6609,7 +6624,7 @@ EOM && $self->[_logger_object_] ); $self->warn_mismatched_args() - if ( $rOpts->{'warn-mismatched-arg-types'} + if ( $rOpts->{'warn-mismatched-args'} && $self->[_logger_object_] ); if ( $rOpts->{'dump-mismatched-args'} ) { @@ -14086,7 +14101,7 @@ sub cross_check_call_args { my ( $self, $warn_mode ) = @_; # Input parameter: - # $warn_mode = true for --warn-mismatched-arg-types + # $warn_mode = true for --warn-mismatched-args # $warn_mode = false for --dump-mismatched-args # The current possible checks are indicated by these letters: @@ -14412,23 +14427,15 @@ sub stringify_line_range { return $string; } ## end sub stringify_line_range -sub initialize_warn_mismatched_arg_types { +sub initialize_warn_mismatched_args { # Initialization for: - # --dump-mismatched-args - # --warn-mismatched-arg-types=s and + # --warn-mismatched-args + # --warn-mismatched-arg-types=s # --warn-mismatched-arg-exclusion-list=s %warn_mismatched_arg_types = (); %is_warn_mismatched_arg_excluded_name = (); - - # The --dump-mismatched-args needs data structures which are not - # available with -io. Since it causes immediate exit without formatting, - # we can turn off --indent-only to allow it to work. - if ( $rOpts->{'dump-mismatched-args'} ) { - if ( $rOpts->{'indent-only'} ) { - $rOpts->{'indent-only'} = 0; - } - } + return unless $rOpts->{'warn-mismatched-args'}; # Note: coding here is similar to sub initialize_warn_variable_types @@ -14437,12 +14444,13 @@ sub initialize_warn_mismatched_arg_types { #----------------------------------- my $wmat_key = 'warn-mismatched-arg-types'; my $wmat_option = $rOpts->{$wmat_key}; - return unless ($wmat_option); + $wmat_option = '1' unless defined($wmat_option); # The -indent-only option skips production of data structures needed by # the --warn-mismatched-args if ( $rOpts->{'indent-only'} ) { - Warn("Note: '--$wmat_key' is ignored if '--indent-only' is set\n"); + my $wma_key = 'warn-mismatched-args'; + Warn("Note: '--$wma_key' is ignored if '--indent-only' is set\n"); return; } @@ -14525,24 +14533,23 @@ sub initialize_warn_mismatched_arg_types { @is_warn_mismatched_arg_excluded_name{@xl} = (1) x scalar(@xl); } return; -} ## end sub initialize_warn_mismatched_arg_types +} ## end sub initialize_warn_mismatched_args sub warn_mismatched_args { my ($self) = @_; - # process a --warn-mismatched-arg-types command + # process a --warn-mismatched-args command # additional control parameters are: - # - mismatched-arg-exclusion-list + # - warn-mismatched-arg-types + # - warn-mismatched-arg-exclusion-list # - warn-mismatched-arg-undercount-cutoff - my $wma_key = 'warn-mismatched-arg-types'; - my $wma_option = $rOpts->{$wma_key}; - my $rwarnings = $self->cross_check_call_args(1); return unless ( $rwarnings && @{$rwarnings} ); - my $output_string = "Begin scan for --$wma_key=$wma_option\n"; + my $wma_key = 'warn-mismatched-args'; + my $output_string = "Begin scan for --$wma_key\n"; $output_string .= <