documentation-file-ref-check 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (! -d ".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. # Skip this script
  75. next if ($f eq $scriptname);
  76. # Ignore the dir where documentation will be built
  77. next if ($ln =~ m,\b(\S*)Documentation/output,);
  78. if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
  79. my $prefix = $1;
  80. my $ref = $2;
  81. my $base = $2;
  82. my $extra = $3;
  83. # some file references are like:
  84. # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
  85. # For now, ignore them
  86. next if ($extra =~ m/^{/);
  87. # Remove footnotes at the end like:
  88. # Documentation/devicetree/dt-object-internal.txt[1]
  89. $ref =~ s/(txt|rst)\[\d+]$/$1/;
  90. # Remove ending ']' without any '['
  91. $ref =~ s/\].*// if (!($ref =~ m/\[/));
  92. # Remove puntuation marks at the end
  93. $ref =~ s/[\,\.]+$//;
  94. my $fulref = "$prefix$ref";
  95. $fulref =~ s/^(\<file|ref)://;
  96. $fulref =~ s/^[\'\`]+//;
  97. $fulref =~ s,^\$\(.*\)/,,;
  98. $base =~ s,.*/,,;
  99. # Remove URL false-positives
  100. next if ($fulref =~ m/^http/);
  101. # Remove sched-pelt false-positive
  102. next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
  103. # Discard some build examples from Documentation/target/tcm_mod_builder.rst
  104. next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
  105. # Check if exists, evaluating wildcards
  106. next if (grep -e, glob("$ref $fulref"));
  107. # Accept relative Documentation patches for tools/
  108. if ($f =~ m/tools/) {
  109. my $path = $f;
  110. $path =~ s,(.*)/.*,$1,;
  111. next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
  112. }
  113. # Discard known false-positives
  114. if (defined($false_positives{$f})) {
  115. next if ($false_positives{$f} eq $fulref);
  116. }
  117. if ($fix) {
  118. if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
  119. $broken_ref{$ref}++;
  120. }
  121. } elsif ($warn) {
  122. print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
  123. } else {
  124. print STDERR "$f: $fulref\n";
  125. }
  126. }
  127. }
  128. close IN;
  129. exit 0 if (!$fix);
  130. # Step 2: Seek for file name alternatives
  131. print "Auto-fixing broken references. Please double-check the results\n";
  132. foreach my $ref (keys %broken_ref) {
  133. my $new =$ref;
  134. my $basedir = ".";
  135. # On translations, only seek inside the translations directory
  136. $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
  137. # get just the basename
  138. $new =~ s,.*/,,;
  139. my $f="";
  140. # usual reason for breakage: DT file moved around
  141. if ($ref =~ /devicetree/) {
  142. # usual reason for breakage: DT file renamed to .yaml
  143. if (!$f) {
  144. my $new_ref = $ref;
  145. $new_ref =~ s/\.txt$/.yaml/;
  146. $f=$new_ref if (-f $new_ref);
  147. }
  148. if (!$f) {
  149. my $search = $new;
  150. $search =~ s,^.*/,,;
  151. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  152. if (!$f) {
  153. # Manufacturer name may have changed
  154. $search =~ s/^.*,//;
  155. $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
  156. }
  157. }
  158. }
  159. # usual reason for breakage: file renamed to .rst
  160. if (!$f) {
  161. $new =~ s/\.txt$/.rst/;
  162. $f=qx(find $basedir -iname $new) if ($new);
  163. }
  164. # usual reason for breakage: use dash or underline
  165. if (!$f) {
  166. $new =~ s/[-_]/[-_]/g;
  167. $f=qx(find $basedir -iname $new) if ($new);
  168. }
  169. # Wild guess: seek for the same name on another place
  170. if (!$f) {
  171. $f = qx(find $basedir -iname $new) if ($new);
  172. }
  173. my @find = split /\s+/, $f;
  174. if (!$f) {
  175. print STDERR "ERROR: Didn't find a replacement for $ref\n";
  176. } elsif (scalar(@find) > 1) {
  177. print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
  178. foreach my $j (@find) {
  179. $j =~ s,^./,,;
  180. print STDERR " $j\n";
  181. }
  182. } else {
  183. $f = $find[0];
  184. $f =~ s,^./,,;
  185. print "INFO: Replacing $ref to $f\n";
  186. foreach my $j (qx(git grep -l $ref)) {
  187. qx(sed "s\@$ref\@$f\@g" -i $j);
  188. }
  189. }
  190. }