perf intel-pt: Synthesize PEBS sample basic information
[linux/fpc-iii.git] / scripts / documentation-file-ref-check
blob63e9542656f1d93d8e01a74656ca9ff06efdda1e
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: GPL-2.0
4 # Treewide grep for references to files under Documentation, and report
5 # non-existing files in stderr.
7 use warnings;
8 use strict;
9 use Getopt::Long qw(:config no_auto_abbrev);
11 my $scriptname = $0;
12 $scriptname =~ s,.*/([^/]+/),$1,;
14 # Parse arguments
15 my $help = 0;
16 my $fix = 0;
18 GetOptions(
19 'fix' => \$fix,
20 'h|help|usage' => \$help,
23 if ($help != 0) {
24 print "$scriptname [--help] [--fix]\n";
25 exit -1;
28 # Step 1: find broken references
29 print "Finding broken references. This may take a while... " if ($fix);
31 my %broken_ref;
33 my $doc_fix = 0;
35 open IN, "git grep ':doc:\`' Documentation/|"
36 or die "Failed to run git grep";
37 while (<IN>) {
38 next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
40 my $d = $1;
41 my $doc_ref = $2;
43 my $f = $doc_ref;
45 $d =~ s,(.*/).*,$1,;
46 $f =~ s,.*\<([^\>]+)\>,$1,;
48 $f ="$d$f.rst";
50 next if (grep -e, glob("$f"));
52 if ($fix && !$doc_fix) {
53 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
55 $doc_fix++;
57 print STDERR "$f: :doc:`$doc_ref`\n";
59 close IN;
61 open IN, "git grep 'Documentation/'|"
62 or die "Failed to run git grep";
63 while (<IN>) {
64 next if (!m/^([^:]+):(.*)/);
66 my $f = $1;
67 my $ln = $2;
69 # On linux-next, discard the Next/ directory
70 next if ($f =~ m,^Next/,);
72 # Makefiles and scripts contain nasty expressions to parse docs
73 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
75 # Skip this script
76 next if ($f eq $scriptname);
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;
84 # some file references are like:
85 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
86 # For now, ignore them
87 next if ($extra =~ m/^{/);
89 # Remove footnotes at the end like:
90 # Documentation/devicetree/dt-object-internal.txt[1]
91 $ref =~ s/(txt|rst)\[\d+]$/$1/;
93 # Remove ending ']' without any '['
94 $ref =~ s/\].*// if (!($ref =~ m/\[/));
96 # Remove puntuation marks at the end
97 $ref =~ s/[\,\.]+$//;
99 my $fulref = "$prefix$ref";
101 $fulref =~ s/^(\<file|ref)://;
102 $fulref =~ s/^[\'\`]+//;
103 $fulref =~ s,^\$\(.*\)/,,;
104 $base =~ s,.*/,,;
106 # Remove URL false-positives
107 next if ($fulref =~ m/^http/);
109 # Remove sched-pelt false-positive
110 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
112 # Discard some build examples from Documentation/target/tcm_mod_builder.txt
113 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
115 # Check if exists, evaluating wildcards
116 next if (grep -e, glob("$ref $fulref"));
118 # Accept relative Documentation patches for tools/
119 if ($f =~ m/tools/) {
120 my $path = $f;
121 $path =~ s,(.*)/.*,$1,;
122 next if (grep -e, glob("$path/$ref $path/$fulref"));
125 if ($fix) {
126 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
127 $broken_ref{$ref}++;
129 } else {
130 print STDERR "$f: $fulref\n";
134 close IN;
136 exit 0 if (!$fix);
138 # Step 2: Seek for file name alternatives
139 print "Auto-fixing broken references. Please double-check the results\n";
141 foreach my $ref (keys %broken_ref) {
142 my $new =$ref;
144 # get just the basename
145 $new =~ s,.*/,,;
147 my $f="";
149 # usual reason for breakage: DT file moved around
150 if ($ref =~ /devicetree/) {
151 my $search = $new;
152 $search =~ s,^.*/,,;
153 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
154 if (!$f) {
155 # Manufacturer name may have changed
156 $search =~ s/^.*,//;
157 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
161 # usual reason for breakage: file renamed to .rst
162 if (!$f) {
163 $new =~ s/\.txt$/.rst/;
164 $f=qx(find . -iname $new) if ($new);
167 # usual reason for breakage: use dash or underline
168 if (!$f) {
169 $new =~ s/[-_]/[-_]/g;
170 $f=qx(find . -iname $new) if ($new);
173 # Wild guess: seek for the same name on another place
174 if (!$f) {
175 $f = qx(find . -iname $new) if ($new);
178 my @find = split /\s+/, $f;
180 if (!$f) {
181 print STDERR "ERROR: Didn't find a replacement for $ref\n";
182 } elsif (scalar(@find) > 1) {
183 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
184 foreach my $j (@find) {
185 $j =~ s,^./,,;
186 print STDERR " $j\n";
188 } else {
189 $f = $find[0];
190 $f =~ s,^./,,;
191 print "INFO: Replacing $ref to $f\n";
192 foreach my $j (qx(git grep -l $ref)) {
193 qx(sed "s\@$ref\@$f\@g" -i $j);