<commit_before>(********************************************************************)
(*                                                                  *)
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
(*                                                                  *)
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
(*  under the terms of the GNU Lesser General Public License        *)
(*  version 2.1.                                                    *)
(*                                                                  *)
(********************************************************************)

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

open Format
open Log

open Utils
open LustreSpec
open Compiler_common

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

let extensions = [".lus"]

let pp_trace trace_filename mutation_list = 
  let trace_file = open_out trace_filename in
  let trace_fmt = formatter_of_out_channel trace_file in
  Format.fprintf trace_fmt "@[<v 2>{@ %a@ }@]"
    (fprintf_list
       ~sep:",@ "
       (fun fmt (mutation, mutation_loc, mutant_name) ->
	 Format.fprintf fmt "\"%s\": { @[<v 0>%a,@ %a@ }@]" 
	   mutant_name
	   Mutation.print_directive_json mutation
	   Mutation.print_loc_json mutation_loc
       ))
    mutation_list;
  Format.fprintf trace_fmt "@.@?" 
  
  
let testgen_source dirname basename extension =
  let source_name = dirname ^ "/" ^ basename ^ extension in

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

  (* Parsing source *)
  let prog = parse_source source_name in

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

  (* Two cases
     - generation of coverage conditions
     - generation of mutants: a number of mutated lustre files 
  *)
  
  if !Options.gen_mcdc then (
    let prog_mcdc = PathConditions.mcdc prog in
    let _, type_env, _ = import_dependencies prog_mcdc in

    let _ = type_decls type_env prog_mcdc in

    let destname = !Options.dest_dir ^ "/" ^ basename in
    let source_file = destname ^ ".mcdc.lus" in (* Could be changed *)
    let source_out = open_out source_file in
    let fmt = formatter_of_out_channel source_out in
    Printers.pp_prog fmt prog_mcdc;
    Format.fprintf fmt "@.@?";
    exit 0
  ) ;

  
  (* generate mutants *)
  let mutants = Mutation.mutate !Options.nb_mutants prog in
  
  (* Print generated mutants in target directory. *)
  let cpt = ref 0 in
  let mutation_list =
    List.map (fun (mutation, mutation_loc, mutant) ->
    (* Debugging code *)
    (* if List.mem !cpt [238;371;601;799;875;998] then *)
    (*   Format.eprintf "Mutant %i: %a -> %a" !cpt Printers.pp_expr orig_e Printers.pp_expr new_e  *)
    (* ; *)
      incr cpt;
      let mutant_basename = (Filename.basename basename)^ ".mutant.n" ^ (string_of_int !cpt) ^ extension  in<commit_msg>[lustret] When generating MC/DC conditions, produce them as EMF XML output<commit_after>(********************************************************************)
(*                                                                  *)
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
(*                                                                  *)
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
(*  under the terms of the GNU Lesser General Public License        *)
(*  version 2.1.                                                    *)
(*                                                                  *)
(********************************************************************)

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

open Format
open Log

open Utils
open LustreSpec
open Compiler_common

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

let extensions = [".lus"]

let pp_trace trace_filename mutation_list = 
  let trace_file = open_out trace_filename in
  let trace_fmt = formatter_of_out_channel trace_file in
  Format.fprintf trace_fmt "@[<v 2>{@ %a@ }@]"
    (fprintf_list
       ~sep:",@ "
       (fun fmt (mutation, mutation_loc, mutant_name) ->
	 Format.fprintf fmt "\"%s\": { @[<v 0>%a,@ %a@ }@]" 
	   mutant_name
	   Mutation.print_directive_json mutation
	   Mutation.print_loc_json mutation_loc
       ))
    mutation_list;
  Format.fprintf trace_fmt "@.@?" 
  
  
let testgen_source dirname basename extension =
  let source_name = dirname ^ "/" ^ basename ^ extension in

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

  (* Parsing source *)
  let prog = parse_source source_name in
  let prog, dependencies = Compiler_stages.stage1 prog dirname basename in

  (* Two cases
     - generation of coverage conditions
     - generation of mutants: a number of mutated lustre files 
  *)
  
  if !Options.gen_mcdc then (
    let prog_mcdc = PathConditions.mcdc prog in
    (* We re-type the fresh equations *)
    let _, type_env, _ = import_dependencies prog_mcdc in
    let _ = type_decls type_env prog_mcdc in

    let destname = !Options.dest_dir ^ "/" ^ basename in
    let source_file = destname ^ ".mcdc" in (* Could be changed *)

    (* Modified Lustre is produced in fresh .lus file *)
    let source_lus = source_file ^ ".lus" in
    let source_out = open_out source_lus in
    let fmt = formatter_of_out_channel source_out in
    Printers.pp_prog fmt prog_mcdc;
    Format.fprintf fmt "@.@?";

    (* Prog is 
       (1) cleaned from initial equations TODO
       (2) produced as EMF
    *)
    Options.output := "emf";
    let prog_mcdc = Normalization.normalize_prog ~backend:"emf" prog_mcdc in
    let machine_code = Compiler_stages.stage2 prog_mcdc in
    let source_emf = source_file ^ ".emf" in 
    let source_out = open_out source_emf in
    let fmt = formatter_of_out_channel source_out in
    EMF_backend.translate fmt basename prog_mcdc machine_code;

    exit 0
  ) ;

  
  (* generate mutants *)
  let mutants = Mutation.mutate !Options.nb_mutants prog in
  
  (* Print generated mutants in target directory. *)
  let cpt = ref 0 in
  let mutation_list =
    List.map (fun (mutation, mutation_loc, mutant) ->
    (* Debugging code *)
    (* if List.mem !cpt [238;371;601;799;875;998] then *)
    (*   Format.eprintf "Mutant %i: %a -> %a" !cpt Printers.pp_expr orig_e Printers.pp_expr new_e  *)
    (* ; *)
      incr cpt;
      let mutant_basename = (Filename.basename basename)^ ".mutant.n" ^ (string_of_int !cpt) ^ extension  in
{'fraction_non_alphanumeric': 0.13577063106796117, 'fraction_numerical': 0.010770631067961165, 'mean_word_length': 2.4302809573361084, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5739622', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1959, 'n_words': 578}