2 # SPDX-License-Identifier: GPL-2.0
4 # Treewide grep for references to files under Documentation, and report
5 # non-existing files in stderr.
9 use Getopt
::Long
qw(:config no_auto_abbrev);
11 # NOTE: only add things here when the file was gone, but the text wants
12 # to mention a past documentation file, for example, to give credits for
14 my %false_positives = (
15 "Documentation/scsi/scsi_mid_low_api.rst" => "Documentation/Configure.help",
16 "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c",
20 $scriptname =~ s
,.*/([^/]+/),$1,;
28 printf "Warning: can't check if file exists, as this is not a git tree\n";
35 'h|help|usage' => \
$help,
39 print "$scriptname [--help] [--fix]\n";
43 # Step 1: find broken references
44 print "Finding broken references. This may take a while... " if ($fix);
50 open IN
, "git grep ':doc:\`' Documentation/|"
51 or die "Failed to run git grep";
53 next if (!m
,^([^:]+):.*\
:doc\
:\
`([^\`]+)\
`,);
63 $f =~ s,.*\<([^\>]+)\>,$1,;
67 $f =~ s,^/,Documentation/,;
72 next if (grep -e, glob("$f"));
74 if ($fix && !$doc_fix) {
75 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
79 print STDERR "$file: :doc:`$doc_ref`\n";
83 open IN, "git grep 'Documentation/'|"
84 or die "Failed to run git grep";
86 next if (!m/^([^:]+):(.*)/);
91 # On linux-next, discard the Next/ directory
92 next if ($f =~ m,^Next/,);
94 # Makefiles and scripts contain nasty expressions to parse docs
95 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
97 # It doesn't make sense to parse hidden files
98 next if ($f =~ m#/\.#);
101 next if ($f eq $scriptname);
103 # Ignore the dir where documentation will be built
104 next if ($ln =~ m,\b(\S*)Documentation/output,);
106 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
112 # some file references are like:
113 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
114 # For now, ignore them
115 next if ($extra =~ m/^{/);
117 # Remove footnotes at the end like:
118 # Documentation/devicetree/dt-object-internal.txt[1]
119 $ref =~ s/(txt|rst)\[\d+]$/$1/;
121 # Remove ending ']' without any '['
122 $ref =~ s/\].*// if (!($ref =~ m/\[/));
124 # Remove puntuation marks at the end
125 $ref =~ s/[\,\.]+$//;
127 my $fulref = "$prefix$ref";
129 $fulref =~ s/^(\<file|ref)://;
130 $fulref =~ s/^[\'\`]+//;
131 $fulref =~ s,^\$\(.*\)/,,;
134 # Remove URL false-positives
135 next if ($fulref =~ m/^http/);
137 # Remove sched-pelt false-positive
138 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
140 # Discard some build examples from Documentation/target/tcm_mod_builder.rst
141 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
143 # Check if exists, evaluating wildcards
144 next if (grep -e, glob("$ref $fulref"));
146 # Accept relative Documentation patches for tools/
147 if ($f =~ m/tools/) {
149 $path =~ s,(.*)/.*,$1,;
150 $path =~ s,testing/selftests/bpf,bpf/bpftool,;
151 next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
154 # Discard known false-positives
155 if (defined($false_positives{$f})) {
156 next if ($false_positives{$f} eq $fulref);
160 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
164 print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
166 print STDERR "$f: $fulref\n";
174 # Step 2: Seek for file name alternatives
175 print "Auto-fixing broken references. Please double-check the results\n";
177 foreach my $ref (keys %broken_ref) {
181 # On translations, only seek inside the translations directory
182 $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
184 # get just the basename
189 # usual reason for breakage: DT file moved around
190 if ($ref =~ /devicetree/) {
191 # usual reason for breakage: DT file renamed to .yaml
194 $new_ref =~ s/\.txt$/.yaml/;
195 $f=$new_ref if (-f $new_ref);
201 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
203 # Manufacturer name may have changed
205 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
210 # usual reason for breakage: file renamed to .rst
212 $new =~ s/\.txt$/.rst/;
213 $f=qx(find $basedir -iname $new) if ($new);
216 # usual reason for breakage: use dash or underline
218 $new =~ s/[-_]/[-_]/g;
219 $f=qx(find $basedir -iname $new) if ($new);
222 # Wild guess: seek for the same name on another place
224 $f = qx(find $basedir -iname $new) if ($new);
227 my @find = split /\s+/, $f;
230 print STDERR "ERROR: Didn't find a replacement for $ref\n";
231 } elsif (scalar(@find) > 1) {
232 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
233 foreach my $j (@find) {
235 print STDERR " $j\n";
240 print "INFO: Replacing $ref to $f\n";
241 foreach my $j (qx(git grep -l $ref)) {
242 qx(sed "s\@$ref\@$f\@g" -i $j);