fix-deprecation 3.5 KB

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