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

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

&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 ) );
# Remove Krazy conditional blocks
@lines = RemoveCondBlockC( $Prog, @lines );

my($dcnt) = 0;
my($linecnt) = 0;
my($line,$pline);
my($dlstr) = "";
foreach $line (@lines) {
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $dcnt = 0;
    last;
  }

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

  next if ($line =~ m+^\s*#+);
  next if ($line =~ m+^\s*extern+);

  $pline = $line;
  $pline =~ s/\'\"\'//g;

  if ($pline =~ m:\":) {
    next if ($pline =~ m+[\s\(](QLatin1String|QString)\s*\(+);
    next if ($pline =~ m+[\s\(](tr|i18n|i18nc|i18np|i18ncp)\s*\(+);

    next if ($pline =~ m+[\s\(](ki18n|ki18nc|ki18np|ki18ncp)\s*\(+);
    next if ($pline =~ m+[\s\(](qFatal|qError|qWarning|qDebug)\s*\(+);
    next if ($pline =~ m+[\s\(](kFatal|kError|kWarning|kDebug)\s*\(+);
    next if ($pline =~ m+[\s](Q_ASSERT|Q_ASSERT_X)\s*\(+);
    next if ($pline =~ m+[\s](QRegExp)\s*\(+);
    next if ($pline =~ m+[\s\(\.](printf|fprintf|vsprintf|sprintf|scanf|fscanf|sscanf)\s*\(+);
    next if ($pline =~ m+[\s\(](strcmp|strncmp)\s*\(+);
    next if ($pline =~ m+[\s](qgetenv|setenv)\s*\(+);
    next if ($pline =~ m+[\s\(](fopen|popen)\s*\(+);

    next if ($pline =~ m+\sreturn\s\"+);
    next if ($pline =~ m+\schar\s*\*+);
    next if ($pline =~ m+(<<|=)\s*\"+ && $pline !~ m+\".*(<<|=).*\"+);
    next if ($pline =~ m/(\+|\+=)\s*\"/);
    next if ($pline =~ m/^\s*\"/);
    next if ($pline =~ m/(QString|QLatin1String|QRegExp)\s\w+\s*\(\s*\"/);

    # methods that have const char * as arg1
    next if ($pline =~ m/(addAction|QPixmap|setObjectName|inherits|fromAscii|fromLatin1|fromLocal8Bit|readEntry|readPathEntry|writeEntry|writePathEntry|setCodec|locateLocal|fromUtf8|fromUtf16|[Ii]con|[Gg]roup)\s*\(\s*\"/);

    # Even thought append/prepend would be faster with QLatin1String's, this
    # would lead to thousands and thousands of issues. We'd also need to
    # check +=, <<, and >> operators.
    next if ($pline =~ m/(append|prepend)\s*\(\s*\"/);

    # For now, we really only care about a few QString methods
    next unless ($pline =~ m/(startsWith|endsWith)\s*\(\s*\"/);

    # TODO: find cases where i18n() should be used

    $dcnt++;
    if ($dcnt == 1) {
      $dlstr = "QLatin1String issues line\#" . $linecnt;
    } else {
      $dlstr = $dlstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
    next;
  }
}

my($total_count) = $dcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  if (!&quietArg()) {
    print "$dlstr ($dcnt)\n" if ($dcnt);
  }
  Exit $total_count;
}

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 strings used improperly or should be i18n.\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "Some QString methods (like startsWith() and endsWith()) are more efficient if they are passed a QLatin1String, avoiding an implicit conversion from const char *.\n";
  Exit 0 if &explainArg();
}
