aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorGiovanni Di Sirio <gdisirio@gmail.com>2016-04-05 14:53:57 +0000
committerGiovanni Di Sirio <gdisirio@gmail.com>2016-04-05 14:53:57 +0000
commit9323ae6bffdbfd45fd86cf4fa540a29a22a1ad1b (patch)
tree305d713d95114076b253a2e56e73d82943507ae7 /tools
parent361ad6aa408a2197a6a094380e41addeb433e66d (diff)
downloadChibiOS-9323ae6bffdbfd45fd86cf4fa540a29a22a1ad1b.tar.gz
ChibiOS-9323ae6bffdbfd45fd86cf4fa540a29a22a1ad1b.tar.bz2
ChibiOS-9323ae6bffdbfd45fd86cf4fa540a29a22a1ad1b.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9248 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'tools')
-rw-r--r--tools/stylecheck.pl259
-rw-r--r--tools/test.c20
2 files changed, 279 insertions, 0 deletions
diff --git a/tools/stylecheck.pl b/tools/stylecheck.pl
new file mode 100644
index 000000000..d7e5a802a
--- /dev/null
+++ b/tools/stylecheck.pl
@@ -0,0 +1,259 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use File::Basename;
+
+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 $c_comment = "";
+my $state = "start";
+my $cr = "\r";
+my $lf = "\n";
+my $f_lineno = 0;
+my $f_name = "";
+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 = "";
+
+foreach my $line (@c_source) {
+
+ $lineno += 1;
+ $line =~ s/$cr//;
+ $line =~ s/$lf//;
+
+ #******************************************************************************
+ # State machine handling.
+ if ($state eq "start") {
+ # Searching for a global code element or a comment start.
+
+ #******************************************************************************
+ # 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 . "(";
+
+ # Function line number.
+ $f_lineno = $lineno;
+
+ # Checking if all parameters are on the same line.
+ if ($line =~ /.*\)\s*{/) {
+ print $line . "\n";
+ $state = "infunc";
+ }
+ else {
+ print $line;
+ $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 params end and function body begin.
+ elsif ($state eq "inparams") {
+ if ($line =~ /.*\)\s*{/) {
+ print $line . "\n";
+ $state = "infunc";
+ }
+ else {
+ print $line;
+ }
+ }
+ #******************************************************************************
+ # Scanning for function end.
+ elsif ($state eq "infunc") {
+ if (($line =~ /^\}/) || ($line =~ /^\);/)) {
+ # Function end because the final '}' or ');' in case of a prototype.
+ $state = "start";
+ }
+ elsif ($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: $!";
diff --git a/tools/test.c b/tools/test.c
new file mode 100644
index 000000000..b55cb2c86
--- /dev/null
+++ b/tools/test.c
@@ -0,0 +1,20 @@
+static struct pippo **pluto (void) {
+
+}
+
+static struct pp qq (void) {
+
+}
+
+static cc ss (void) {
+
+}
+
+static aa bb (int a,
+ char *p) {
+
+}
+
+dd *ee (void) {
+
+}