Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 2 | # |
| 3 | # headers_check.pl execute a number of trivial consistency checks |
| 4 | # |
Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 5 | # Usage: headers_check.pl dir arch [files...] |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 6 | # dir: dir to look for included files |
| 7 | # arch: architecture |
| 8 | # files: list of files to check |
| 9 | # |
| 10 | # The script reads the supplied files line by line and: |
| 11 | # |
| 12 | # 1) for each include statement it checks if the |
| 13 | # included file actually exists. |
| 14 | # Only include files located in asm* and linux* are checked. |
| 15 | # The rest are assumed to be system include files. |
| 16 | # |
Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 17 | # 2) It is checked that prototypes does not use "extern" |
| 18 | # |
Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 19 | # 3) Check for leaked CONFIG_ symbols |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 20 | |
| 21 | use strict; |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 22 | |
| 23 | my ($dir, $arch, @files) = @ARGV; |
| 24 | |
| 25 | my $ret = 0; |
| 26 | my $line; |
| 27 | my $lineno = 0; |
| 28 | my $filename; |
| 29 | |
| 30 | foreach my $file (@files) { |
Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 31 | local *FH; |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 32 | $filename = $file; |
Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 33 | open(FH, "<$filename") or die "$filename: $!\n"; |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 34 | $lineno = 0; |
Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 35 | while ($line = <FH>) { |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 36 | $lineno++; |
Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 37 | &check_include(); |
| 38 | &check_asm_types(); |
| 39 | &check_sizetypes(); |
Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 40 | &check_declarations(); |
Sam Ravnborg | 7e3fa56 | 2009-01-30 23:56:42 +0100 | [diff] [blame] | 41 | # Dropped for now. Too much noise &check_config(); |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 42 | } |
Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 43 | close FH; |
Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 44 | } |
| 45 | exit $ret; |
| 46 | |
| 47 | sub check_include |
| 48 | { |
| 49 | if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) { |
| 50 | my $inc = $1; |
| 51 | my $found; |
| 52 | $found = stat($dir . "/" . $inc); |
| 53 | if (!$found) { |
| 54 | $inc =~ s#asm/#asm-$arch/#; |
| 55 | $found = stat($dir . "/" . $inc); |
| 56 | } |
| 57 | if (!$found) { |
| 58 | printf STDERR "$filename:$lineno: included file '$inc' is not exported\n"; |
| 59 | $ret = 1; |
| 60 | } |
| 61 | } |
| 62 | } |
Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 63 | |
Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 64 | sub check_declarations |
Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 65 | { |
Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 66 | if ($line =~m/^\s*extern\b/) { |
| 67 | printf STDERR "$filename:$lineno: " . |
| 68 | "userspace cannot call function or variable " . |
| 69 | "defined in the kernel\n"; |
Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 72 | |
| 73 | sub check_config |
| 74 | { |
Robert P. J. Day | 1581c1c | 2009-05-12 13:43:36 -0700 | [diff] [blame] | 75 | if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) { |
Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 76 | printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n"; |
| 77 | } |
| 78 | } |
| 79 | |
Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 80 | my $linux_asm_types; |
| 81 | sub check_asm_types() |
| 82 | { |
Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 83 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { |
| 84 | return; |
| 85 | } |
Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 86 | if ($lineno == 1) { |
| 87 | $linux_asm_types = 0; |
| 88 | } elsif ($linux_asm_types >= 1) { |
| 89 | return; |
| 90 | } |
| 91 | if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) { |
| 92 | $linux_asm_types = 1; |
| 93 | printf STDERR "$filename:$lineno: " . |
| 94 | "include of <linux/types.h> is preferred over <asm/types.h>\n" |
| 95 | # Warn until headers are all fixed |
| 96 | #$ret = 1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | my $linux_types; |
| 101 | sub check_sizetypes |
| 102 | { |
Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 103 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { |
| 104 | return; |
| 105 | } |
Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 106 | if ($lineno == 1) { |
| 107 | $linux_types = 0; |
| 108 | } elsif ($linux_types >= 1) { |
| 109 | return; |
| 110 | } |
| 111 | if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { |
| 112 | $linux_types = 1; |
| 113 | return; |
| 114 | } |
| 115 | if ($line =~ m/__[us](8|16|32|64)\b/) { |
| 116 | printf STDERR "$filename:$lineno: " . |
| 117 | "found __[us]{8,16,32,64} type " . |
| 118 | "without #include <linux/types.h>\n"; |
| 119 | $linux_types = 2; |
| 120 | # Warn until headers are all fixed |
| 121 | #$ret = 1; |
| 122 | } |
| 123 | } |
| 124 | |