]> git.donarmstrong.com Git - perltidy.git/commitdiff
some tokenizer clean-ups, part 6
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 28 Mar 2023 13:57:42 +0000 (06:57 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 28 Mar 2023 13:57:42 +0000 (06:57 -0700)
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/Logger.pm
lib/Perl/Tidy/Tokenizer.pm

index 446f217c0065bca52be3b20d8b63783ca0eefe54..18ddaf8474541d709ee903d59424391ca0c3fbc5 100644 (file)
@@ -1237,15 +1237,17 @@ sub check_token_array {
 
     # interface to Perl::Tidy::Logger routines
     sub warning {
-        my ($msg) = @_;
-        if ($logger_object) { $logger_object->warning($msg); }
+        my ( $msg, $msg_line_number ) = @_;
+        if ($logger_object) {
+            $logger_object->warning( $msg, $msg_line_number );
+        }
         return;
     }
 
     sub complain {
-        my ($msg) = @_;
+        my ( $msg, $msg_line_number ) = @_;
         if ($logger_object) {
-            $logger_object->complain($msg);
+            $logger_object->complain( $msg, $msg_line_number );
         }
         return;
     } ## end sub complain
@@ -30258,8 +30260,14 @@ sub add_closing_side_comment {
                     # otherwise we'll make a note of it
                     else {
 
+                        my $msg_line_number;
+                        my $K = $K_to_go[$i_terminal];
+                        if ( defined($K) ) {
+                            $msg_line_number = $rLL->[$K]->[_LINE_INDEX_] + 1;
+                        }
                         warning(
-"perltidy -cscw replaced: $tokens_to_go[$max_index_to_go]\n"
+"perltidy -cscw replaced: $tokens_to_go[$max_index_to_go]\n",
+                            $msg_line_number
                         );
 
                         # save the old side comment in a new trailing block
index 55bd35639e9d0d46a45831e82e608a64ca1e5f22..bbcc211acff7c4805bd0b6fb30202ca547de3d7e 100644 (file)
@@ -90,6 +90,7 @@ sub new {
         _rOpts                         => $rOpts,
         _fh_warnings                   => $fh_warnings,
         _last_input_line_written       => 0,
+        _last_input_line_number        => undef,
         _at_end_of_file                => 0,
         _use_prefix                    => 1,
         _block_log_output              => 0,
@@ -115,6 +116,11 @@ sub get_input_stream_name {
     return $self->{_input_stream_name};
 }
 
+sub set_last_input_line_number {
+    my ( $self, $lno ) = @_;
+    $self->{_last_input_line_number} = $lno;
+}
+
 sub get_warning_count {
     my $self = shift;
     return $self->{_warning_count};
@@ -316,13 +322,13 @@ sub increment_brace_error {
 }
 
 sub brace_warning {
-    my ( $self, $msg ) = @_;
+    my ( $self, $msg, $msg_line_number ) = @_;
 
     use constant BRACE_WARNING_LIMIT => 10;
     my $saw_brace_error = $self->{_saw_brace_error};
 
     if ( $saw_brace_error < BRACE_WARNING_LIMIT ) {
-        $self->warning($msg);
+        $self->warning( $msg, $msg_line_number );
     }
     $saw_brace_error++;
     $self->{_saw_brace_error} = $saw_brace_error;
@@ -336,17 +342,22 @@ sub brace_warning {
 sub complain {
 
     # handle non-critical warning messages based on input flag
-    my ( $self, $msg ) = @_;
+    my ( $self, $msg, $msg_line_number ) = @_;
     my $rOpts = $self->{_rOpts};
 
     # these appear in .ERR output only if -w flag is used
     if ( $rOpts->{'warning-output'} ) {
-        $self->warning($msg);
+        $self->warning( $msg, $msg_line_number );
     }
 
     # otherwise, they go to the .LOG file
     else {
         $self->{_complaint_count}++;
+        if ($msg_line_number) {
+
+            # TODO: consider using same prefix as warning()
+            $msg = $msg_line_number . ':' . $msg;
+        }
         $self->write_logfile_entry($msg);
     }
     return;
@@ -355,7 +366,7 @@ sub complain {
 sub warning {
 
     # report errors to .ERR file (or stdout)
-    my ( $self, $msg ) = @_;
+    my ( $self, $msg, $msg_line_number ) = @_;
 
     use constant WARNING_LIMIT => 50;
 
@@ -402,14 +413,11 @@ sub warning {
                 }
             }
 
-            if ( $self->get_use_prefix() > 0 ) {
+            if ( $self->get_use_prefix() > 0 && defined($msg_line_number) ) {
                 $self->write_logfile_entry("WARNING: $msg");
 
                 # add prefix 'filename:line_no: ' to message lines
-                my $input_line_number =
-                  Perl::Tidy::Tokenizer::get_input_line_number();
-                if ( !defined($input_line_number) ) { $input_line_number = -1 }
-                my $pre_string = $filename_stamp . $input_line_number . ': ';
+                my $pre_string = $filename_stamp . $msg_line_number . ': ';
                 chomp $msg;
                 $msg =~ s/\n/\n$pre_string/g;
                 $msg = $pre_string . $msg . "\n";
@@ -461,28 +469,32 @@ sub finish {
     # called after all formatting to summarize errors
     my ($self) = @_;
 
-    my $warning_count = $self->{_warning_count};
-    my $save_logfile  = $self->{_save_logfile};
-    my $log_file      = $self->{_log_file};
+    my $warning_count   = $self->{_warning_count};
+    my $save_logfile    = $self->{_save_logfile};
+    my $log_file        = $self->{_log_file};
+    my $msg_line_number = $self->{_last_input_line_number};
 
     if ($warning_count) {
         if ($save_logfile) {
             $self->block_log_output();    # avoid echoing this to the logfile
             $self->warning(
-                "The logfile $log_file may contain useful information\n");
+                "The logfile $log_file may contain useful information\n",
+                $msg_line_number );
             $self->unblock_log_output();
         }
 
         if ( $self->{_complaint_count} > 0 ) {
             $self->warning(
-"To see $self->{_complaint_count} non-critical warnings rerun with -w\n"
+"To see $self->{_complaint_count} non-critical warnings rerun with -w\n",
+                $msg_line_number
             );
         }
 
         if ( $self->{_saw_brace_error}
             && ( $self->{_logfile_gap} > 1 || !$save_logfile ) )
         {
-            $self->warning("To save a full .LOG file rerun with -g\n");
+            $self->warning( "To save a full .LOG file rerun with -g\n",
+                $msg_line_number );
         }
     }
 
index e0094d85b068fb0a8f66e0ea1f52ff41eb847195..fc094ae6d7c33c2ec960d6567db53c49b99558b6 100644 (file)
@@ -542,7 +542,8 @@ sub warning {
     my $msg           = shift;
     my $logger_object = $tokenizer_self->[_logger_object_];
     if ($logger_object) {
-        $logger_object->warning($msg);
+        my $msg_line_number = $tokenizer_self->[_last_line_number_];
+        $logger_object->warning( $msg, $msg_line_number );
     }
     return;
 } ## end sub warning
@@ -557,12 +558,13 @@ sub get_input_stream_name {
 } ## end sub get_input_stream_name
 
 sub complain {
-    my $msg           = shift;
+
+    my $msg = shift;
+
     my $logger_object = $tokenizer_self->[_logger_object_];
     if ($logger_object) {
-        my $input_line_number = $tokenizer_self->[_last_line_number_] + 1;
-        $msg = "Line $input_line_number: $msg";
-        $logger_object->complain($msg);
+        my $input_line_number = $tokenizer_self->[_last_line_number_];
+        $logger_object->complain( $msg, $input_line_number );
     }
     return;
 } ## end sub complain
@@ -601,10 +603,11 @@ sub increment_brace_error {
 } ## end sub increment_brace_error
 
 sub brace_warning {
-    my $msg           = shift;
-    my $logger_object = $tokenizer_self->[_logger_object_];
+    my ( $self, $msg ) = @_;
+    my $logger_object = $self->[_logger_object_];
     if ($logger_object) {
-        $logger_object->brace_warning($msg);
+        my $msg_line_number = $self->[_last_line_number_];
+        $logger_object->brace_warning( $msg, $msg_line_number );
     }
     return;
 } ## end sub brace_warning
@@ -660,6 +663,13 @@ sub report_tokenization_errors {
     # Both block formatting and cause the input stream to be output verbatim.
     my $severe_error = $self->[_in_error_] || $self->[_in_trouble_];
 
+    # Inform the logger object on length of input stream
+    my $logger_object = $self->[_logger_object_];
+    if ($logger_object) {
+        my $last_line_number = $self->[_last_line_number_];
+        $logger_object->set_last_input_line_number($last_line_number);
+    }
+
     my $maxle = $self->[_rOpts_maximum_level_errors_];
     my $maxue = $self->[_rOpts_maximum_unexpected_errors_];
     $maxle = 1 unless defined($maxle);
@@ -833,10 +843,6 @@ sub is_valid_token_type {
     return $is_valid_token_type{$type};
 }
 
-sub get_input_line_number {
-    return $tokenizer_self->[_last_line_number_];
-}
-
 sub log_numbered_msg {
     my ( $self, $msg ) = @_;
 
@@ -5273,7 +5279,7 @@ EOM
         if ( $level_in_tokenizer < 0 ) {
             if ( $input_line =~ /^\s*(sub|package)\s+(\w+)/ ) {
                 reset_indentation_level(0);
-                brace_warning("resetting level to 0 at $1 $2\n");
+                $self->brace_warning("resetting level to 0 at $1 $2\n");
             }
         }