| 1 | #!/usr/bin/perl -w |
| 2 | ################################################################################ |
| 3 | # |
| 4 | # apicheck.pl -- generate apicheck.c: C source for automated API check |
| 5 | # |
| 6 | # WARNING: This script will be run on very old perls. You need to not use |
| 7 | # modern constructs. See HACKERS file for examples. |
| 8 | # |
| 9 | ################################################################################ |
| 10 | # |
| 11 | # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz. |
| 12 | # Version 2.x, Copyright (C) 2001, Paul Marquess. |
| 13 | # Version 1.x, Copyright (C) 1999, Kenneth Albanowski. |
| 14 | # |
| 15 | # This program is free software; you can redistribute it and/or |
| 16 | # modify it under the same terms as Perl itself. |
| 17 | # |
| 18 | ################################################################################ |
| 19 | |
| 20 | use strict; |
| 21 | require './parts/ppptools.pl'; |
| 22 | |
| 23 | if (@ARGV) { |
| 24 | my $file = pop @ARGV; |
| 25 | open OUT, ">$file" or die "$file: $!\n"; |
| 26 | } |
| 27 | else { |
| 28 | *OUT = \*STDOUT; |
| 29 | } |
| 30 | |
| 31 | # Get list of functions/macros to test |
| 32 | my @f = parse_embed(qw( parts/embed.fnc parts/apidoc.fnc parts/ppport.fnc )); |
| 33 | |
| 34 | # Read in what we've decided in previous calls should be #ifdef'd out for this |
| 35 | # call. The keys are the symbols to test; each value is a subhash, like so: |
| 36 | # 'utf8_hop_forward' => { |
| 37 | # 'version' => '5.025007' |
| 38 | # }, |
| 39 | # We don't care here about other subkeys |
| 40 | my %todo = %{&parse_todo}; |
| 41 | |
| 42 | # We convert these types into these other types |
| 43 | my %tmap = ( |
| 44 | void => 'int', |
| 45 | ); |
| 46 | |
| 47 | # These are for special marker argument names, as mentioned in embed.fnc |
| 48 | my %amap = ( |
| 49 | SP => 'SP', |
| 50 | type => 'int', |
| 51 | cast => 'int', |
| 52 | block => '{1;}', |
| 53 | ); |
| 54 | |
| 55 | # Certain return types are instead considered void |
| 56 | my %void = ( |
| 57 | void => 1, |
| 58 | Free_t => 1, |
| 59 | Signal_t => 1, |
| 60 | ); |
| 61 | |
| 62 | # khw doesn't know why these exist. These have an explicit (void) cast added. |
| 63 | # Undef'ing this hash made no difference. Maybe it's for older compilers? |
| 64 | my %castvoid = ( |
| 65 | map { ($_ => 1) } qw( |
| 66 | G_ARRAY |
| 67 | G_DISCARD |
| 68 | G_EVAL |
| 69 | G_NOARGS |
| 70 | G_SCALAR |
| 71 | G_VOID |
| 72 | HEf_SVKEY |
| 73 | MARK |
| 74 | Nullav |
| 75 | Nullch |
| 76 | Nullcv |
| 77 | Nullhv |
| 78 | Nullsv |
| 79 | SP |
| 80 | SVt_IV |
| 81 | SVt_NV |
| 82 | SVt_PV |
| 83 | SVt_PVAV |
| 84 | SVt_PVCV |
| 85 | SVt_PVHV |
| 86 | SVt_PVMG |
| 87 | SvUOK |
| 88 | XS_VERSION |
| 89 | ), |
| 90 | ); |
| 91 | |
| 92 | # Ignore the return value of these |
| 93 | my %ignorerv = ( |
| 94 | map { ($_ => 1) } qw( |
| 95 | newCONSTSUB |
| 96 | ), |
| 97 | ); |
| 98 | |
| 99 | my @simple_my_cxt_prereqs = ( 'typedef struct { int count; } my_cxt_t;', 'START_MY_CXT;' ); |
| 100 | my @my_cxt_prereqs = ( @simple_my_cxt_prereqs, 'MY_CXT_INIT;' ); |
| 101 | |
| 102 | # The value of each key is a list of things that need to be declared in order |
| 103 | # for the key to compile. |
| 104 | my %stack = ( |
| 105 | MULTICALL => ['dMULTICALL;'], |
| 106 | ORIGMARK => ['dORIGMARK;'], |
| 107 | POP_MULTICALL => ['dMULTICALL;', 'U8 gimme;' ], |
| 108 | PUSH_MULTICALL => ['dMULTICALL;', 'U8 gimme;' ], |
| 109 | POPpbytex => ['STRLEN n_a;'], |
| 110 | POPpx => ['STRLEN n_a;'], |
| 111 | PUSHi => ['dTARG;'], |
| 112 | PUSHn => ['dTARG;'], |
| 113 | PUSHp => ['dTARG;'], |
| 114 | PUSHu => ['dTARG;'], |
| 115 | RESTORE_LC_NUMERIC => ['DECLARATION_FOR_LC_NUMERIC_MANIPULATION;'], |
| 116 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING => ['DECLARATION_FOR_LC_NUMERIC_MANIPULATION;'], |
| 117 | STORE_LC_NUMERIC_SET_TO_NEEDED => ['DECLARATION_FOR_LC_NUMERIC_MANIPULATION;'], |
| 118 | STORE_LC_NUMERIC_SET_TO_NEEDED_IN => ['DECLARATION_FOR_LC_NUMERIC_MANIPULATION;'], |
| 119 | UNDERBAR => ['dUNDERBAR;'], |
| 120 | XCPT_CATCH => ['dXCPT;'], |
| 121 | XCPT_RETHROW => ['dXCPT;'], |
| 122 | XCPT_TRY_END => ['dXCPT;'], |
| 123 | XCPT_TRY_START => ['dXCPT;'], |
| 124 | XPUSHi => ['dTARG;'], |
| 125 | XPUSHn => ['dTARG;'], |
| 126 | XPUSHp => ['dTARG;'], |
| 127 | XPUSHu => ['dTARG;'], |
| 128 | XS_APIVERSION_BOOTCHECK => ['CV * cv;'], |
| 129 | XS_VERSION_BOOTCHECK => ['CV * cv;'], |
| 130 | MY_CXT_INIT => [ @simple_my_cxt_prereqs ], |
| 131 | MY_CXT_CLONE => [ @simple_my_cxt_prereqs ], |
| 132 | dMY_CXT => [ @simple_my_cxt_prereqs ], |
| 133 | MY_CXT => [ @my_cxt_prereqs ], |
| 134 | _aMY_CXT => [ @my_cxt_prereqs ], |
| 135 | aMY_CXT => [ @my_cxt_prereqs ], |
| 136 | aMY_CXT_ => [ @my_cxt_prereqs ], |
| 137 | pMY_CXT => [ @my_cxt_prereqs ], |
| 138 | ); |
| 139 | |
| 140 | # The entries in %ignore have two components, separated by this. |
| 141 | my $sep = '~'; |
| 142 | |
| 143 | # Things to not try to check. (The component after $sep is empty.) |
| 144 | my %ignore = map { ("$_$sep" => 1) } keys %{&known_but_hard_to_test_for()}; |
| 145 | |
| 146 | # XXX The NEED_foo lines should be autogenerated |
| 147 | print OUT <<HEAD; |
| 148 | /* |
| 149 | * !!!!!!! DO NOT EDIT THIS FILE !!!!!!! |
| 150 | * This file is built by $0. |
| 151 | * Any changes made here will be lost! |
| 152 | */ |
| 153 | |
| 154 | #include "EXTERN.h" |
| 155 | #include "perl.h" |
| 156 | |
| 157 | #define NO_XSLOCKS |
| 158 | #include "XSUB.h" |
| 159 | |
| 160 | #ifdef DPPP_APICHECK_NO_PPPORT_H |
| 161 | |
| 162 | /* This is just to avoid too many baseline failures with perls < 5.6.0 */ |
| 163 | |
| 164 | #ifndef dTHX |
| 165 | # define dTHX extern int Perl___notused |
| 166 | #endif |
| 167 | |
| 168 | #else |
| 169 | |
| 170 | #define NEED_PL_parser |
| 171 | #define NEED_PL_signals |
| 172 | #define NEED_caller_cx |
| 173 | #define NEED_croak_xs_usage |
| 174 | #define NEED_die_sv |
| 175 | #define NEED_eval_pv |
| 176 | #define NEED_grok_bin |
| 177 | #define NEED_grok_hex |
| 178 | #define NEED_grok_number |
| 179 | #define NEED_grok_numeric_radix |
| 180 | #define NEED_grok_oct |
| 181 | #define NEED_load_module |
| 182 | #define NEED_mess |
| 183 | #define NEED_mess_nocontext |
| 184 | #define NEED_mess_sv |
| 185 | #define NEED_mg_findext |
| 186 | #define NEED_my_snprintf |
| 187 | #define NEED_my_sprintf |
| 188 | #define NEED_my_strlcat |
| 189 | #define NEED_my_strlcpy |
| 190 | #define NEED_my_strnlen |
| 191 | #define NEED_newCONSTSUB |
| 192 | #define NEED_newSVpvn_share |
| 193 | #define NEED_pv_display |
| 194 | #define NEED_pv_escape |
| 195 | #define NEED_pv_pretty |
| 196 | #define NEED_sv_catpvf_mg |
| 197 | #define NEED_sv_catpvf_mg_nocontext |
| 198 | #define NEED_sv_setpvf_mg |
| 199 | #define NEED_sv_setpvf_mg_nocontext |
| 200 | #define NEED_sv_unmagicext |
| 201 | #define NEED_utf8_to_uvchr_buf |
| 202 | #define NEED_vload_module |
| 203 | #define NEED_vmess |
| 204 | #define NEED_warner |
| 205 | |
| 206 | #include "ppport.h" |
| 207 | |
| 208 | #endif |
| 209 | |
| 210 | static int VARarg1; |
| 211 | static char *VARarg2; |
| 212 | static double VARarg3; |
| 213 | |
| 214 | #if defined(PERL_BCDVERSION) && (PERL_BCDVERSION < 0x5009005) |
| 215 | /* needed to make PL_parser apicheck work */ |
| 216 | typedef void yy_parser; |
| 217 | #endif |
| 218 | |
| 219 | HEAD |
| 220 | |
| 221 | # Caller can restrict what functions tests are generated for |
| 222 | if (@ARGV) { |
| 223 | my %want = map { ($_ => 0) } @ARGV; |
| 224 | @f = grep { exists $want{$_->{'name'}} } @f; |
| 225 | for (@f) { $want{$_->{'name'}}++ } |
| 226 | for (keys %want) { |
| 227 | die "nothing found for '$_'\n" unless $want{$_}; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | my $f; |
| 232 | for $f (@f) { # Loop through all the tests to add |
| 233 | |
| 234 | # Just the name isn't unique; We also need the #if or #else condition |
| 235 | my $unique = "$f->{'name'}$sep$f->{'cond'}"; |
| 236 | $ignore{$unique} and next; |
| 237 | |
| 238 | $f->{'flags'}{'A'} or next; # only public API members |
| 239 | |
| 240 | $ignore{$unique} = 1; # ignore duplicates |
| 241 | |
| 242 | my $Perl_ = $f->{'flags'}{'p'} ? 'Perl_' : ''; |
| 243 | |
| 244 | my $stack = ''; |
| 245 | my @arg; |
| 246 | my $aTHX = ''; |
| 247 | |
| 248 | my $i = 1; |
| 249 | my $ca; |
| 250 | my $varargs = 0; |
| 251 | |
| 252 | for $ca (@{$f->{'args'}}) { # Loop through the function's args |
| 253 | my $a = $ca->[0]; # 1th is the name, 0th is its type |
| 254 | if ($a eq '...') { |
| 255 | $varargs = 1; |
| 256 | push @arg, qw(VARarg1 VARarg2 VARarg3); |
| 257 | last; |
| 258 | } |
| 259 | |
| 260 | # Split this type into its components |
| 261 | my($n, $p, $d) = $a =~ /^ ( (?: " [^"]* " ) # literal string type => $n |
| 262 | | (?: \w+ (?: \s+ \w+ )* ) # name of type => $n |
| 263 | ) |
| 264 | \s* |
| 265 | ( \** ) # optional pointer(s) => $p |
| 266 | (?: \s* \b const \b \s* )? # opt. const |
| 267 | ( (?: \[ [^\]]* \] )* ) # opt. dimension(s)=> $d |
| 268 | $/x |
| 269 | or die "$0 - cannot parse argument: [$a] in $f->{'name'}\n"; |
| 270 | |
| 271 | # Replace a special argument name by something that will compile. |
| 272 | if (exists $amap{$n}) { |
| 273 | die "$f->{'name'} had type $n, which should have been the whole type" |
| 274 | if $p or $d; |
| 275 | push @arg, $amap{$n}; |
| 276 | next; |
| 277 | } |
| 278 | |
| 279 | # Certain types, like 'void', get remapped. |
| 280 | $n = $tmap{$n} || $n; |
| 281 | |
| 282 | if ($n =~ / ^ " [^"]* " $/x) { # Use the literal string, literally |
| 283 | push @arg, $n; |
| 284 | } |
| 285 | else { |
| 286 | my $v = 'arg' . $i++; # Argument number |
| 287 | push @arg, $v; |
| 288 | my $no_const_n = $n; # Get rid of any remaining 'const's |
| 289 | $no_const_n =~ s/\bconst\b// unless $p; |
| 290 | |
| 291 | # Declare this argument |
| 292 | $stack .= " static $no_const_n $p$v$d;\n"; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | # Declare thread context for functions and macros that might need it. |
| 297 | # (Macros often fail to say they don't need it.) |
| 298 | unless ($f->{'flags'}{'T'}) { |
| 299 | $stack = " dTHX;\n$stack"; # Harmless to declare even if not needed |
| 300 | $aTHX = @arg ? 'aTHX_ ' : 'aTHX'; |
| 301 | } |
| 302 | |
| 303 | # If this function is on the list of things that need declarations, add |
| 304 | # them. |
| 305 | if ($stack{$f->{'name'}}) { |
| 306 | my $s = ''; |
| 307 | for (@{$stack{$f->{'name'}}}) { |
| 308 | $s .= " $_\n"; |
| 309 | } |
| 310 | $stack = "$s$stack"; |
| 311 | } |
| 312 | |
| 313 | my $args = join ', ', @arg; |
| 314 | my $prefix = ""; |
| 315 | |
| 316 | # Failure to specify a return type in the apidoc line means void |
| 317 | my $rvt = $f->{'ret'} || 'void'; |
| 318 | |
| 319 | my $ret; |
| 320 | if ($void{$rvt}) { # Certain return types are instead considered void |
| 321 | $ret = $castvoid{$f->{'name'}} ? '(void) ' : ''; |
| 322 | } |
| 323 | else { |
| 324 | $stack .= " $rvt rval;\n"; |
| 325 | $ret = $ignorerv{$f->{'name'}} ? '(void) ' : "rval = "; |
| 326 | } |
| 327 | |
| 328 | my $aTHX_args = ""; |
| 329 | my $aTHX_prefix = ""; |
| 330 | |
| 331 | # Add parens to functions that take an argument list, even if empty |
| 332 | unless ($f->{'flags'}{'n'}) { |
| 333 | $aTHX_args = "($aTHX$args)"; |
| 334 | $args = "($args)"; |
| 335 | } |
| 336 | |
| 337 | # Single trailing underscore in name means is a comma operator |
| 338 | if ($f->{'name'} =~ /[^_]_$/) { |
| 339 | $aTHX_args .= ' 1'; |
| 340 | $args .= ' 1'; |
| 341 | } |
| 342 | |
| 343 | print OUT <<HEAD; |
| 344 | /****************************************************************************** |
| 345 | * |
| 346 | * $f->{'name'} |
| 347 | * |
| 348 | ******************************************************************************/ |
| 349 | |
| 350 | HEAD |
| 351 | |
| 352 | # #ifdef out if marked as todo (not known in) this version |
| 353 | if (exists $todo{$f->{'name'}}) { |
| 354 | my($five, $ver,$sub) = parse_version($todo{$f->{'name'}}{'version'}); |
| 355 | print OUT "#if PERL_VERSION > $ver || (PERL_VERSION == $ver && PERL_SUBVERSION >= $sub) /* TODO */\n"; |
| 356 | } |
| 357 | |
| 358 | my $final = $varargs |
| 359 | ? "$aTHX_prefix$Perl_$f->{'name'}$aTHX_args" |
| 360 | : "$prefix$f->{'name'}$args"; |
| 361 | |
| 362 | # If there is a '#if' associated with this, add that |
| 363 | $f->{'cond'} and print OUT "#if $f->{'cond'}\n"; |
| 364 | |
| 365 | print OUT <<END; |
| 366 | void _DPPP_test_$f->{'name'} (void) |
| 367 | { |
| 368 | dXSARGS; |
| 369 | $stack |
| 370 | { |
| 371 | #ifdef $f->{'name'} |
| 372 | $ret$prefix$f->{'name'}$args; |
| 373 | #endif |
| 374 | } |
| 375 | |
| 376 | { |
| 377 | #ifdef $f->{'name'} |
| 378 | $ret$final; |
| 379 | #else |
| 380 | $ret$aTHX_prefix$Perl_$f->{'name'}$aTHX_args; |
| 381 | #endif |
| 382 | } |
| 383 | } |
| 384 | END |
| 385 | |
| 386 | $f->{'cond'} and print OUT "#endif\n"; |
| 387 | exists $todo{$f->{'name'}} and print OUT "#endif\n"; |
| 388 | |
| 389 | print OUT "\n"; |
| 390 | } |
| 391 | |
| 392 | @ARGV and close OUT; |