cb77f0d623ff3 (Kamil Rytarowski 2017-05-07 23:25:26 +0200 1) #! /usr/bin/env perl
b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 2) # SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) #
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 4) # checkversion finds uses of all macros in <linux/version.h>
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 5) # where the source files do not #include <linux/version.h>; or cases
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 6) # of including <linux/version.h> where it is not needed.
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 7) # Copyright (C) 2003, Randy Dunlap <rdunlap@infradead.org>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8)
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 9) use strict;
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 10)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) $| = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12)
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 13) my $debugging;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14)
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 15) foreach my $file (@ARGV) {
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 16) next if $file =~ "include/generated/uapi/linux/version\.h";
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 17) next if $file =~ "usr/include/linux/version\.h";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) # Open this file.
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 19) open( my $f, '<', $file )
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 20) or die "Can't open $file: $!\n";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) # Initialize variables.
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 23) my ($fInComment, $fInString, $fUseVersion);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) my $iLinuxVersion = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25)
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 26) while (<$f>) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) # Strip comments.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28) $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29) m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 31) # Pick up definitions.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 32) if ( m/^\s*#/o ) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 34) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 36) # Strip strings.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37) $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38) m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40) # Pick up definitions.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 41) if ( m/^\s*#/o ) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42) $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 43) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 44)
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 45) # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION,
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 46) # LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 47) if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/) ||
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 48) ($_ =~ /LINUX_VERSION_MAJOR/) || ($_ =~ /LINUX_VERSION_PATCHLEVEL/) ||
54eacba0e3bbd (Randy Dunlap 2021-07-26 19:57:37 -0700 49) ($_ =~ /LINUX_VERSION_SUBLEVEL/)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 50) $fUseVersion = 1;
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 51) last if $iLinuxVersion;
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 52) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 53) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 55) # Report used version IDs without include?
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) if ($fUseVersion && ! $iLinuxVersion) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) print "$file: $.: need linux/version.h\n";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 59)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 60) # Report superfluous includes.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61) if ($iLinuxVersion && ! $fUseVersion) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 62) print "$file: $iLinuxVersion linux/version.h not needed.\n";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 63) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 65) # debug: report OK results:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66) if ($debugging) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67) if ($iLinuxVersion && $fUseVersion) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) print "$file: version use is OK ($iLinuxVersion)\n";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) if (! $iLinuxVersion && ! $fUseVersion) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) print "$file: version use is OK (none)\n";
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 73) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 74)
a208868fc0a90 (Stephen Hemminger 2010-02-22 15:17:14 -0800 75) close($f);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76) }