#!/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) 2006-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 methods that should be declared 'const'

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --explain:       print an explanation with solving instructions, then exit
#   --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 Getopt::Long;

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

my($help) = '';
my($version) = '';
my($explain) = '';
my($quiet) = '';
my($verbose) = '';

exit 1
if (!GetOptions('help' => \$help, 'version' => \$version,
		'explain' => \$explain,
                'verbose' => \$verbose, 'quiet' => \$quiet));

&Help() if $help;
&Version() if $version;
&Explain() if $explain;
if ($#ARGV != 0){ &Help(); exit 0; }

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

if (($f =~ m/\.h$/ || $f =~ m/\.hxx$/)) {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!$quiet);
  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($line,$cname);
my($ll);
while ($linecnt < $#lines) {
  $linecnt++;
  $line = $lines[$linecnt];

  $cname = &Cname($line,$lines[$linecnt-1]);
  if ($cname ne "") {

    while ($linecnt < $#lines) {
      # search for constness
      $linecnt++;
      $line = $lines[$linecnt];
      if (&Cname($line,$lines[$linecnt-1])) { $linecnt--; last; }
      if (&Const($line)
	  || ($line =~ m/\);[[:space:]]*/ && &constBack($linecnt,2))) {
	$cnt++;
	$ll = $linecnt+1;
	if ($cnt == 1) {
	  $lstr = "line\#" . $ll;
	} else {
	  $lstr = $lstr . "," . $ll;
	}
	print "=> $line\n" if ($verbose);
      }
    }
  }
}
close(F);

if (!$cnt) {
  print "okay\n" if (!$quiet);
  exit 0;
} else {
  print "$lstr ($cnt)\n" if (!$quiet);
  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
  return 0 if ($l =~ m/_EXPORT_DEPRECATED/);
  if ($l =~ m+^[[:space:]]*class[[:space:]].*+ && $l !~ m/;$/) {
    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+$//;
    }
  }
  return $cname;
}

sub Const() {
  my($l) = @_;
  my($sl) = $l;

  return 0 if ($l =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  return 0 if ($l =~ m+//.*const+);

  $l =~ s+//.*++;  #skip C++ comments
  $l =~ s/\s+$//;  #strip trailing whitespace
  $l =~ s/^\s+//;  #strip leading whitespace

  $l =~ s/\([[:space:]]*\)//;
  $l =~ s/{.*$//;

  return 0 unless (length($l));
  return 0 if ($l =~ m/^#/);
  return 0 if ($l =~ m/operator/);
  return 0 if ($l =~ m/=[[:space:]]*0[[:space:]]*;/);

  my(@args);
  my($type,$const);
  if ($l =~ m+\(+) {
    (@args) = split(" ",$l);
    $type = $args[0];
    $const = $args[$#args];
    return 0 if($const =~ m/const/);

    if ($type =~ m/KDE_DEPRECATED/ || $type =~ m/KDE_CONSTRUCTOR_DEPRECATED/) {
      shift @args;
      $type = $args[0];
    }
    if ($type eq "const" || $type eq "inline") {
      shift @args;
      $type = $args[0];
    }

    return 0 if ($type =~ m/\(/);
    return 0 if ($type eq "explicit");
    return 0 if ($type eq "static");
    return 0 if ($type eq "virtual");
    return 0 if ($type eq "void");
    return 0 if ($type =~ m/List/);
    return 0 if ($type =~ m/\*$/);

    return 0 if ($args[1] =~ m/^\*/);

    return 1;
  }
  return 0;
}

# search the previous $n lines from line $lc for necessary constness
sub constBack {
  my($lc,$n) = @_;
  my($i);
  return 0 if ($lc<$n);
  for($i=1; $i<=$n; $i++) {
    if (&Const($lines[$lc-$i])) {
      return 1;
    }
  }
  return 0;
}

sub Help {
  print "[TEST] Check for class methods that should be declared \'const\'\n";
  exit 0 if $help;
}

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

sub Explain {
  print "As a general rule methods should be const except when it's not possible to make them such. See <http://developer.kde.org/~wheeler/cpp-pitfalls.html>.\n";
  exit 0 if $explain;
}
