*** empty log message ***
[libfirm] / tools / remove_cpp_comands.perl
1 #!/usr/local/bin/perl
2 #
3 # Take a C header file and remove all preprocessor commands.
4 # Wrap all typedefs with a preprocessor guard,
5 # add all possible typedefs in firm at the beginning of the file, also
6 # wrapped with a preprocessor guard
7 #
8 # Call
9 #  perl remove_cpp_comands.perl <file>
10 # for a file with name <file>.h.
11
12 # open files
13 $infile = $ARGV[0];
14
15 open(IN, $infile);
16 @lines = <IN>;
17 close(IN);
18
19 $outfile = $infile;
20 open(OUT, ">$outfile");
21
22 $typedeffile = "firm_typedefs.h";
23 open(TDF, ">>$typedeffile");
24
25 #dump headers
26 print OUT "\n#include \"firm_typedefs.h\"\n\n";
27
28
29 #Unresolved preprocessor commands
30 print TDF "#define INLINE\n";
31 print TDF "#define FILE int *\n";
32 print TDF "#ifndef MYTYPEDEFS\n#define MYTYPEDEFS\n";
33 print TDF "typedef unsigned long size_t;\n";
34 #print TDF "typedef enum { false = 0, true = 1 } bool;\n";  geht nicht, false und true JAVA Schluesselwoerter
35 print TDF "typedef int bool;\n";
36 # Some typedefs we need because of wrond order resultion by this script
37 print TDF "#ifndef _ENTITY_TYPEDEF_\n#define _ENTITY_TYPEDEF_\ntypedef struct entity entity;\n#endif\n";
38 # Some typedefs we need because we do not include the according header files
39 print TDF "typedef struct dbg_info dbg_info;\n";
40 print TDF "#endif /* MYTYPEDEFS */ \n";
41
42 #to collect typedefs
43 $openbracket = 0;
44 $guardedtypedef = 0;
45
46 $scndlastline = "";
47 $lastline = "";
48
49 foreach $line (@lines) {
50
51     if (($line =~ /^\#/)   ) {
52         # eat the line
53         $scndlastline = $lastline;
54         $lastline = $line;
55     } elsif ($openbracket == 1) {
56         print TDF "$line";
57         if ((index($line, "}") > -1)) {
58             $openbracket = 0;
59             if (($guardedtypedef == 1)) {
60                 print TDF "#endif\n";
61                 $guardedtypedef = 0;
62             }
63         }
64         $lastline = "";
65     } elsif ($line =~ /typedef/) {
66         # move the full typedef to firm_typedefs.h
67
68         if (($lastline =~ /^\#/)   ) {
69             $guardedtypedef = 1;
70             print TDF "$scndlastline"; $scndlastline = "";
71             print TDF "$lastline";      $lastline = "";
72         }
73         print TDF "$line";
74         if ((index($line, "{") > -1)) {
75             $openbracket = 1;
76         } elsif (($guardedtypedef == 1)) {
77             print TDF "#endif\n";
78             $guardedtypedef = 0;
79         }
80         if ((index($line, "}") > -1)) {
81             $openbracket = 0;
82             if (($guardedtypedef == 1)) {
83                 print TDF "#endif\n";
84                 $guardedtypedef = 0;
85             }
86         }
87     } else {
88         print OUT "$line";
89         $scndlastline = $lastline;
90         $lastline = "";
91     }
92 }
93
94
95 close(TDF);
96 close(OUT);