X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=tools%2Fremove_cpp_comands.perl;h=ca19e7a17e9ccf2b49ed296c638ab6516a117ad3;hb=ff0e8d7fcb34481652f0bf521ba04b1eca5e2106;hp=9f0703c6cb61802943d1590908e42a229387050c;hpb=3a54957505decc179c0951298bce7e0361a79462;p=libfirm diff --git a/tools/remove_cpp_comands.perl b/tools/remove_cpp_comands.perl index 9f0703c6c..ca19e7a17 100644 --- a/tools/remove_cpp_comands.perl +++ b/tools/remove_cpp_comands.perl @@ -61,19 +61,42 @@ $guardedtypedef = 0; $scndlastline = ""; $lastline = ""; +$eat = 0; +$multiline = 0; + foreach $line (@lines) { - if (($line =~ /^\#/) ) { + if ($line =~ /\#ifdef __cplusplus/) { +# There is extern "C" in some header files guarded by #ifdef __cplusplus +# crecoder does not grok the extern "C", so remove thses three lines. + $eat = 2; + } elsif ($eat > 0) { + $eat = $eat -1; + } elsif ($line =~ /wchar_t/) { + # of course crecoder cannot handle wchar_t, what else? + } elsif ($multiline > 0) { + # this line connects a previous one, kill it + if ($line =~ /\\$/) { + $multiline = 1; + } else { + $multiline = 0; + } + } elsif ($line =~ /^\#/) { # eat the line $scndlastline = $lastline; $lastline = $line; - } elsif ($openbracket == 1) { + if ($line =~ /\\$/) { + $multiline = 1; + } else { + $multiline = 0; + } + } elsif ($openbracket >= 1) { print TDF "$line"; - if ((index($line, "}") > -1)) { - $openbracket = 0; - if (($guardedtypedef == 1)) { - print TDF "#endif\n"; - $guardedtypedef = 0; + $openbracket += num_brackets($line); + if ($openbracket == 0) { + if (($guardedtypedef == 1)) { + print TDF "#endif\n"; + $guardedtypedef = 0; } } $lastline = ""; @@ -86,21 +109,13 @@ foreach $line (@lines) { print TDF "$lastline"; $lastline = ""; } print TDF "$line"; - if ((index($line, "{") > -1)) { - $openbracket = 1; - } elsif (($guardedtypedef == 1)) { - print TDF "#endif\n"; - $guardedtypedef = 0; - } - if ((index($line, "}") > -1)) { - $openbracket = 0; + $openbracket += num_brackets($line); + if ($openbracket == 0) { if (($guardedtypedef == 1)) { - print TDF "#endif\n"; + print TDF "#endif\n"; $guardedtypedef = 0; } } - } elsif ($line =~ /extern "C"/) { - print OUT "/* extern C */ {"; } else { print OUT "$line"; $scndlastline = $lastline; @@ -111,3 +126,44 @@ foreach $line (@lines) { close(TDF); close(OUT); + +# count the bracket ballance +sub num_brackets { + my $line = shift; + my $lastpos; + my $cnt = 0; + + $lastpos = -1; + while(1) { + $lastpos = index($line, "{", $lastpos+1); + if ($lastpos < 0) { + last; + } + $cnt++; + } + $lastpos = -1; + while(1) { + $lastpos = index($line, "(", $lastpos+1); + if ($lastpos < 0) { + last; + } + $cnt++; + } + $lastpos = -1; + while(1) { + $lastpos = index($line, "}", $lastpos+1); + if ($lastpos < 0) { + last; + } + $cnt--; + } + $lastpos = -1; + while(1) { + $lastpos = index($line, ")", $lastpos+1); + if ($lastpos < 0) { + last; + } + $cnt--; + } + return $cnt; +}