blob: b62c319611a25a725eeb513b1522c605fea06293 [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#
5# Usage: headers_check.pl dir [files...]
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 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) {
Jeremy Huntwork15a2ee72008-10-29 14:20:13 -070031 local *FH;
Sam Ravnborg77124012008-06-15 21:41:09 +020032 $filename = $file;
Jeremy Huntwork15a2ee72008-10-29 14:20:13 -070033 open(FH, "<$filename") or die "$filename: $!\n";
Sam Ravnborg77124012008-06-15 21:41:09 +020034 $lineno = 0;
Jeremy Huntwork15a2ee72008-10-29 14:20:13 -070035 while ($line = <FH>) {
Sam Ravnborg77124012008-06-15 21:41:09 +020036 $lineno++;
Sam Ravnborg483b4122008-12-30 11:34:58 +010037 &check_include();
38 &check_asm_types();
39 &check_sizetypes();
40 &check_prototypes();
41 &check_config();
Sam Ravnborg77124012008-06-15 21:41:09 +020042 }
Jeremy Huntwork15a2ee72008-10-29 14:20:13 -070043 close FH;
Sam Ravnborg77124012008-06-15 21:41:09 +020044}
45exit $ret;
46
47sub 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 Frysinger46b8af52008-12-27 02:43:36 -050063
64sub check_prototypes
65{
66 if ($line =~ m/^\s*extern\b/) {
67 printf STDERR "$filename:$lineno: extern's make no sense in userspace\n";
68 }
69}
Sam Ravnborg7e557a22008-12-27 19:52:20 +010070
71sub check_config
72{
73 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9]+)[^a-zA-Z0-9]/) {
74 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
75 }
76}
77
Sam Ravnborg483b4122008-12-30 11:34:58 +010078my $linux_asm_types;
79sub check_asm_types()
80{
81 if ($lineno == 1) {
82 $linux_asm_types = 0;
83 } elsif ($linux_asm_types >= 1) {
84 return;
85 }
86 if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
87 $linux_asm_types = 1;
88 printf STDERR "$filename:$lineno: " .
89 "include of <linux/types.h> is preferred over <asm/types.h>\n"
90 # Warn until headers are all fixed
91 #$ret = 1;
92 }
93}
94
95my $linux_types;
96sub check_sizetypes
97{
98 if ($lineno == 1) {
99 $linux_types = 0;
100 } elsif ($linux_types >= 1) {
101 return;
102 }
103 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
104 $linux_types = 1;
105 return;
106 }
107 if ($line =~ m/__[us](8|16|32|64)\b/) {
108 printf STDERR "$filename:$lineno: " .
109 "found __[us]{8,16,32,64} type " .
110 "without #include <linux/types.h>\n";
111 $linux_types = 2;
112 # Warn until headers are all fixed
113 #$ret = 1;
114 }
115}
116