#!/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-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 incorrect use of cpp macros

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

&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
if (&installedArg()) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  open(F, "$f") || die "Couldn't open $f";
}
my(@data_lines) = <F>;
close(F);

# Remove C-style comments and #if 0 blocks from the file input
my(@lines) = RemoveIfZeroBlockC( RemoveCommentsC( @data_lines ) );

# init
my($cnt) = 0;
my($hcnt) = 0;
my($wcnt) = 0;
my($linecnt) = 0;
my($line);
my($lstr) = "";
my($hstr) = "";
my($wstr) = "";

# a list of all known HAVE_FOO macros found by looking for cmakedefines
# in all the *.cmake files we could find. most likely incomplete and
# in need of constant updates.
my %have_map = map {($_) => 1} qw {
HAVE_ARC4RANDOM
HAVE_ASSUAN_FD_T
HAVE_ASSUAN_INQUIRE_EXT
HAVE_ASSUAN_SOCK_GET_NONCE
HAVE_AUTH_TIMEOK
HAVE_BACKTRACE
HAVE_BER_MEMFREE
HAVE_BZIP2_SUPPORT
HAVE_C99_INITIALIZERS
HAVE_CAPTURY
HAVE_CDDA_IOCTL_DEVICE
HAVE_CFSETSPEED
HAVE_CKCONNECTOR
HAVE_CLUCENE
HAVE_CRYPT
HAVE_DIRENT_D_TYPE
HAVE_DPMS
HAVE_DPMSCAPABLE_PROTO
HAVE_DPMSINFO_PROTO
HAVE_DRAND48
HAVE_EIGEN
HAVE_EXIV2
HAVE_FACILE
HAVE_FADVISE
HAVE_FAM
HAVE_FGETGRENT
HAVE_FGETPWENT
HAVE_FGETSPENT
HAVE_FONTCONFIG
HAVE_FOPEN64
HAVE_FREEADDRINFO
HAVE_FREETYPE
HAVE_FSEEKO64
HAVE_FSTAT64
HAVE_FTELLO64
HAVE_FTRUNCATE64
HAVE_FUNC_FINITE
HAVE_FUNC_ISINF
HAVE_FUNC_ISNAN
HAVE_FUNC_POSIX_MEMALIGN
HAVE_GAI_STRERROR_PROTO
HAVE_GETADDRINFO
HAVE_GETDOMAINNAME
HAVE_GETHOSTBYNAME
HAVE_GETHOSTBYNAME2
HAVE_GETHOSTBYNAME2_R
HAVE_GETHOSTBYNAME_R
HAVE_GETHOSTNAME
HAVE_GETHOSTNAME_PROTO
HAVE_GETIFADDRS
HAVE_GETLOADAVG
HAVE_GETMNTINFO
HAVE_GETNAMEINFO
HAVE_GETPAGESIZE
HAVE_GETPASSPHRASE
HAVE_GETPEEREID
HAVE_GETPROTOBYNAME_R
HAVE_GETPT
HAVE_GETSERVBYNAME_R
HAVE_GETSERVBYNAME_R_PROTO
HAVE_GETSERVBYPORT_R
HAVE_GETSPNAM
HAVE_GETTIMEOFDAY
HAVE_GETUSERSHELL
HAVE_GL
HAVE_GLIB2
HAVE_GLXCHOOSEVISUAL
HAVE_GNU_INLINE_ASM
HAVE_GPG_ERR_ALREADY_SIGNED
HAVE_GPG_ERR_NO_PASSPHRASE
HAVE_GPGME_ASSUAN_ENGINE
HAVE_GPGME_CANCEL_ASYNC
HAVE_GPGME_CTX_GETSET_ENGINE_INFO
HAVE_GPGME_DATA_SET_FILE_NAME
HAVE_GPGME_DECRYPT_RESULT_T_FILE_NAME
HAVE_GPGME_DECRYPT_RESULT_T_RECIPIENTS
HAVE_GPGME_ENCRYPT_NO_ENCRYPT_TO
HAVE_GPGME_ENGINE_INFO_T_HOME_DIR
HAVE_GPGME_G13_VFS
HAVE_GPGME_GET_FDPTR
HAVE_GPGME_INCLUDE_CERTS_DEFAULT
HAVE_GPGME_KEYLIST_MODE_EPHEMERAL
HAVE_GPGME_KEYLIST_MODE_SIG_NOTATIONS
HAVE_GPGME_KEY_SIG_NOTATIONS
HAVE_GPGME_KEY_T_IS_QUALIFIED
HAVE_GPGME_OP_GETAUDITLOG
HAVE_GPGME_OP_IMPORT_KEYS
HAVE_GPGME_PROTOCOL_GPGCONF
HAVE_GPGME_SIGNATURE_T_ALGORITHM_FIELDS
HAVE_GPGME_SIGNATURE_T_PKA_FIELDS
HAVE_GPGME_SIG_NOTATION_CLEARADDGET
HAVE_GPGME_SIG_NOTATION_CRITICAL
HAVE_GPGME_SIG_NOTATION_FLAGS_T
HAVE_GPGME_SIG_NOTATION_HUMAN_READABLE
HAVE_GPGME_SUBKEY_T_IS_CARDKEY
HAVE_GPGME_SUBKEY_T_IS_QUALIFIED
HAVE_GPGME_VERIFY_RESULT_T_FILE_NAME
HAVE_GRANTPT
HAVE_IF_NAMETOINDEX
HAVE_INET_ATON
HAVE_INET_NTOP
HAVE_INET_PTON
HAVE_INITGROUPS
HAVE_INITGROUPS_PROTO
HAVE_INOTIFY
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ESYSTEMWATCHER
HAVE_RANDOM
HAVE_RANDOM_PROTO
HAVE_READDIR_R
HAVE_RES_INIT
HAVE_RES_INIT_PROTO
HAVE_REVOKE
HAVE_SENDFILE
HAVE_SETENV
HAVE_SETENV_PROTO
HAVE_SETEUID
HAVE_SETLOGIN
HAVE_SETMNTENT
HAVE_SETPRIORITY
HAVE_SETPROCTITLE
HAVE_SETUSERCONTEXT
HAVE_SHORTSETGROUPS
HAVE_SIGACTION
HAVE_SIGSET
HAVE_S_ISSOCK
HAVE_SLP
HAVE_SND_PCM_RESUME
HAVE_SOPRANO_INDEX
HAVE_SRANDOM
HAVE_SRANDOM_PROTO
HAVE_STAT64
HAVE_STATFS
HAVE_STATVFS
HAVE_STATVFS64
HAVE_STRCASESTR
HAVE_STRCASESTR_PROTO
HAVE_STRCMP
HAVE_STRDUP
HAVE_STRIGIDBUS
HAVE_STRIGI_SOPRANO_BACKEND
HAVE_STRLCAT
HAVE_STRLCAT_PROTO
HAVE_STRLCPY
HAVE_STRLCPY_PROTO
HAVE_STRNLEN
HAVE_STRRCHR
HAVE_STRSIGNAL
HAVE_STRTOLL
HAVE_STRUCT_ADDRINFO
HAVE_STRUCT_PASSWD_PW_EXPIRE
HAVE_STRUCT_SOCKADDR_IN6
HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
HAVE_STRUCT_SOCKADDR_SA_LEN
HAVE_STRUCT_TM_TM_ZONE
HAVE_STRUCT_UCRED
HAVE_STRUCT_UTMP_UT_HOST
HAVE_STRUCT_UTMP_UT_ID
HAVE_STRUCT_UTMP_UT_PID
HAVE_STRUCT_UTMP_UT_SESSION
HAVE_STRUCT_UTMP_UT_SYSLEN
HAVE_STRUCT_UTMP_UT_TYPE
HAVE_STRUCT_UTMP_UT_USER
HAVE_SYSINFO
HAVE_KSTATUSNOTIFIERITEM
HAVE_TESTS_ENABLED
HAVE_TIDY_ULONG_VERSION
HAVE_TIMEZONE
HAVE_TM_GMTOFF
HAVE_TRUNC
HAVE_TUNEPIMP
HAVE_UNAME
HAVE_UNLOCKPT
HAVE_UNSETENV
HAVE_UNSETENV_PROTO
HAVE_UPDWTMP
HAVE_USABLE_ASSUAN
HAVE_USLEEP
HAVE_USLEEP_PROTO
HAVE_UTEMPTER
HAVE_UTMPX
HAVE_VOLMGT
HAVE_VORBIS
HAVE_VSNPRINTF
HAVE_VSYSLOG
HAVE_WEBKITKDE
HAVE_WEBVIEW
HAVE_X86_MMX
HAVE_X86_SSE
HAVE_X86_SSE2
HAVE_XCOMPOSITE
HAVE_XCURSOR
HAVE_XDAMAGE
HAVE_XF86MISC
HAVE_XF86MISCSETGRABKEYSSTATE
HAVE_XFIXES
HAVE_XINERAMA
HAVE_XKB
HAVE_XMMS
HAVE_XPLANET
HAVE_XRANDR
HAVE_XRENDER
HAVE_XRES
HAVE_XSCREENSAVER
HAVE_XSHM
HAVE_XSHMGETEVENTBASE
HAVE_XSLT
HAVE_XSYNC
HAVE_XTEST
HAVE_MMX
HAVE_X86_MMX
HAVE_SSE
HAVE_SSE2
HAVE__GETPTY
HAVE___PROGNAME
HAVE___PROGNAME_FULL
HAVE_LIBXML2
HAVE_PULSEAUDIO
HAVE_PCIUTILS
HAVE_I8K_SUPPORT
HAVE_XKLAVIER
HAVE_XINPUT
HAVE_LIBFLAC
HAVE_XINE
HAVE_WEBKITKDE
HAVE_KONQUEROR
HAVE_SQLITE
HAVE_KWORKSPACE
HAVE_SNPRINTF
HAVE_STRCASECMP
HAVE_STRNCASECMP
HAVE_KSTAT
HAVE_IONICE
HAVE_VTS
};

# Check Condition
foreach $line (@lines) {
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = 0;
    last;
  }

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

  if ($line =~ m/^[[:space:]]*#[[:space:]]*if[[:space:]][_[:alnum:]]/ ||
      $line =~ m/^[[:space:]]*#[[:space:]]*ifdef[[:space:]][_[:alnum:]]/ ||
      $line =~ m/^[[:space:]]*#[[:space:]]*ifndef[[:space:]][_[:alnum:]]/ ||
      $line =~ m/^[[:space:]]*#[[:space:]]*elif[[:space:]][_[:alnum:]]/) {
    next if ($line =~ m/if[[:space:]][01]$/);
    next if ($line =~ m/ifndef[[:space:]].*_H/);
    next if ($line =~ m/DEBUG/ || $line =~ m/VERBOSE/);
    next if ($line =~ m/QT/);
    next if ($line =~ m/KDE/);
    next if ($line =~ m/EXPORT/);
    if (&osMacros($line,$lines[$linecnt]) || &compilerMacros($line,$lines[$linecnt])) {
      my($res) = &haves($line);
      $cnt++;
      if ($cnt == 1) {
        $lstr = "O/S or Compiler specific macro line\#" . $linecnt . "[$res]";
      } else {
        $lstr = $lstr . "," . $linecnt . "[$res]";
      }
      print "=> $line\n" if (&verboseArg());
    }

    my(@res) = &knownHaveMacro($line,$lines[$linecnt]);
    if ($#res >= 0) {
      $hcnt++;
      if ($hcnt == 1) {
        $hstr = "Unknown HAVE_FOO macro line\#" . $linecnt . "[@res]";
      } else {
        $hstr = $hstr . "," . $linecnt . "[@res]";
      }
      print "=> $line\n" if (&verboseArg());
    }
    next;
  }

  if ($line =~ m/^[[:space:]]*#[[:space:]]*warning/) {
    if (!&searchBack('\s*#\s*ifdef\s__GNUC__',$linecnt-1,2)) {
      $wcnt++;
      if ($wcnt == 1) {
        $wstr = "Guard #warning with '#ifdef __GNUC__' line\#" . $linecnt;
      } else {
        $wstr = $wstr . "," . $linecnt;
      }
      print "=> $line\n" if (&verboseArg());
    }
    next;
  }
}

my($total_count) = $cnt + $hcnt + $wcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  if (!&quietArg()) {
    print "$lstr ($cnt)\n" if ($cnt);
    print "$hstr ($hcnt)\n" if ($hcnt);
    print "$wstr ($wcnt)\n" if ($wcnt);
  }
  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 cpp macros and usage\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "C++ source files and non-installed headers should NOT use cpp conditionals that check for a certain O/S or compiler; instead use CMake HAVE_foo macros. We want to check for features discovered during CMake time rather than for a specific O/S.\n";
  Exit 0 if &explainArg();
}

sub osMacros() {
  my($l,$lp1) = @_;
  return 1
      if (
#       allow Q_OS_
        $l =~ m/USE_SOLARIS/ ||
        $l =~ m/USE_NETBSD/ ||
        $l =~ m/USE_FREEBSD/ ||
        $l =~ m/_WIN32/ ||
        $l =~ m/_WIN64/ ||
        $l =~ m/__APPLE__/ ||
        $l =~ m/__svr4__/ ||
        $l =~ m/__linux__/ ||
        $l =~ m/__bsdi__/ ||
        $l =~ m/__FreeBSD__/ ||
        $l =~ m/__NetBSD__/ ||
        $l =~ m/__OpenBSD__/ ||
        $l =~ m/__DragonFly__/ ||
        $l =~ m/__GNU__/ ||
        $l =~ m/__osf__/ ||
        $l =~ m/__hpux/ ||
        $l =~ m/__sun/ ||
        $l =~ m/__sgi/ ||
        $l =~ m/_AIX/ ||
        $l =~ m/__CYGWIN__/
      );
  return 0;
}

sub compilerMacros() {
  my($l,$lp1) = @_;
  return 1
      if (
        $l =~ m/__BORLANDC__/ ||
        $l =~ m/__INTEL_COMPILER/
      );
  # allow Q_CC_
  return 1 if ($l =~ m/__GNUC__/ && $lp1 !~ /warning/);
  return 0;
}

sub knownHaveMacro() {
  my($l,$lp1) = @_;
  my(@res)=();

  my(@haves) = &havesArray($l);
  if ($#haves >= 0) {
    my($have);
    foreach $have (@haves) {
      if (!$have_map{$have}) {
        push(@res, $have);
      }
    }
  }
  return @res;
}

sub haves() {
  my($l) = @_;
  my(@a) = split(' ',$l);
  my($guy);
  my($res)="";
  foreach $guy (@a) {
    next unless ($guy =~ m/_/);
    next if ($guy =~ m/HAVE_/);
    $guy =~ s/.*defined\(//;
    $guy =~ s/.*\(//;
    $guy =~ s/\).*//;
    $res .= $guy . ",";
  }
  $res =~ s/,$//;
  return $res;
}

sub havesArray() {
  my($l) = @_;
  my(@a) = split(' ',$l);
  my($guy);
  my(@res)=();
  foreach $guy (@a) {
    next unless ($guy =~ m/^HAVE_/);
    next if ($guy =~ m/_H$/);
    $guy =~ s/.*defined\(//;
    $guy =~ s/.*def\s*//;
    $guy =~ s/.*\(//;
    $guy =~ s/\).*//;
    push(@res, $guy);
  }
  return @res;
}
