2 # SPDX-License-Identifier: GPL-2.0
4 # headers_check.pl execute a number of trivial consistency checks
6 # Usage: headers_check.pl dir arch [files...]
7 # dir: dir to look for included files
9 # files: list of files to check
11 # The script reads the supplied files line by line and:
13 # 1) for each include statement it checks if the
14 # included file actually exists.
15 # Only include files located in asm* and linux* are checked.
16 # The rest are assumed to be system include files.
18 # 2) It is checked that prototypes does not use "extern"
20 # 3) Check for leaked CONFIG_ symbols
26 my ($dir, $arch, @files) = @ARGV;
33 foreach my $file (@files) {
36 open(my $fh, '<', $filename)
37 or die "$filename: $!\n";
39 while ($line = <$fh>) {
44 &check_declarations();
45 # Dropped for now. Too much noise &check_config();
53 if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
56 $found = stat($dir . "/" . $inc);
58 $inc =~ s#asm/#asm-$arch/#;
59 $found = stat($dir . "/" . $inc);
62 printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
68 sub check_declarations
70 # soundcard.h is what it is
71 if ($line =~ m/^void seqbuf_dump\(void\);/) {
74 # drm headers are being C++ friendly
75 if ($line =~ m/^extern "C"/) {
78 if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
79 printf STDERR "$filename:$lineno: " .
80 "userspace cannot reference function or " .
81 "variable defined in the kernel\n";
87 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
88 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
95 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
100 } elsif ($linux_asm_types >= 1) {
103 if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
104 $linux_asm_types = 1;
105 printf STDERR "$filename:$lineno: " .
106 "include of <linux/types.h> is preferred over <asm/types.h>\n"
107 # Warn until headers are all fixed
113 my %import_stack = ();
114 sub check_include_typesh
120 my @file_paths = ($path, $dir . "/" . $path, dirname($filename) . "/" . $path);
121 for my $possible ( @file_paths ) {
122 if (not $import_stack{$possible} and open($fh, '<', $possible)) {
123 $import_path = $possible;
124 $import_stack{$import_path} = 1;
133 while ($line = <$fh>) {
134 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
138 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
139 check_include_typesh($included);
143 delete $import_stack{$import_path};
148 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
153 } elsif ($linux_types >= 1) {
156 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
160 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
161 check_include_typesh($included);
163 if ($line =~ m/__[us](8|16|32|64)\b/) {
164 printf STDERR "$filename:$lineno: " .
165 "found __[us]{8,16,32,64} type " .
166 "without #include <linux/types.h>\n";
168 # Warn until headers are all fixed