mytasks.pl 4.75 KB
Newer Older
1 2
#!/usr/bin/perl

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
############################################################################
#
# Copyright (C) 2016 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of Qt Creator.
#
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and The Qt Company. For licensing terms
# and conditions see https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
############################################################################

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
use strict;

my $date = `date`;
print "# $date";

sub escape {
    my $text = shift;
    $text =~ s/\\/\\\\/g;
    $text =~ s/\n/\\n/g;
    $text =~ s/\t/\\t/g;
    return $text;
}
sub checkLine {
    my $line = shift;
    my $file = shift;
    my $row = shift;

    $file = escape($file);
    $row = escape($row);

48
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /(FIXME|TODO)(.*)$/) {
49 50
        print "$file\t$row\tWARNING\t$1$2\n";
    }
51 52
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /(qDebug|QDebug>|QtDebug>|<debug>)/) {
        print "$file\t$row\tWARNING\tRemove debug code.\n";
53 54
    }
    if ($line =~ /^(<<<<|====|>>>>)/) {
55
        print "$file\n$row\tERROR\tResolved conflict.\n";
56
    }
57 58
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /(Q_SIGNALS|Q_SLOTS)/) {
        print "$file\t$row\tWARNING\tPrefer signals and slots over Q_SIGNALS and Q_SLOTS.\n";
59 60
    }
    if ($line =~ /^#\s*(warning|error) (.*)$/) {
61 62 63 64 65 66 67 68
        print "$file\t$row\t$1\tClean up preprocessor $1:$2.\n";
    }
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /\s+$/) {
        print "$file\t$row\tWARNING\tTrailing whitespaces found.\n";
    }
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /^ *\t */) {
        print "$file\t$row\tWARNING\tTabs used to indent.\n";
    }
69
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /^\s+{$/) {
70 71 72
        print "$file\t$row\tWARNING\tOpening bracket found all alone on a line.\n";
    }
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /{.*[^\s].*$/) {
73
        if ($line !~ /\{\s+\/\*/ && $line !~ /^\s*\{ \}$/ && $line !~ /^namespace .* \}$/)
74 75
        { print "$file\t$row\tWARNING\tText found after opening bracket.\n"; }
    }
76 77 78 79
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /[a-zA-Z0-9_]\*[^\/]/) {
        next if $line =~ /SIGNAL\(/;
        next if $line =~ /SLOT\(/;
        print "$file\t$row\tWARNING\tUse \"TYPE *NAME\" (currently the space between TYPE and * is missing).\n";
80
    }
81 82 83 84 85
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /[a-zA-Z0-9_]&[^&]/) {
        print "$file\t$row\tWARNING\tUse \"TYPE &NAME\" (currently the space between TYPE and & is missing).\n";
    }
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /\*\s+/) {
        next if ($line =~ /^\s*\*\*?\s+/);
86
        next if ($line =~ /\/\*/);
87 88 89 90
        print "$file\t$row\tWARNING\tUse \"TYPE *NAME\" (currently there is a space between * and NAME).\n";
    }
    if ($file =~ /(.cpp|.c|.cxx|.h)$/ && $line =~ /[^&]&\s+/) {
        print "$file\t$row\tWARNING\tUse \"TYPE &NAME\" (currently there is a space between & and NAME).\n";
91 92 93 94 95 96
    }
}

my $filename = "";
my $pos = 0;

97 98 99
sub getDiffOrigin {
   my $currentBranch = `git branch | grep "^* "`;
   chop $currentBranch;
100
   $currentBranch =~ s/^\s*\* //;
101 102 103 104 105 106 107 108

   my $remoteRepo = `git config --local --get branch.$currentBranch.remote`;
   chop $remoteRepo;

   return "HEAD" if (!$remoteRepo);

   my $remoteBranch = `git config --local --get branch.$currentBranch.merge`;
   chop $remoteBranch;
109
   $remoteBranch =~ s!^refs/heads/!!;
110 111 112 113 114 115

   return "HEAD" if (!$remoteBranch);

   return "$remoteRepo/$remoteBranch";
}

116 117
my $origin = shift;
$origin = getDiffOrigin unless $origin;
118 119 120
print "# running: git diff $origin ...\n";

open(PIPE, "git diff $origin|");
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
while (<PIPE>) {
    chomp;
    my $line = $_;
    print "#$line\n";
    next if ($line =~ /^-/);
    if ($line =~ /^\+\+\+ (.*)$/) {
        $filename = $1;
        $filename =~ s/^b\///;
        $pos = 0;
        next;
    }
    next if $filename =~ /^\/dev\/null$/;
    if ($line =~ /^@@ -\d+,\d+\s\+(\d+),\d+ @@$/) {
        $pos = $1 - 1;
        next;
    }
    $pos = $pos + 1;
    if ($line =~ /^\+(.*)/) {
        checkLine($1, $filename, $pos);
    }
Orgad Shaneh's avatar
Orgad Shaneh committed
141 142
}
close(PIPE);