#!/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) 2008-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 kDebug issues

# 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 Cwd 'abs_path';
use FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::PreProcess;
use Krazy::Utils;

my($Prog) = "kdebug";
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 (C++ only)
if (fileType($f) eq "c++"){
  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($acnt) = 0;
my($ecnt) = 0;
my($mcnt) = 0;
my($linecnt) = 0;
my($alstr) = "";
my($elstr) = "";
my($mlstr) = "";

my($line);
while ($linecnt < $#lines) {
  $line = $lines[$linecnt++];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $acnt = $ecnt = $mcnt = 0;
    last;
  }
  next if ($line =~ m/^\s*#\s*define\s*.*/);
  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  $line =~ s+//.*++;  #skip C++ comments

  #debug areas
  if ($line =~ m+k(Debug|Warning|Error)\s*\(\s*[[:digit:]]\s*+) {
    $acnt++;
    if ($acnt == 1) {
      $alstr = "line\#" . $linecnt;
    } else {
      $alstr = $alstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
  }

  #endl;
  if ($line =~ m+k(Debug|Warning|Error).*endl\s*;\s*$+) {
    $ecnt++;
    if ($ecnt == 1) {
      $elstr = "line\#" . $linecnt;
    } else {
      $elstr = $elstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
  }

  #method
  my($pline) = $line;
  $pline =~ s/QDate.*:://g;
  $pline =~ s/QDir.*:://g;
  $pline =~ s/QFile.*:://g;
  $pline =~ s/QString.*:://g;
  $pline =~ s/KGlobal.*:://g;
  $pline =~ s/KLocale.*:://g;
  $pline =~ s/KSystem.*:://g;
  if ($pline =~ m+k(Debug|Warning|Error).*::.*\(+ ||
      $pline =~ m+k(Debug|Warning|Error).*k_funcinfo+) {
    $mcnt++;
    if ($mcnt == 1) {
      $mlstr = "line\#" . $linecnt;
    } else {
      $mlstr = $mlstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
  }
}

my($total_count) = $acnt + $ecnt + $mcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  if (!&quietArg()) {
    print "using debug areas: $alstr ($acnt)" if ($acnt);
    print ", " if ($acnt && $mcnt);
    print "printing method name: $mlstr ($mcnt)" if ($mcnt);
    print ", " if (($mcnt || $acnt) && $ecnt);
    print "using endl: $elstr ($ecnt)" if ($ecnt);
    print "\n";
  }
  Exit $total_count;
}

sub Help {
  print "Check for kDebug issues\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "No need to print the method name or a trailing endl with kDebug and friends -- all that is done for you as if by magic.\n";
  Exit 0 if &explainArg();
}
