documentation-file-ref-check 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Treewide grep for references to files under Documentation, and report
  5. # non-existing files in stderr.
  6. use warnings;
  7. use strict;
  8. use Getopt::Long qw(:config no_auto_abbrev);
  9. # NOTE: only add things here when the file was gone, but the text wants
  10. # to mention a past documentation file, for example, to give credits for
  11. # the original work.
  12. my %false_positives = (
  13. "Documentation/scsi/scsi_mid_low_api.rst" => "Documentation/Configure.help",
  14. "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c",
  15. );
  16. my $scriptname = $0;
  17. $scriptname =~ s,.*/([^/]+/),$1,;
  18. # Parse arguments
  19. my $help = 0;
  20. my $fix = 0;
  21. my $warn = 0;
  22. if (! -e ".git") {
  23. printf "Warning: can't check if file exists, as this is not a git tree\n";
  24. exit 0;
  25. }
  26. GetOptions(
  27. 'fix' => \$fix,
  28. 'warn' => \$warn,
  29. 'h|help|usage' => \$help,
  30. );
  31. if ($help != 0) {
  32. print "$scriptname [--help] [--fix]\n";
  33. exit -1;
  34. }
  35. # Step 1: find broken references
  36. print "Finding broken references. This may take a while... " if ($fix);
  37. my %broken_ref;
  38. my $doc_fix = 0;
  39. open IN, "git grep ':doc:\`' Documentation/|"
  40. or die "Failed to run git grep";
  41. while (<IN>) {
  42. next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
  43. next if (m,sphinx/,);
  44. my $file = $1;
  45. my $d = $1;
  46. my $doc_ref = $2;
  47. my $f = $doc_ref;
  48. $d =~ s,(.*/).*,$1,;
  49. $f =~ s,.*\<([^\>]+)\>,$1,;
  50. if ($f =~ m,^/,) {
  51. $f = "$f.rst";
  52. $f =~ s,^/,Documentation/,;
  53. } else {
  54. $f = "$d$f.rst";
  55. }
  56. next if (grep -e, glob("$f"));
  57. if ($fix && !$doc_fix) {
  58. print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
  59. }
  60. $doc_fix++;
  61. print STDERR "$file: :doc:`$doc_ref`\n";
  62. }
  63. close IN;
  64. open IN, "git grep 'Documentation/'|"
  65. or die "Failed to run git grep";
  66. while (<IN>) {
  67. next if (!m/^([^:]+):(.*)/);
  68. my $f = $1;
  69. my $ln = $2;
  70. # On linux-next, discard the Next/ directory
  71. next if ($f =~ m,^Next/,);
  72. # Makefiles and scripts contain nasty expressions to parse docs
  73. next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
  74. # It doesn't make sense to parse hidden files
  75. next if ($f =~ m#/\.#);
  76. # Skip this script
  77. next if ($f eq $scriptname);
  78. # Ignore the dir where documentation will be built
  79. next if ($ln =~ m,\b(\S*)Documentation/output,);
  80. if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
  81. my $prefix = $1;
  82. my $ref = $2;
  83. my $base = $2;
  84. my $extra = $3;
  85. # some file references are like:
  86. # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
  87. # For now, ignore them
  88. next if ($extra =~ m/^{/);
  89. # Remove footnotes at the end like:
  90. # Documentation/devicetree/dt-object-internal.txt[1]
  91. $ref =~ s/(txt|rst)\[\d+]$/$1/;
  92. # Remove ending ']' without any '['
  93. $ref =~ s/\].*// if (!($ref =~ m/\[/));
  94. # Remove puntuation marks at the end
  95. $ref =~ s/[\,\.]+$//;
  96. my $fulref = "$prefix$ref";
  97. $fulref =~ s/^(\<file|ref)://;
  98. $fulref =~ s/^[\'\`]+//;
  99. $fulref =~ s,^\$\(.*\)/,,;
  100. $base =~ s,.*/,,;
  101. # Remove URL false-positives
  102. next if ($fulref =~ m/^http/);
  103. # Remove sched-pelt false-positive
  104. next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
  105. # Discard some build examples from Documentation/target/tcm_mod_builder.rst
  106. next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
  107. # Check if exists, evaluating wildcards
  108. next if (grep -e, glob("$ref $fulref"));
  109. # Accept relative Documentation patches for tools/
  110. if ($f =~ m/tools/) {
  111. my $path = $f;
  112. $path =~ s,(.*)/.*,$1,;
  113. $path =~ s,testing/selftests/bpf,bpf/bpftool,;
  114. next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
  115. }
  116. # Discard known false-positives
  117. if (defined($false_positives{$f})) {
  118. next if ($false_positives{$f} eq $fulref);
  119. }
  120. if ($fix) {
  121. if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
  122. $broken_ref{$ref}++;
  123. }
  124. } elsif ($warn) {
  125. print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
  126. } else {
  127. print STDERR "$f: $fulref\n";
  128. }
  129. }
  130. }
  131. close IN;
  132. exit 0 if (!$fix);
  133. # Step 2: Seek for file name alternatives
  134. print "Auto-fixing broken references. Please double-check the results\n";
  135. foreach my $ref (keys %broken_ref) {
  136. my $new =$ref;
  137. my $basedir = ".";
  138. # On translations, only seek inside the translations directory
  139. $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
  140. # get just the basename
  141. $new =~ s,.*/,,;
  142. my $f="";
  143. # usual reason for breakage: DT file moved around
  144. if ($ref =~ /devicetree/) {
  145. # usual reason for breakage: DT file renamed to .yaml
  146. if (!$f) {
  147. my $new_ref = $ref;
  148. $new_ref =~ s/\.txt$/.yaml/;
  149. $f=$new_ref if (-f $new_ref);
  150. }
  151. if (!$f) {
  152. my $search = $new;
  153. $search =~ s,^.*/,,;
  154. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  155. if (!$f) {
  156. # Manufacturer name may have changed
  157. $search =~ s/^.*,//;
  158. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  159. }
  160. }
  161. }
  162. # usual reason for breakage: file renamed to .rst
  163. if (!$f) {
  164. $new =~ s/\.txt$/.rst/;
  165. $f=qx(find $basedir -iname $new) if ($new);
  166. }
  167. # usual reason for breakage: use dash or underline
  168. if (!$f) {
  169. $new =~ s/[-_]/[-_]/g;
  170. $f=qx(find $basedir -iname $new) if ($new);
  171. }
  172. # Wild guess: seek for the same name on another place
  173. if (!$f) {
  174. $f = qx(find $basedir -iname $new) if ($new);
  175. }
  176. my @find = split /\s+/, $f;
  177. if (!$f) {
  178. print STDERR "ERROR: Didn't find a replacement for $ref\n";
  179. } elsif (scalar(@find) > 1) {
  180. print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
  181. foreach my $j (@find) {
  182. $j =~ s,^./,,;
  183. print STDERR " $j\n";
  184. }
  185. } else {
  186. $f = $find[0];
  187. $f =~ s,^./,,;
  188. print "INFO: Replacing $ref to $f\n";
  189. foreach my $j (qx(git grep -l $ref)) {
  190. qx(sed "s\@$ref\@$f\@g" -i $j);
  191. }
  192. }
  193. }