Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_testgen.ml @ ef8ef3ed

History | View | Annotate | Download (4.79 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11

    
12
(* This module is used for the lustre test generator *)
13

    
14
open Format
15
open Log
16

    
17
open Utils
18
open LustreSpec
19
open Compiler_common
20

    
21
let usage = "Usage: lustret [options] \x1b[4msource file\x1b[0m"
22

    
23
let extensions = [".lus"]
24

    
25
let pp_trace trace_filename mutation_list = 
26
  let trace_file = open_out trace_filename in
27
  let trace_fmt = formatter_of_out_channel trace_file in
28
  Format.fprintf trace_fmt "@[<v 2>{@ %a@ }@]"
29
    (fprintf_list
30
       ~sep:",@ "
31
       (fun fmt (mutation, mutation_loc, mutant_name) ->
32
	 Format.fprintf fmt "\"%s\": { @[<v 0>%a,@ %a@ }@]" 
33
	   mutant_name
34
	   Mutation.print_directive_json mutation
35
	   Mutation.print_loc_json mutation_loc
36
       ))
37
    mutation_list;
38
  Format.fprintf trace_fmt "@.@?" 
39
  
40
  
41
let testgen_source dirname basename extension =
42
  let source_name = dirname ^ "/" ^ basename ^ extension in
43

    
44
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
45

    
46
  (* Parsing source *)
47
  let prog = parse_source source_name in
48

    
49
  let prog, dependencies = Compiler_stages.stage1 prog dirname basename in
50

    
51
  (* Two cases
52
     - generation of coverage conditions
53
     - generation of mutants: a number of mutated lustre files 
54
  *)
55
  
56
  if !Options.gen_mcdc then (
57
    PathConditions.mcdc prog;
58
    exit 0
59
  ) ;
60

    
61
  
62
  (* generate mutants *)
63
  let mutants = Mutation.mutate !Options.nb_mutants prog in
64
  
65
  (* Print generated mutants in target directory. *)
66
  let cpt = ref 0 in
67
  let mutation_list =
68
    List.map (fun (mutation, mutation_loc, mutant) ->
69
    (* Debugging code *)
70
    (* if List.mem !cpt [238;371;601;799;875;998] then *)
71
    (*   Format.eprintf "Mutant %i: %a -> %a" !cpt Printers.pp_expr orig_e Printers.pp_expr new_e  *)
72
    (* ; *)
73
      incr cpt;
74
      let mutant_basename = (Filename.basename basename)^ ".mutant.n" ^ (string_of_int !cpt) ^ extension  in
75
      let mutant_filename = 
76
	match !Options.dest_dir with
77
	| "" -> (* Mutants are generated in source directory *)
78
	   basename^ ".mutant.n" ^ (string_of_int !cpt) ^ extension 
79
      | dir ->  (* Mutants are generated in target directory *)
80
	 dir ^ "/" ^ mutant_basename 
81
    in
82
    let mutant_out = (
83
      try 
84
	open_out mutant_filename 
85
      with
86
	Sys_error _ -> Format.eprintf "Unable to open file %s for writing.@." mutant_filename; exit 1
87
    )
88
    in
89
    let mutant_fmt = formatter_of_out_channel mutant_out in
90
    report ~level:1 (fun fmt -> fprintf fmt ".. generating mutant %s: %a@,@?"
91
      mutant_filename
92
      Mutation.print_directive mutation
93
    );
94
    Format.fprintf mutant_fmt "%a@." Printers.pp_prog mutant;
95
    mutation, mutation_loc, mutant_basename
96
    )
97
      mutants
98
  in
99
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
100
  
101
  (* Printing traceability *)
102
  let trace_filename = 
103
    match !Options.dest_dir with
104
    | "" -> (* Mutant report is generated in source directory *)
105
       basename^ ".mutation.json" 
106
    | dir ->  (* Mutants are generated in target directory *)
107
       dir ^ "/" ^ (Filename.basename basename)^ ".mutation.json"
108
  in
109
  pp_trace trace_filename mutation_list;
110
  (* We stop the process here *)
111
  exit 0
112
    
113
let testgen dirname basename extension =
114
  match extension with
115
  | ".lus"   -> testgen_source dirname basename extension
116
  | _        -> assert false
117

    
118
let anonymous filename =
119
  let ok_ext, ext = List.fold_left
120
    (fun (ok, ext) ext' ->
121
      if not ok && Filename.check_suffix filename ext' then
122
	true, ext'
123
      else
124
	ok, ext)
125
    (false, "") extensions in
126
  if ok_ext then
127
    let dirname = Filename.dirname filename in
128
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
129
    testgen dirname basename ext
130
  else
131
    raise (Arg.Bad ("Can only compile *.lus files"))
132

    
133
let _ =
134
  Global.initialize ();
135
  Corelang.add_internal_funs ();
136
  try
137
    Printexc.record_backtrace true;
138

    
139
    let options = Options_management.lustret_options
140

    
141
    in
142
    
143
    Arg.parse options anonymous usage
144
  with
145
  | Parse.Error _
146
  | Types.Error (_,_) | Clocks.Error (_,_)
147
  | Corelang.Error _ (*| Task_set.Error _*)
148
  | Causality.Error _ -> exit 1
149
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
150
  | exc -> (track_exception (); raise exc)
151

    
152
(* Local Variables: *)
153
(* compile-command:"make -C .." *)
154
(* End: *)