headers_check.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/perl -w
  2. #
  3. # headers_check.pl execute a number of trivial consistency checks
  4. #
  5. # Usage: headers_check.pl dir arch [files...]
  6. # dir: dir to look for included files
  7. # arch: architecture
  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 strict;
  21. use File::Basename;
  22. my ($dir, $arch, @files) = @ARGV;
  23. my $ret = 0;
  24. my $line;
  25. my $lineno = 0;
  26. my $filename;
  27. foreach my $file (@files) {
  28. $filename = $file;
  29. open(my $fh, '<', $filename)
  30. or die "$filename: $!\n";
  31. $lineno = 0;
  32. while ($line = <$fh>) {
  33. $lineno++;
  34. &check_include();
  35. &check_asm_types();
  36. &check_sizetypes();
  37. &check_declarations();
  38. # Dropped for now. Too much noise &check_config();
  39. }
  40. close $fh;
  41. }
  42. exit $ret;
  43. sub check_include
  44. {
  45. if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
  46. my $inc = $1;
  47. my $found;
  48. $found = stat($dir . "/" . $inc);
  49. if (!$found) {
  50. $inc =~ s#asm/#asm-$arch/#;
  51. $found = stat($dir . "/" . $inc);
  52. }
  53. if (!$found) {
  54. printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
  55. $ret = 1;
  56. }
  57. }
  58. }
  59. sub check_declarations
  60. {
  61. # soundcard.h is what it is
  62. if ($line =~ m/^void seqbuf_dump\(void\);/) {
  63. return;
  64. }
  65. # drm headers are being C++ friendly
  66. if ($line =~ m/^extern "C"/) {
  67. return;
  68. }
  69. if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
  70. printf STDERR "$filename:$lineno: " .
  71. "userspace cannot reference function or " .
  72. "variable defined in the kernel\n";
  73. }
  74. }
  75. sub check_config
  76. {
  77. if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
  78. printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
  79. }
  80. }
  81. my $linux_asm_types;
  82. sub check_asm_types
  83. {
  84. if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  85. return;
  86. }
  87. if ($lineno == 1) {
  88. $linux_asm_types = 0;
  89. } elsif ($linux_asm_types >= 1) {
  90. return;
  91. }
  92. if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
  93. $linux_asm_types = 1;
  94. printf STDERR "$filename:$lineno: " .
  95. "include of <linux/types.h> is preferred over <asm/types.h>\n"
  96. # Warn until headers are all fixed
  97. #$ret = 1;
  98. }
  99. }
  100. my $linux_types;
  101. my %import_stack = ();
  102. sub check_include_typesh
  103. {
  104. my $path = $_[0];
  105. my $import_path;
  106. my $fh;
  107. my @file_paths = ($path, $dir . "/" . $path, dirname($filename) . "/" . $path);
  108. for my $possible ( @file_paths ) {
  109. if (not $import_stack{$possible} and open($fh, '<', $possible)) {
  110. $import_path = $possible;
  111. $import_stack{$import_path} = 1;
  112. last;
  113. }
  114. }
  115. if (eof $fh) {
  116. return;
  117. }
  118. my $line;
  119. while ($line = <$fh>) {
  120. if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  121. $linux_types = 1;
  122. last;
  123. }
  124. if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
  125. check_include_typesh($included);
  126. }
  127. }
  128. close $fh;
  129. delete $import_stack{$import_path};
  130. }
  131. sub check_sizetypes
  132. {
  133. if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  134. return;
  135. }
  136. if ($lineno == 1) {
  137. $linux_types = 0;
  138. } elsif ($linux_types >= 1) {
  139. return;
  140. }
  141. if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  142. $linux_types = 1;
  143. return;
  144. }
  145. if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
  146. check_include_typesh($included);
  147. }
  148. if ($line =~ m/__[us](8|16|32|64)\b/) {
  149. printf STDERR "$filename:$lineno: " .
  150. "found __[us]{8,16,32,64} type " .
  151. "without #include <linux/types.h>\n";
  152. $linux_types = 2;
  153. # Warn until headers are all fixed
  154. #$ret = 1;
  155. }
  156. }