converted to doxygen comments
[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 print TDF "#endif\n";
37
38 #to collect typedefs
39 $openbracket = 0;
40 $guardedtypedef = 0;
41
42 $scndlastline = "";
43 $lastline = "";
44
45 foreach $line (@lines) {
46
47     if (($line =~ /^\#/)   ) {
48         # eat the line
49         $scndlastline = $lastline;
50         $lastline = $line;
51     } elsif ($openbracket == 1) {
52         print TDF "$line";
53         if ((index($line, "}") > -1)) {
54             $openbracket = 0;
55             if (($guardedtypedef == 1)) {
56                 print TDF "#endif\n";
57                 $guardedtypedef = 0;
58             }
59         }
60         $lastline = "";
61     } elsif ($line =~ /typedef/) {
62         # move the full typedef to firm_typedefs.h
63
64         if (($lastline =~ /^\#/)   ) {
65             $guardedtypedef = 1;
66             print TDF "$scndlastline"; $scndlastline = "";
67             print TDF "$lastline";      $lastline = "";
68         }
69         print TDF "$line";
70         if ((index($line, "{") > -1)) {
71             $openbracket = 1;
72         } elsif (($guardedtypedef == 1)) {
73             print TDF "#endif\n";
74             $guardedtypedef = 0;
75         }
76         if ((index($line, "}") > -1)) {
77             $openbracket = 0;
78             if (($guardedtypedef == 1)) {
79                 print TDF "#endif\n";
80                 $guardedtypedef = 0;
81             }
82         }
83     } else {
84         print OUT "$line";
85         $scndlastline = $lastline;
86         $lastline = "";
87     }
88 }
89
90
91 close(TDF);
92 close(OUT);