#!/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 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 foreach loop problems.

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

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

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

  if ($line =~ m/^\s*(foreach|Q_FOREACH)\s*\(/) {

     if ($line =~ m/[\.>]values\(\)\s*\)/ ||
         $line =~ m/[\.>]keys\(\)\s*\)/) {
       $vcnt++;
       if ($vcnt == 1) {
         $vlstr = "values or keys iteration line\#" . $linecnt;
       } else {
         $vlstr = $vlstr . "," . $linecnt;
       }
       print "=> $line\n" if (&verboseArg());
     }

    next if ($line =~ m/\(\s*const\s[:\w]+\s*&\s*\w+\s*,/);
    next if ($line =~ m/\(\s*(const\s)?[:\w]+\s*\*(\s*const\s)?\s*\w+\s*,/);
    next if ($line =~ m/\(\s*(const\s)?(T|int|uint|uint32_t|qint8|quint8|qint16|quint16|qint32|quint32|quint64|qint64|unsigned|unsigned int|unsigned long|GLint|GLuint|ulong|long|float|double|qreal|unsigned char|char|QChar)\s\w+\s*,/);
    next if ($line =~ m/\(\s*(const\s)?(QList|QLinkedList|QPointer|QSharedPointer|QSharedDataPointer|KSharedPtr|QVector|QMap|QSet|shared_ptr|ptr)\s*<[\s\w\*:]+>\s*[&\*]?\s*[:\w]+\s*,/);
    next if ($line =~ m/\(\s*(const\s)?[:\w]+(Ptr|Info|Information|Error|Type|Id)\s\w+\s*,/ &&
             $line !~ m/\(\s*(QSqlError)\s\w+\s*,/);
    next if ($line =~ m/\(\s*(::)?(Types)::\w+\s\w+\s*,/);
    next if ($line =~ m/\(\s*(const\s)?typename\sT/);
    next if ($line =~ m/\(\s*[:\w]+::(List|const_reference|value_type)\s\w+\s*,/);
    # known enums. not much else to do except hard-code them as we find 'em.
    next if ($line =~ m/\(\s*(ElectricBorder|Direction)\s\w+\s*,/);

    # see if the iterator variable is declared a pointer a couple lines above
    if ($line =~ m/(foreach|Q_FOREACH)\s*\(\s*(\w+)\s*,/) {
      my($pat) = "\\s*\\*\\s*" . $2 . "\\s*";
      next if(&searchBack($pat,$linecnt,12));
    }

    $cnt++;
    if ($cnt == 1) {
      $lstr = "non-const ref iterator line\#" . $linecnt;
    } else {
      $lstr = $lstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
  }
}


my($total_count) = $cnt + $vcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  $lstr =~ s/,$//;
  if (!&quietArg()) {
    print "$lstr ($cnt)\n" if ($cnt);
    print "$vlstr ($vcnt)\n" if ($vcnt);
  }
  Exit $total_count;
}

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

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

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

sub Explain {
  print "When not using POD types (int, double, pointer, ...) you should use const & for your foreach variables. There are two reasons for this: 1) Prevents you from the mistake of writing foreach loops that modify the list, that is \'foreach(Foo f, list) f.a = f.b = f.c = 0;\' compiles but does not modify the contents of list 2) Saves a copy constructor call for each of the list elements\nDo not call values in Hashes/Maps/Sets inside foreach loops.\nRead <http://tsdgeos.blogspot.com/2009/04/how-to-make-foreach-loops-that-dont.html> for more information about iterating over QHash values and keys.";
  Exit 0 if &explainArg();
}
