Fri Aug 12 22:40:40 2022 UTC ()
lintpkgsrc: fix parsing of comments in makefile lines


(rillig)
diff -r1.79 -r1.80 pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl
diff -r1.8 -r1.9 pkgsrc/pkgtools/lintpkgsrc/files/t/parse_makefile.t

cvs diff -r1.79 -r1.80 pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl (expand / switch to unified diff)

--- pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl 2022/08/12 22:32:21 1.79
+++ pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl 2022/08/12 22:40:40 1.80
@@ -1,16 +1,16 @@ @@ -1,16 +1,16 @@
1#!@PERL5@ 1#!@PERL5@
2 2
3# $NetBSD: lintpkgsrc.pl,v 1.79 2022/08/12 22:32:21 rillig Exp $ 3# $NetBSD: lintpkgsrc.pl,v 1.80 2022/08/12 22:40:40 rillig Exp $
4 4
5# Written by David Brownlee <abs@netbsd.org>. 5# Written by David Brownlee <abs@netbsd.org>.
6# 6#
7# Caveats: 7# Caveats:
8# The 'Makefile parsing' algorithm used to obtain package versions and 8# The 'Makefile parsing' algorithm used to obtain package versions and
9# DEPENDS information is geared towards speed rather than perfection, 9# DEPENDS information is geared towards speed rather than perfection,
10# though it has gotten somewhat better over time, it only parses the 10# though it has gotten somewhat better over time, it only parses the
11# simpler Makefile conditionals. 11# simpler Makefile conditionals.
12# 12#
13# TODO: Handle fun DEPENDS like avifile-devel with 13# TODO: Handle fun DEPENDS like avifile-devel with
14# {qt2-designer>=2.2.4,qt2-designer-kde>=2.3.1nb1} 14# {qt2-designer>=2.2.4,qt2-designer-kde>=2.3.1nb1}
15 15
16use v5.36; 16use v5.36;
@@ -513,35 +513,38 @@ sub parse_makefile_vars($file, $cwd = un @@ -513,35 +513,38 @@ sub parse_makefile_vars($file, $cwd = un
513 if ($cwd) { 513 if ($cwd) {
514 $vars{'.CURDIR'} = $cwd; 514 $vars{'.CURDIR'} = $cwd;
515 } elsif ($file =~ m#(.*)/#) { 515 } elsif ($file =~ m#(.*)/#) {
516 $vars{'.CURDIR'} = $1; 516 $vars{'.CURDIR'} = $1;
517 } else { 517 } else {
518 $vars{'.CURDIR'} = getcwd; 518 $vars{'.CURDIR'} = getcwd;
519 } 519 }
520 520
521 push @incdirs, $vars{'.CURDIR'}; 521 push @incdirs, $vars{'.CURDIR'};
522 if ($opt{L}) { 522 if ($opt{L}) {
523 print "$file\n"; 523 print "$file\n";
524 } 524 }
525 525
526 while (defined($_ = shift(@lines))) { 526 while (defined($_ = shift @lines)) {
527 s/\s*[^\\]#.*//; 527 s/(*negative_lookbehind:\\)#.*//;
 528 s/\s+$//;
528 529
529 # Join continuation lines. 530 # Join continuation lines.
530 # See devel/bmake/files/parse.c, 'replace following'. 531 # See devel/bmake/files/parse.c, 'replace following'.
531 while (substr($_, -1) eq "\\" && @lines > 0) { 532 while (substr($_, -1) eq "\\" && @lines > 0) {
532 my $line = shift @lines; 533 my $cont = shift @lines;
533 $line =~ s,^\s*, ,; 534 $cont =~ s,^\s*, ,;
534 substr($_, -1) = $line; 535 $cont =~ s/(*negative_lookbehind:\\)#.*//;
 536 $cont =~ s/\s+$//;
 537 substr($_, -1) = $cont;
535 } 538 }
536 539
537 # Conditionals 540 # Conditionals
538 # 541 #
539 if (m#^ \. \s* if(|def|ndef) \s+ (.*) #x) { 542 if (m#^ \. \s* if(|def|ndef) \s+ (.*) #x) {
540 my $type = $1; 543 my $type = $1;
541 if ($if_false[$#if_false]) { 544 if ($if_false[$#if_false]) {
542 push @if_false, 2; 545 push @if_false, 2;
543 546
544 } elsif ($type eq '') { 547 } elsif ($type eq '') {
545 # Straight if 548 # Straight if
546 push @if_false, parse_eval_make_false($2, \%vars); 549 push @if_false, parse_eval_make_false($2, \%vars);
547 550

cvs diff -r1.8 -r1.9 pkgsrc/pkgtools/lintpkgsrc/files/t/parse_makefile.t (expand / switch to unified diff)

--- pkgsrc/pkgtools/lintpkgsrc/files/t/parse_makefile.t 2022/08/12 22:32:21 1.8
+++ pkgsrc/pkgtools/lintpkgsrc/files/t/parse_makefile.t 2022/08/12 22:40:40 1.9
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: parse_makefile.t,v 1.8 2022/08/12 22:32:21 rillig Exp $ 1# $NetBSD: parse_makefile.t,v 1.9 2022/08/12 22:40:40 rillig Exp $
2 2
3use strict; 3use strict;
4use warnings; 4use warnings;
5use File::Slurp; 5use File::Slurp;
6use File::Temp; 6use File::Temp;
7use Test; 7use Test;
8 8
9BEGIN { plan tests => 29, onfail => sub { die } } 9BEGIN { plan tests => 29, onfail => sub { die } }
10 10
11require('../lintpkgsrc.pl'); 11require('../lintpkgsrc.pl');
12 12
13sub enable_debug_logging() { 13sub enable_debug_logging() {
14 export_for_test()->{opt}->{D} = 1; 14 export_for_test()->{opt}->{D} = 1;
@@ -48,31 +48,29 @@ sub test_parse_makefile_vars() { @@ -48,31 +48,29 @@ sub test_parse_makefile_vars() {
48 "COMMENT=\tvalue#comment", 48 "COMMENT=\tvalue#comment",
49 "MULTI=\tone\\", 49 "MULTI=\tone\\",
50 "\ttwo\\", 50 "\ttwo\\",
51 'three#comment', 51 'three#comment',
52 )); 52 ));
53 53
54 my $vars = parse_makefile_vars($file, undef); 54 my $vars = parse_makefile_vars($file, undef);
55 55
56 ok( 56 ok(
57 join(', ', sort keys %$vars), 57 join(', ', sort keys %$vars),
58 '.CURDIR, BSD_PKG_MK, COMMENT, MULTI, VAR'); 58 '.CURDIR, BSD_PKG_MK, COMMENT, MULTI, VAR');
59 ok($vars->{BSD_PKG_MK}, 'YES'); 59 ok($vars->{BSD_PKG_MK}, 'YES');
60 60
61 # FIXME: must be 'value' 61 ok($vars->{COMMENT}, 'value');
62 ok($vars->{COMMENT}, 'valu'); 
63 62
64 # FIXME: must be 'one two three' 63 ok($vars->{MULTI}, 'one two three');
65 ok($vars->{MULTI}, "one two three#comment"); 
66 64
67 ok($vars->{VAR}, 'value'); 65 ok($vars->{VAR}, 'value');
68} 66}
69 67
70sub test_parse_makefile_vars_cond() { 68sub test_parse_makefile_vars_cond() {
71 my $dir = File::Temp->newdir(); 69 my $dir = File::Temp->newdir();
72 my $file = "$dir/filename.mk"; 70 my $file = "$dir/filename.mk";
73 71
74 write_file($file, map { "$_\n" } ( 72 write_file($file, map { "$_\n" } (
75 '.if ${COND} == then', 73 '.if ${COND} == then',
76 'BRANCH= then', 74 'BRANCH= then',
77 '.elif ${COND} == elif', 75 '.elif ${COND} == elif',
78 'BRANCH= elif', 76 'BRANCH= elif',