documentation-file-ref-check 5.0 KB

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