xref: /linux/usr/include/headers_check.pl (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3#
4# headers_check.pl execute a number of trivial consistency checks
5#
6# Usage: headers_check.pl dir [files...]
7# dir:   dir to look for included files
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#
17# 2) It is checked that prototypes does not use "extern"
18#
19# 3) Check for leaked CONFIG_ symbols
20
21use warnings;
22use strict;
23use File::Basename;
24
25my ($dir, @files) = @ARGV;
26
27my $ret = 0;
28my $line;
29my $lineno = 0;
30my $filename;
31
32foreach my $file (@files) {
33	$filename = $file;
34
35	open(my $fh, '<', $filename)
36		or die "$filename: $!\n";
37	$lineno = 0;
38	while ($line = <$fh>) {
39		$lineno++;
40		&check_include();
41		&check_asm_types();
42		&check_declarations();
43	}
44	close $fh;
45}
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			printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
56			$ret = 1;
57		}
58	}
59}
60
61sub check_declarations
62{
63	# soundcard.h is what it is
64	if ($line =~ m/^void seqbuf_dump\(void\);/) {
65		return;
66	}
67	# drm headers are being C++ friendly
68	if ($line =~ m/^extern "C"/) {
69		return;
70	}
71	if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
72		printf STDERR "$filename:$lineno: " .
73			      "userspace cannot reference function or " .
74			      "variable defined in the kernel\n";
75		$ret = 1;
76	}
77}
78
79my $linux_asm_types;
80sub check_asm_types
81{
82	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
83		return;
84	}
85	if ($lineno == 1) {
86		$linux_asm_types = 0;
87	} elsif ($linux_asm_types >= 1) {
88		return;
89	}
90	if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
91		$linux_asm_types = 1;
92		printf STDERR "$filename:$lineno: " .
93		"include of <linux/types.h> is preferred over <asm/types.h>\n";
94		$ret = 1;
95	}
96}
97