temporary switched off the detection of more than one node of mode M from the same...
[libfirm] / tools / remove_cpp_comands.perl
1 #!/usr/local/bin/perl
2
3 #
4 # Project:     libFIRM
5 # File name:   ir/tools/remove_cpp_commands.perl
6 # Purpose:
7 # Author:      Goetz Lindenmaier
8 # Modified by:
9 # Created:     8.2002
10 # CVS-ID:      $Id$
11 # Copyright:   (c) 2002-2003 Universität Karlsruhe
12 # Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
13 #
14
15 # This is necessary until CRECODER is integrated with the preprocessor.
16 #
17 # Take a C header file and remove all preprocessor commands.
18 # Wrap all typedefs with a preprocessor guard,
19 # add all possible typedefs in firm at the beginning of the file, also
20 # wrapped with a preprocessor guard
21 #
22 # Call
23 #  perl remove_cpp_comands.perl <file>
24 # for a file with name <file>.h.
25
26 # open files
27 $infile = $ARGV[0];
28
29 open(IN, $infile);
30 @lines = <IN>;
31 close(IN);
32
33 $outfile = $infile;
34 open(OUT, ">$outfile");
35
36 $typedeffile = "firm_typedefs.h";
37 open(TDF, ">>$typedeffile");
38
39 #dump headers
40 print OUT "\n#include \"firm_typedefs.h\"\n\n";
41
42
43 #Unresolved preprocessor commands
44 print TDF "#define INLINE\n";
45 print TDF "#define FILE int *\n";
46 print TDF "#ifndef MYTYPEDEFS\n#define MYTYPEDEFS\n";
47 #print TDF "#undef __cplusplus\n";
48 print TDF "typedef unsigned long size_t;\n";
49 #print TDF "typedef enum { false = 0, true = 1 } bool;\n";  geht nicht, false und true JAVA Schluesselwoerter
50 print TDF "typedef int bool;\n";
51 # Some typedefs we need because of wrond order resultion by this script
52 print TDF "#ifndef _ENTITY_TYPEDEF_\n#define _ENTITY_TYPEDEF_\ntypedef struct entity entity;\n#endif\n";
53 # Some typedefs we need because we do not include the according header files
54 print TDF "typedef struct dbg_info dbg_info;\n";
55 print TDF "#endif /* MYTYPEDEFS */ \n";
56
57 #to collect typedefs
58 $openbracket = 0;
59 $guardedtypedef = 0;
60
61 $scndlastline = "";
62 $lastline = "";
63
64 $eat = 0;
65 $multiline = 0;
66
67 foreach $line (@lines) {
68
69     if ($line =~ /\#ifdef __cplusplus/) {
70 #       There is extern "C" in some header files guarded by #ifdef __cplusplus
71 #       crecoder does not grok the extern "C", so remove thses three lines.
72         $eat = 2;
73     } elsif ($eat > 0) {
74         $eat = $eat -1;
75     } elsif ($line =~ /wchar_t/) {
76         # of course crecoder cannot handle wchar_t, what else?
77     } elsif ($multiline > 0) {
78         # this line connects a previous one, kill it
79         if ($line =~ /\\$/) {
80           $multiline = 1;
81         } else {
82           $multiline = 0;
83         }
84     } elsif ($line =~ /^\#/) {
85         # eat the line
86         $scndlastline = $lastline;
87         $lastline = $line;
88         if ($line =~ /\\$/) {
89           $multiline = 1;
90         } else {
91           $multiline = 0;
92         }
93     } elsif ($openbracket >= 1) {
94         print TDF "$line";
95         $openbracket += num_brackets($line);
96         if ($openbracket == 0) {
97             if (($guardedtypedef == 1)) {
98                 print TDF "#endif\n";
99                 $guardedtypedef = 0;
100             }
101         }
102         $lastline = "";
103     } elsif ($line =~ /typedef/) {
104         # move the full typedef to firm_typedefs.h
105
106         if (($lastline =~ /^\#/)   ) {
107             $guardedtypedef = 1;
108             print TDF "$scndlastline"; $scndlastline = "";
109             print TDF "$lastline";      $lastline = "";
110         }
111         print TDF "$line";
112         $openbracket += num_brackets($line);
113         if ($openbracket == 0) {
114             if (($guardedtypedef == 1)) {
115                 print TDF "#endif\n";
116                 $guardedtypedef = 0;
117             }
118         }
119     } else {
120         print OUT "$line";
121         $scndlastline = $lastline;
122         $lastline = "";
123     }
124 }
125
126
127 close(TDF);
128 close(OUT);
129
130 # count the bracket ballance
131 sub num_brackets {
132   my $line = shift;
133   my $lastpos;
134   my $cnt = 0;
135
136   $lastpos = -1;
137   while(1) {
138     $lastpos = index($line, "{", $lastpos+1);
139     if ($lastpos < 0) {
140       last;
141     }
142     $cnt++;
143   }
144   $lastpos = -1;
145   while(1) {
146     $lastpos = index($line, "(", $lastpos+1);
147     if ($lastpos < 0) {
148       last;
149     }
150     $cnt++;
151   }
152   $lastpos = -1;
153   while(1) {
154     $lastpos = index($line, "}", $lastpos+1);
155     if ($lastpos < 0) {
156       last;
157     }
158     $cnt--;
159   }
160   $lastpos = -1;
161   while(1) {
162     $lastpos = index($line, ")", $lastpos+1);
163     if ($lastpos < 0) {
164       last;
165     }
166     $cnt--;
167   }
168   return $cnt;
169 }