Project

General

Profile

Download (4.75 KB) Statistics
| Branch: | Tag: | Revision:
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_filename = 
75
      match !Options.dest_dir with
76
      | "" -> (* Mutants are generated in source directory *)
77
	 basename^ ".mutant.n" ^ (string_of_int !cpt) ^ extension 
78
      | dir ->  (* Mutants are generated in target directory *)
79
	 dir ^ "/" ^ (Filename.basename basename)^ ".mutant.n" ^ (string_of_int !cpt) ^ extension 
80
    in
81
    let mutant_out = (
82
      try 
83
	open_out mutant_filename 
84
      with
85
	Sys_error _ -> Format.eprintf "Unable to open file %s for writing.@." mutant_filename; exit 1
86
    )
87
    in
88
    let mutant_fmt = formatter_of_out_channel mutant_out in
89
    report ~level:1 (fun fmt -> fprintf fmt ".. generating mutant %s: %a@,@?"
90
      mutant_filename
91
      Mutation.print_directive mutation
92
    );
93
    Format.fprintf mutant_fmt "%a@." Printers.pp_prog mutant;
94
    mutation, mutation_loc, mutant_filename
95
    )
96
      mutants
97
  in
98
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
99
  
100
  (* Printing traceability *)
101
  let trace_filename = 
102
    match !Options.dest_dir with
103
    | "" -> (* Mutant report is generated in source directory *)
104
       basename^ ".mutation.json" 
105
    | dir ->  (* Mutants are generated in target directory *)
106
       dir ^ "/" ^ (Filename.basename basename)^ ".mutation.json"
107
  in
108
  pp_trace trace_filename mutation_list;
109
  (* We stop the process here *)
110
  exit 0
111
    
112
let testgen dirname basename extension =
113
  match extension with
114
  | ".lus"   -> testgen_source dirname basename extension
115
  | _        -> assert false
116

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

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

    
138
    let options = Options_management.lustret_options
139

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

    
151
(* Local Variables: *)
152
(* compile-command:"make -C .." *)
153
(* End: *)
(37-37/65)