blob: b444e89a0095dc16d207fb23a7600a148c67834b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#! /usr/bin/perl
2#
Sam Ravnborg63104ee2006-07-03 23:30:54 +02003# checkversion find uses of LINUX_VERSION_CODE or KERNEL_VERSION
4# without including <linux/version.h>, or cases of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005# including <linux/version.h> that don't need it.
Adrian Bunkf4b09eb2006-01-03 13:37:51 +01006# Copyright (C) 2003, Randy Dunlap <rdunlap@xenotime.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Stephen Hemmingera2088682010-02-22 15:17:14 -08008use strict;
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010$| = 1;
11
Stephen Hemmingera2088682010-02-22 15:17:14 -080012my $debugging;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Stephen Hemmingera2088682010-02-22 15:17:14 -080014foreach my $file (@ARGV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 # Open this file.
Stephen Hemmingera2088682010-02-22 15:17:14 -080016 open( my $f, '<', $file )
17 or die "Can't open $file: $!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19 # Initialize variables.
Stephen Hemmingera2088682010-02-22 15:17:14 -080020 my ($fInComment, $fInString, $fUseVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 my $iLinuxVersion = 0;
22
Stephen Hemmingera2088682010-02-22 15:17:14 -080023 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 # Strip comments.
25 $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
26 m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
27
28 # Pick up definitions.
29 if ( m/^\s*#/o ) {
30 $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
31 }
32
33 # Strip strings.
34 $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
35 m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
36
37 # Pick up definitions.
38 if ( m/^\s*#/o ) {
39 $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
40 }
41
42 # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION, UTS_RELEASE
Sam Ravnborg63104ee2006-07-03 23:30:54 +020043 if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 $fUseVersion = 1;
Stephen Hemmingera2088682010-02-22 15:17:14 -080045 last if $iLinuxVersion;
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48
49 # Report used version IDs without include?
50 if ($fUseVersion && ! $iLinuxVersion) {
51 print "$file: $.: need linux/version.h\n";
52 }
53
54 # Report superfluous includes.
55 if ($iLinuxVersion && ! $fUseVersion) {
56 print "$file: $iLinuxVersion linux/version.h not needed.\n";
57 }
58
59 # debug: report OK results:
60 if ($debugging) {
61 if ($iLinuxVersion && $fUseVersion) {
62 print "$file: version use is OK ($iLinuxVersion)\n";
63 }
64 if (! $iLinuxVersion && ! $fUseVersion) {
65 print "$file: version use is OK (none)\n";
66 }
67 }
68
Stephen Hemmingera2088682010-02-22 15:17:14 -080069 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}