Skip to content

Commit 91d5d2b

Browse files
authored
Merge pull request #97 from goblint/frontc-standalone-exp
Expose standalone expression parsing via Frontc
2 parents bbfc0b3 + fe7da7b commit 91d5d2b

File tree

7 files changed

+92
-3
lines changed

7 files changed

+92
-3
lines changed

src/frontc/cabs2cil.ml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ let env : (string, envdata * location) H.t = H.create 307
332332
(* Just like env, except that it simply collects all the information (i.e. entries
333333
* are never removed and it is also not emptied after every file). *)
334334
let environment : (string, envdata * location) H.t = H.create 307
335+
let genvironment : (string, envdata * location) H.t = H.create 307
335336

336337
(* We also keep a global environment. This is always a subset of the env *)
337338
let genv : (string, envdata * location) H.t = H.create 307
@@ -373,7 +374,8 @@ let addGlobalToEnv (k: string) (d: envdata) : unit =
373374
H.add env k (d, !currentLoc);
374375
H.add environment k (d, !currentLoc);
375376
(* Also add it to the global environment *)
376-
H.add genv k (d, !currentLoc)
377+
H.add genv k (d, !currentLoc);
378+
H.add genvironment k (d, !currentLoc)
377379

378380

379381

@@ -6988,3 +6990,45 @@ let convFile (f : A.file) : Cil.file =
69886990
globinit = None;
69896991
globinitcalled = false;
69906992
}
6993+
6994+
6995+
let convStandaloneExp ~genv:genv' ~env:env' (e : A.expression) : Cil.exp option =
6996+
Cil.initCIL (); (* make sure we have initialized CIL *)
6997+
6998+
(* remove parentheses from the Cabs *)
6999+
let e = V.visitCabsExpression (new stripParenClass) e in
7000+
7001+
(* Clean up the global types *)
7002+
initGlobals();
7003+
startFile ();
7004+
IH.clear noProtoFunctions;
7005+
H.clear compInfoNameEnv;
7006+
H.clear enumInfoNameEnv;
7007+
IH.clear mustTurnIntoDef;
7008+
H.clear alreadyDefined;
7009+
H.clear staticLocals;
7010+
H.clear typedefs;
7011+
H.clear isomorphicStructs;
7012+
annonCompFieldNameId := 0;
7013+
if !E.verboseFlag || !Cilutil.printStages then
7014+
ignore (E.log "Converting CABS->CIL\n");
7015+
7016+
H.iter (H.add genv) genv';
7017+
H.iter (H.add env) env';
7018+
7019+
let cil_exp = doPureExp e in
7020+
7021+
IH.clear noProtoFunctions;
7022+
IH.clear mustTurnIntoDef;
7023+
H.clear alreadyDefined;
7024+
H.clear compInfoNameEnv;
7025+
H.clear enumInfoNameEnv;
7026+
H.clear isomorphicStructs;
7027+
H.clear staticLocals;
7028+
H.clear typedefs;
7029+
H.clear env;
7030+
H.clear genv;
7031+
IH.clear callTempVars;
7032+
7033+
(* We are done *)
7034+
cil_exp

src/frontc/cabs2cil.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,8 @@ type envdata =
106106
(** A hashtable containing a mapping of variables, enums, types and labels to varinfo, typ, etc. *)
107107
(* It enables a lookup of the original variable names before the alpha conversion by cabs2cil *)
108108
val environment : (string, envdata * Cil.location) Hashtbl.t
109+
val genvironment : (string, envdata * Cil.location) Hashtbl.t
110+
111+
val convStandaloneExp: genv:(string, envdata * Cil.location) Hashtbl.t -> env:(string, envdata * Cil.location) Hashtbl.t -> Cabs.expression -> Cil.exp option
112+
113+
val currentFunctionFDEC: Cil.fundec ref

src/frontc/clexer.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444

4545
val init: filename:string -> Lexing.lexbuf
46+
val initFromString: string -> Lexing.lexbuf
4647
val finish: unit -> unit
4748

4849
(* This is the main parser function *)

src/frontc/clexer.mll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ let init ~(filename: string) : Lexing.lexbuf =
283283
Lexerhack.add_identifier := add_identifier;
284284
E.startParsing filename
285285

286+
let initFromString (s: string) : Lexing.lexbuf =
287+
init_lexicon ();
288+
(* Inititialize the pointer in Errormsg *)
289+
Lexerhack.add_type := add_type;
290+
(* add some built-in types which are handled as Tnamed in cabs2cil *)
291+
add_type "__int128_t"; (* __int128 *)
292+
add_type "__uint128_t"; (* unsigned __int128 *)
293+
Lexerhack.push_context := push_context;
294+
Lexerhack.pop_context := pop_context;
295+
Lexerhack.add_identifier := add_identifier;
296+
E.startParsingFromString s
297+
286298

287299
let finish () =
288300
E.finishParsing ()

src/frontc/cparser.mly

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ let transformOffsetOf (speclist, dtype) member =
346346
%left IDENT
347347

348348
/* Non-terminals informations */
349-
%start interpret file
349+
%start interpret file expression
350350
%type <Cabs.definition list> file interpret globals
351351

352352
%type <Cabs.definition> global
@@ -356,7 +356,7 @@ let transformOffsetOf (speclist, dtype) member =
356356
%type <Cabs.statement> statement
357357
%type <Cabs.constant * cabsloc> constant
358358
%type <int64 list Queue.t * Cabs.wchar_type * cabsloc> string_list
359-
%type <Cabs.expression * cabsloc> expression
359+
%type <Cabs.expression * Cabs.cabsloc> expression
360360
%type <Cabs.expression> opt_expression
361361
%type <Cabs.init_expression> init_expression
362362
%type <Cabs.expression list * cabsloc> comma_expression

src/frontc/frontc.ml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,28 @@ let parse_helper fname =
264264
let parse fname = (fun () -> snd(parse_helper fname ()))
265265

266266
let parse_with_cabs fname = (fun () -> parse_helper fname ())
267+
268+
let parse_standalone_exp s =
269+
try
270+
if !E.verboseFlag then ignore (E.log "Frontc is parsing string: %s\n" s);
271+
flush !E.logChannel;
272+
(* if !E.verboseFlag then ignore @@ Parsing.set_trace true; *)
273+
let lexbuf = Clexer.initFromString s in
274+
let (cabs, _) = Stats.time "parse" (Cparser.expression (Whitetrack.wraplexer clexer)) lexbuf in
275+
Whitetrack.setFinalWhite (Clexer.get_white ());
276+
Clexer.finish ();
277+
cabs
278+
with
279+
| Parsing.Parse_error -> begin
280+
ignore (E.log "Parsing error");
281+
Clexer.finish ();
282+
close_output ();
283+
(* raise (ParseError("Parse error")) *)
284+
let backtrace = Printexc.get_raw_backtrace () in
285+
Printexc.raise_with_backtrace (ParseError("Parse error")) backtrace (* re-raise with captured inner backtrace *)
286+
end
287+
| e -> begin
288+
ignore (E.log "Caught %s while parsing\n" (Printexc.to_string e));
289+
Clexer.finish ();
290+
raise e
291+
end

src/frontc/frontc.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ val args: (string * Arg.spec * string) list
5050
val parse: string -> (unit -> Cil.file)
5151

5252
val parse_with_cabs: string -> (unit -> Cabs.file * Cil.file)
53+
54+
val parse_standalone_exp: string -> Cabs.expression

0 commit comments

Comments
 (0)