12 my ($num, $col, $file, $line, $msg, $error) = @_;
14 my $w=$error?"error":"warning";
24 print "$file:$num:$col: $w: $msg\n";
28 my $pref = (' ' x $col);
34 print "checksrc.pl <single C or H file>\n";
40 open(R, "<$file") || die;
49 # check for a copyright statement
50 if(!$copyright && ($l =~ /copyright .* \d\d\d\d/i)) {
55 if(length($l) > $max_column) {
56 checkwarn($line, length($l), $file, $l, "Longer than $max_column columns");
58 # detect TAB characters
60 checkwarn($line, length($1), $file, $l, "Contains TAB character", 1);
62 # detect trailing white space
63 if($l =~ /^(\S+)[ \t]+\z/) {
64 checkwarn($line, length($1), $file, $l, "Trailing whitespace");
67 # detect return statements with parenthesis
68 # doesn't really work unless we filter off typecasts
69 #if($l =~ /(.*)return \(/) {
70 # checkwarn($line, length($1)+6, $file, $l, "return with paretheses");
73 # check spaces after for/if/while
74 if($l =~ /^(.*)(for|if|while) \(/) {
76 # this is a #if, treat it differently
79 checkwarn($line, length($1)+length($2), $file, $l,
84 if($l =~ /^(.*)\} else/) {
85 checkwarn($line, length($1), $file, $l, "else after closing brace on same line");
87 # check for open brace first on line but not first column
88 # only alert if previous line ended with a close paren and wasn't a cpp
90 if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
91 checkwarn($line, length($1), $file, $l, "badly placed open brace");
94 # if the previous line starts with if/while/for AND ends with an open
95 # brace, check that this line is indented $indent more steps, if not
97 if($prevl =~ /^( *)(if|while|for)\(.*\{\z/) {
98 my $first = length($1);
100 # this line has some character besides spaces
101 if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
102 my $second = length($1);
103 my $expect = $first+$indent;
104 if($expect != $second) {
105 my $diff = $second - $first;
106 checkwarn($line, length($1), $file, $l,
107 "not indented $indent steps, uses $diff)");
113 # check for // letters, but skip them if a double quote or asterisk was
114 # on the same line to avoid strings and comments. Not reliable.
115 #if($l =~ /^([^\"*]*)\/\//) {
116 # checkwarn($line, length($1), $file, $l, "non-C89 compliant comment",
125 checkwarn(1, 0, $file, "", "Missing copyright statement", 1);
130 if($errors || $warnings) {
131 exit 5; # return failure