#!/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) 2009-2010 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 crashy code.

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

&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($linecnt) = 0;
my($cnt) = 0;
my($lstr) = "";

my($var,$pat,$expat1,$expat2,$expatp);
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

  # Check Condition
  if ($line =~ m/exec\(\s*\)/) {
    $var = $line;
    $var =~ s/->.*$//;
    $var =~ s/\..*$//;
    $var =~ s/^\s*if\s*\(\s*//;
    $var =~ s/^.*\s*=\s*//;
    $var =~ s/^\s*//;
    $var =~ s/!//;

    next if ($var =~ m/^return/);
    next if ($var =~ m/^exit/);
    next if ($var =~ m/^app/);
    $pat = "\\*\\s*" . $var . "\\s*=\\s*new\\s*\\w+\\(";
    if ($line =~ m/$var\.exec/ || &searchBack($pat,$linecnt,15)) {
      # don't complain if the exec is the last statement in a method
      next if ( $lines[$linecnt] =~ m/^\s{1,2}\}/); #ugly HACK to detect end-of-method
      $expat1 = "(QSqlQuery|KApplication|.*Job|.*App)\\s&?" . $var;
      $expat2 = $var . "\\s*=\\s*new\\s(QSqlQuery|KApplication|.*Job|.*App)";
      $expatp = "\\w\+\\s" . $var . "\\s*;";  #parentless, on the stack
      if (!&searchBack($expat1,$linecnt,25) &&
          !&searchBack($expat2,$linecnt,25) &&
          !&searchBack($expatp,$linecnt,15)) {
        $cnt++;
        if ($cnt == 1) {
          $lstr = "line\#" . $linecnt;
        } else {
          $lstr = $lstr . "," . $linecnt;
        }
        print "=> $line\n" 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 code that should be considered crashy.\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "Looks for not using QPointers when showing modal dialogs via exec(), as discussed in <http://www.kdedevelopers.org/node/3919>\n";
  Exit 0 if &explainArg();
}

# 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;
}
