#!/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 Thorsten Roeder <thorsten.roeder@weihenstephan.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 classes that should use the Q_OBJECT macro if derived
# from QObject

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

&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 (headers only)
if ($f =~ m/\.h$/ || $f =~ m/\.hxx$/) {
  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($classcnt) = 0;
my(%classline);
my($linecnt) = 0;
my($line);
my($lstr) = "";

# TODO: line numbering maybe incorrect if a macro is missing inside nested classes (rare case)
my($lastl)=""; #lastline
while ($linecnt < $#lines) {
    $lastl = $line;
    $line = $lines[$linecnt++];
    if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
	$line =~ m+//.*[Kk]razy:skip+) {
         $cnt = 0;
         last;
    }

    if ($line =~ m/[[:space:]]*class[[:space:]]+[[:alnum:]]+[[:space:]]*:[[:space:]]*[[:alnum:]]+[[:space:]]+QObject/) {
        next if ($lastl =~ m/template/ || $lastl =~ m/#define[[:space:]]/);
        $classcnt++;
        $classline{$classcnt}= $linecnt;
    } elsif ($line =~ m/Q_OBJECT/) {
        delete $classline{$classcnt} if exists $classline{$classcnt};
        $classcnt--;
    }
}

my (@classlinekeys) = sort keys(%classline);
foreach(@classlinekeys) {
    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $classline{$_};
    } else {
      $lstr = $lstr . "," . $classline{$_};
    }
#    print "=> $lines[$classline{$_}]" if (&verboseArg());
}

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

sub Help {
  print "Check for classes that should use the \'Q_OBJECT\' macro\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "QObject derived classes should use the Q_OBJECT macro.\nclass A : public QObject {...} becomes class A : public QObject { Q_OBJECT ... }\n";
  Exit 0 if &explainArg();
}
