aboutsummaryrefslogtreecommitdiffstats
path: root/tools/stylecheck.pl
blob: 3e2821686e173fe22cf52943a7ec92925b610908 (plain)
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

# Desired indentation.
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");
}

# Indentation check.
sub check_indentation {

  shift =~ /^(\s*)/;
  if (length($1) != $i_level) {
    style "indentation violation";
  }
}

my $emptycnt = 0;
foreach my $line (@c_source) {

  $lineno += 1;

  #****************************************************************************
  # Check on EOL.
  if (not ($line =~ /$cr$lf$/)) {
    error "detected malformed EOL";
  }
  $line =~ s/$cr//;
  $line =~ s/$lf//;

  #****************************************************************************
  # Check on trailing spaces.
  if ($line =~ /\s$/) {
    style "detected trailing spaces";
  }

  #****************************************************************************
  # Check on TABs.
  if ($line =~ /$tab/) {
    style "detected TAB";
    $line =~ s/$tab/    /;
  }

  #****************************************************************************
  # Check on empty lines.
  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;
  }

  #****************************************************************************
  # Stripping strings content for ease of parsing, all strings become _string_.
  $line =~ s/\\\"//;
  if ($line =~ s/(\"[^"]*\")/_string_/) {
#    print "string: $1 replaced by _string_\n";
  }

  #******************************************************************************
  # State machine handling.
  if ($state eq "start") {
    # Searching for a global code element or a comment start.

    # Indentation check.
    check_indentation $line;

    #******************************************************************************
    # Functions matching, triggered by the "(".
    if ($line =~ /^(static|)\s*(struct|union|)\s*([a-zA-Z_][a-zA-Z0-9_]*\s*[*]*)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/) {
      # $1=scope $2$3=return type $4=name
      $line =~ s/^(static|)\s*(struct|union|)\s*([a-zA-Z_][a-zA-Z0-9_]*\s*[*]*)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\(//;
#      print "function: " . $1 . " " . $2 . " " . $3 . " " . $4 . "(";

      # Size of the match up to parameters.
      my $size = $+[0] - $-[0];

      # Function line number.
      $f_lineno = $lineno;
      
      # Checking if all parameters are on the same line.
      if ($line =~ /.*\)\s*{\s*$/) {
        $line =~ s/\)\s*{\s*$//;
#        print $line . "\n";
        $f_params = $line;
        $i_level = $indentation;
        $state = "infunc";
      }
      else {
#        print $line;
        $f_params = $line;
        $i_level = $size;
        $state = "inparams";
      }
    }
    #******************************************************************************
    # Structures matching.
    elsif ($line =~ /^\s*struct\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
    }
    #******************************************************************************
    # Single line "typedef" matching.
    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*$/) {
    }
    #******************************************************************************
    # Single line "typedef function" matching.
    elsif ($line =~ /^\s*typedef\s+.*\(\s*\*\s*([a-zA-Z0-9_]*)\s*\)\(/) {
    }
    #******************************************************************************
    # Multi line "typedef struct" matching.
    elsif ($line =~ /^\s*typedef\s*struct\s*([a-zA-Z_][a-zA-Z0-9_]*|\s?)?/) {
      $t_lineno = $lineno;
      $t_type   = "struct " . $1;
      $state = "intypedef";
    }
    #******************************************************************************
    # Multi line "typedef enum" matching.
    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*$/) {
        # Special case of a single-line typedef enum.
      }
      else {
        $state = "intypedef";
      }
    }
    #******************************************************************************
    # Multi line "enum" matching.
    elsif ($line =~ /^\s*enum\s*([a-zA-Z_][a-zA-Z0-9_]*)/) {
      $e_name = $1;
      $e_lineno = $lineno;
      if ($line =~ /;\s*$/) {
        # Special case of a single-line enum.
      }
      else {
        $state = "inenum";
      }
    }
    #******************************************************************************
    # Struct variable matching.
    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*$|=)/) {
    }
    #******************************************************************************
    # Variable matching.
    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*$|=)/) {
      # variable declaration.
    }
    #******************************************************************************
    # #nclude matching.
    elsif ($line =~ /^\s*#include\s+"([^"]+)"/) {
    }
    #******************************************************************************
    # #if matching.
    elsif ($line =~ /^\s*#if\s+(.+)/) {
    }
    #******************************************************************************
    # #ifdef matching.
    elsif ($line =~ /^\s*#ifdef\s+([\w_]+)/) {
    }
    #******************************************************************************
    # #define matching.
    elsif ($line =~ /^\s*#define\s+([\w_]+)\s*(.*)/) {
      $d_lineno = $lineno;
      $d_name   = $1;
      $d_name2   = $2;
      # enum typedef declaration.
      if ($line =~ /[^\\]$/) {
        # Special case of a single-line typedef enum.
      }
      else {
        $state = "indefine";
      }
    }
    #******************************************************************************
    # Comment start matching.
    elsif ("$line" =~ /^\s*\/\*/) {
      if ("$line" =~ /\*\//) {
        # Special case of single line comments.
        $line =~ /^\s*(\/\*.*\*\/)/;
      }
      else {
        # Start of multi-line comment.
        $line =~ /(\/\*.*)/;
        $c_comment = $1 . " ";
        $state = "incomment";
      }
    }
  }
  #******************************************************************************
  # Scanning for function parameters end and function body begin.
  elsif ($state eq "inparams") {

    # Indentation check.
    check_indentation $line;

    if ($line =~ /.*\)\s*{\s*$/) {
#      print $line . "\n";
      $line =~ s/\)\s*{\s*$//;
      $f_params = $f_params . $line;
      $i_level = $indentation;
      $state = "infunc";
      print "$f_params\n";
    }
    else {
      $f_params = $f_params . $line;
#      print $line;
    }
  }
  #******************************************************************************
  # Scanning for function end.
  elsif ($state eq "infunc") {
    
    # Checking for function end, the final "}".
    if ($line =~ /^\}/) {
      $i_level = 0;
      $state = "start";
      next;
    }
    
    # Indentation check.
    check_indentation $line;

    if ($line =~ /(\/\*.*\*\/)/) {
      # Single line comments inside functions.
    }
    elsif ("$line" =~ /(\/\*.*)/) {
      # Start of multi-line comment inside a function.
      $c_comment = $1 . " ";
      $state = "infunccomment";
    }
  }
  #******************************************************************************
  # Scanning for comment end within a function body.
  elsif ($state eq "infunccomment") {
    if ("$line" =~ /\*\/\s*$/) {
      # End of mult-line comment.
      $line =~ /(.*\*\/)/;
      $c_comment .= $1;

      $state = "infunc";
    }
    else {
      # Add the whole line.
      $c_comment .= $line . " ";
    }
  }
  #******************************************************************************
  # Scanning for comment end.
  elsif ($state eq "incomment") {
    if ("$line" =~ /\*\/\s*$/) {
      # End of mult-line comment.
      $line =~ /(.*\*\/)/;
      $c_comment .= $1;

      $state = "start";
    }
    else {
      # Add the whole line.
      $c_comment .= $line . " ";
    }
  }
  #******************************************************************************
  # Scanning for typedef end.
  elsif ($state eq "intypedef") {
    if ($line =~ /^\s*}\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*$/) {
      # typedef end because the final '} <name>;'.

      $state = "start";
    }
  }
  #******************************************************************************
  # Scanning for enum end.
  elsif ($state eq "inenum") {
    if ($line =~ /^\s*}\s*;\s*$/) {
      # enum end because the final '};'.

      $state = "start";
    }
  }
  #******************************************************************************
  # Scanning for define end.
  elsif ($state eq "indefine") {
    if ($line =~ /[^\\]$/) {
      # define end because the final 'not \'.
      # remove blank from starting of the line
      $line =~ s/^\s+|\s+$//g;
      # Add the whole line.
      $d_name2 .= $line;

      $state = "start";
    }
    else {
      # Add the whole line.
      # $line =~ s/^\s+|\s+$//g;
      $line =~ s/^\s*(.*?)\s*$/$1/;
      $d_name2 .= $line;
    }
  }
}

close $in or die "$in: $!";