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