checkversion.pl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # 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>
  8. use strict;
  9. $| = 1;
  10. my $debugging;
  11. foreach my $file (@ARGV) {
  12. next if $file =~ "include/generated/uapi/linux/version\.h";
  13. next if $file =~ "usr/include/linux/version\.h";
  14. # Open this file.
  15. open( my $f, '<', $file )
  16. or die "Can't open $file: $!\n";
  17. # Initialize variables.
  18. my ($fInComment, $fInString, $fUseVersion);
  19. my $iLinuxVersion = 0;
  20. while (<$f>) {
  21. # Strip comments.
  22. $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
  23. m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
  24. # Pick up definitions.
  25. if ( m/^\s*#/o ) {
  26. $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
  27. }
  28. # Strip strings.
  29. $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
  30. m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
  31. # Pick up definitions.
  32. if ( m/^\s*#/o ) {
  33. $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
  34. }
  35. # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION,
  36. # LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL
  37. if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/) ||
  38. ($_ =~ /LINUX_VERSION_MAJOR/) || ($_ =~ /LINUX_VERSION_PATCHLEVEL/) ||
  39. ($_ =~ /LINUX_VERSION_SUBLEVEL/)) {
  40. $fUseVersion = 1;
  41. last if $iLinuxVersion;
  42. }
  43. }
  44. # Report used version IDs without include?
  45. if ($fUseVersion && ! $iLinuxVersion) {
  46. print "$file: $.: need linux/version.h\n";
  47. }
  48. # Report superfluous includes.
  49. if ($iLinuxVersion && ! $fUseVersion) {
  50. print "$file: $iLinuxVersion linux/version.h not needed.\n";
  51. }
  52. # debug: report OK results:
  53. if ($debugging) {
  54. if ($iLinuxVersion && $fUseVersion) {
  55. print "$file: version use is OK ($iLinuxVersion)\n";
  56. }
  57. if (! $iLinuxVersion && ! $fUseVersion) {
  58. print "$file: version use is OK (none)\n";
  59. }
  60. }
  61. close($f);
  62. }