1
(**************************************************************************)
3
(* Copyright (C) 2001-2003, *)
4
(* George C. Necula <necula@cs.berkeley.edu> *)
5
(* Scott McPeak <smcpeak@cs.berkeley.edu> *)
6
(* Wes Weimer <weimer@cs.berkeley.edu> *)
7
(* Ben Liblit <liblit@cs.berkeley.edu> *)
8
(* All rights reserved. *)
10
(* Redistribution and use in source and binary forms, with or without *)
11
(* modification, are permitted provided that the following conditions *)
14
(* 1. Redistributions of source code must retain the above copyright *)
15
(* notice, this list of conditions and the following disclaimer. *)
17
(* 2. Redistributions in binary form must reproduce the above copyright *)
18
(* notice, this list of conditions and the following disclaimer in the *)
19
(* documentation and/or other materials provided with the distribution. *)
21
(* 3. The names of the contributors may not be used to endorse or *)
22
(* promote products derived from this software without specific prior *)
23
(* written permission. *)
25
(* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *)
26
(* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *)
27
(* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *)
28
(* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *)
29
(* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *)
30
(* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *)
31
(* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *)
32
(* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *)
33
(* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *)
34
(* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *)
35
(* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *)
36
(* POSSIBILITY OF SUCH DAMAGE. *)
38
(* File modified by CEA (Commissariat � l'�nergie Atomique). *)
39
(**************************************************************************)
47
(* Output management *)
48
let out : out_channel option ref = ref None
49
let close_me = ref false
56
if !close_me then close_out o else ();
60
let set_output filename =
62
let out_chan = try open_out filename
64
(output_string stderr ("Error while opening output: " ^ msg); exit 1) in
66
Whitetrack.setOutput out_chan;
69
(* Signal that we are in MS VC mode *)
71
Cprint.msvcMode := true
73
(* filename for patching *)
74
let patchFileName : string ref = ref "" (* by default do no patching *)
76
(* patching file contents *)
77
let patchFile : Cabs.file option ref = ref None
79
(* whether to print the patched CABS files *)
80
let printPatchedFiles : bool ref = ref false
82
(* whether to print a file of prototypes after parsing *)
83
let doPrintProtos : bool ref = ref false
85
(* this seems like something that should be built-in.. *)
86
let isNone (o : 'a option) : bool =
93
let printNotice = ref false
96
** Argument definition
98
let args : (string * Arg.spec * string) list =
100
"--cabsonly", Arg.String set_output, "<fname>: CABS output file name";
101
"--printComments", Arg.Unit (fun _ -> Cprint.printComments := true),
102
": print cabs tree structure in comments in cabs output";
103
"--patchFile", Arg.String (fun pf -> patchFileName := pf),
104
"<fname>: name the file containing patching transformations";
105
"--printPatched", Arg.Unit (fun _ -> printPatchedFiles := true),
106
": print patched CABS files after patching, to *.patched";
107
"--printProtos", Arg.Unit (fun _ -> doPrintProtos := true),
108
": print prototypes to safec.proto.h after parsing";
109
"--printNotice", Arg.Set printNotice,
110
": include a comment saying printed by FrontC";
113
exception ParseError of string
116
(* parse, and apply patching *)
117
let rec parse_to_cabs fname =
119
(* parse the patch file if it isn't parsed already *)
120
if ((!patchFileName <> "") && (isNone !patchFile)) then (
121
(* parse the patch file *)
122
patchFile := Some(parse_to_cabs_inner !patchFileName);
124
(failwith "There were parsing errors in the patch file")
127
(* now parse the file we came here to parse *)
128
let cabs = parse_to_cabs_inner fname in
130
E.s (E.error "There were parsing errors in %s\n" fname);
131
(* and apply the patch file, return transformed file *)
132
let patched = match !patchFile with
135
(* save old value of out so I can use it for debugging during patching *)
138
(* reset out so we don't try to print the patch file to it *)
141
(trace "patch" (dprintf "newpatching %s\n" fname));
142
let result = (Stats.time "newpatch" (Patch.applyPatch pf) cabs) in
144
if (!printPatchedFiles) then begin
145
let outFname:string = fname ^ ".patched" in
146
(trace "patch" (dprintf "printing patched version of %s to %s\n"
148
let o = (open_out outFname) in
149
(Cprint.printFile o result);
164
(trace "sm" (dprintf "writing the cabs output\n"));
165
if !printNotice then output_string o ("/* Generated by Frontc */\n");
166
Stats.time "printCABS" (Cprint.printFile o) patched;
172
raise Parsing.Parse_error;
174
(* and return the patched source *)
179
Clexer.clear_white ();
180
Clexer.clear_lexeme ();
181
let token = Clexer.initial lexbuf in
182
let white = Clexer.get_white () in
183
let cabsloc = Clexer.currentLoc () in
184
let lexeme = Clexer.get_extra_lexeme () ^ Lexing.lexeme lexbuf in
185
white,lexeme,token,cabsloc
188
and parse_to_cabs_inner (fname : string) =
190
if !E.verboseFlag then ignore (E.log "Frontc is parsing %s\n" fname);
192
E.hadErrors := false;
193
let lexbuf = Clexer.init fname in
194
let cabs = Stats.time "parse" (Cparser.file (Whitetrack.wraplexer clexer)) lexbuf in
195
(* Cprint.print_defs cabs;*)
196
Whitetrack.setFinalWhite (Clexer.get_white ());
198
let fname = match !E.first_filename_encountered with
203
with (Sys_error msg) -> begin
204
ignore (E.log "Cannot open %s : %s\n" fname msg);
207
raise (ParseError("Cannot open " ^ fname ^ ": " ^ msg ^ "\n"))
209
| Parsing.Parse_error -> begin
210
ignore (E.log "Parsing error\n");
213
raise (ParseError("Parse error"))
216
ignore (E.log "Caught %s while parsing\n" (Printexc.to_string e));
222
(* print to safec.proto.h the prototypes of all functions that are defined *)
223
let printPrototypes ((_fname, file) : Cabs.file) : unit =
225
(*ignore (E.log "file has %d defns\n" (List.length file));*)
227
let chan = open_out "safec.proto.h" in
228
ignore (fprintf chan "/* generated prototypes file, %d defs */\n" (List.length file));
231
let counter : int ref = ref 0 in
233
let rec loop (_ghost,(d : Cabs.definition)) = begin
235
| Cabs.FUNDEF(_,name, _, loc, _) -> (
237
| (_, (funcname, Cabs.PROTO(_,_,_), _, _)) -> (
239
ignore (fprintf chan "\n/* %s from %s:%d */\n"
240
funcname (fst loc).Lexing.pos_fname (fst loc).Lexing.pos_lnum);
242
Cprint.print_single_name name;
243
Cprint.print_unescaped_string ";";
244
Cprint.force_new_line ();
252
(List.iter loop file);
254
ignore (fprintf chan "\n/* wrote %d prototypes */\n" !counter);
256
ignore (E.log "printed %d prototypes from %d defns to safec.proto.h\n"
257
!counter (List.length file))
263
(trace "sm" (dprintf "parsing %s to Cabs\n" fname));
264
let cabs = parse_to_cabs fname in
265
(*Cprint.printFile stdout cabs;*)
266
(* Now (return a function that will) convert to CIL *)
268
(trace "sm" (dprintf "converting %s from Cabs to CIL\n" fname));
269
let cil = Stats.time "conv" Cabs2cil.convFile cabs in
270
if !doPrintProtos then (printPrototypes cabs);
271
(*Cil.dumpFile Cil.defaultCilPrinter stdout "behue" cil;*)