summaryrefslogtreecommitdiff
path: root/trunk/TAO/bin/rm_exception_macros.pl
blob: 7f3691380a14f433e6220524afadb3cc648157d7 (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
    & eval 'exec perl -w -S $0 $argv:q'
    if 0;

# ******************************************************************
#      Author: Chad Elliott
#        Date: 1/24/2007
#         $Id$
# Description: This script attempts to remove the ACE related exception
#              macros from source code provided on the comand line.  It
#              is not perfect, but does handle a large number of cases.
#              You may need to hand edit files after running this
#              script.
# ******************************************************************

# ******************************************************************
# Pragma Section
# ******************************************************************

use strict;
use FileHandle;
use File::Basename;

# ******************************************************************
# Data Section
# ******************************************************************

my(%macros) = ('ACE_ENV_TYPE'                  => 'CORBA::Environment',
               'ACE_TRY_ENV'                   => '_ACE_CORBA_Environment_variable',
               'ACE_EXCEPTION_TYPE'            => 'CORBA::Exception',
               'ACE_DEFAULT_GET_ENV_METHOD'    => 'TAO_default_environment',
               'ACE_DECLARE_NEW_ENV'           => '',
               'ACE_DECLARE_NEW_CORBA_ENV'     => '',
               'ACE_CHECK *\(.*\)'             => '',
               'ACE_CHECK'                     => '',
               'ACE_CHECK_RETURN *\(.*\)'      => '',
               'ACE_THROW_INT *\((.*)\)'       => 'throw $1',
               'ACE_THROW *\((.*)\)'           => 'throw $1',
               'ACE_THROW_RETURN *\((.*),.+\)' => 'throw $1',
               'ACE_THROW_SPEC *\((.*)\)'      => '',
               'ACE_TRY'                       => 'try',
               'ACE_TRY_NEW_ENV'               => 'try',
               'ACE_TRY_EX *\([^\)]+\)'        => 'try',
               'ACE_TRY_CHECK'                 => '',
               'ACE_TRY_CHECK_EX *\([^\)]+\)'  => '',
               'ACE_TRY_THROW *\((.*)\)'       => 'throw $1',
               'ACE_TRY_THROW_EX *\((.*),.+\)' => 'throw $1',
               'ACE_CATCH *\((.*),(.+)\)'      => 'catch (const $1& $2)',
               'ACE_CATCHANY'                  => 'catch (const CORBA::Exception& ex)',
               'ACE_CATCHALL'                  => 'catch (...)',
               'ACE_RETHROW'                   => 'throw',
               'ACE_RE_THROW'                  => 'throw',
               'ACE_RE_THROW_EX *\(.+\)'       => 'throw',
               'ACE_ENDTRY'                    => '',
               'ACE_ENV_RAISE *\((.+)\)'       => '$1->_raise ()',
               'ACE_PRINT_TAO_EXCEPTION *\(([^,]+),(.+)\)' => '$1._tao_print_exception ($2)',
               'ACE_PRINT_EXCEPTION *\(([^,]+),(.+)\)'     => '$1._tao_print_exception ($2)',
               'TAO_INTERCEPTOR_CHECK_RETURN *\(.*\)'      => '',
               'TAO_INTERCEPTOR_CHECK'                     => '',
              );
my(@keys)   = sort { length($b) <=> length($a) } keys %macros;

# ******************************************************************
# Subroutine Section
# ******************************************************************

sub process_file {
  my($file)   = shift;
  my($fh)     = new FileHandle();
  my($status) = 0;

  if (open($fh, $file)) {
    my(@lines) = ();
    my($mod)   = undef;
    my($line)  = '';
    my($cont_until_this) = undef;
    while(<$fh>) {
      my($part) = $_;
      $part =~ s/\s+$//;

      if ($cont_until_this) {
        if ($part =~ s/^\s+// && $line =~ /[,\)]$/) {
          $part = ' ' . $part;
        }
        $line .= $part;
        if (index($part, $cont_until_this) >= 0) {
          $cont_until_this = undef;
        }
        else {
          next;
        }
      }
      else {
        $line = $part;
      }

      my($skip_blank) = undef;
      foreach my $key (@keys) {
        my($ats)    = (index($key, 'ACE_THROW_SPEC') == 0);
        my($search) = ($ats ? '))' : ';');
        my($base)   = undef;
        if ($key =~ /^([^\s]+\s\*\\\()/) {
          $base = $1;
        }

        if ($line =~ /^(\s*)?($key\s*[;]?)/) {
          my($space)  = $1;
          my($rest)   = $2;
          my($first)  = $3 || '';
          my($second) = $4 || '';
          my($val)    = $macros{$key};
          while($val =~ /\$(\d+)/) {
            my($num) = $1;
            if ($num == 1) {
              $first =~ s/^\s+//;
              $first =~ s/\s+$//;
              $val =~ s/\$1/$first/;
            }
            elsif ($num == 2) {
              $second =~ s/^\s+//;
              $second =~ s/\s+$//;
              $val =~ s/\$2/$second/;
            }
            else {
              $val =~ s/\$\d+//;
            }
          }

          $line =~ s/^(\s*)?($key\s*[;]?)//;

          ## A special concession for ACE_THROW_SPEC.  In the header
          ## we want to ensure that the semi-colon is preserved and if
          ## possible placed on the previous line.  In the source file
          ## we want the whole thing to go away.
          $val .= ';' if (($ats || $val ne '') && $rest =~ /;$/);
          if ($ats && index($val, ';') == 0 &&
              !($lines[$#lines] =~ /\/\/.*$/ ||
                $lines[$#lines] =~ /\/\*.*\*\/$/)) {
            $lines[$#lines] .= $val;                          
            $line = '';
          }
          else {
            $line = $space . $val . $line;
            $line =~ s/^\s+$//;
          }

          ## Fix up problems where ACE_TRY_THROW is used
          ## on a line by itself with the parenthesis following
          ## on the second line.  The parser gets confused and
          ## thinks it needs to replace the ACE_TRY
          if ($key eq 'ACE_TRY') {
            $line =~ s/try_THROW/throw/g;
          }

          my($len) = length($line);
          if ($len > 78 || $line =~ /""/) {
            my($level) = 0;
            my($indouble) = 0;
            my($pch) = '';
            my($fix_ace_text) = undef;
            for(my $i = 0; $i < $len; $i++) {
              my($ch) = substr($line, $i, 1);
              if ($ch eq '"' && $pch ne '\\') {
                $indouble ^= 1;
              }
              if (!$indouble) {
                my($nch) = substr($line, $i + 1, 1);
                if ($ch eq ',' || ($ch eq '"' && $nch eq '"') || $ch eq '(') {
                  $level++ if ($ch eq '(');
                  $fix_ace_text = (substr($line, 0, $i) =~ /ACE_TEXT\s*$/);
                  if ($nch ne ')' && !$fix_ace_text) {
                    my($add) = "\n" . $space . ('  ' x $level);
                    my($start) = 0;
                    while(substr($line, $i + 1 + $start, 1) =~ /\s/) {
                      $start++;
                    }
                    substr($line, $i + 1, $start) = $add;
                    $i += length($add) - $start;
                    $len += length($add) - $start;
                  }
                }
                elsif ($ch eq ')') {
                  $level--;
                  if ($fix_ace_text) {
                    if (substr($line, $i) =~ /ACE_TEXT/) {
                      my($add) = "\n" . $space . ('  ' x $level);
                      my($start) = 0;
                      while(substr($line, $i + 1 + $start, 1) =~ /\s/) {
                        $start++;
                      }
                      substr($line, $i + 1, $start) = $add;
                      $i += length($add) - $start;
                      $len += length($add) - $start;
                    }
                    $fix_ace_text = undef;
                  }
                }
              }
              $pch = $ch;
            }
          }

          $mod = 1;
          $skip_blank = 1;
          last;
        }
        elsif (defined $base &&
               index($line, $search) == -1 && $line =~ /^(\s*)?$base/) {
          $cont_until_this = $search;
          last;
        }
      }

      if ($line =~ s/ACE_ANY_EXCEPTION/ex/g) {
        $mod = 1;
      }
      if (!$cont_until_this) {
        if ($line =~ s/(\s*)ACE_ENV(_SINGLE)?_ARG_DECL_WITH_DEFAULTS// ||
            $line =~ s/(\s*)ACE_ENV(_SINGLE)?_ARG_DECL_NOT_USED// ||
            $line =~ s/(\s*)ACE_ENV(_SINGLE)?_ARG_DECL// ||
            $line =~ s/(\s*)ACE_ENV(_SINGLE)?_ARG_(PARAMETER|NOT_USED)//) {
          my($sp) = $1;
          if ($line =~ /^\)/) {
            if ($lines[$#lines] =~ /\/\/.*$/ ||
                $lines[$#lines] =~ /\/\*.*\*\/$/) {
              $line = $sp . $line;
            }
            else {
              $lines[$#lines] .= $line;                          
              $line = '';
            }
          }
          $mod = 1;
          $skip_blank = 1;
        }
        push(@lines, $line) if ($line ne '' || !$skip_blank);
      }
    }
    close($fh);

    if ($mod) {
      if (open($fh, ">$file")) {
        foreach my $line (@lines) {
          print $fh $line, "\n";
        }
        close($fh);
      }
      else {
        print "ERROR: Unable to write to $file\n";
        $status++;
      }
    }
  }
  else {
    print "ERROR: Unable to open $file\n";
    $status++;
  }

  return $status;
}

sub process {
  my($arg)    = shift;
  my($status) = 0;

  if (-d $arg) {
    my($fh)  = new FileHandle();
    if (opendir($fh, $arg)) {
      foreach my $file (grep(!/^\.\.?$/, readdir($fh))) {
        if ($file ne '.svn') {
          $status += process($arg . '/' . $file);
        }
      }
      closedir($fh);
    }
  }
  elsif ($arg =~ /\.(h|hh|hpp|hxx|hh|cpp|cxx|cc|c|C|i|ipp|inl)$/) {
    $status += process_file($arg);
  }

  return $status;
}

sub usageAndExit {
  print "Usage: ", basename($0), " [directories or files]\n\n",
        "Removes all exception related ACE macros.\n";
  exit(0);
}

# ******************************************************************
# Main Section
# ******************************************************************

my($status) = 0;

if ($#ARGV == -1) {
  usageAndExit();
}

foreach my $arg (@ARGV) {
  if (index($arg, '-') == 0) {
    usageAndExit();
  }
  else {
    $status += process($arg);
  }
}

exit($status);