Back to home page

OSCL-LXR

 
 

    


0001 #! /usr/bin/env perl
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # checkversion finds uses of all macros in <linux/version.h>
0005 # where the source files do not #include <linux/version.h>; or cases
0006 # of including <linux/version.h> where it is not needed.
0007 # Copyright (C) 2003, Randy Dunlap <rdunlap@infradead.org>
0008 
0009 use strict;
0010 
0011 $| = 1;
0012 
0013 my $debugging;
0014 
0015 foreach my $file (@ARGV) {
0016     next if $file =~ "include/generated/uapi/linux/version\.h";
0017     next if $file =~ "usr/include/linux/version\.h";
0018     # Open this file.
0019     open( my $f, '<', $file )
0020       or die "Can't open $file: $!\n";
0021 
0022     # Initialize variables.
0023     my ($fInComment, $fInString, $fUseVersion);
0024     my $iLinuxVersion = 0;
0025 
0026     while (<$f>) {
0027     # Strip comments.
0028     $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
0029     m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
0030 
0031     # Pick up definitions.
0032     if ( m/^\s*#/o ) {
0033         $iLinuxVersion      = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
0034     }
0035 
0036     # Strip strings.
0037     $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
0038     m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
0039 
0040     # Pick up definitions.
0041     if ( m/^\s*#/o ) {
0042         $iLinuxVersion      = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
0043     }
0044 
0045     # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION,
0046     # LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL
0047     if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/) ||
0048         ($_ =~ /LINUX_VERSION_MAJOR/) || ($_ =~ /LINUX_VERSION_PATCHLEVEL/) ||
0049         ($_ =~ /LINUX_VERSION_SUBLEVEL/)) {
0050         $fUseVersion = 1;
0051             last if $iLinuxVersion;
0052         }
0053     }
0054 
0055     # Report used version IDs without include?
0056     if ($fUseVersion && ! $iLinuxVersion) {
0057     print "$file: $.: need linux/version.h\n";
0058     }
0059 
0060     # Report superfluous includes.
0061     if ($iLinuxVersion && ! $fUseVersion) {
0062     print "$file: $iLinuxVersion linux/version.h not needed.\n";
0063     }
0064 
0065     # debug: report OK results:
0066     if ($debugging) {
0067         if ($iLinuxVersion && $fUseVersion) {
0068         print "$file: version use is OK ($iLinuxVersion)\n";
0069         }
0070         if (! $iLinuxVersion && ! $fUseVersion) {
0071         print "$file: version use is OK (none)\n";
0072         }
0073     }
0074 
0075     close($f);
0076 }