blob: f67b125c5269ed6cd4cdb761396b1db6d237710f [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#! /usr/bin/env perl
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#
Sam Ravnborg63104ee2006-07-03 23:30:54 +02004# checkversion find uses of LINUX_VERSION_CODE or KERNEL_VERSION
5# without including <linux/version.h>, or cases of
Linus Torvalds1da177e2005-04-16 15:20:36 -07006# including <linux/version.h> that don't need it.
Adrian Bunkf4b09eb2006-01-03 13:37:51 +01007# Copyright (C) 2003, Randy Dunlap <rdunlap@xenotime.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Stephen Hemmingera2088682010-02-22 15:17:14 -08009use strict;
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011$| = 1;
12
Stephen Hemmingera2088682010-02-22 15:17:14 -080013my $debugging;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Stephen Hemmingera2088682010-02-22 15:17:14 -080015foreach my $file (@ARGV) {
Peter Foley6088e9f2011-04-26 19:07:56 -040016 next if $file =~ "include/linux/version\.h";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 # Open this file.
Stephen Hemmingera2088682010-02-22 15:17:14 -080018 open( my $f, '<', $file )
19 or die "Can't open $file: $!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21 # Initialize variables.
Stephen Hemmingera2088682010-02-22 15:17:14 -080022 my ($fInComment, $fInString, $fUseVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 my $iLinuxVersion = 0;
24
Stephen Hemmingera2088682010-02-22 15:17:14 -080025 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 # Strip comments.
27 $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
28 m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
29
30 # Pick up definitions.
31 if ( m/^\s*#/o ) {
32 $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
33 }
34
35 # Strip strings.
36 $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
37 m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
38
39 # Pick up definitions.
40 if ( m/^\s*#/o ) {
41 $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
42 }
43
44 # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION, UTS_RELEASE
Sam Ravnborg63104ee2006-07-03 23:30:54 +020045 if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 $fUseVersion = 1;
Stephen Hemmingera2088682010-02-22 15:17:14 -080047 last if $iLinuxVersion;
48 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50
51 # Report used version IDs without include?
52 if ($fUseVersion && ! $iLinuxVersion) {
53 print "$file: $.: need linux/version.h\n";
54 }
55
56 # Report superfluous includes.
57 if ($iLinuxVersion && ! $fUseVersion) {
58 print "$file: $iLinuxVersion linux/version.h not needed.\n";
59 }
60
61 # debug: report OK results:
62 if ($debugging) {
63 if ($iLinuxVersion && $fUseVersion) {
64 print "$file: version use is OK ($iLinuxVersion)\n";
65 }
66 if (! $iLinuxVersion && ! $fUseVersion) {
67 print "$file: version use is OK (none)\n";
68 }
69 }
70
Stephen Hemmingera2088682010-02-22 15:17:14 -080071 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}