Commit | Line | Data |
---|---|---|
15a2ee74 | 1 | #!/usr/bin/perl -w |
7712401a SR |
2 | # |
3 | # headers_check.pl execute a number of trivial consistency checks | |
4 | # | |
67b7ebe0 | 5 | # Usage: headers_check.pl dir arch [files...] |
7712401a SR |
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 | # | |
46b8af50 MF |
17 | # 2) It is checked that prototypes does not use "extern" |
18 | # | |
7e557a25 | 19 | # 3) Check for leaked CONFIG_ symbols |
7712401a SR |
20 | |
21 | use strict; | |
f75a8df3 | 22 | use File::Basename; |
7712401a SR |
23 | |
24 | my ($dir, $arch, @files) = @ARGV; | |
25 | ||
26 | my $ret = 0; | |
27 | my $line; | |
28 | my $lineno = 0; | |
29 | my $filename; | |
30 | ||
31 | foreach my $file (@files) { | |
32 | $filename = $file; | |
dbbe33e9 SH |
33 | |
34 | open(my $fh, '<', $filename) | |
35 | or die "$filename: $!\n"; | |
7712401a | 36 | $lineno = 0; |
dbbe33e9 | 37 | while ($line = <$fh>) { |
7712401a | 38 | $lineno++; |
483b4121 SR |
39 | &check_include(); |
40 | &check_asm_types(); | |
41 | &check_sizetypes(); | |
67b7ebe0 | 42 | &check_declarations(); |
7e3fa561 | 43 | # Dropped for now. Too much noise &check_config(); |
7712401a | 44 | } |
dbbe33e9 | 45 | close $fh; |
7712401a SR |
46 | } |
47 | exit $ret; | |
48 | ||
49 | sub check_include | |
50 | { | |
51 | if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) { | |
52 | my $inc = $1; | |
53 | my $found; | |
54 | $found = stat($dir . "/" . $inc); | |
55 | if (!$found) { | |
56 | $inc =~ s#asm/#asm-$arch/#; | |
57 | $found = stat($dir . "/" . $inc); | |
58 | } | |
59 | if (!$found) { | |
60 | printf STDERR "$filename:$lineno: included file '$inc' is not exported\n"; | |
61 | $ret = 1; | |
62 | } | |
63 | } | |
64 | } | |
46b8af50 | 65 | |
67b7ebe0 | 66 | sub check_declarations |
46b8af50 | 67 | { |
a7e1d98f PB |
68 | # soundcard.h is what it is |
69 | if ($line =~ m/^void seqbuf_dump\(void\);/) { | |
70 | return; | |
71 | } | |
72 | if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) { | |
67b7ebe0 | 73 | printf STDERR "$filename:$lineno: " . |
d52784eb | 74 | "userspace cannot reference function or " . |
75 | "variable defined in the kernel\n"; | |
46b8af50 MF |
76 | } |
77 | } | |
7e557a25 SR |
78 | |
79 | sub check_config | |
80 | { | |
1581c1ce | 81 | if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) { |
7e557a25 SR |
82 | printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n"; |
83 | } | |
84 | } | |
85 | ||
483b4121 | 86 | my $linux_asm_types; |
dbbe33e9 | 87 | sub check_asm_types |
483b4121 | 88 | { |
b67ff8ce SR |
89 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { |
90 | return; | |
91 | } | |
483b4121 SR |
92 | if ($lineno == 1) { |
93 | $linux_asm_types = 0; | |
94 | } elsif ($linux_asm_types >= 1) { | |
95 | return; | |
96 | } | |
97 | if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) { | |
98 | $linux_asm_types = 1; | |
99 | printf STDERR "$filename:$lineno: " . | |
100 | "include of <linux/types.h> is preferred over <asm/types.h>\n" | |
101 | # Warn until headers are all fixed | |
102 | #$ret = 1; | |
103 | } | |
104 | } | |
105 | ||
106 | my $linux_types; | |
f75a8df3 BP |
107 | my %import_stack = (); |
108 | sub check_include_typesh | |
109 | { | |
110 | my $path = $_[0]; | |
111 | my $import_path; | |
112 | ||
113 | my $fh; | |
114 | my @file_paths = ($path, $dir . "/" . $path, dirname($filename) . "/" . $path); | |
115 | for my $possible ( @file_paths ) { | |
116 | if (not $import_stack{$possible} and open($fh, '<', $possible)) { | |
117 | $import_path = $possible; | |
118 | $import_stack{$import_path} = 1; | |
119 | last; | |
120 | } | |
121 | } | |
122 | if (eof $fh) { | |
123 | return; | |
124 | } | |
125 | ||
126 | my $line; | |
127 | while ($line = <$fh>) { | |
128 | if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { | |
129 | $linux_types = 1; | |
130 | last; | |
131 | } | |
132 | if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) { | |
133 | check_include_typesh($included); | |
134 | } | |
135 | } | |
136 | close $fh; | |
137 | delete $import_stack{$import_path}; | |
138 | } | |
139 | ||
483b4121 SR |
140 | sub check_sizetypes |
141 | { | |
b67ff8ce SR |
142 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { |
143 | return; | |
144 | } | |
483b4121 SR |
145 | if ($lineno == 1) { |
146 | $linux_types = 0; | |
147 | } elsif ($linux_types >= 1) { | |
148 | return; | |
149 | } | |
150 | if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { | |
151 | $linux_types = 1; | |
152 | return; | |
153 | } | |
f75a8df3 BP |
154 | if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) { |
155 | check_include_typesh($included); | |
156 | } | |
483b4121 SR |
157 | if ($line =~ m/__[us](8|16|32|64)\b/) { |
158 | printf STDERR "$filename:$lineno: " . | |
159 | "found __[us]{8,16,32,64} type " . | |
160 | "without #include <linux/types.h>\n"; | |
161 | $linux_types = 2; | |
162 | # Warn until headers are all fixed | |
163 | #$ret = 1; | |
164 | } | |
165 | } |