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