scripts: added a two simple perl scripts for checking code.

This commit is contained in:
Daniel Marjamäki 2011-01-04 18:50:23 +01:00
parent c2a194ead0
commit 3cf4e74013
3 changed files with 93 additions and 0 deletions

47
scripts/comment.pl Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/perl
# Missing comments (it is a good idea to write a comment before a variable declaration)
# usage:
# scripts/comment.pl lib/checkstl.cpp
sub checkfile
{
my $filename = $_[0];
# parse file
open(FILE, $filename);
my @lines = <FILE>;
close(FILE);
# check comments..
my $comment = false;
my $linenr = 0;
foreach $line (@lines)
{
$linenr = $linenr + 1;
# missing comment before variable declaration?
if (($comment == 0) &&
($line =~ /^\s+([a-z]+)? [a-z]+(\s)+[a-z][a-z0-9]*\s*[;=]/) &&
(!($line =~ /return|delete/)))
{
print "[$filename:$linenr] No comment before variable declaration\n";
}
# set comment variable
if ($line =~ /\/\//)
{
$comment = 1;
}
else
{
$comment = 0;
}
}
}
foreach $filename (@ARGV)
{
checkfile($filename)
}

36
scripts/magic-numbers.pl Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/perl
# warn if there are magic numbers in the code.
# usage:
# scripts/magic-numbers.pl lib/checkstl.cpp
sub checkfile
{
my $filename = $_[0];
# parse file
open(FILE, $filename);
my @lines = <FILE>;
close(FILE);
# check comments..
my $linenr = 0;
foreach $line (@lines)
{
$linenr = $linenr + 1;
# is there a magic number?
if (($line =~ /[^a-zA-Z_][0-9]{3,}/) &&
(!($line =~ /define|const|(\/\/)/)))
{
print "[$filename:$linenr] Magic number\n";
}
}
}
foreach $filename (@ARGV)
{
checkfile($filename)
}

10
scripts/readme.txt Normal file
View File

@ -0,0 +1,10 @@
simple perl scripts that performs stylistic checking of C/C++ source code.
They are not intended to be 'released' as extra checks. I will just use them
to check the Cppcheck source code.
feel free to try them.
feel free to add additional scripts.