chiark / gitweb /
Warnings: Infrastructure for tracking and warning about variables
[subdirmk.git] / generate
index a6a6cda90c6b1e032dabf49797086c0280789a41..03aabb056787e4e98a1d6d6f6167ec32b24620ec 100755 (executable)
--- a/generate
+++ b/generate
@@ -152,6 +152,8 @@ Makefile run-main.mk:
 END
 }
 
+our %varref;
+
 our ($dir_prefix, $dir_suffix, $dir_name,
      $var_prefix, $var_prefix_name);
 
@@ -176,6 +178,11 @@ sub err ($) {
     die "subdirmk: ${err_file}:$.: $m\n";
 }
 
+sub wrn ($) {
+    my ($m) = @_;
+    print STDERR "subdirmk: warning: ${err_file}:$.: $m\n";
+}
+
 sub ddbl_only ($) {
     my ($e) = @_;
     return if $ddbl;
@@ -216,6 +223,7 @@ sub process_input_mk ($$$$) {
 
     local $ddbl;
     my @nest = (['']);
+    my $evalcall_brackets;
 
     my $push_nest = sub {
        my ($nk, $nndbl, $what) = @_;
@@ -229,6 +237,13 @@ sub process_input_mk ($$$$) {
        $ddbl = (shift @nest)[1];
     };
 
+    # Our detection of variable settings does not have to be completely
+    # accurate, since it is only going to be used for advice to the user.
+    my $note_varref = sub {
+       my ($vn,$amp) = @_;
+       $varref{$vn}{$amp}{"$f:$."} = 1;
+    };
+
     while (<$input>) {
        if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) {
            $$esclitr = $1;
@@ -255,12 +270,14 @@ sub process_input_mk ($$$$) {
                    : s{^(.*?)($esc)}{}) { od $_; last; }
            od $1;
            if ($2 eq '{') {
-               $ddbl++;
+               od $2;
+               $evalcall_brackets++;
                next;
            } elsif ($2 eq '}') {
-               next if --$ddbl;
+               od $2;
+               next if --$evalcall_brackets;
                $pop_nest->('eval');
-               od '}}';
+               od '}';
                next;
            }
            if (s{^\\$esc}{}) { od "$$esclitr" }
@@ -282,6 +299,7 @@ sub process_input_mk ($$$$) {
            elsif (s{^\$\{}{}) {
                err 'macro invocation cannot be re-$-doubled' if $ddbl;
                od '${eval ${call ';
+               $evalcall_brackets = 1;
                $push_nest->('eval',1, '&${...}');
            } elsif (s{^([~^]?)(?=[ \t])}{}) {
                my $prefix = $pfxmap{$1} // die "internal error ($1?)";
@@ -384,16 +402,16 @@ sub process_subtree ($$) {
     my @targets = sort keys %targets;
     foreach my $target (@targets) {
        my $target_varname = target_varname($var_prefix, $target);
-       print O "${dir_prefix}${target}:: \$($target_varname)";
+       oraw "${dir_prefix}${target}:: \$($target_varname)";
        foreach my $child_subdir (@{ $targets{$target} }) {
-           print O " $child_subdir/$target";
+           oraw " $child_subdir/$target";
        }
-       print O "\n";
+       oraw "\n";
     }
     if (@targets) {
-       print O ".PHONY:";
-       print O " ${dir_prefix}${_}" foreach @targets;
-       print O "\n";
+       oraw ".PHONY:";
+       oraw " ${dir_prefix}${_}" foreach @targets;
+       oraw "\n";
     }
 
     return @targets;
@@ -431,6 +449,20 @@ sub process_tree() {
     oraw "include \$(SUBDIRMK_MAKEFILES)\n";
 }
 
+sub print_varref_warnings () {
+    foreach my $vn (sort keys %varref) {
+       my $vv = $varref{$vn};
+       next unless $vv->{''} && $vv->{1};
+       print STDERR "subdirmk: warning: saw both $vn and &$vn\n";
+       foreach my $amp ('', 1) {
+           printf STDERR " saw %s%s at %s\n",
+               ($amp ? '&' : ''), $vn, $_
+               foreach sort keys %{ $vv->{$amp} };
+       }
+    }
+}
+
 build_tree();
 process_tree();
+print_varref_warnings();
 install_output_files();