#!/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) 2007-2008 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 methods in public, exported classes that return
# const references.

# 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) = "constref";
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 (headers only)
if (&installedArg() && ($f =~ m/\.h$/ || $f =~ m/\.hxx$/)) {
  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($linecnt) = 0;
my($lstr) = "";
my($cname) = "";
my($cref) = "";
my($line);
while ($linecnt < $#lines) {
  $linecnt++;
  $line = $lines[$linecnt];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = 0;
    last;
  }

  $cname = &Cname($line,$lines[$linecnt-1]);
  if ($cname ne "") {
    #found a public, exported class
    $cref = "";

    while ($linecnt < $#lines) {
      # search for a methods returning const references
      $linecnt++;
      $line = $lines[$linecnt];
      if (&Cname($line,$lines[$linecnt-1])) { $linecnt--; last; }
      last if (&endClass($line,$linecnt));
      next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
      if ($line =~ m/^[[:space:]]*const[[:space:]].*\&[[:space:]]*[[:alnum:]]+[[:space:]]*\(/ ||
          $line =~ m/^[[:space:]]*static[[:space:]]const[[:space:]].*\&[[:space:]]*[[:alnum:]]+[[:space:]]*\(/) {
        # we found a possible problem method
        $cref = $line;
        $linecnt++;

        #look for such a method (that isn't private)
        if (&ConstRef($cref) &&
            !&searchBack("private:", $linecnt-1, 100)) {
          # found one
          $cnt++;
          if ($cnt == 1) {
            $lstr = "line\#" . $linecnt;
          } else {
            $lstr = $lstr . "," . $linecnt;
          }
          print "($linecnt) => $line\n" if (&verboseArg());
        }
      }
    }
  }
}

if (!$cnt) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "$lstr ($cnt)\n" if (!&quietArg());
  Exit $cnt;
}

# determine if the current line $l has a class, checking the previous line $l1
# for classes to ignore (like "template").  Must be a FOO_EXPORTed class.
# return the class name, or empty if no class is found
sub Cname {
  my($l,$l1) = @_;
  my($cname)="";
  $l =~ s+//.*++; #strip trailing C++ comment
  return 0 if ($l =~ m/_EXPORT_DEPRECATED/);
  if ($l =~ m+^[[:space:]]*class[[:space:]].*+ && $l =~ m/_EXPORT/ && $l !~ m/;\s*$/ && $l !~ m/\\\s*$/) {
    if ($l1 !~ m/template/ && $l1 !~ m/#define[[:space:]]/) {
      $cname = $l;
      $cname =~ s/:.*$//;
      $cname =~ s/{.*$//;
      $cname =~ s/[[:space:]]*class[[:space:]].*EXPORT[[:space:]]//;
      $cname =~ s/[[:space:]]*class[[:space:]]//;
      $cname =~ s/\s+$//;
    }
  }
  return $cname;
}

# determine if the current line marks the end of a class
sub endClass {
  my($l,$lc) = @_;
  return 0 if ($l !~ m/^[[:space:]]*}[[:space:]]*;/);
  return 0 if (&searchBack('enum',$lc,5));
  return 1;
}

# determine if the current line $l has a method that returns a const reference
sub ConstRef {
  my($l) = @_;
  #return 0 for conditions where const ref returns are ok
  return 1;
}

# 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 methods that return 'const' refs in public classes\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "For binary compatibility reasons avoid const references for return types. For example, \"const QList<int> &someProperty() const\" should be rewritten to return a value instead, eg \"QList<int> someProperty() const\". See <http://techbase.kde.org/Policies/Library_Code_Policy#Const_References> for more details.\n";
  Exit 0 if &explainArg();
}
