123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- use strict;
- use warnings;
- use File::Basename;
- my $indentation = 2;
- if ($#ARGV != 0) {
- print "\nUsage: stylecheck.pl source\n";
- exit;
- }
- my $source = $ARGV[0];
- open(my $in, "<", $source) or die "Can't open source: $!";
- my $lineno = 0;
- my @c_source = <$in>;
- my $filename = $source;
- $filename =~ y/\\/\//;
- $filename = basename($filename);
- my $i_level = 0;
- my $c_comment = "";
- my $state = "start";
- my $cr = "\r";
- my $lf = "\n";
- my $tab = "\t";
- my $f_lineno = 0;
- my $f_params = "";
- my $t_lineno = 0;
- my $t_type = "";
- my $e_lineno = 0;
- my $e_name = "";
- my $d_lineno = 0;
- my $d_name = "";
- my $d_name2 = "";
- my $d_name_app = "";
- my $scope = "";
- sub style {
- my $desc = shift;
- print("style: $desc at line $lineno in \"$filename\"\n");
- }
- sub error {
- my $desc = shift;
- print("error: $desc at line $lineno in \"$filename\"\n");
- }
- sub check_indentation {
- shift =~ /^(\s*)/;
- if (length($1) != $i_level) {
- style "indentation violation";
- }
- }
- my $emptycnt = 0;
- foreach my $line (@c_source) {
- $lineno += 1;
-
-
- if (not ($line =~ /$cr$lf$/)) {
- error "detected malformed EOL";
- }
- $line =~ s/$cr//;
- $line =~ s/$lf//;
-
-
- if ($line =~ /\s$/) {
- style "detected trailing spaces";
- }
-
-
- if ($line =~ /$tab/) {
- style "detected TAB";
- $line =~ s/$tab/ /;
- }
-
-
- my $tmp = $line;
- $tmp =~ s/\s//;
- if (length($tmp) == 0) {
- $emptycnt = $emptycnt + 1;
- if ($emptycnt == 2) {
- style "detected multiple empty lines"
- }
- next;
- }
- else {
- $emptycnt = 0;
- }
-
-
- $line =~ s/\\\"//;
- if ($line =~ s/(\"[^"]*\")/_string_/) {
- }
-
-
- if ($state eq "start") {
-
-
- check_indentation $line;
-
-
- if ($line =~ /^(static|)\s*(struct|union|)\s*([a-zA-Z_][a-zA-Z0-9_]*\s*[*]*)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/) {
-
- $line =~ s/^(static|)\s*(struct|union|)\s*([a-zA-Z_][a-zA-Z0-9_]*\s*[*]*)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\(//;
-
- my $size = $+[0] - $-[0];
-
- $f_lineno = $lineno;
-
-
- if ($line =~ /.*\)\s*{\s*$/) {
- $line =~ s/\)\s*{\s*$//;
- $f_params = $line;
- $i_level = $indentation;
- $state = "infunc";
- }
- else {
- $f_params = $line;
- $i_level = $size;
- $state = "inparams";
- }
- }
-
-
- elsif ($line =~ /^\s*struct\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
- }
-
-
- elsif ($line =~ /^\s*typedef\s+([\sa-zA-Z0-9_]*\*+)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*$/ or
- $line =~ /^\s*typedef\s+([\sa-zA-Z0-9_]*)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*$/) {
- }
-
-
- elsif ($line =~ /^\s*typedef\s+.*\(\s*\*\s*([a-zA-Z0-9_]*)\s*\)\(/) {
- }
-
-
- elsif ($line =~ /^\s*typedef\s*struct\s*([a-zA-Z_][a-zA-Z0-9_]*|\s?)?/) {
- $t_lineno = $lineno;
- $t_type = "struct " . $1;
- $state = "intypedef";
- }
-
-
- elsif ($line =~ /^\s*typedef\s*enum\s*([a-zA-Z_][a-zA-Z0-9_]*|\s?)?/) {
- $t_lineno = $lineno;
- $t_type = "enum " . $1;
- if ($line =~ /([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*$/) {
-
- }
- else {
- $state = "intypedef";
- }
- }
-
-
- elsif ($line =~ /^\s*enum\s*([a-zA-Z_][a-zA-Z0-9_]*)/) {
- $e_name = $1;
- $e_lineno = $lineno;
- if ($line =~ /;\s*$/) {
-
- }
- else {
- $state = "inenum";
- }
- }
-
-
- elsif ($line =~ /^\s*(static|)\s+([\sa-zA-Z0-9_]*)\s+([a-zA-Z_][a-zA-Z0-9_]*\[.*\]|[a-zA-Z_][a-zA-Z0-9_]*)\s*(;\s*$|=)/ or
- $line =~ /^\s*(static|)\s+([\sa-zA-Z0-9_]*\*+)\s*([a-zA-Z_][a-zA-Z0-9_]*\[.*\]|[a-zA-Z_][a-zA-Z0-9_]*)\s*(;\s*$|=)/) {
- }
-
-
- elsif ($line =~ /^\s*(static|).*\s+([a-zA-Z_][a-zA-Z0-9_]*\s*\*+|[a-zA-Z_][a-zA-Z0-9_]*)\s*([a-zA-Z_][a-zA-Z0-9_]*\[.*\]|[a-zA-Z_][a-zA-Z0-9_]*)\s*(;\s*$|=)/) {
-
- }
-
-
- elsif ($line =~ /^\s*#include\s+"([^"]+)"/) {
- }
-
-
- elsif ($line =~ /^\s*#if\s+(.+)/) {
- }
-
-
- elsif ($line =~ /^\s*#ifdef\s+([\w_]+)/) {
- }
-
-
- elsif ($line =~ /^\s*#define\s+([\w_]+)\s*(.*)/) {
- $d_lineno = $lineno;
- $d_name = $1;
- $d_name2 = $2;
-
- if ($line =~ /[^\\]$/) {
-
- }
- else {
- $state = "indefine";
- }
- }
-
-
- elsif ("$line" =~ /^\s*\/\*/) {
- if ("$line" =~ /\*\//) {
-
- $line =~ /^\s*(\/\*.*\*\/)/;
- }
- else {
-
- $line =~ /(\/\*.*)/;
- $c_comment = $1 . " ";
- $state = "incomment";
- }
- }
- }
-
-
- elsif ($state eq "inparams") {
-
- check_indentation $line;
- if ($line =~ /.*\)\s*{\s*$/) {
- $line =~ s/\)\s*{\s*$//;
- $f_params = $f_params . $line;
- $i_level = $indentation;
- $state = "infunc";
- print "$f_params\n";
- }
- else {
- $f_params = $f_params . $line;
- }
- }
-
-
- elsif ($state eq "infunc") {
-
-
- if ($line =~ /^\}/) {
- $i_level = 0;
- $state = "start";
- next;
- }
-
-
- check_indentation $line;
- if ($line =~ /(\/\*.*\*\/)/) {
-
- }
- elsif ("$line" =~ /(\/\*.*)/) {
-
- $c_comment = $1 . " ";
- $state = "infunccomment";
- }
- }
-
-
- elsif ($state eq "infunccomment") {
- if ("$line" =~ /\*\/\s*$/) {
-
- $line =~ /(.*\*\/)/;
- $c_comment .= $1;
- $state = "infunc";
- }
- else {
-
- $c_comment .= $line . " ";
- }
- }
-
-
- elsif ($state eq "incomment") {
- if ("$line" =~ /\*\/\s*$/) {
-
- $line =~ /(.*\*\/)/;
- $c_comment .= $1;
- $state = "start";
- }
- else {
-
- $c_comment .= $line . " ";
- }
- }
-
-
- elsif ($state eq "intypedef") {
- if ($line =~ /^\s*}\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*$/) {
-
- $state = "start";
- }
- }
-
-
- elsif ($state eq "inenum") {
- if ($line =~ /^\s*}\s*;\s*$/) {
-
- $state = "start";
- }
- }
-
-
- elsif ($state eq "indefine") {
- if ($line =~ /[^\\]$/) {
-
-
- $line =~ s/^\s+|\s+$//g;
-
- $d_name2 .= $line;
- $state = "start";
- }
- else {
-
-
- $line =~ s/^\s*(.*?)\s*$/$1/;
- $d_name2 .= $line;
- }
- }
- }
- close $in or die "$in: $!";
|