blob: 94cd49eff6054fe3190fc7665c47df9dac9d6abb [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#
Randy Dunlap54eacba2021-07-26 19:57:37 -07004# checkversion finds uses of all macros in <linux/version.h>
5# where the source files do not #include <linux/version.h>; or cases
6# of including <linux/version.h> where it is not needed.
7# Copyright (C) 2003, Randy Dunlap <rdunlap@infradead.org>
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) {
Randy Dunlap54eacba2021-07-26 19:57:37 -070016 next if $file =~ "include/generated/uapi/linux/version\.h";
17 next if $file =~ "usr/include/linux/version\.h";
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 # Open this file.
Stephen Hemmingera2088682010-02-22 15:17:14 -080019 open( my $f, '<', $file )
20 or die "Can't open $file: $!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22 # Initialize variables.
Stephen Hemmingera2088682010-02-22 15:17:14 -080023 my ($fInComment, $fInString, $fUseVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 my $iLinuxVersion = 0;
25
Stephen Hemmingera2088682010-02-22 15:17:14 -080026 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 # Strip comments.
28 $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
29 m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
30
31 # Pick up definitions.
32 if ( m/^\s*#/o ) {
33 $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
34 }
35
36 # Strip strings.
37 $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
38 m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
39
40 # Pick up definitions.
41 if ( m/^\s*#/o ) {
42 $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
43 }
44
Randy Dunlap54eacba2021-07-26 19:57:37 -070045 # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION,
46 # LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL
47 if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/) ||
48 ($_ =~ /LINUX_VERSION_MAJOR/) || ($_ =~ /LINUX_VERSION_PATCHLEVEL/) ||
49 ($_ =~ /LINUX_VERSION_SUBLEVEL/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 $fUseVersion = 1;
Stephen Hemmingera2088682010-02-22 15:17:14 -080051 last if $iLinuxVersion;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
54
55 # Report used version IDs without include?
56 if ($fUseVersion && ! $iLinuxVersion) {
57 print "$file: $.: need linux/version.h\n";
58 }
59
60 # Report superfluous includes.
61 if ($iLinuxVersion && ! $fUseVersion) {
62 print "$file: $iLinuxVersion linux/version.h not needed.\n";
63 }
64
65 # debug: report OK results:
66 if ($debugging) {
67 if ($iLinuxVersion && $fUseVersion) {
68 print "$file: version use is OK ($iLinuxVersion)\n";
69 }
70 if (! $iLinuxVersion && ! $fUseVersion) {
71 print "$file: version use is OK (none)\n";
72 }
73 }
74
Stephen Hemmingera2088682010-02-22 15:17:14 -080075 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}