8d753e5553d68c5dfdf36b2dce8b32d155ea0ab9
[ghc-hetmet.git] / ghc / utils / hscpp / hscpp.prl
1 #
2 # reads CPP output and turns #line things into appropriate Haskell
3 # pragmas
4 #
5 # considered to be GHC-project specific
6 #
7 #
8 # NOTE: this script needs RAWCPP set in order to do something
9 # useful:
10 #
11
12 $Verbose = 0;
13 $file = '';
14 @args = ();
15
16 $Cpp = ${RAWCPP};
17
18 foreach (@ARGV) {
19     /^-v$/ && do { $Verbose = 1; next; };
20
21     /^[^-]/ && do { 
22         if ($file ne '') { 
23             die "usage: hscpp [arg...] file";
24         } else {
25             $file = $_; 
26         };
27         next;
28     };
29
30     push @args, $_;
31 }
32
33 print STDERR "hscpp:CPP invoked: $Cpp @args - <$file\n" if $Verbose;
34 open(INPIPE, "$Cpp @args - <$file |") 
35         || die "Can't open C pre-processor pipe\n";
36
37 while (<INPIPE>) {
38
39 # line directives come in flavo[u]rs:
40 #   s/^#\s*line\s+\d+$/\{\-# LINE \-\}/;   IGNORE THIS ONE FOR NOW
41     s/^#\s*line\s+(\d+)\s+(\".+\")$/\{\-# LINE \1 \"$file\" \-\}/;
42     s/^#\s*(\d+)\s+(\".*\").*/\{\-# LINE \1 \"$file\" \-\}/;
43
44     print $_;
45 }
46
47 close(INPIPE) || exit(1); # exit is so we reflect any errors.
48
49 exit(0);