Added a new order for the blocks.
[libfirm] / tools / remove_cpp_comands.perl
index a6b8022..ca19e7a 100644 (file)
@@ -1,4 +1,18 @@
 #!/usr/local/bin/perl
+
+#
+# Project:     libFIRM
+# File name:   ir/tools/remove_cpp_commands.perl
+# Purpose:
+# Author:      Goetz Lindenmaier
+# Modified by:
+# Created:     8.2002
+# CVS-ID:      $Id$
+# Copyright:   (c) 2002-2003 Universität Karlsruhe
+# Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+#
+
+# This is necessary until CRECODER is integrated with the preprocessor.
 #
 # Take a C header file and remove all preprocessor commands.
 # Wrap all typedefs with a preprocessor guard,
@@ -30,10 +44,15 @@ print OUT "\n#include \"firm_typedefs.h\"\n\n";
 print TDF "#define INLINE\n";
 print TDF "#define FILE int *\n";
 print TDF "#ifndef MYTYPEDEFS\n#define MYTYPEDEFS\n";
+#print TDF "#undef __cplusplus\n";
 print TDF "typedef unsigned long size_t;\n";
 #print TDF "typedef enum { false = 0, true = 1 } bool;\n";  geht nicht, false und true JAVA Schluesselwoerter
 print TDF "typedef int bool;\n";
-print TDF "#endif\n";
+# Some typedefs we need because of wrond order resultion by this script
+print TDF "#ifndef _ENTITY_TYPEDEF_\n#define _ENTITY_TYPEDEF_\ntypedef struct entity entity;\n#endif\n";
+# Some typedefs we need because we do not include the according header files
+print TDF "typedef struct dbg_info dbg_info;\n";
+print TDF "#endif /* MYTYPEDEFS */ \n";
 
 #to collect typedefs
 $openbracket = 0;
@@ -42,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 = "";
@@ -67,16 +109,10 @@ 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;
            }
        }
@@ -90,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;
+}