checkpatch.pl: Check .cpp files

Enable checkpatch.pl to apply the same checks as C source files for
C++ files with .cpp extensions. It also adds some exceptions for C++
sources to suppress errors for:
  - <> used in C++ template arguments (e.g. template <class T>)
  - :: used to represent namespaces   (e.g. SomeClass::method())
  - : used in class declaration       (e.g. class T : public Super)
  - ~ used in destructor method name  (e.g. T::~T())
  - spacing around 'catch'            (e.g. catch (...))

Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama@hds.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
This commit is contained in:
Tomoki Sekiyama 2013-08-07 11:39:50 -04:00 committed by Michael Roth
parent 6f88009ee5
commit 69d5d21f90

View File

@ -1363,7 +1363,7 @@ sub process {
# Check for incorrect file permissions # Check for incorrect file permissions
if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
my $permhere = $here . "FILE: $realfile\n"; my $permhere = $here . "FILE: $realfile\n";
if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { if ($realfile =~ /(Makefile|Kconfig|\.c|\.cpp|\.h|\.S|\.tmpl)$/) {
ERROR("do not set execute permissions for source files\n" . $permhere); ERROR("do not set execute permissions for source files\n" . $permhere);
} }
} }
@ -1460,7 +1460,7 @@ sub process {
} }
# check we are in a valid source file if not then ignore this hunk # check we are in a valid source file if not then ignore this hunk
next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); next if ($realfile !~ /\.(h|c|cpp|s|S|pl|sh)$/);
#80 column limit #80 column limit
if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
@ -1495,7 +1495,7 @@ sub process {
} }
# check we are in a valid source file C or perl if not then ignore this hunk # check we are in a valid source file C or perl if not then ignore this hunk
next if ($realfile !~ /\.(h|c|pl)$/); next if ($realfile !~ /\.(h|c|cpp|pl)$/);
# in QEMU, no tabs are allowed # in QEMU, no tabs are allowed
if ($rawline =~ /^\+.*\t/) { if ($rawline =~ /^\+.*\t/) {
@ -1505,7 +1505,7 @@ sub process {
} }
# check we are in a valid C source file if not then ignore this hunk # check we are in a valid C source file if not then ignore this hunk
next if ($realfile !~ /\.(h|c)$/); next if ($realfile !~ /\.(h|c|cpp)$/);
# check for RCS/CVS revision markers # check for RCS/CVS revision markers
if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
@ -1969,6 +1969,9 @@ sub process {
asm|__asm__)$/x) asm|__asm__)$/x)
{ {
# Ignore 'catch (...)' in C++
} elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
# cpp #define statements have non-optional spaces, ie # cpp #define statements have non-optional spaces, ie
# if there is a space between the name and the open # if there is a space between the name and the open
# parenthesis it is simply not a parameter group. # parenthesis it is simply not a parameter group.
@ -1992,7 +1995,7 @@ sub process {
\+=|-=|\*=|\/=|%=|\^=|\|=|&=| \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
=>|->|<<|>>|<|>|=|!|~| =>|->|<<|>>|<|>|=|!|~|
&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
\?|: \?|::|:
}x; }x;
my @elements = split(/($ops|;)/, $opline); my @elements = split(/($ops|;)/, $opline);
my $off = 0; my $off = 0;
@ -2062,6 +2065,10 @@ sub process {
# // is a comment # // is a comment
} elsif ($op eq '//') { } elsif ($op eq '//') {
# Ignore : used in class declaration in C++
} elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
$line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
# No spaces for: # No spaces for:
# -> # ->
# : when part of a bitfield # : when part of a bitfield
@ -2088,7 +2095,10 @@ sub process {
} elsif ($op eq '!' || $op eq '~' || } elsif ($op eq '!' || $op eq '~' ||
$opv eq '*U' || $opv eq '-U' || $opv eq '*U' || $opv eq '-U' ||
$opv eq '&U' || $opv eq '&&U') { $opv eq '&U' || $opv eq '&&U') {
if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
# '~' used as a name of Destructor
} elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
ERROR("space required before that '$op' $at\n" . $hereptr); ERROR("space required before that '$op' $at\n" . $hereptr);
} }
if ($op eq '*' && $cc =~/\s*$Modifier\b/) { if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
@ -2135,6 +2145,18 @@ sub process {
} elsif ($ctx !~ /[EWC]x[CWE]/) { } elsif ($ctx !~ /[EWC]x[CWE]/) {
my $ok = 0; my $ok = 0;
if ($realfile =~ /\.cpp|\.h$/) {
# Ignore template arguments <...> in C++
if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
$ok = 1;
}
# Ignore :: in C++
if ($op eq '::') {
$ok = 1;
}
}
# Ignore email addresses <foo@bar> # Ignore email addresses <foo@bar>
if (($op eq '<' && if (($op eq '<' &&
$cc =~ /^\S+\@\S+>/) || $cc =~ /^\S+\@\S+>/) ||