1#!/usr/bin/perl
2
3# Copyright (c) 2002, 2021, Oracle and/or its affiliates.
4# Use is subject to license terms.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License, version 2.0,
8# as published by the Free Software Foundation.
9#
10# This program is also distributed with certain software (including
11# but not limited to OpenSSL) that is licensed under separate terms,
12# as designated in a particular file or component or in included license
13# documentation.  The authors of MySQL hereby grant you an additional
14# permission to link the program and your derivative works with the
15# separately licensed software that they have included with MySQL.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License, version 2.0, for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
25
26die "No files specified\n" unless $ARGV[0];
27
28$ctags="exctags -x -f - --c-types=f -u";
29
30sub get_tag {
31  local $.; local $_=<TAGS>;
32  ($symbol, $line)= /^(.*\S)\s+function\s+(\d+)/;
33  $symbol=$1 if /[\s*]([^\s*]+)\s*\(/;
34  $line=1e50 unless $line;
35}
36
37while($src=shift)
38{
39  warn "==> $src\n";
40
41  $dst=$src.$$;
42  open(TAGS, "$ctags $src|") || die "Cannot exec('$ctags $src'): $!";
43  open(SRC, "<$src") || die "Cannot open $src: $!";
44  open(DST, ">$dst") || die "Cannot create $dst: $!";
45  select DST;
46
47  &get_tag;
48  $in_func=0;
49  while(<SRC>)
50  {
51    my $orig=$_;
52    if ($in_func)
53    {
54      if (/\breturn\b/ && !/\/\*.*\breturn\b.*\*\// && !/;/ )
55      {
56        $_.=<SRC> until /;/;
57      }
58      s/(?<=\s)return\s*;/DBUG_VOID_RETURN;/;
59      s/(?<=\s)return\s*(.+)\s*;/DBUG_RETURN(\1);/s;
60      $ret_line=$. if /DBUG_(VOID_)?RETURN/; #{{
61      print "$tab  DBUG_VOID_RETURN;\n" if /^$tab}/ && $ret_line < $.-1;
62      $in_func=0 if /^$tab}/;
63      warn "$src:".($.-1)."\t$orig" if /\breturn\b/;
64    }
65    print;
66    next if $. < $line;
67    die "Something wrong: \$.=$., \$line=$line, \$symbol=$symbol\n" if $. > $line;
68    &get_tag && next if /^\s*inline /;
69    print $_=<SRC> until /{/; $tab=$`;
70    &get_tag && next if /}/; # skip one-liners
71    $semicolon=1;
72    while(<SRC>)
73    {
74      $skip=!$semicolon;
75      $semicolon= /;\s*$/;
76      print && next if $skip ||
77        (/^\s+\w+((::\w+)?|<\w+>)\s+\**\w+/ && !/^\s*return\b/);
78      last if /DBUG_ENTER/;
79      print "$tab  DBUG_ENTER(\"$symbol\");\n";
80      print "\n" unless $_ eq "\n";
81      last;
82    }
83    $in_func=1;
84    &get_tag;
85    redo;
86  }
87  close SRC;
88  close DST;
89  close TAGS;
90  unlink("$src.orig");
91  rename($src, "$src.orig") || die "Cannot rename $src to $src.orig: $!";
92  rename($dst, $src) || die "Cannot rename $dst to $src: $!";
93}
94
95warn "All done!\n";
96
97