#!/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) 2007 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 KDE source for invalid SIGNALS and SLOTS passed to Qt's connect()

# 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) = "qconnect";
my($Version) = "1.1";

&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 (C++, non-headers only)
if ($f =~ m/\.cpp$/ || $f =~ m/\.cxx$/ || $f =~ m/\.cc$/) {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!&quietArg());
  exit 0;
}
my(@data_lines) = <F>;
close(F);

# Remove C-style comments and #if 0 blocks from the file input
my(@lines) = RemoveIfZeroBlockC( RemoveCommentsC( @data_lines ) );

my($cnt) = 0;
my($linecnt) = 0;
my($lstr) = "";

my($line);
while ($linecnt < $#lines) {
  $line = $lines[$linecnt++];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = 0;
    last;
  }

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

  &doIt($line,'\"(.*)\"','"fooMethod()"','fooMethod()');
  &doIt($line,'clicked\s*\(\s*Q3ListViewItem\s*\*\s*\)',
	"clicked(Q3ListViewItem*)","itemClicked(QTreeWidgetItem*,int)");
  &doIt($line,'processedSize\s*\(KJob\s*\*,\s*KIO::filesize_t\s*\)',
	"processedSize(KJob*,KIO::filesize_t)","processedSize(KJob*,qulonglong)");
  &doIt($line,'result\s*\(\s*KIO::Job\*\s*\)',
	"result(KIO::Job*)","result(KJob*)");
  &doIt($line,'triggered\s*\(\s*\"bool\"\s*\)',
	"triggered(\"bool\")","triggered(bool)");
  &doIt($line,'capturedShortcut\s*\(\s*const\sQKeySequence\&\s*\)',
	"capturedShortcut(const QKeySequence&)","keySequenceChanged(const QKeySequence&)");
  &doIt($line,'processExited\s*\(\s*int,\s*QProcess::ExitStatus\s*\)',
	"processExited(int,QProcess::ExitStatus)","finished(int,QProcess::ExitStatus)");
  &doIt($line,'receivedStdout\(\s*\)',
	"receivedStdout()","readyReadStandardOutput()");
  &doIt($line,'receivedStderr\(\s*\)',
	"receivedStderr()","readyReadStandardError()");
  &doIt($line,'leftClickedUrl\s*\(\s*const\sKUrl\s*\&\s*\)',
	"leftClickedUrl(const KUrl&)","leftClickedUrl(const QString&)");
  &doIt($line,'slotFind\s*\(\s*\)',
	"slotFind()","slotFindText()");
  &doIt($line,'slotReplace\s*\(\s*\)',
	"slotReplace()","slotReplaceText()");
  &doIt($line,'dragRequested\s*\(\s*const\sQList<KTNEFAttach\s*\*\s*>\s*&\s*\)',
	"dragRequested(const QList<KTNEFAttach*>&)","dragRequested(const QList<KTnef::KTNEFAttach*>&)");
  &doIt($line,'selectionInfo\s*\(\s*const\sKFileItemList\s*\&\s*\)',
	"selectionInfo(const KFileItemList&)","selectionInfo(const QList<KFileItem>&)");
  &doIt($line,'mouseOverInfo\s*\(\s*const\sKFileItem\s*\*\s*\)',
	"mouseOverInfo(const KFileItem *)","mouseOverInfo(const KFileItem&)");
}

if (!$cnt) {
  print "okay\n" if (!&quietArg());
  exit 0;
} else {
  print "$lstr ($cnt)\n" if (!&quietArg());
  exit $cnt;
}

sub doIt() {
  my($line,$regex,$old,$new) = @_;
  if (&invalidSignal($line,$regex)) {
    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $linecnt . "[$old=>$new]";
    } else {
      $lstr = $lstr . "," . $linecnt . "[$old=>$new]";
    }
    print "=> $line\n" if (&verboseArg());
  }
}

sub invalidSignal() {
  my($l,$old) = @_;
  if ($l =~ m/SIGNAL\s*\(\s*$old/) {
    return 1;
  } else {
    return 0;
  }
}

sub Help {
  print "Check for invalid SIGNALs and SLOTs passed to connect()\n";
  exit 0 if &helpArg();
}

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

sub Explain {
  print "Things will break badly unless you connect valid SIGNALs and SLOTs.\n";
  exit 0 if &explainArg();
}
