hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/scripts/documentation-file-ref-check
....@@ -8,15 +8,30 @@
88 use strict;
99 use Getopt::Long qw(:config no_auto_abbrev);
1010
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.
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",
17
+);
18
+
1119 my $scriptname = $0;
1220 $scriptname =~ s,.*/([^/]+/),$1,;
1321
1422 # Parse arguments
1523 my $help = 0;
1624 my $fix = 0;
25
+my $warn = 0;
26
+
27
+if (! -d ".git") {
28
+ printf "Warning: can't check if file exists, as this is not a git tree\n";
29
+ exit 0;
30
+}
1731
1832 GetOptions(
1933 'fix' => \$fix,
34
+ 'warn' => \$warn,
2035 'h|help|usage' => \$help,
2136 );
2237
....@@ -30,6 +45,41 @@
3045
3146 my %broken_ref;
3247
48
+my $doc_fix = 0;
49
+
50
+open IN, "git grep ':doc:\`' Documentation/|"
51
+ or die "Failed to run git grep";
52
+while (<IN>) {
53
+ next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
54
+ next if (m,sphinx/,);
55
+
56
+ my $file = $1;
57
+ my $d = $1;
58
+ my $doc_ref = $2;
59
+
60
+ my $f = $doc_ref;
61
+
62
+ $d =~ s,(.*/).*,$1,;
63
+ $f =~ s,.*\<([^\>]+)\>,$1,;
64
+
65
+ if ($f =~ m,^/,) {
66
+ $f = "$f.rst";
67
+ $f =~ s,^/,Documentation/,;
68
+ } else {
69
+ $f = "$d$f.rst";
70
+ }
71
+
72
+ next if (grep -e, glob("$f"));
73
+
74
+ if ($fix && !$doc_fix) {
75
+ print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
76
+ }
77
+ $doc_fix++;
78
+
79
+ print STDERR "$file: :doc:`$doc_ref`\n";
80
+}
81
+close IN;
82
+
3383 open IN, "git grep 'Documentation/'|"
3484 or die "Failed to run git grep";
3585 while (<IN>) {
....@@ -38,11 +88,17 @@
3888 my $f = $1;
3989 my $ln = $2;
4090
91
+ # On linux-next, discard the Next/ directory
92
+ next if ($f =~ m,^Next/,);
93
+
4194 # Makefiles and scripts contain nasty expressions to parse docs
4295 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
4396
4497 # Skip this script
4598 next if ($f eq $scriptname);
99
+
100
+ # Ignore the dir where documentation will be built
101
+ next if ($ln =~ m,\b(\S*)Documentation/output,);
46102
47103 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
48104 my $prefix = $1;
....@@ -78,7 +134,7 @@
78134 # Remove sched-pelt false-positive
79135 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
80136
81
- # Discard some build examples from Documentation/target/tcm_mod_builder.txt
137
+ # Discard some build examples from Documentation/target/tcm_mod_builder.rst
82138 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
83139
84140 # Check if exists, evaluating wildcards
....@@ -88,18 +144,26 @@
88144 if ($f =~ m/tools/) {
89145 my $path = $f;
90146 $path =~ s,(.*)/.*,$1,;
91
- next if (grep -e, glob("$path/$ref $path/$fulref"));
147
+ next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
148
+ }
149
+
150
+ # Discard known false-positives
151
+ if (defined($false_positives{$f})) {
152
+ next if ($false_positives{$f} eq $fulref);
92153 }
93154
94155 if ($fix) {
95156 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
96157 $broken_ref{$ref}++;
97158 }
159
+ } elsif ($warn) {
160
+ print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
98161 } else {
99162 print STDERR "$f: $fulref\n";
100163 }
101164 }
102165 }
166
+close IN;
103167
104168 exit 0 if (!$fix);
105169
....@@ -109,6 +173,10 @@
109173 foreach my $ref (keys %broken_ref) {
110174 my $new =$ref;
111175
176
+ my $basedir = ".";
177
+ # On translations, only seek inside the translations directory
178
+ $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
179
+
112180 # get just the basename
113181 $new =~ s,.*/,,;
114182
....@@ -116,31 +184,40 @@
116184
117185 # usual reason for breakage: DT file moved around
118186 if ($ref =~ /devicetree/) {
119
- my $search = $new;
120
- $search =~ s,^.*/,,;
121
- $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
187
+ # usual reason for breakage: DT file renamed to .yaml
122188 if (!$f) {
123
- # Manufacturer name may have changed
124
- $search =~ s/^.*,//;
189
+ my $new_ref = $ref;
190
+ $new_ref =~ s/\.txt$/.yaml/;
191
+ $f=$new_ref if (-f $new_ref);
192
+ }
193
+
194
+ if (!$f) {
195
+ my $search = $new;
196
+ $search =~ s,^.*/,,;
125197 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
198
+ if (!$f) {
199
+ # Manufacturer name may have changed
200
+ $search =~ s/^.*,//;
201
+ $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
202
+ }
126203 }
127204 }
128205
129206 # usual reason for breakage: file renamed to .rst
130207 if (!$f) {
131208 $new =~ s/\.txt$/.rst/;
132
- $f=qx(find . -iname $new) if ($new);
209
+ $f=qx(find $basedir -iname $new) if ($new);
133210 }
134211
135212 # usual reason for breakage: use dash or underline
136213 if (!$f) {
137214 $new =~ s/[-_]/[-_]/g;
138
- $f=qx(find . -iname $new) if ($new);
215
+ $f=qx(find $basedir -iname $new) if ($new);
139216 }
140217
141218 # Wild guess: seek for the same name on another place
142219 if (!$f) {
143
- $f = qx(find . -iname $new) if ($new);
220
+ $f = qx(find $basedir -iname $new) if ($new);
144221 }
145222
146223 my @find = split /\s+/, $f;