#!/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-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 adding a single char string to a QString, or other
# cases where QString containing a single char is better as a QChar.
# For example: QString str += "/"; should be QString += '/';

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

&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/\.cc$/ || $f =~ m/\.cxx$/) {
  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($scnt) = 0; #QString::startsWith(), QString::endsWith() issues
my($rcnt) = 0; #QString::replace() issues
my($xcnt) = 0; #QString::remove() issues
my($tcnt) = 0; #QString::section() issues
my($spcnt) = 0; #QString::split() issues
my($linecnt) = 0;
my($line);
my($lstr) = "";
my($slstr) = "";
my($rlstr) = "";
my($xlstr) = "";
my($tlstr) = "";
my($splstr) = "";
foreach $line (@lines) {
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = $scnt = $rcnt = $xcnt = $tcnt = $spcnt = 0;
    last;
  }

  $linecnt++;
  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  $line =~ s+//.*++;  #skip C++ comments

  if ($line =~ m/\+\s*\"[[:print:]]\"/ ||
      $line =~ m/\"[[:print:]]\"\s*\+/ ||
      $line =~ m/[[:print:]]\s*=\s*\"[[:print:]]\"/ ||
      $line =~ m/\+=\s*\"[[:print:]]\"/) {
    next if ($line =~ m/\\\"[[:print:]]\"/);
    next if ($line =~ m/\"[[:print:]]\\\"/);
    next if ($line =~ m/\"\+\",\"/ || $line =~ m/\",\"\+\"/); # "+","x
    next if ($line =~ m/\"\\\"/);
    next if ($line =~ m/\"[[:print:]]\"\s[A-Z]/);
    next if ($line =~ m/\(\s*\)\s*\+=/);  #skip foo()+=
    next if ($line =~ m/[[:print:]]\s*[=!]=\s*\"/);
    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $linecnt;
    } else {
      $lstr = $lstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
    next;
  }
  if ($line =~ m/\+\s*\"\\n\"/ ||
      $line =~ m/\"\\n\"\s*\+/ ||
      $line =~ m/\+=\s*\"\\n\"/) {
    next if ($line =~ m/\\\"\\n\"/);
    next if ($line =~ m/\"\\n\\\"/);
    next if ($line =~ m/\"\\\"/);
    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $linecnt;
    } else {
      $lstr = $lstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
    next;
  }
  if ($line =~ m/\+\s*\"\\t\"/ ||
      $line =~ m/\"\\t\"\s*\+/ ||
      $line =~ m/\+=\s*\"\\t\"/) {
    next if ($line =~ m/\\\"\\t\"/);
    next if ($line =~ m/\"\\t\\\"/);
    next if ($line =~ m/\"\\\"/);
    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $linecnt;
    } else {
      $lstr = $lstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
    next;
  }
  my ($pline) = $line;
  $pline =~ s/\\\"/ /g; # remove quoted stuff
  $pline =~ s/QString\(\s*\)//g;

  #QString::startsWith() and QString::endsWith() checks
  if ($pline =~ m/startsWith\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/startsWith\s*\(\s*\"\\n\"/ ||
      $pline =~ m/startsWith\s*\(\s*\"\\t\"/ ||
      $pline =~ m/startsWith\s*\(\s*\"\\r\"/ ||
      $pline =~ m/startsWith\s*\(\s*\"\\\"\"/ ||
      $pline =~ m/endsWith\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/endsWith\s*\(\s*\"\\n\"/ ||
      $pline =~ m/endsWith\s*\(\s*\"\\t\"/ ||
      $pline =~ m/endsWith\s*\(\s*\"\\r\"/ ||
      $pline =~ m/endsWith\s*\(\s*\"\\\"\"/) {
    $scnt++;
    if ($scnt == 1) {
      $slstr = "starts/endsWith issues line\#" . $linecnt;
    } else {
      $slstr = $slstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
    next;
  }

  #QString::replace() checks
  if ((($pline =~ m/replace\s*\(.*,\s*\"\"/ || $pline =~ m/replace\s*\(.*,\s*QString\(\s*\)/ ) &&
       $pline !~ m/\(.*,\s*\d*,.*\)/) ||
      $pline =~ m/replace\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/replace\s*\(\s*\"\\n\"/ ||
      $pline =~ m/replace\s*\(\s*\"\\t\"/ ||
      $pline =~ m/replace\s*\(\s*\"\\r\"/ ||
      $pline =~ m/replace\s*\(\s*\"\\\"\"/) {
    if ($pline !~ m/:replace/) {
      $rcnt++;
      if ($rcnt == 1) {
	$rlstr = "replace issues line\#" . $linecnt;
      } else {
	$rlstr = $rlstr . "," . $linecnt;
      }
      print "=> $line\n" if (&verboseArg());
      next;
    }
  }

  #QString::remove() checks
  if ($pline =~ m/remove\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/remove\s*\(\s*\"\\n\"/ ||
      $pline =~ m/remove\s*\(\s*\"\\t\"/ ||
      $pline =~ m/remove\s*\(\s*\"\\r\"/ ||
      $pline =~ m/remove\s*\(\s*\"\\\"\"/) {
    if ($pline !~ m/:remove/) {
      $xcnt++;
      if ($xcnt == 1) {
	$xlstr = "remove issues line\#" . $linecnt;
      } else {
	$xlstr = $xlstr . "," . $linecnt;
      }
      print "=> $line\n" if (&verboseArg());
      next;
    }
  }

  #QString::section() checks
  if ($pline =~ m/section\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/section\s*\(\s*\"\\n\"/ ||
      $pline =~ m/section\s*\(\s*\"\\t\"/ ||
      $pline =~ m/section\s*\(\s*\"\\r\"/ ||
      $pline =~ m/section\s*\(\s*\"\\\"\"/) {
    if ($pline !~ m/:section/) {
      $tcnt++;
      if ($tcnt == 1) {
	$tlstr = "section issues line\#" . $linecnt;
      } else {
	$tlstr = $tlstr . "," . $linecnt;
      }
      print "=> $line\n" if (&verboseArg());
      next;
    }
  }

  #QString::split() checks
  if ($pline =~ m/split\s*\(\s*\"[[:print:]]\"/ ||
      $pline =~ m/split\s*\(\s*\"\\n\"/ ||
      $pline =~ m/split\s*\(\s*\"\\t\"/ ||
      $pline =~ m/split\s*\(\s*\"\\r\"/ ||
      $pline =~ m/split\s*\(\s*\"\\\"\"/) {
    if ($pline !~ m/:split/) {
      $spcnt++;
      if ($spcnt == 1) {
	$splstr = "split issues line\#" . $linecnt;
      } else {
	$splstr = $splstr . "," . $linecnt;
      }
      print "=> $line\n" if (&verboseArg());
      next;
    }
  }
}

my($total_count) = $cnt + $scnt + $rcnt + $xcnt + $tcnt + $spcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  if (!&quietArg()) {
    print "$lstr ($cnt)\n" if ($cnt);
    print "$slstr ($scnt)\n" if ($scnt);
    print "$rlstr ($rcnt)\n" if ($rcnt);
    print "$xlstr ($xcnt)\n" if ($xcnt);
    print "$tlstr ($tcnt)\n" if ($tcnt);
    print "$splstr ($spcnt)\n" if ($spcnt);
  }
  Exit $total_count;
}

sub Help {
  print "Check single-char QString operations for efficiency\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "Adding single characters to a QString is faster if the characters are QChars and not QStrings.  For example: QString path = oldpath + \"/\" + base is better written as QString path = oldpath + \'/\' + base. Same holds for arguments to QString::startsWith(), QString::endsWith(), QString::remove(), QString::section(), and QString::split(). Use QString::remove() instead of QString::replace(foo,\"\")\n";
  Exit 0 if &explainArg();
}
