headers_check.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # headers_check.pl execute a number of trivial consistency checks
  5. #
  6. # Usage: headers_check.pl dir [files...]
  7. # dir: dir to look for included files
  8. # files: list of files to check
  9. #
  10. # The script reads the supplied files line by line and:
  11. #
  12. # 1) for each include statement it checks if the
  13. # included file actually exists.
  14. # Only include files located in asm* and linux* are checked.
  15. # The rest are assumed to be system include files.
  16. #
  17. # 2) It is checked that prototypes does not use "extern"
  18. #
  19. # 3) Check for leaked CONFIG_ symbols
  20. use warnings;
  21. use strict;
  22. use File::Basename;
  23. my ($dir, @files) = @ARGV;
  24. my $ret = 0;
  25. my $line;
  26. my $lineno = 0;
  27. my $filename;
  28. foreach my $file (@files) {
  29. $filename = $file;
  30. open(my $fh, '<', $filename)
  31. or die "$filename: $!\n";
  32. $lineno = 0;
  33. while ($line = <$fh>) {
  34. $lineno++;
  35. &check_include();
  36. &check_asm_types();
  37. &check_declarations();
  38. }
  39. close $fh;
  40. }
  41. exit $ret;
  42. sub check_include
  43. {
  44. if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
  45. my $inc = $1;
  46. my $found;
  47. $found = stat($dir . "/" . $inc);
  48. if (!$found) {
  49. printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
  50. $ret = 1;
  51. }
  52. }
  53. }
  54. sub check_declarations
  55. {
  56. # soundcard.h is what it is
  57. if ($line =~ m/^void seqbuf_dump\(void\);/) {
  58. return;
  59. }
  60. # drm headers are being C++ friendly
  61. if ($line =~ m/^extern "C"/) {
  62. return;
  63. }
  64. if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
  65. printf STDERR "$filename:$lineno: " .
  66. "userspace cannot reference function or " .
  67. "variable defined in the kernel\n";
  68. $ret = 1;
  69. }
  70. }
  71. my $linux_asm_types;
  72. sub check_asm_types
  73. {
  74. if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  75. return;
  76. }
  77. if ($lineno == 1) {
  78. $linux_asm_types = 0;
  79. } elsif ($linux_asm_types >= 1) {
  80. return;
  81. }
  82. if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
  83. $linux_asm_types = 1;
  84. printf STDERR "$filename:$lineno: " .
  85. "include of <linux/types.h> is preferred over <asm/types.h>\n";
  86. $ret = 1;
  87. }
  88. }