blob: 50d6cfd1fa778808b659a461884113de502f3f99 [file] [log] [blame]
Jeremy Huntwork15a2ee72008-10-29 14:20:13 -07001#!/usr/bin/perl -w
Sam Ravnborg77124012008-06-15 21:41:09 +02002#
3# headers_check.pl execute a number of trivial consistency checks
4#
Amerigo Wang67b7ebe2009-06-04 22:12:01 -04005# Usage: headers_check.pl dir arch [files...]
Sam Ravnborg77124012008-06-15 21:41:09 +02006# 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 Frysinger46b8af52008-12-27 02:43:36 -050017# 2) It is checked that prototypes does not use "extern"
18#
Sam Ravnborg7e557a22008-12-27 19:52:20 +010019# 3) Check for leaked CONFIG_ symbols
Sam Ravnborg77124012008-06-15 21:41:09 +020020
21use strict;
Sam Ravnborg77124012008-06-15 21:41:09 +020022
23my ($dir, $arch, @files) = @ARGV;
24
25my $ret = 0;
26my $line;
27my $lineno = 0;
28my $filename;
29
30foreach my $file (@files) {
31 $filename = $file;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080032
33 open(my $fh, '<', $filename)
34 or die "$filename: $!\n";
Sam Ravnborg77124012008-06-15 21:41:09 +020035 $lineno = 0;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080036 while ($line = <$fh>) {
Sam Ravnborg77124012008-06-15 21:41:09 +020037 $lineno++;
Sam Ravnborg483b4122008-12-30 11:34:58 +010038 &check_include();
39 &check_asm_types();
40 &check_sizetypes();
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040041 &check_declarations();
Sam Ravnborg7e3fa562009-01-30 23:56:42 +010042 # Dropped for now. Too much noise &check_config();
Sam Ravnborg77124012008-06-15 21:41:09 +020043 }
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080044 close $fh;
Sam Ravnborg77124012008-06-15 21:41:09 +020045}
46exit $ret;
47
48sub check_include
49{
50 if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
51 my $inc = $1;
52 my $found;
53 $found = stat($dir . "/" . $inc);
54 if (!$found) {
55 $inc =~ s#asm/#asm-$arch/#;
56 $found = stat($dir . "/" . $inc);
57 }
58 if (!$found) {
59 printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
60 $ret = 1;
61 }
62 }
63}
Mike Frysinger46b8af52008-12-27 02:43:36 -050064
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040065sub check_declarations
Mike Frysinger46b8af52008-12-27 02:43:36 -050066{
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040067 if ($line =~m/^\s*extern\b/) {
68 printf STDERR "$filename:$lineno: " .
69 "userspace cannot call function or variable " .
70 "defined in the kernel\n";
Mike Frysinger46b8af52008-12-27 02:43:36 -050071 }
72}
Sam Ravnborg7e557a22008-12-27 19:52:20 +010073
74sub check_config
75{
Robert P. J. Day1581c1c2009-05-12 13:43:36 -070076 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
Sam Ravnborg7e557a22008-12-27 19:52:20 +010077 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
78 }
79}
80
Sam Ravnborg483b4122008-12-30 11:34:58 +010081my $linux_asm_types;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080082sub check_asm_types
Sam Ravnborg483b4122008-12-30 11:34:58 +010083{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +010084 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
85 return;
86 }
Sam Ravnborg483b4122008-12-30 11:34:58 +010087 if ($lineno == 1) {
88 $linux_asm_types = 0;
89 } elsif ($linux_asm_types >= 1) {
90 return;
91 }
92 if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
93 $linux_asm_types = 1;
94 printf STDERR "$filename:$lineno: " .
95 "include of <linux/types.h> is preferred over <asm/types.h>\n"
96 # Warn until headers are all fixed
97 #$ret = 1;
98 }
99}
100
101my $linux_types;
102sub check_sizetypes
103{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +0100104 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
105 return;
106 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100107 if ($lineno == 1) {
108 $linux_types = 0;
109 } elsif ($linux_types >= 1) {
110 return;
111 }
112 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
113 $linux_types = 1;
114 return;
115 }
116 if ($line =~ m/__[us](8|16|32|64)\b/) {
117 printf STDERR "$filename:$lineno: " .
118 "found __[us]{8,16,32,64} type " .
119 "without #include <linux/types.h>\n";
120 $linux_types = 2;
121 # Warn until headers are all fixed
122 #$ret = 1;
123 }
124}
125