forked from kframework/c-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontc.ml
155 lines (134 loc) · 5 KB
/
frontc.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(*
*
* Copyright (c) 2001-2002,
* George C. Necula <[email protected]>
* Scott McPeak <[email protected]>
* Wes Weimer <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*)
module E = Errormsg
(* open Trace *)
(* open Pretty *)
(* Output management *)
let out : out_channel option ref = ref None
let close_me = ref false
let close_output _ =
match !out with
None -> ()
| Some o -> begin
flush o;
if !close_me then close_out o else ();
close_me := false
end
(* Signal that we are in MS VC mode *)
let setMSVCMode () =
Cprint.msvcMode := true
(* filename for patching *)
let patchFileName : string ref = ref "" (* by default do no patching *)
(* patching file contents *)
let patchFile : Cabs.file option ref = ref None
(* whether to print the patched CABS files *)
let printPatchedFiles : bool ref = ref false
(* whether to print a file of prototypes after parsing *)
let doPrintProtos : bool ref = ref false
(* this seems like something that should be built-in.. *)
let isNone (o : 'a option) : bool =
begin
match o with
| Some _ -> false
| None -> true
end
let printNotice = ref false
exception ParseError of string
exception CabsOnly
(* parse, and apply patching *)
let rec parse_to_cabs fname =
begin
(* parse the patch file if it isn't parsed already *)
if ((!patchFileName <> "") && (isNone !patchFile)) then (
(* parse the patch file *)
patchFile := Some(parse_to_cabs_inner !patchFileName);
if !E.hadErrors then
(failwith "There were parsing errors in the patch file")
);
(* now parse the file we came here to parse *)
let cabs = parse_to_cabs_inner fname in
if !E.hadErrors then
E.s (Printf.printf "There were parsing errors in %s" fname); cabs
end
and clexer lexbuf =
Clexer.clear_white ();
Clexer.clear_lexeme ();
let token = Clexer.initial lexbuf in
let white = Clexer.get_white () in
let cabsloc = Clexer.currentLoc () in
let lexeme = Clexer.get_extra_lexeme () ^ Lexing.lexeme lexbuf in
white,lexeme,token,cabsloc
(* just parse *)
and parse_to_cabs_inner (fname : string) =
try
if !E.verboseFlag then ignore ((Printf.printf "Frontc is parsing %s\n" fname));
flush !E.logChannel;
let lexbuf = Clexer.init fname in
(* let cabs = Stats.time "parse" (Cparser.interpret (Whitetrack.wraplexer clexer)) lexbuf in *)
let cabs = Cparser.interpret (Whitetrack.wraplexer clexer) lexbuf in
Whitetrack.setFinalWhite (Clexer.get_white ());
Clexer.finish ();
(fname, cabs)
with (Sys_error msg) -> begin
ignore (Printf.printf "Cannot open %s : %s\n" fname msg);
Clexer.finish ();
close_output ();
raise (ParseError("Cannot open " ^ fname ^ ": " ^ msg ^ "\n"))
end
| Parsing.Parse_error -> begin
ignore (Printf.printf "Parsing error");
Clexer.finish ();
close_output ();
raise (ParseError("Parse error"))
end
| e -> begin
ignore (Printf.printf "Caught %s while parsing\n" (Printexc.to_string e));
Clexer.finish ();
raise e
end
let parse_helper fname =
(*(trace "sm" (dprintf "parsing %s to Cabs\n" fname)); *)
let cabs = parse_to_cabs fname in
(* Now (return a function that will) convert to CIL *)
fun _ ->
(* (trace "sm" (dprintf "converting %s from Cabs to CIL\n" fname)); *)
(* let cil = Stats.time "convert to CIL" Cabs2cil.convFile cabs in *)
(* let cil = Cabs2cil.convFile cabs in
if !doPrintProtos then (printPrototypes cabs); *)
cabs
let parse fname = (fun () -> snd(parse_helper fname ()))
let parse_with_cabs fname = (fun () -> parse_helper fname ())