#!/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.               #
#                                                                             #
###############################################################################

#TODO: Handle nested classes

# Tests KDE source for multiple public, visible classes in a header file.

# 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::Utils;

my($Prog) = "multiclasses";
my($Version) = "1.1";

&parseArgs();

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

# Check Condition
my($f) = $ARGV[0];

if (($f =~ m/\.h$/ || $f =~ m/\.hxx$/) && $f !~ m+/tests/+) {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!&quietArg());
  Exit 0;
}

#open file and slurp it in
open(F, "$f") || die "Couldn't open $f";
my(@data_lines) = <F>;
close(F);

#get all the c-style comments from the file
my($data)="@data_lines";
my(@comments) = ($data =~ /\/\*.*?\*\//gs);

#for each comment, remove everything but the linebreaks, so
#our line numbering report does not get screwed up.
foreach my $comment ( @comments ) {
        my($fixed_comment) = $comment;
        $fixed_comment =~ s/[^\n]//gs;
        $fixed_comment =~ s/\n/\n/gs;
        $data =~ s/\Q$comment/$fixed_comment/s;
}

#put it back into an array so we can iterate over it
my(@lines) = split(/\n/, $data);

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

  $cname = &Cname($line,$lines[$linecnt-1]);
  if ($cname ne "" && $line !~ m+//.*[Kk]razy:exclude=.*$Prog+) {
    my($lt) = $linecnt+1;
    $cnt++;
    if ($cnt > 1) {
      if ($cnt == 2) {
	$lstr = "line\#" . $lt;
      } else {
	$lstr = $lstr . "," . $lt;
      }
      print "($lt) => $line\n" if (&verboseArg());
    }
  }
}
close(F);

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


# determine if the current line $l has a class, checking the previous line $l1
# for classes to ignore (like "template").
# return the class name, or empty if no class is found
sub Cname {
  my($l,$l1) = @_;
  my($cname)="";
  $l =~ s+//.*++; #strip trailing C++ comment
  if ($l =~ m+^[[:space:]]*class[[:space:]].*+ && $l !~ m/;\s*$/ && $l !~ m/\\\s*$/) {
    if ($l1 !~ m/template/ && $l1 !~ m/#define[[:space:]]/) {
      $cname = $l;
      $cname =~ s/:.*$//;
      $cname =~ s/{.*$//;
      $cname =~ s/[[:space:]]*class[[:space:]].*EXPORT[[:space:]]//;
      $cname =~ s/[[:space:]]*class[[:space:]]//;
      $cname =~ s/\s+$//;
    }
  }

  #looks like we found a class $cname on line $l1.
  #but is it public?
  $cname="" if ($l !~ m/_EXPORT/);

  #but have we found an exclude directive?
  $cname="" if ($l =~ m+//.*[Kk]razy:exclude=.*$Prog+);

  return $cname;
}

# search the previous $n lines for a pattern $p
sub searchBack {
  my($p,$l,$n) = @_;
  my($i);
  $n = $#lines if ($#lines < $n);
  for($i=1; $i<=$n; $i++) {
    if ($lines[$l-$i] =~ $p) {
      return 1;
    }
  }
  return 0;
}

sub Help {
  print "Check for multiple public classes in a C++ header\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "According to policy, a C++ header file should contain only 1 publicly visible class.\n";
  Exit 0 if &explainArg();
}
