forked from neovim/neovim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovedocs.pl
executable file
·180 lines (146 loc) · 3.35 KB
/
movedocs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/perl
use strict;
use warnings;
if ($ARGV[0] eq '--help') {
print << "EOF";
Usage:
$0 file.h file.c
Removes documentation attached to function declarations in file.h and adds them
to function definitions found in file.c.
$0 file.c
Moves documentation attached to function declaration present in the same file as
the definition.
EOF
exit 0;
}
my $hfile = shift @ARGV;
my @cfiles = @ARGV;
my %docs = ();
my $F;
sub write_lines {
my $file = shift;
my @lines = @_;
my $F;
open $F, '>', $file;
print $F (join "", @lines);
close $F;
}
if (@cfiles) {
open $F, '<', $hfile
or die "Failed to open $hfile.";
my @hlines = ();
my $lastdoc = '';
while (<$F>) {
if (/^\/\/\/?/) {
$lastdoc .= $_;
} elsif (/^\S.*?(\w+)\(.*(?:,|\);?|FUNC_ATTR_\w+;?)$/) {
die "Documentation for $1 was already defined" if (defined $docs{$1});
if ($lastdoc ne '') {
$docs{$1} = $lastdoc;
$lastdoc = '';
}
push @hlines, $_;
} elsif ($lastdoc ne '') {
push @hlines, $lastdoc;
$lastdoc = '';
push @hlines, $_;
} else {
push @hlines, $_;
}
}
close $F;
my %clines_hash = ();
for my $cfile (@cfiles) {
open $F, '<', $cfile
or die "Failed to open $cfile.";
my @clines = ();
while (<$F>) {
if (/^\S.*?(\w+)\(.*[,)]$/ and defined $docs{$1}) {
push @clines, $docs{$1};
delete $docs{$1};
} elsif (/^(?!static\s)\S.*?(\w+)\(.*[,)]$/ and not defined $docs{$1}) {
print STDERR "Documentation not defined for $1\n";
}
push @clines, $_;
}
close $F;
$clines_hash{$cfile} = \@clines;
}
while (my ($func, $value) = each %docs) {
die "Function not found: $func\n";
}
write_lines($hfile, @hlines);
while (my ($cfile, $clines) = each %clines_hash) {
write_lines($cfile, @$clines);
}
} else {
open $F, '<', $hfile;
my @lines;
my $lastdoc = '';
my $defstart = '';
my $funcname;
sub clear_lastdoc {
if ($lastdoc ne '') {
push @lines, $lastdoc;
$lastdoc = '';
}
}
sub record_lastdoc {
my $funcname = shift;
if ($lastdoc ne '') {
$docs{$funcname} = $lastdoc;
$lastdoc = '';
}
}
sub add_doc {
my $funcname = shift;
if (defined $docs{$funcname}) {
push @lines, $docs{$funcname};
delete $docs{$funcname};
}
}
sub clear_defstart {
push @lines, $defstart;
$defstart = '';
}
while (<$F>) {
if (/\/\*/ .. /\*\// and not /\/\*.*?\*\//) {
push @lines, $_;
} elsif (/^\/\/\/?/) {
$lastdoc .= $_;
} elsif (/^\S.*?(\w+)\(.*(?:,|(\);?))$/) {
if (not $2) {
$defstart .= $_;
$funcname = $1;
} elsif ($2 eq ');') {
record_lastdoc $1;
push @lines, $_;
} elsif ($2 eq ')') {
clear_lastdoc;
add_doc $1;
push @lines, $_;
}
} elsif ($defstart ne '') {
$defstart .= $_;
if (/[{}]/) {
clear_lastdoc;
clear_defstart;
} elsif (/\);$/) {
record_lastdoc $funcname;
clear_defstart;
} elsif (/\)$/) {
clear_lastdoc;
add_doc $funcname;
clear_defstart;
}
} else {
clear_lastdoc;
push @lines, $_;
}
}
close $F;
while (my ($func, $value) = each %docs) {
die "Function not found: $func\n";
}
write_lines($hfile, @lines);
}