162306a36Sopenharmony_ci#! /usr/bin/env perl
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
362306a36Sopenharmony_ci#
462306a36Sopenharmony_ci# checkversion finds uses of all macros in <linux/version.h>
562306a36Sopenharmony_ci# where the source files do not #include <linux/version.h>; or cases
662306a36Sopenharmony_ci# of including <linux/version.h> where it is not needed.
762306a36Sopenharmony_ci# Copyright (C) 2003, Randy Dunlap <rdunlap@infradead.org>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciuse strict;
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci$| = 1;
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cimy $debugging;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciforeach my $file (@ARGV) {
1662306a36Sopenharmony_ci    next if $file =~ "include/generated/uapi/linux/version\.h";
1762306a36Sopenharmony_ci    next if $file =~ "usr/include/linux/version\.h";
1862306a36Sopenharmony_ci    # Open this file.
1962306a36Sopenharmony_ci    open( my $f, '<', $file )
2062306a36Sopenharmony_ci      or die "Can't open $file: $!\n";
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci    # Initialize variables.
2362306a36Sopenharmony_ci    my ($fInComment, $fInString, $fUseVersion);
2462306a36Sopenharmony_ci    my $iLinuxVersion = 0;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci    while (<$f>) {
2762306a36Sopenharmony_ci	# Strip comments.
2862306a36Sopenharmony_ci	$fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
2962306a36Sopenharmony_ci	m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	# Pick up definitions.
3262306a36Sopenharmony_ci	if ( m/^\s*#/o ) {
3362306a36Sopenharmony_ci	    $iLinuxVersion      = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
3462306a36Sopenharmony_ci	}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	# Strip strings.
3762306a36Sopenharmony_ci	$fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
3862306a36Sopenharmony_ci	m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	# Pick up definitions.
4162306a36Sopenharmony_ci	if ( m/^\s*#/o ) {
4262306a36Sopenharmony_ci	    $iLinuxVersion      = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	# Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION,
4662306a36Sopenharmony_ci	# LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL
4762306a36Sopenharmony_ci	if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/) ||
4862306a36Sopenharmony_ci	    ($_ =~ /LINUX_VERSION_MAJOR/) || ($_ =~ /LINUX_VERSION_PATCHLEVEL/) ||
4962306a36Sopenharmony_ci	    ($_ =~ /LINUX_VERSION_SUBLEVEL/)) {
5062306a36Sopenharmony_ci	    $fUseVersion = 1;
5162306a36Sopenharmony_ci            last if $iLinuxVersion;
5262306a36Sopenharmony_ci        }
5362306a36Sopenharmony_ci    }
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci    # Report used version IDs without include?
5662306a36Sopenharmony_ci    if ($fUseVersion && ! $iLinuxVersion) {
5762306a36Sopenharmony_ci	print "$file: $.: need linux/version.h\n";
5862306a36Sopenharmony_ci    }
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci    # Report superfluous includes.
6162306a36Sopenharmony_ci    if ($iLinuxVersion && ! $fUseVersion) {
6262306a36Sopenharmony_ci	print "$file: $iLinuxVersion linux/version.h not needed.\n";
6362306a36Sopenharmony_ci    }
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci    # debug: report OK results:
6662306a36Sopenharmony_ci    if ($debugging) {
6762306a36Sopenharmony_ci        if ($iLinuxVersion && $fUseVersion) {
6862306a36Sopenharmony_ci	    print "$file: version use is OK ($iLinuxVersion)\n";
6962306a36Sopenharmony_ci        }
7062306a36Sopenharmony_ci        if (! $iLinuxVersion && ! $fUseVersion) {
7162306a36Sopenharmony_ci	    print "$file: version use is OK (none)\n";
7262306a36Sopenharmony_ci        }
7362306a36Sopenharmony_ci    }
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci    close($f);
7662306a36Sopenharmony_ci}
77