#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
###############################################################################
# Sanity check plugin for the Krazy project.                                  #
# Copyright (C) 2013 by Allen Winter <winter@kde.org>                         #
#                                                                             #
# This program is free software; you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation; either version 2 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.               #
#                                                                             #
###############################################################################

# Tests Python (PyQt) code for emitting a tuple to Qt SIGNALs.

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --priority:      report issues of the specified priority only
#   --strict:        report issues with the specified strictness level only
#   --explain:       print an explanation with solving instructions
#   --installed      file is to be installed
#   --quiet:         suppress all output messages
#   --verbose:       print the offending content

# Exits with status=0 if test condition is not present in the source;
# else exits with the number of failures encountered.

use strict;
use FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::PreProcess;
use Krazy::Utils;

my($Prog) = "qtemits";
my($Version) = "1.0";

&parseArgs();

&Help() if &helpArg();
&Version() if &versionArg();
&Explain() if &explainArg();
if ($#ARGV != 0){ &Help(); Exit 0; }

my($f) = $ARGV[0];

# open file and slurp it in (Python only)
if (&fileType($f) eq "python") {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!&quietArg());
  Exit 0;
}
my(@lines) = <F>;
close(F);

# Check Condition
my($cnt) = 0;
my($wcnt) = 0;
my($ncnt) = 0;
my($linecnt) = 0;
my($saveline);
my($line);
my($pline);
my($lstr) = "";
my($wlstr) = "";
my($nlstr) = "";

while ($linecnt <= $#lines) {
  $saveline = $linecnt;
  $line = $lines[$linecnt++];
  $pline = $line;
  $pline =~ s/#.*$//;

  if ($line =~ m+#.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+#.*[Kk]razy:skip+) {
    $cnt = 0; $wcnt = 0; $ncnt = 0;
    last;
  }

  next if ($line =~ m+#.*[Kk]razy:exclude=.*$Prog+);

  if ($pline =~ m/\.emit\s*\(\s*.*SIGNAL\s*\(/) {
    # if line is continued, look skip ahead 1 line
    if ($pline !~ m/\)\s*$/) {
      $line  = $lines[$linecnt++];
      $pline = $line;
      $pline =~ s/#.*$//;
    }
    # search for empty last value of n-tuple
    if ($pline =~ m/,\s*\)\s*\)\s*$/ ||
        $pline =~ m/,\s*\([_,.()\w\s]*\s*\)\s*$/ ||
        $pline =~ m/^\s*\([_,.()\w\s]*\s*\)\s*$/ ||
        $pline =~ m/tuple\s*\(/) {
      $cnt++;
      if ($cnt == 1) {
	$lstr = "line\#" . $linecnt;
      } else {
	$lstr = $lstr . "," . $linecnt;
      }
      print "=> emit n-tuple $line\n" if (&verboseArg());
      goto nextCheck;
    }
  }

  $linecnt = $saveline;
  $line = $lines[$linecnt++];
  $pline = $line;
  $pline =~ s/#.*$//;

  if ($pline =~ m/\.emit\s*\(\s*$/) {
    $line  = $lines[$linecnt++];
    $pline = $line;
    $pline =~ s/#.*$//;
    if ($pline =~ m/.*SIGNAL\s*\(/) {
      # if line is continued, look skip ahead 1 line
      if ($pline !~ m/\)\s*$/) {
	$line  = $lines[$linecnt++];
        $pline = $line;
        $pline =~ s/#.*$//;
      }
      # search for empty last value of n-tuple
      if ($pline =~ m/,\s*\)\s*\)\s*$/ ||
          $pline =~ m/,\s*\([_,.()\w\s]*\s*\)\s*$/ ||
          $pline =~ m/^\s*\([_,.()\w\s]*\s*\)\s*$/ ||
          $pline =~ m/tuple\s*\(/) {
	$cnt++;
	if ($cnt == 1) {
	  $lstr = "line\#" . $linecnt;
	} else {
	  $lstr = $lstr . "," . $linecnt;
	}
	print "=> emit n-tuple $line\n" if (&verboseArg());
      }
    }
  }

 nextCheck:
  # Check for QWidget argument in SIGNAL signature
  $linecnt = $saveline;
  $line = $lines[$linecnt++];
  $pline = $line;
  $pline =~ s/#.*$//;

  if ($pline =~ m/.*SIGNAL\s*\(/) {
    # if line is continued, look skip ahead 1 line
    if ($pline !~ m/\)\s*$/ && $pline !~ m/\)\s*,\s*$/) {
      $line  = $lines[$linecnt++];
      $pline = $line;
      $pline =~ s/#.*$//;
    }
    if ($pline =~ m/[\(,]+\s*QWidget\s*[\),]+/) {
      $wcnt++;
      if ($wcnt == 1) {
        $wlstr = "SIGNAL method(QWidget) line\#" . $linecnt;
      } else {
        $wlstr = $wlstr . "," . $linecnt;
      }
      print "=> SIGNAL(method(QWidget)) $line\n" if (&verboseArg());
    }
  }

  # Check that the number of args in the SIGNAL signature matches
  # the number of args emitted.
  $linecnt = $saveline;
  $line = $lines[$linecnt++];
  $pline = $line;
  $pline =~ s/#.*$//;

  my($foo,$bar);
  my($sargs, $eargs);
  if ($pline =~ m/\.emit\s*\(/) {
    if ($pline !~ m/QtCore\.SIGNAL\s*\(/) {
      # skip ahead 1 line
      $line  = $lines[$linecnt++];
      $pline = $line;
      $pline =~ s/#.*$//;
    }
    if ($pline !~ m/QtCore\.SIGNAL\s*\(/) {
      #an emit without a SIGNAL
      next;
    }
    if ($pline !~ m/"/ && $pline !~ m/\'/) {
      # skip ahead 1 line
      $line  = $lines[$linecnt++];
      $pline = $line;
      $pline =~ s/#.*$//;
    }
    $sargs = 0;
    if ($pline =~ m/"/) {
      ($foo,$bar) = split('"',$pline);
    } elsif ( $pline =~ m/\'/) {
      ($foo,$bar) = split('\'',$pline);
    }
    if ($bar !~ m/\(\s*\)/) {
      if ($bar =~ m/,/) {
        $sargs = scalar(split(',',$bar));
      } else {
        $sargs = 1;
      }
    }
    # if line is continued, look skip ahead 1 line
    if ($pline !~ m/\)\s*$/) {
      $line  = $lines[$linecnt++];
      $pline = $line;
      $pline =~ s/#.*$//;
    }
    $foo = $pline;
    $foo =~ s/^.*SIGNAL\s*\(.*\)\s*"\s*\)\s*,//;
    $foo =~ s/^.*SIGNAL\s*\(.*\)\s*\'\s*\)\s*,//;
    $foo =~ s/^.*\)\s*"\s*\)\s*,//;
    $foo =~ s/^.*\)\s*\'\s*\)\s*,//;
    $eargs = 0;
    if ($foo !~ m/^\s*\)/ &&
      $foo !~ m/\(\s*\)\s*"\)\s*\)/ &&
      $foo !~ m/\(\s*\)\s*\'\)\s*\)/) {
      if ($foo =~ m/,/) {
        $eargs = scalar(split(',',$foo));
      } else {
#        if ($foo !~ m/None/) {
          $eargs = 1;
#        }
      }
    }
    if ($sargs != $eargs) {
      $ncnt++;
      if ($ncnt == 1) {
        $nlstr = "SIGNAL number args mismatch line\#" . $linecnt;
      } else {
        $nlstr = $nlstr . "," . $linecnt;
      }
      print "=> SIGNAL number of args ($sargs) mismatch with number emitted ($eargs) $line\n" if (&verboseArg());
    }
  }
}
close(F);

my($total_count) = $cnt + $wcnt + $ncnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "$lstr ($cnt)\n" if (!&quietArg() && $cnt);
  print "$wlstr ($wcnt)\n" if (!&quietArg() && $wcnt);
  print "$nlstr ($ncnt)\n" if (!&quietArg() && $ncnt);
  Exit $total_count;
}

sub Help {
  print "Check for problems with Qt emits in Python code\n";
  Exit 0 if &helpArg();
}

sub Version {
  print "$Prog, version $Version\n";
  Exit 0 if &versionArg();
}

sub Explain {
  print "Older versions of PyQt required all the values emitted to a Qt SIGNAL to be an n-tuple.  New PyQt (starting with PyQt4) require that tuples be emitted as a list of scalars.\n";
  Exit 0 if &explainArg();
}
