xref: /netbsd-src/crypto/external/bsd/openssl/dist/util/fix-deprecation (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos#! /usr/bin/env perl
2*b0d17251Schristos
3*b0d17251Schristosuse strict;
4*b0d17251Schristosuse warnings;
5*b0d17251Schristos
6*b0d17251Schristosmy $debug = $ENV{DEBUG};
7*b0d17251Schristos
8*b0d17251Schristos# This scripts finds DEPRECATEDIN declarations and converts them to
9*b0d17251Schristos# C declarations with the corresponding OSSL_DEPRECATEDIN attribute
10*b0d17251Schristos# macro.  It also makes sure they are guarded them with a corresponding
11*b0d17251Schristos# '#ifndef OPENSSL_NO_DEPRECATED', and pays extra attention to only have
12*b0d17251Schristos# one such guard around a group of deprecations for the same version.
13*b0d17251Schristos
14*b0d17251Schristosmy $parens_re =
15*b0d17251Schristos    qr/(
16*b0d17251Schristos           \(                   # The start of what we recurse on
17*b0d17251Schristos               (?:
18*b0d17251Schristos                   (?> [^()]+ )     # Non-parens, without backtracking
19*b0d17251Schristos               |
20*b0d17251Schristos                   (?-1)            # Recurse to start of parens group
21*b0d17251Schristos               )*
22*b0d17251Schristos           \)                   # The end of what we recurse on
23*b0d17251Schristos       )/x;
24*b0d17251Schristos
25*b0d17251Schristosmy $deprecated_kw_re = qr/(DEPRECATEDIN)_(\d+_\d+(?:_\d+)?)/;
26*b0d17251Schristosmy $deprecated_re =
27*b0d17251Schristos    qr/
28*b0d17251Schristos          $deprecated_kw_re
29*b0d17251Schristos          \(
30*b0d17251Schristos          (
31*b0d17251Schristos              (?:
32*b0d17251Schristos                  (?> [^()]+ )
33*b0d17251Schristos              |
34*b0d17251Schristos                  $parens_re
35*b0d17251Schristos              )*
36*b0d17251Schristos          )
37*b0d17251Schristos          \)
38*b0d17251Schristos    /x;
39*b0d17251Schristosmy $headertext;
40*b0d17251Schristos{
41*b0d17251Schristos    local $/;
42*b0d17251Schristos    $headertext = <STDIN>;
43*b0d17251Schristos}
44*b0d17251Schristos$headertext =~ s/\R/\n/g;
45*b0d17251Schristos
46*b0d17251Schristosmy $cppspaces = '';
47*b0d17251Schristosmy $last_cppspaces = '';
48*b0d17251Schristosmy $currentguard = "";
49*b0d17251Schristosmy $cnt = 0;
50*b0d17251Schristoswhile ( $headertext =~ m/(.*?)                          # $1
51*b0d17251Schristos                         (                              # $2
52*b0d17251Schristos                             ^
53*b0d17251Schristos                             (?|
54*b0d17251Schristos                                 (\#)(\s*)(if)?.*?      # $3 ('#')
55*b0d17251Schristos                                                        # $4 (spaces)
56*b0d17251Schristos                                                        # $5 ('if'?)
57*b0d17251Schristos                             |
58*b0d17251Schristos                                 \s*$deprecated_kw_re\(.*?
59*b0d17251Schristos                                                        # $3 = 'DEPRECATEDIN'
60*b0d17251Schristos                                                        # $4 (vers)
61*b0d17251Schristos                             )
62*b0d17251Schristos                             \n
63*b0d17251Schristos                         )
64*b0d17251Schristos                        /msx ) {
65*b0d17251Schristos    my $before = $1;
66*b0d17251Schristos    my $capture = $2;
67*b0d17251Schristos    my $after = $';
68*b0d17251Schristos
69*b0d17251Schristos    my $deprecation = '';
70*b0d17251Schristos    my $test = $capture.$';
71*b0d17251Schristos    my $version = undef;
72*b0d17251Schristos
73*b0d17251Schristos    print STDERR "DEBUG: captured:\n$capture"
74*b0d17251Schristos        if $debug;
75*b0d17251Schristos
76*b0d17251Schristos    if ($3 eq '#') {
77*b0d17251Schristos        # Treat preprocessor lines (count spaces)
78*b0d17251Schristos        $cppspaces = $4;
79*b0d17251Schristos        $cppspaces .= ' ' if (defined $5 && $5 eq 'if');
80*b0d17251Schristos        print STDERR "DEBUG: cpp spaces set to ", length($cppspaces), "\n"
81*b0d17251Schristos            if $debug;
82*b0d17251Schristos        $before .= $capture;
83*b0d17251Schristos    } elsif ($test =~ m/^\s*$deprecated_re(.*?\n)/) {
84*b0d17251Schristos        # Treat DEPRECATEDIN_...
85*b0d17251Schristos        $version = $2;
86*b0d17251Schristos        $deprecation = "OSSL_DEPRECATEDIN_$version $3;$5";
87*b0d17251Schristos        $after = $';            # Different from the previous!
88*b0d17251Schristos        print STDERR "DEBUG: changed to:\n$deprecation\n"
89*b0d17251Schristos            if $debug;
90*b0d17251Schristos    }
91*b0d17251Schristos
92*b0d17251Schristos    if ($currentguard ne ''
93*b0d17251Schristos        && (defined $version && $currentguard ne $version
94*b0d17251Schristos            || $before !~ /^\s*$/s)) {
95*b0d17251Schristos        print "#${last_cppspaces}endif\n";
96*b0d17251Schristos        $cppspaces = substr($cppspaces, 0, -1);
97*b0d17251Schristos        $currentguard = "";
98*b0d17251Schristos    }
99*b0d17251Schristos    print $before;
100*b0d17251Schristos    if ($deprecation) {
101*b0d17251Schristos        if ($currentguard eq '' && defined $version) {
102*b0d17251Schristos            $currentguard = $version;
103*b0d17251Schristos            print "#${cppspaces}ifndef OPENSSL_NO_DEPRECATED_$version\n";
104*b0d17251Schristos            $last_cppspaces = $cppspaces;
105*b0d17251Schristos            $cppspaces .= ' ';
106*b0d17251Schristos            print STDERR "DEBUG: cpp spaces set to ", length($cppspaces), "\n"
107*b0d17251Schristos                if $debug;
108*b0d17251Schristos        }
109*b0d17251Schristos        print $deprecation;
110*b0d17251Schristos    }
111*b0d17251Schristos    $headertext = $after;
112*b0d17251Schristos}
113*b0d17251Schristosprint "#endif\n" if $currentguard ne '';
114*b0d17251Schristosprint $headertext;
115