pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/pkglint When checking Makefiles, get rid of t...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/756f0afe61cd
branches:  trunk
changeset: 501867:756f0afe61cd
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Oct 30 22:11:38 2005 +0000

description:
When checking Makefiles, get rid of the "physical" lines as soon as
possible and base all other checks on the logical lines. The physical
source lines are saved literally to make implementing the --autofix
option easier.

diffstat:

 pkgtools/pkglint/Makefile         |    4 +-
 pkgtools/pkglint/files/pkglint.pl |  203 +++++++++++++++++++++----------------
 2 files changed, 119 insertions(+), 88 deletions(-)

diffs (truncated from 347 to 300 lines):

diff -r ed2c6a336a8e -r 756f0afe61cd pkgtools/pkglint/Makefile
--- a/pkgtools/pkglint/Makefile Sun Oct 30 21:54:52 2005 +0000
+++ b/pkgtools/pkglint/Makefile Sun Oct 30 22:11:38 2005 +0000
@@ -1,7 +1,7 @@
-# $NetBSD: Makefile,v 1.270 2005/10/26 23:17:49 rillig Exp $
+# $NetBSD: Makefile,v 1.271 2005/10/30 22:11:38 rillig Exp $
 #
 
-DISTNAME=      pkglint-4.31
+DISTNAME=      pkglint-4.31.1
 CATEGORIES=    pkgtools devel
 MASTER_SITES=  # empty
 DISTFILES=     # empty
diff -r ed2c6a336a8e -r 756f0afe61cd pkgtools/pkglint/files/pkglint.pl
--- a/pkgtools/pkglint/files/pkglint.pl Sun Oct 30 21:54:52 2005 +0000
+++ b/pkgtools/pkglint/files/pkglint.pl Sun Oct 30 22:11:38 2005 +0000
@@ -11,7 +11,7 @@
 # Freely redistributable.  Absolutely no warranty.
 #
 # From Id: portlint.pl,v 1.64 1998/02/28 02:34:05 itojun Exp
-# $NetBSD: pkglint.pl,v 1.308 2005/10/30 19:54:36 rillig Exp $
+# $NetBSD: pkglint.pl,v 1.309 2005/10/30 22:11:38 rillig Exp $
 #
 # This version contains lots of changes necessary for NetBSD packages
 # done by:
@@ -205,9 +205,15 @@
 # A Line is a class that contains the read-only fields C<file>, C<lineno>
 # and C<text>, as well as some methods for printing diagnostics easily.
 #==========================================================================
+BEGIN {
+       import PkgLint::Util qw(
+               false true
+       );
+}
+       
 sub new($$$$) {
-       my ($class, $file, $lineno, $text) = @_;
-       my ($self) = ([$file, $lineno, $text]);
+       my ($class, $file, $lines, $text, $physlines) = @_;
+       my ($self) = ([$file, $lines, $text, $physlines, false]);
        bless($self, $class);
        return $self;
 }
@@ -251,31 +257,107 @@
        use Exporter;
        use vars qw(@ISA @EXPORT_OK);
        @ISA = qw(Exporter);
-       @EXPORT_OK = qw(load_file);
+       @EXPORT_OK = qw(
+               load_file load_lines
+       );
 
        import PkgLint::Util qw(
                false true
        );
+       import PkgLint::Logging qw(
+               log_error
+       );
+}
+
+sub load_physical_lines($) {
+       my ($fname) = @_;
+       my ($physlines, $line, $lineno);
+
+       $physlines = [];
+       open(F, "< $fname") or return undef;
+       $lineno = 0;
+       while (defined($line = <F>)) {
+               $lineno++;
+               push(@{$physlines}, [$lineno, $line]);
+       }
+       close(F) or return undef;
+       return $physlines;
+}
+
+sub get_logical_line($$$) {
+       my ($fname, $lines, $ref_lineno) = @_;
+       my ($value, $lineno, $first, $firstlineno, $lastlineno);
+
+       $value = "";
+       $first = true;
+       $lineno = ${$ref_lineno};
+       $firstlineno = $lines->[$lineno]->[0];
+
+       for (; $lineno <= $#{$lines}; $lineno++) {
+               if ($lines->[$lineno]->[1] =~ qr"^([ \t]*)(.*?)([ \t]*)(\\?)\n?$") {
+                       my ($indent, $text, $outdent, $cont) = ($1, $2, $3, $4);
+
+                       if ($first) {
+                               $value .= $indent;
+                               $first = false;
+                       }
+
+                       $value .= $text;
+
+                       if ($cont eq "\\") {
+                               $value .= " ";
+                       } else {
+                               $value .= $outdent;
+                               last;
+                       }
+               }
+       }
+
+       $lastlineno = $lines->[$lineno]->[0];
+       ${$ref_lineno} = $lineno + 1;
+
+       return PkgLint::FileUtil::Line->new($fname,
+           $firstlineno == $lastlineno
+               ? $firstlineno
+               : "$firstlineno--$lastlineno",
+           $value);
+}
+
+sub load_lines($$) {
+       my ($fname, $fold_backslash_lines) = @_;
+       my ($physlines, $seen_newline, $loglines) = @_;
+
+       $physlines = load_physical_lines($fname);
+       if (!$physlines) {
+               return false;
+       }
+
+       $seen_newline = true;
+       $loglines = [];
+       if ($fold_backslash_lines) {
+               for (my $lineno = 0; $lineno <= $#{$physlines}; ) {
+                       push(@{$loglines}, get_logical_line($fname, $physlines, \$lineno));
+               }
+       } else {
+               foreach my $physline (@{$physlines}) {
+                       my $text = $physline->[1];
+
+                       $text =~ s/\n$//;
+                       push(@{$loglines}, PkgLint::FileUtil::Line->new($fname, $physline->[0], $text, [$physline]));
+               }
+       }
+
+       if (0 <= $#{$physlines} && $physlines->[-1]->[1] !~ qr"\n$") {
+               log_error($fname, $physlines->[-1]->[0], "File must end with a newline.");
+       }
+
+       return $loglines;
 }
 
 sub load_file($) {
        my ($fname) = @_;
-       my ($result, $line, $lineno, $seen_newline);
-
-       $result = [];
-       open(F, "< $fname") or return undef;
-       $lineno = 0;
-       $seen_newline = true;
-       while (defined($line = <F>)) {
-               $lineno++;
-               $seen_newline = ($line =~ s/\n$//);
-               push(@{$result}, PkgLint::FileUtil::Line->new($fname, $lineno, $line));
-       }
-       if (!$seen_newline) {
-               $result->[-1]->log_error("File must end with a newline.");
-       }
-       close(F) or return undef;
-       return $result;
+
+       return load_lines($fname, false);
 }
 
 #== End of PkgLint::FileUtil ==============================================
@@ -298,7 +380,7 @@
                print_summary_and_exit
        );
        import PkgLint::FileUtil qw(
-               load_file
+               load_file load_lines
        );
 }
 
@@ -514,55 +596,6 @@
        }
 }
 
-sub get_logical_line($$) {
-       my ($lines, $ref_lineno) = @_;
-       my ($value, $lineno, $first, $file, $firstlineno, $lastlineno);
-
-       $value = "";
-       $first = true;
-       $lineno = ${$ref_lineno};
-       $file = $lines->[$lineno]->file;
-       $firstlineno = $lines->[$lineno]->lineno;
-
-       for (; $lineno <= $#{$lines}; $lineno++) {
-               if ($lines->[$lineno]->text =~ qr"^(\s*)(.*?)\s*(\\?)$") {
-                       my ($indent, $text, $cont) = ($1, $2, $3);
-
-                       if ($first) {
-                               $value .= $indent;
-                               $first = false;
-                       }
-
-                       $value .= $text;
-
-                       if ($cont eq "\\") {
-                               $value .= " ";
-                       } else {
-                               last;
-                       }
-               }
-       }
-
-       $lastlineno = $lines->[$lineno]->lineno;
-       ${$ref_lineno} = $lineno + 1;
-
-       return PkgLint::FileUtil::Line->new($file,
-           $firstlineno == $lastlineno
-               ? $firstlineno
-               : "$firstlineno--$lastlineno",
-           $value);
-}
-
-sub to_logical_lines($) {
-       my ($lines) = @_;
-
-       my @loglines = ();
-       for (my $lineno = 0; $lineno <= $#{$lines}; ) {
-               push(@loglines, get_logical_line($lines, \$lineno));
-       }
-       return \@loglines;
-}
-
 sub load_make_vars_typemap() {
        my ($lines, $vartypes);
 
@@ -887,7 +920,7 @@
        log_subinfo("checkfile_MESSAGE", $fname, NO_LINE_NUMBER, undef);
 
        checkperms($fname);
-       if (!defined($message = load_file($fname))) {
+       if (!($message = load_file($fname))) {
                log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
                return;
        }
@@ -1134,7 +1167,7 @@
        my $contents = "";
        my ($includefile, $dirname, $lines);
 
-       $lines = load_file($file);
+       $lines = load_lines($file, true);
        if (!defined ($lines)) {
                return false;
        }
@@ -1563,7 +1596,7 @@
        foreach my $line (@{$lines}) {
                my $text = $line->text;
 
-               if ($line->lineno == 1) {
+               if ($line->lineno eq "1") {
                        checkline_rcsid_regex($line, qr"#\s+", "# ");
                }
 
@@ -1683,10 +1716,10 @@
        }
 }
 
-sub load_package_Makefile($$$$$) {
+sub load_package_Makefile($$$$) {
        my ($subr) = "load_package_Makefile";
-       my ($dir, $fname, $ref_whole, $ref_lines, $ref_loglines) = @_;
-       my ($whole, $lines, $loglines);
+       my ($dir, $fname, $ref_whole, $ref_lines) = @_;
+       my ($whole, $lines);
 
        log_info($fname, NO_LINE_NUMBER, "Checking package Makefile.");
 
@@ -1695,11 +1728,10 @@
                log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
                return false;
        }
-       $loglines = to_logical_lines($lines);
 
        if ($opt_dumpmakefile) {
                print("OK: whole Makefile (with all included files) follows:\n");
-               foreach my $line (@{$loglines}) {
+               foreach my $line (@{$lines}) {
                        printf("%s\n", $line->to_string());
                }
        }
@@ -1734,12 +1766,11 @@
 
        ${$ref_whole} = $whole;
        ${$ref_lines} = $lines;
-       ${$ref_loglines} = $loglines;
        return true;
 }
 
-sub checkfile_package_Makefile($$$$$) {
-       my ($dir, $fname, $rawwhole, $lines, $loglines) = @_;
+sub checkfile_package_Makefile($$$$) {
+       my ($dir, $fname, $rawwhole, $loglines) = @_;
        my ($distname, $category, $distfiles,



Home | Main Index | Thread Index | Old Index