blob: 8b2da054cdc30a0ddeaf1d59dee73d220083d57c [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;
Bobby Powersf75a8df2012-03-05 15:08:09 -080022use File::Basename;
Sam Ravnborg77124012008-06-15 21:41:09 +020023
24my ($dir, $arch, @files) = @ARGV;
25
26my $ret = 0;
27my $line;
28my $lineno = 0;
29my $filename;
30
31foreach my $file (@files) {
32 $filename = $file;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080033
34 open(my $fh, '<', $filename)
35 or die "$filename: $!\n";
Sam Ravnborg77124012008-06-15 21:41:09 +020036 $lineno = 0;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080037 while ($line = <$fh>) {
Sam Ravnborg77124012008-06-15 21:41:09 +020038 $lineno++;
Sam Ravnborg483b4122008-12-30 11:34:58 +010039 &check_include();
40 &check_asm_types();
41 &check_sizetypes();
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040042 &check_declarations();
Sam Ravnborg7e3fa562009-01-30 23:56:42 +010043 # Dropped for now. Too much noise &check_config();
Sam Ravnborg77124012008-06-15 21:41:09 +020044 }
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080045 close $fh;
Sam Ravnborg77124012008-06-15 21:41:09 +020046}
47exit $ret;
48
49sub 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}
Mike Frysinger46b8af52008-12-27 02:43:36 -050065
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040066sub check_declarations
Mike Frysinger46b8af52008-12-27 02:43:36 -050067{
Paul Bollea7e1d982014-01-23 15:54:08 -080068 # soundcard.h is what it is
69 if ($line =~ m/^void seqbuf_dump\(void\);/) {
70 return;
71 }
Arnd Bergmann92181d42016-05-18 18:07:29 +020072 # drm headers are being C++ friendly
73 if ($line =~ m/^extern "C"/) {
74 return;
75 }
Paul Bollea7e1d982014-01-23 15:54:08 -080076 if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040077 printf STDERR "$filename:$lineno: " .
akpm@linux-foundation.orgd52784e2010-11-30 13:52:14 -080078 "userspace cannot reference function or " .
79 "variable defined in the kernel\n";
Mike Frysinger46b8af52008-12-27 02:43:36 -050080 }
81}
Sam Ravnborg7e557a22008-12-27 19:52:20 +010082
83sub check_config
84{
Robert P. J. Day1581c1c2009-05-12 13:43:36 -070085 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
Sam Ravnborg7e557a22008-12-27 19:52:20 +010086 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
87 }
88}
89
Sam Ravnborg483b4122008-12-30 11:34:58 +010090my $linux_asm_types;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080091sub check_asm_types
Sam Ravnborg483b4122008-12-30 11:34:58 +010092{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +010093 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
94 return;
95 }
Sam Ravnborg483b4122008-12-30 11:34:58 +010096 if ($lineno == 1) {
97 $linux_asm_types = 0;
98 } elsif ($linux_asm_types >= 1) {
99 return;
100 }
101 if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
102 $linux_asm_types = 1;
103 printf STDERR "$filename:$lineno: " .
104 "include of <linux/types.h> is preferred over <asm/types.h>\n"
105 # Warn until headers are all fixed
106 #$ret = 1;
107 }
108}
109
110my $linux_types;
Bobby Powersf75a8df2012-03-05 15:08:09 -0800111my %import_stack = ();
112sub check_include_typesh
113{
114 my $path = $_[0];
115 my $import_path;
116
117 my $fh;
118 my @file_paths = ($path, $dir . "/" . $path, dirname($filename) . "/" . $path);
119 for my $possible ( @file_paths ) {
120 if (not $import_stack{$possible} and open($fh, '<', $possible)) {
121 $import_path = $possible;
122 $import_stack{$import_path} = 1;
123 last;
124 }
125 }
126 if (eof $fh) {
127 return;
128 }
129
130 my $line;
131 while ($line = <$fh>) {
132 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
133 $linux_types = 1;
134 last;
135 }
136 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
137 check_include_typesh($included);
138 }
139 }
140 close $fh;
141 delete $import_stack{$import_path};
142}
143
Sam Ravnborg483b4122008-12-30 11:34:58 +0100144sub check_sizetypes
145{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +0100146 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
147 return;
148 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100149 if ($lineno == 1) {
150 $linux_types = 0;
151 } elsif ($linux_types >= 1) {
152 return;
153 }
154 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
155 $linux_types = 1;
156 return;
157 }
Bobby Powersf75a8df2012-03-05 15:08:09 -0800158 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
159 check_include_typesh($included);
160 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100161 if ($line =~ m/__[us](8|16|32|64)\b/) {
162 printf STDERR "$filename:$lineno: " .
163 "found __[us]{8,16,32,64} type " .
164 "without #include <linux/types.h>\n";
165 $linux_types = 2;
166 # Warn until headers are all fixed
167 #$ret = 1;
168 }
169}