Blame view

scripts/headers_check.pl 2.69 KB
15a2ee74d   Jeremy Huntwork   Fix incompatibili...
1
  #!/usr/bin/perl -w
7712401ae   Sam Ravnborg   kbuild: optimize ...
2
3
4
  #
  # headers_check.pl execute a number of trivial consistency checks
  #
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
5
  # Usage: headers_check.pl dir arch [files...]
7712401ae   Sam Ravnborg   kbuild: optimize ...
6
7
8
9
10
11
12
13
14
15
16
  # dir:   dir to look for included files
  # arch:  architecture
  # files: list of files to check
  #
  # The script reads the supplied files line by line and:
  #
  # 1) for each include statement it checks if the
  #    included file actually exists.
  #    Only include files located in asm* and linux* are checked.
  #    The rest are assumed to be system include files.
  #
46b8af50b   Mike Frysinger   headers_check.pl:...
17
18
  # 2) It is checked that prototypes does not use "extern"
  #
7e557a250   Sam Ravnborg   kbuild: check for...
19
  # 3) Check for leaked CONFIG_ symbols
7712401ae   Sam Ravnborg   kbuild: optimize ...
20
21
  
  use strict;
7712401ae   Sam Ravnborg   kbuild: optimize ...
22
23
24
25
26
27
28
29
30
  
  my ($dir, $arch, @files) = @ARGV;
  
  my $ret = 0;
  my $line;
  my $lineno = 0;
  my $filename;
  
  foreach my $file (@files) {
15a2ee74d   Jeremy Huntwork   Fix incompatibili...
31
  	local *FH;
7712401ae   Sam Ravnborg   kbuild: optimize ...
32
  	$filename = $file;
15a2ee74d   Jeremy Huntwork   Fix incompatibili...
33
34
  	open(FH, "<$filename") or die "$filename: $!
  ";
7712401ae   Sam Ravnborg   kbuild: optimize ...
35
  	$lineno = 0;
15a2ee74d   Jeremy Huntwork   Fix incompatibili...
36
  	while ($line = <FH>) {
7712401ae   Sam Ravnborg   kbuild: optimize ...
37
  		$lineno++;
483b41218   Sam Ravnborg   kbuild: add check...
38
39
40
  		&check_include();
  		&check_asm_types();
  		&check_sizetypes();
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
41
  		&check_declarations();
7e3fa5614   Sam Ravnborg   kbuild: drop chec...
42
  		# Dropped for now. Too much noise &check_config();
7712401ae   Sam Ravnborg   kbuild: optimize ...
43
  	}
15a2ee74d   Jeremy Huntwork   Fix incompatibili...
44
  	close FH;
7712401ae   Sam Ravnborg   kbuild: optimize ...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  }
  exit $ret;
  
  sub check_include
  {
  	if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
  		my $inc = $1;
  		my $found;
  		$found = stat($dir . "/" . $inc);
  		if (!$found) {
  			$inc =~ s#asm/#asm-$arch/#;
  			$found = stat($dir . "/" . $inc);
  		}
  		if (!$found) {
  			printf STDERR "$filename:$lineno: included file '$inc' is not exported
  ";
  			$ret = 1;
  		}
  	}
  }
46b8af50b   Mike Frysinger   headers_check.pl:...
65

67b7ebe09   Amerigo Wang   kbuild/headers_ch...
66
  sub check_declarations
46b8af50b   Mike Frysinger   headers_check.pl:...
67
  {
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
68
69
70
71
72
  	if ($line =~m/^\s*extern\b/) {
  		printf STDERR "$filename:$lineno: " .
  		              "userspace cannot call function or variable " .
  		              "defined in the kernel
  ";
46b8af50b   Mike Frysinger   headers_check.pl:...
73
74
  	}
  }
7e557a250   Sam Ravnborg   kbuild: check for...
75
76
77
  
  sub check_config
  {
1581c1ced   Robert P. J. Day   scripts/headers_c...
78
  	if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
7e557a250   Sam Ravnborg   kbuild: check for...
79
80
81
82
  		printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid
  ";
  	}
  }
483b41218   Sam Ravnborg   kbuild: add check...
83
84
85
  my $linux_asm_types;
  sub check_asm_types()
  {
b67ff8ce1   Sam Ravnborg   kbuild: ignore a ...
86
87
88
  	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  		return;
  	}
483b41218   Sam Ravnborg   kbuild: add check...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  	if ($lineno == 1) {
  		$linux_asm_types = 0;
  	} elsif ($linux_asm_types >= 1) {
  		return;
  	}
  	if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
  		$linux_asm_types = 1;
  		printf STDERR "$filename:$lineno: " .
  		"include of <linux/types.h> is preferred over <asm/types.h>
  "
  		# Warn until headers are all fixed
  		#$ret = 1;
  	}
  }
  
  my $linux_types;
  sub check_sizetypes
  {
b67ff8ce1   Sam Ravnborg   kbuild: ignore a ...
107
108
109
  	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  		return;
  	}
483b41218   Sam Ravnborg   kbuild: add check...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  	if ($lineno == 1) {
  		$linux_types = 0;
  	} elsif ($linux_types >= 1) {
  		return;
  	}
  	if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  		$linux_types = 1;
  		return;
  	}
  	if ($line =~ m/__[us](8|16|32|64)\b/) {
  		printf STDERR "$filename:$lineno: " .
  		              "found __[us]{8,16,32,64} type " .
  		              "without #include <linux/types.h>
  ";
  		$linux_types = 2;
  		# Warn until headers are all fixed
  		#$ret = 1;
  	}
  }