forked from gridcf/gct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globus-vararg-enums-doxygen-filter.pl
executable file
·296 lines (252 loc) · 8.88 KB
/
globus-vararg-enums-doxygen-filter.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/perl
#
# Copyright 1999-2006 University of Chicago
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# this script expects source file as an arg
# types are defined with
# GlobusVarArgDefine(name, rettype, function, ... /* fixed params */)
# the enum will be the first param after the fixed params followed by
# any params defined in the GlobusVarArgEnum block
# enums can be preceded with 2 comment blocks, the first starting with
# GlobusVarArgEnum(<comma separated list of types>) The second is the vararg
# parameter list
use strict;
# these two are used as 'static' variables for following function
my $line = '';
my $comment = '';
# function returns either whole comment or everything between comments
sub get_next_blob()
{
my $blob = '';
if(!$line && !defined($line = <SOURCE>))
{
return '';
}
while($line)
{
if($comment)
{
# still looking for the end of the comment
if($line =~ m/\*\//)
{
my $comment_end = $line;
# extract the comment portion
$comment_end =~ s/(.*?\*\/).*\n?/$1/;
$comment_end = $comment . $comment_end;
$comment = '';
# keep remainder of line
$line =~ s/.*?\*\/(.*)/$1/;
return $comment_end;
}
$comment .= $line;
}
elsif($line =~ m/\/\*/)
{
# got a beginning of a comment
my $prefix = $line;
# keep the comment portion
$line =~ s/.*?\/\*(.*)/$1/;
# save the preceding portion
$prefix =~ s/(.*?)\/\*.*\n?/$1/;
$blob .= $prefix;
# make sure this isn't a /* embedded in a string
# this does not catch multi-line strings (which are deprecated in C)
# this is hacky perl which counts the number of matches in a string
my $count = () = $prefix =~ m/(?<!\\)\"/g;
$count -= () = $prefix =~ m/'\"'/g;
if($count % 2)
{
# odd number of un-escaped double quotes... this /* is embedded
$blob .= '/*';
# get the rest of the string so we even up quote count again
$prefix = $line;
$prefix =~ s/(.*?(?<!\\)\").*\n?/$1/;
$line =~ s/.*?(?<!\\)\"(.*)/$1/;
$blob .= $prefix;
}
else
{
$comment = '/*';
if($blob)
{
return $blob;
}
}
redo;
}
else
{
# not a comment
$blob .= $line;
}
$line = <SOURCE>;
}
# only get here on eof
if(!$blob)
{
$blob = $comment;
}
return $blob;
}
sub strip_comments($)
{
my $str = shift;
return join("", split(/(?:^\s*(?:\/\*+|\*+(?!\/)))|(?:\*+\/)/m, $str));
}
sub normalize_params($)
{
my $str = shift;
$str =~ s/^\s*(.*?)\s*$/$1/s; # trim whitespace from ends
$str =~ s/\s+/ /sg; # collaspe all whitespace into single space
$str =~ s/\s*,\s*/, /sg; # make all ',' have only one space on right
return $str;
}
############################################################################
my $source_file = shift(@ARGV) || die("Error: Missing argument!");
my %types;
my $functions = '';
open(SOURCE, $source_file)
|| die("Error: could not open $source_file");
while((my $blob = get_next_blob()))
{
if($blob =~ m/^\/\*/)
{
my $stripped = strip_comments($blob);
my @matches = ($stripped =~ m/GlobusVarArgDefine\(\s*(.*?)\s*\)/sg);
if(@matches)
{
# strip from blob
$blob =~ s/GlobusVarArgDefine\(.*?\)//sg;
foreach my $match (@matches)
{
my @params = split(/\s*,\s*/s, $match);
if(@params >= 3)
{
my $type = shift(@params);
my $rettype = shift(@params);
my $func_name = shift(@params);
$types{$type} = {
'rettype' => $rettype,
'name' => $func_name,
'begin_prototype' =>
'(' . normalize_params(join(',', @params)) };
}
else
{
print(STDERR
"Warning: GlobusVarArgDefine has too few args\n");
}
}
}
elsif($stripped =~ s/^.*GlobusVarArgEnum\((.*?)\).*$/$1/s)
{
# stripped types that we need to output for
my @types = split(/\s*,\s*/s, $stripped);
my $docblock;
my $end_prototype = ')';
# strip GlobusVarArgEnum from blob
$blob =~ s/GlobusVarArgEnum\(.*?\)/\@overload/s;
$docblock = $blob . "\n";
# next blob is either an optional prototype comment or the enum
$blob = get_next_blob();
# we might get all whitespace first
if($blob =~ m/^\s+$/s)
{
$blob = get_next_blob();
}
if($blob =~ m/^\/\*/)
{
# we've got the var arg parameters now
$blob = strip_comments($blob);
if(!($blob =~ m/^\s*$/s))
{
$end_prototype =
', ' . normalize_params($blob) . $end_prototype;
}
$blob = get_next_blob();
}
while($blob =~ m/^((\/\*)|(\s*$))/)
{
# any additional comments or whitespace we just spew out
print($blob);
$blob = get_next_blob();
}
# blob now has to be enum or bust
my $enum = $blob;
if($enum =~ s/^\s*(\w+).*$/$1/s)
{
my $comment = ' /** See usage for: ';
my $found = 0;
# create function sigs
foreach my $type (@types)
{
if(defined($types{$type}))
{
my $function = $types{$type}{'name'} .
$types{$type}{'begin_prototype'} . ', ' .
$enum . $end_prototype;
$functions .= $docblock .
$types{$type}{'rettype'} . " $function;\n";
if($found)
{
$comment .= ', ';
}
$comment .= "\@link $function " .
$types{$type}{'name'} . ' @endlink';
$found = 1;
}
else
{
print(STDERR
"Warning: undefined type '$type' in GlobusVarArgEnum\n");
}
}
$comment .= " */\n";
if($found)
{
$blob = $comment . $blob;
if(0)
{
# add comment with references to usage
if($blob =~ m/[^,}]*,/)
{
$blob =~ s/^(\s*\w+\s*(=[^,]+)?,)/$1$comment/s;
}
else
{
# last enum in the block
$blob =~
s/^(\s*\w+(\s*=[^}]+?(?=\n\s*(}|$)))?)/$1$comment/s;
}
}
}
else
{
print($docblock);
}
}
else
{
# not enum, warn and spew docblock
print(STDERR
"Warning: missing enum value after GlobusVarArgEnum block\n");
print($docblock);
}
}
}
print($blob);
}
print($functions);
exit(0);