#!/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-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 classes that contain private members in a public class.
# For installed headers only; else exit "okay"

# 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($debug) = 0;  #set to go into debug mode
my($Prog) = "dpointer";
my($Version) = "1.9";

&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 ) );

#remove cpp directives
my($i)=0;
while($i <= $#lines) {
  $lines[$i] = "\n" if ($lines[$i] =~ m/^[[:space:]]*#/);
  $i++;
}

my($CNAME) = ""; #current class name
my(@classes) = ();
my(%stuff);
my($cnt) = 0;
my($ccnt) = 0;
my($mcnt) = 0;
my($linecnt) = 0;
my($lstr) = "";
my($clstr) = "";
my($mlstr) = "";
my($line) = "";

my($lastl)=""; #last line
while ($linecnt < $#lines) {
  $lastl = $line;
  $line = $lines[$linecnt++];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = $ccnt = $mcnt = 0;
    last;
  }

  $CNAME = &Cname($line,$lastl);
  if ($CNAME ne "") {
    print "($linecnt) Start Class $CNAME\n" if ($debug);

    $stuff{$CNAME}{'dpointer'} = 0;
    $stuff{$CNAME}{'excluded'} = 0;       #is this class krazy excluded?
    $stuff{$CNAME}{'section'} = "private";#default visibility in classes
    $stuff{$CNAME}{'privMembers'} = 0;    #count private members
    $stuff{$CNAME}{'privLinesList'} = ""; #list of lines with private members
    $stuff{$CNAME}{'pureVirt'} = 0;       #count pure virtuals
    $stuff{$CNAME}{'qInterfaces'} = 0;    #count Q_INTERFACES(..)
    $stuff{$CNAME}{'declarePrivate'} = 0; #count .*_DECLARE_PRIVATE(..) (eg: kdeui has its own KDEUI_DECLARE_PRIVATE)

    $stuff{$CNAME}{'excluded'} = 1 if($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);

    print " Searching for private: section\n" if ($debug);
    while ($linecnt < $#lines && $#classes >= 0) {
      $lastl = $line;
      $line = $lines[$linecnt++];
      $line =~ s+//.*++; #strip trailing C++ comment
      &Section($line);
      if (&Cname($line,$lastl)) { $linecnt--; last; }
      next if (&endClass($line,$linecnt));

      $stuff{$CNAME}{'pureVirt'}++ if (&isPureVirtual($line));
      $stuff{$CNAME}{'qInterfaces'}++ if (&isQInterfaces($line));
      $stuff{$CNAME}{'declarePrivate'}++ if (&isDeclarePrivate($line));

      if ($CNAME ne "" && defined($stuff{$CNAME}{'section'}) && $stuff{$CNAME}{'section'} eq "private" && defined($stuff{$CNAME}{'exported'}) && $stuff{$CNAME}{'exported'} == 1) {
        # we are in the private declarations
	print "  Searching for dpointer\n" if ($debug);
        while ($linecnt < $#lines) {
          $lastl = $line;
          $line = $lines[$linecnt++];
          my $krazyexclude = 0;
          if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+) {
            $krazyexclude = 1;
          }
	  $line =~ s+//.*++; #strip trailing C++ comment
	  &Section($line);
          if (&endClass($line,$linecnt)) { last; }
          next if ($line =~ m/[[:space:]]*private[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*private[[:space:]]*Q_SLOTS[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*private[[:space:]]*slots[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*protected[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*protected[[:space:]]*Q_SLOTS[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*protected[[:space:]]*slots[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*public[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*public[[:space:]]*Q_SLOTS[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*public[[:space:]]*slots[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*signals[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*k_dcop_signals[[:space:]]*:/);
          last if ($line =~ m/[[:space:]]*Q_SIGNALS[[:space:]]*:/);
	  $stuff{$CNAME}{'declarePrivate'}++ if(isDeclarePrivate($line));
          if (&Priv($line,$linecnt)) {
            # found a private member
	    print "   ($linecnt) found private member ($line)\n" if ($debug);
            $stuff{$CNAME}{'privMembers'}++;
            if ($stuff{$CNAME}{'privMembers'} == 1) {
              $stuff{$CNAME}{'privLinesList'} = $linecnt;
            } else {
              $stuff{$CNAME}{'privLinesList'} .= "," . $linecnt;
            }
            print "=> $line\n" if (&verboseArg());
          } else {
            # perhaps a non-const d-pointer
            if (($line =~ m/Private/ && $line !~ m/\(.*Private.*\)/ ||
                 $line =~ m/Priv/    && $line !~ m/\(.*Priv.*\)/) &&
                $line !~ m/class/ && $line !~ m/struct/ &&
                $line !~ m/boost::shared_ptr/ &&
                $line !~ m/std::auto_ptr/ &&
                $line !~ m/QSharedDataPointer/ && $line !~ m/QExplicitlySharedDataPointer/ && $line !~ m/KSharedPtr/ && $line !~ m/Q_DISABLE_COPY/) {
              if ($line !~ m/Private[[:alpha:]]*[[:space:]]*\*[[:space:]]*const/ &&
                  $line !~ m/Priv[[:alpha:]]*[[:space:]]*\*[[:space:]]*const/ &&
                  $line !~ m/\(\)/ &&        #d_func() stuff
                  !$krazyexclude &&          #allow non-const dpointer
		  !$stuff{$CNAME}{'excluded'} #exclude class from all checks
		 ) {
                $ccnt++;
                if ($ccnt == 1) {
                  $clstr = "non-const dpointer line\#" . $linecnt;
                } else {
                  $clstr = $clstr . "," . $linecnt;
                }
                print "=> $line\n" if (&verboseArg());
              } else {
		print "found a dpointer on line $linecnt\n" if ($debug);
		$stuff{$CNAME}{'dpointer'} = 1;
	      }
            }
          }
        } # loop over lines in private section searching for dpointer
      } # if in private
    } # loop over lines in class
  } #if in class
} #loop over each line of file

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

sub Section {
  my($l) = @_;
  if ($l =~ m/slots/i) {
    $stuff{$CNAME}{'section'} = "slot";
  } elsif ($l =~ m/signals/i) {
    $stuff{$CNAME}{'section'} = "signal";
  } elsif ($l =~ m/private\s*:/) {
    $stuff{$CNAME}{'section'} = "private";
    print "In private section of $CNAME\n" if ($debug);
  } elsif ($l =~ m/public/) {
    $stuff{$CNAME}{'section'} = "public";
  } elsif ($l =~ m/protected/) {
    $stuff{$CNAME}{'section'} = "protected";
  }
}

# determine if the current line $l has a class, checking the previous line $l1
# for classes to ignore (like "template").
# 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/);
  return 0 if ($l =~ m/_TEST_EXPORT/);
  if ($l =~ m+^[[:space:]]*class[[:space:]].*+ && $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+$//;
      if ($l =~ m/_EXPORT/) {
        $stuff{$cname}{'exported'} = 1;
      } else {
        $stuff{$cname}{'exported'} = 0;
      }
      if ($#classes < 0 || $cname ne $classes[$#classes]) {
	push(@classes,$cname);
	print "push $cname, $#classes in stack\n" if ($debug);
      }
    }
  }
  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:]]*;/);
  # This is getting ridiculous
  # TODO: do it the other way around: when we get to an opening enum or struct
  #       declaration in a private: section, skip forward to the end
  #       but be wary of things like enum { foo, bar, foobar };
  #       (and nested structs?)
  return 0 if (&searchBackWithStop('^[[:space:]]*struct[[:space:]]',$lc-1,75,
		  '^[[:space:]]*class[[:space:]]|^[[:space:]]*}[[:space:]]*;|^[[:space:]]*struct.*;|^[[:space:]]*private:'));
  # enums don't have semicolons in them;
  # unless, of course, someone puts one at
  # the end of a doxygen line (why would they?)
  return 0 if (&searchBackWithStop('^[[:space:]]*enum[[:space:]]',$lc-1,75,
		  ';[[:space:]]*$|^[[:space:]]*private:'));

  #at end of class
  my($cclass) = $classes[$#classes];
  if ($#classes >= 0) {
    &chkDptr($cclass,$l);
    &chkPrivMembers($cclass);
  }
  print "($lc) End Class $cclass ($l)\n" if ($debug);
  pop(@classes);

  if ($#classes >= 0) {
    $CNAME = $classes[$#classes];
    print "pop to class $CNAME, section $stuff{$CNAME}{'section'}, $#classes in stack\n" if ($debug);
  }
  return 1;
}

sub chkDptr {
  my($c,$l) = @_;

  if ($c ne "" && !$stuff{$c}{'excluded'} &&
      $stuff{$c}{'exported'} && !$stuff{$c}{'dpointer'}) {
    # abstract base classes do not require a d-pointer
    # fyi: abstract base classes have at least 1 pure virtual, non-dtor func
    if (!(&searchBack("Q_DECLARE_INTERFACE.*$c",$#lines-1,10) ||
	  $stuff{$c}{'pureVirt'} >= 1 || $stuff{$c}{'qInterfaces'} > 0) &&
	$stuff{$c}{'declarePrivate'} == 0) {
      $mcnt++;
      if ($mcnt == 1) {
	$mlstr = "missing dpointer in classes: " . $CNAME;
      } else {
	$mlstr = $mlstr . "," . $CNAME;
      }
      print "=> $l\n" if (&verboseArg());
    }
  }
}

sub chkPrivMembers {
  my($c) = @_;

  if ($c ne "" && !$stuff{$c}{'excluded'} && 
      defined($stuff{$c}{'qInterfaces'}) && $stuff{$c}{'qInterfaces'} == 0) {
    if ($cnt == 0) {
      $lstr = "private members line\#" . $stuff{$c}{'privLinesList'};
    } else {
      $lstr = $lstr . "," . $stuff{$c}{'privLinesList'} if ($stuff{$c}{'privLinesList'});
    }
    $cnt += $stuff{$c}{'privMembers'};
  }
}

# determine if the current line $l has a private member
sub Priv {
  my($l,$lc) = @_;
  my($args,$a1,$a2);

  return 0 if ($l =~ m+//.*[Kk]razy:exclude=.*$Prog+);

  # one-line enum declaration
  return 0 if ($l =~ m/^[[:space:]]*enum[[:space:]]/);

  $l =~ s+//.*++; #strip trailing C++ comment
  $l =~ s/\s+$//; #strip trailing whitespace
  return 0 unless(length($l));

  #we found a dpointer
  if ($l =~ m/Priv/ || $l =~ m/Private/) {
    $stuff{$CNAME}{'dpointer'} = 1;
    return 0;
  }

  #private member stuff we allow
  return 0 if ($l =~ m/[[:space:]]$CNAME[[:space:]]*\(/);  #private ctor
  return 0 if ($l =~ m/[[:space:]]~$CNAME[[:space:]]*\(/);  #private dtor
  return 0 if ($l =~ m/_DECLARE_PRIVATE/);
  return 0 if ($l =~ m/Q_OBJECT/);
  return 0 if ($l =~ m/Q_PRIVATE_SLOT/);
  return 0 if ($l =~ m/Q_DISABLE_COPY/);
  return 0 if ($l =~ m/QSharedDataPointer/);
  return 0 if ($l =~ m/QExplicitlySharedDataPointer/);
  return 0 if ($l =~ m/KSharedPtr/);
  return 0 if ($l =~ m/[[:space:]]*using[[:space:]]/); #making something inherited private
  return 0 if ($l =~ m/[[:space:]]*typedef[[:space:]]/);
  return 0 if ($l =~ m/[[:space:]]*static[[:space:]]/);
  return 0 if ($l =~ m/[[:space:]]*friend[[:space:]]/);
  return 0 if ($l =~ m/[[:space:]]*template[[:space:]]/);
  return 0 if ($l =~ m/\(/); #easy check for functions
  return 0 if ($l =~ m/^[[:space:]]*}[[:space:]]*;/); # end of an enum or struct declaration

  # search back a couple lines for start of function
  # we only need to start at the previous line, because we
  # already checked for (
  return 0 if ($l =~ m/,/ && &searchBackWithStop('\(',$lc-1,4,';'));
  return 0 if ($l =~ m/\)/ && &searchBackWithStop('\(',$lc-1,5,';'));
  return 0 if (&searchBackWithStop('enum',$lc-1,4,';')); # private enums

  #not permitted private member encountered
  return 1;
}

# determine if the current line $l has a pure virtual function.
sub isPureVirtual {
  my($l) = @_;
  $l =~ s+//.*++; #strip trailing C++ comment
  if ($l =~ m+virtual.*=[[:space:]]*0[[:space:]]*;[[:space:]]*+) {
    return 1;
  } else {
    return 0;
  }
}

# determine if the current line $l has a Q_INTERFACES()
sub isQInterfaces {
  my($l) = @_;
  $l =~ s+//.*++; #strip trailing C++ comment
  if ($l =~ m+Q_INTERFACES[[:space:]]*\(.*\)+) {
    return 1;
  } else {
    return 0;
  }
}

# determine if the current line $l has a .*_DECLARE_PRIVATE()
sub isDeclarePrivate {
  my($l) = @_;
  $l =~ s+//.*++; #strip trailing C++ comment
  if ($l =~ m+_DECLARE_PRIVATE[[:space:]]*\(.*\)+) {
    return 1;
  } else {
    return 0;
  }
}

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

# search the previous $n lines for a pattern $p
# but stop if we encounter $s
sub searchBackWithStop {
  my($p,$l,$n,$s) = @_;
  my($i);
  $n = $#lines if ($#lines < $n);
  for($i=1; $i<=$n; $i++) {
    if ($lines[$l-$i] =~ $s) {
      # stop searching
      return 0;
    }
    if ($lines[$l-$i] =~ $p) {
      # got a match
      return 1;
    }
  }
  return 0;
}

sub Help {
  print "Check public classes with private members or d-pointer issues\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "In order to more easily maintain binary compatibility, a public class in an installed header should not contain private members -- use d-pointers instead. Application headers should not mix d-pointers and private members. Also ensure  that the d-pointer is \'const\' to avoid modifying it by mistake. Please follow the guidelines in the d-pointers section of <http://techbase.kde.org/Policies/Library_Code_Policy#D-Pointers>.\n";
  Exit 0 if &explainArg();
}
