Skip to content
This repository
tag: v1815
Fetching contributors…

Cannot retrieve contributors at this time

file 800 lines (652 sloc) 24.844 kb
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* Please cf mli first *)
(* todo: continue to hoist base *)
(* once Base is hoisted and clean enough, let's restart open Base *)
module String = Base.String
module List = Base.List
module Format = Base.Format
let (@**) f g x y = f (g x) (g y)

(* The map storing all possible conditions defined in WarningClass *)
let cond_map = ref StringMap.empty

(* The map storing all possible printers *)
let printer_map = ref StringMap.empty

(* The map storing all possible trackers *)
let tracker_map = ref StringMap.empty

(************************************************)
(* Options **************************************)
module Arg = struct

  module Arg = Base.Arg

  let split =
    String.slice ','

  let add_strs ?ids str set =
    let () =
      match ids with
      | None -> (* no check are perform *) ()
      | Some (name, help, possible) ->
          if not (StringMap.mem str possible)
          then
            OManager.error (
              "The %s @{<bright>%s@} is not defined !@\n"^^
              "Check the list of available %s with the option @{<bright>%s@}@\n"^^
              "%a"
            )
              name str
              name help
              (HintUtils.pp_suggestion (StringMap.keys possible)) str
    in
    match set with
    | None -> None
    | Some set ->
        Some (List.fold_left
                (fun set pass -> StringSet.add pass set)
                set (split str))

  (* Checks *************************************)
  let checks = ref (Some StringSet.empty)

  let check_all () = checks := None

  let add_check_pass str = checks := add_strs str !checks

  let add_check_conds str =
    let ids = "condition", "--check-help", !cond_map in
    checks := add_strs ~ids str !checks

  (* Tracks *************************************)
  (*
None means everything, <=> Some (infinity)
*)
  let tracks = ref (Some StringSet.empty)

  let track_all () = tracks := None

  let add_tracker str =
    let ids = "tracker", "--track-help", !tracker_map in
    tracks := add_strs ~ids str !tracks

  let add_track_pass str = tracks := add_strs str !tracks

  (* Prints *************************************)
  (*
None means everything, <=> Some (infinity)
*)
  let prints = ref (Some StringSet.empty)

  let print_all () = prints := None

  let add_printer str =
    let ids = "printer", "--print-help", !printer_map in
    prints := add_strs ~ids str !prints

  let add_print_pass str = prints := add_strs str !prints

  (* Compilation shorcuts ***********************)
  let marshals = ref (Some StringSet.empty)
  let marshal_all () = marshals := None
  let add_marshal_pass str = marshals := add_strs str !marshals
  let unmarshal = ref None
  let set_unmarshal s = unmarshal := Some s

  (* Consistency ********************************)
  let consistency = ref false

  let check_consistency () = consistency := true

  (* Helps **************************************)
  let help_factory name map =
    OManager.printf "List of available %ss :@\n" name ;
    let _ =
      StringMap.fold
        (fun k _ a ->
           OManager.printf "%s%s" a k;
           ", ")
        map "" in
    OManager.printf "@\n";
    exit 0

  let help_checker () = help_factory "condition" !cond_map
  let help_printer () = help_factory "printer" !printer_map
  let help_tracker () = help_factory "tracker" !tracker_map

  (* Options ************************************)
  let (!>) = Format.sprintf

  (* following guidelines about arguments, as in bslregister.ml, bslbrowser.ml *)
  let options = [


    (* c *)


    "--check",
    Arg.String add_check_conds,
    "\"cond1,cond2,...\" Check all specified preconditions and postconditions" ;


    "--check-all",
    Arg.Unit check_all,
    " Check all preconditions and postconditions on all passes" ;


    "--check-consistency",
    Arg.Unit check_consistency,
     " Check if the pre/post conditions are consistent" ;


    "--check-help",
    Arg.Unit help_checker,
    " Print the list of conditions" ;


    "--check-pass",
    Arg.String add_check_pass,
    "\"pass1,pass2,...\" Check all preconditions and postconditions for specified passes" ;


    (* m *)


    "--marshal-all",
    Arg.Unit marshal_all,
    " Marshal all the environments" ;


    "--marshal-pass",
    Arg.String add_marshal_pass,
    " Marshal the environment BEFORE the pass" ;


    (* p *)


    "--print",
    Arg.String add_printer,
    "\"prt1,prt2,...\" Print specified printer (like code,size,...)" ;


    "--print-all",
    Arg.Unit print_all,
    " Print all printers on all passes" ;


    "--print-pass",
    Arg.String add_print_pass,
    "\"pass1,pass2,...\" Print all printers at specified passes" ;


    "--print-help",
    Arg.Unit help_printer,
    " Print the list of printers" ;


    (* t *)

    "--track",
    Arg.String add_tracker,
    "\"prt1,prt2,...\" Print specified tracker" ;


    "--track-all",
    Arg.Unit track_all,
    " Track all passes" ;


    "--track-dir",
    Arg.String PassTracker.set_directory,
    !>
      "<dir> Specify the output directory for tracking (def is %s)" (PassTracker.get_directory ());


    "--track-pass",
    Arg.String add_track_pass,
     "\"pass1,pass2,...\" Track specified passes" ;


    "--track-help",
    Arg.Unit help_tracker,
    " Print the list of trackers" ;


    (* u *)


    "--unmarshal-pass",
    Arg.String set_unmarshal,
    " Read the previously marshalled environment to shortcut the compilation and go directly to the pass" ;
    (* most useful to profile just one pass, and not the whole compiler *)


  ]

end

(************************************************)
(* Environments *********************************)
type passname = string
type 'env printer = 'env PassTracker.printer
type 'env tracker = PassTracker.iter_tracker -> 'env -> unit

type printer_id = PassTracker.printer_id
type tracker_id = PassTracker.tracker_id

type ('opt, 'env) one_env = {
  env : 'env;
  options : 'opt;
  printers : 'opt -> (printer_id * 'env printer) list;
  trackers : 'opt -> (tracker_id * 'env tracker) list;
}

let make_env
    ?(printers = fun _ -> [])
    ?(trackers = fun _ -> [])
    options env =
  {
    env = env;
    options = options;
    printers = printers;
    trackers = trackers;
  }

let title = ref "Pass"

let set_title ti = title := ti

module RegisterPrinter =
struct
  let printers = ref []
  let register printer = printers := Obj.magic printer :: !printers
  let retrieve passname =
    let rec aux = function
      | [] -> fun _ -> []
        | h :: t ->
            match h passname with
            | None -> aux t
            | Some real_printer -> Obj.magic real_printer in
    aux !printers
end

(************************************************)
(* Conditions ***********************************)
type cond_id = string
type 'env cond = cond_id * ('env -> unit)

let compose_fun_condition (f : 'a -> 'b) ( (cond_id, g) : 'b cond) : 'a cond =
  (cond_id, (fun env -> g (f env)))

external cond_id : cond_id -> string = "%identity"

(* Internal exception, does not escape out of this module *)
exception CompilerInternalError of (cond_id option * string) HdList.t

(* Sometime for a checker it is more convenient to use an error function *)
exception CheckCondition

let _ =
  Printexc.register_printer
    (function
       | CompilerInternalError hdl -> Some (
           String.concat_map "\n\n\t"
             (fun (id, msg) ->
                match id with
                | Some id -> Printf.sprintf "The condition '%s' is violated\n%s\n" id msg
                | None -> msg)
             (HdList.unwrap hdl)
         )
       | CheckCondition -> Some "CheckCondition"
       | _ -> None)


let define_factory ?(uniq=true) name ref_map id elt =
  if uniq && StringMap.mem id !ref_map
  then
    invalid_arg (Printf.sprintf "%s %s is already defined" name id)
  else (
    ref_map := StringMap.add id elt !ref_map ;
    id
  )

let define_cond wclass =
  let cond_id = WarningClass.get_name wclass in
  define_factory "Condition" cond_map cond_id wclass

let define_printer id = define_factory ~uniq:false "Printer" printer_map id ()
let define_tracker id = define_factory ~uniq:false "Tracker" tracker_map id ()

external printer_id : printer_id -> string = "%identity"
external tracker_id : tracker_id -> string = "%identity"

let make_condition name f = (name, f)

let compose_condition conds =
  let name = fst (List.hd conds) in
  make_condition name
    (fun env ->
       List.iter
         (fun (n, f) ->
            assert (n=name);
            f env) conds)

let and_if lif =
  fun ~options env ->
    List.for_all (fun if_ -> if_ ~options env) lif

let or_if lif =
  fun ~options env ->
    List.exists (fun if_ -> if_ ~options env) lif

let check_condition env (_, check) =
  check env

let check_cond env (name, check) =
  OManager.printf "Checking condition : '@{<bright>%s@}'@\n" name;
  try
    check_condition env (name, check)
  with
  | CheckCondition -> ()
    (*
The check_fail calls done by the checker has produced warnings.
If the warnings are activated, they have been printed already.
If the warnings are errors, exceptions has been caught already.
*)

let check_conds env conds = List.iter (check_cond env) conds

(************************************************)
(* Invariants ***********************************)
type ('env, 'env2) invariant = {
  id : cond_id;
  pre : 'env cond;
  post : 'env2 cond;
}

let compose_fun_invariant f g i = {
  id = i.id;
  pre = compose_fun_condition f i.pre;
  post = compose_fun_condition g i.post
}

let make_invariant cond1 cond2 =
  if not ((fst cond1) = (fst cond2)) then
    invalid_arg (
      Printf.sprintf
        "Try to make invariant with not equals conditions : %s != %s"
        (fst cond1) (fst cond2)
    )
  else
    {
      id = (fst cond1);
      pre = cond1;
      post = cond2;
    }

let make_cons_invariant cond = make_invariant cond cond

(************************************************)
(* Errors ***************************************)
(* support for multi errors: do not crash at the first error if possible *)
let pending_internal_errors = ref []
let push_internal_error ie = pending_internal_errors := ie :: !pending_internal_errors
let raise_internal_error ie =
  let all = ie :: !pending_internal_errors in
  pending_internal_errors := [];
  raise (CompilerInternalError (HdList.wrap (List.rev all)))
let flush_internal_errors () =
  match !pending_internal_errors with
  | [] -> ()
  | ies ->
      pending_internal_errors := [];
      raise (CompilerInternalError (HdList.wrap (List.rev ies)))

let sinternal_error cont printer cond_id context fmt =
  let k message =
    let printer oc context =
      Format.fprintf oc "@{<red>Internal Error@}@\n%a@\n%s@\n" printer context message in
    (* Do not print everything, the function handler does thing too *)
    (* By having some code there, we can print the context is the file system *)
    (* (if not, the Exception should have a type variable in its type !) *)
    PassTracker.internal ~filename:(Option.default "internal_error" cond_id) printer context;
    let message = Format.sprintf "%a" printer context in
    push_internal_error (cond_id, message);
    cont ()
  in
  Format.ksprintf k fmt

let scond_violation printer cond_id = sinternal_error ignore printer (Some cond_id)
let i_serror p = sinternal_error ignore p

let internal_error p =
  let k _ = flush_internal_errors (); assert false (* will necessarly do a raise during sinternal_error *) in
  sinternal_error k p

let cond_violation printer cond_id = internal_error printer (Some cond_id)
let i_error = internal_error

let cont_check_fail cont ~full:full_printer ~console:reduced_printer cond_id context fmt =
  let k message =
    let full_printer oc context =
      Format.fprintf oc "@{<red>Check Failed@}: %s@\n%a@[<2>@\n%s@]@\n" cond_id full_printer context message in
    let wclass =
      match StringMap.find_opt cond_id !cond_map with
      | None -> assert false (* all cond_id are create by using define_cond *)
      | Some wclass -> wclass in
    OManager.printf "@{<red>Internal Error@}, a condition check is broken: '@{<bright>%s@}'@\n" cond_id;
    OManager.printf "The full context is being stored (opatrack) ...@.";
    PassTracker.check_fail ~filename:cond_id full_printer context;
    OManager.printf "Here is a reduced context (use opatrack for full context):@\n";
    reduced_printer OManager.oformatter.contents context;
    OManager.warning ~wclass "%s" message ;
    cont ()
  in
  Format.ksprintf k fmt

let scheck_fail ~full = cont_check_fail ignore ~full
let check_fail ~full = cont_check_fail (fun _ -> raise CheckCondition) ~full

(************************************************)
(* Passes & Handlers ****************************)
type ('opt, 'opt2, 'env, 'env2) pass = {
  invariant : ('env, 'env2) invariant list;
  precond : 'env cond list;
  postcond : 'env2 cond list;
  f : ('opt, 'env) one_env -> ('opt2, 'env2) one_env;
}

type ('opt, 'env, 'env2) old_pass = options:'opt -> 'env -> 'env2

let make_pass ?(invariant = []) ?(precond=[]) ?(postcond=[]) f = {
  f = f;
  invariant = invariant;
  precond = precond;
  postcond = postcond;
}

let init = make_env () ()

(* New handlers *********************************)

(* Current conditions, used for check consistency *)
let current_conds = ref StringSet.empty

(* Keep in mind post cond of prev pass *)
let prev_post_cond = ref StringSet.empty

let handler ?(count_time=true) passname pass env =
  (* common filter for conds and prints *)
  let common_filter set list =
    match set with
    | None -> list
    | Some set ->
        if StringSet.mem passname set then list
        else List.filter
          (fun (str, _) ->
             (StringSet.mem str set)
             || StringSet.exists (fun elt -> String.is_prefix elt str) set)
          list in

  (* Keep in mind what is checked *)
  let checked_conds = Hashtbl.create 32 in

  (* checks conditions to check *)
  let perform_conds str env conds =
    let conds = common_filter !Arg.checks conds in
    if conds != [] then (
      List.iter (fun (id, _) -> Hashtbl.add checked_conds id ()) conds;
      OManager.printf "%sconditions check.@\n" str;
      check_conds env.env conds
    ) else () in

  (* extra print env *)
  let perform_print env =
    let printers = common_filter !Arg.prints (env.printers env.options) in
    List.iter
      (fun (printer_id, printer) ->
         PassTracker.print ~passname ~printer_id printer env.env)
      printers
  in

  (* track pass *)
  let perform_track env =
    let trackers = common_filter !Arg.tracks (env.trackers env.options) in
    List.iter
      (fun (tracker_id, tracker) ->
         PassTracker.track ~passname ~tracker_id tracker env.env)
      trackers
  in

  (* Get invariant to check *)
  let get_invariants =
    let inv =
      ref (
        List.filter
          (fun iv -> StringSet.mem iv.id !prev_post_cond)
          pass.invariant)
    in fun _ -> !inv
  in

  (* Checking invariant on input env - Maybe useless but two check
worth better than one...*)
  let pre_perform_invariant env =
    let ivs = List.map (fun iv -> iv.pre) (get_invariants ()) in
    let ivs = common_filter !Arg.checks ivs in
    if ivs != [] then (
      OManager.printf "Preinvariants check.@\n";
      check_conds env.env ivs
    ) else ()
  in

  (* Checking invariant on output env *)
  let post_perform_invariant env =
    let ivs = List.map (fun iv -> iv.post) (get_invariants ()) in
    let ivs = common_filter !Arg.checks ivs in
    if ivs != [] then (
      OManager.printf "Postinvariants check.@\n";
      check_conds env.env ivs
    ) else ();
    prev_post_cond :=
      List.fold_left (fun a (id, _) -> StringSet.add id a)
        StringSet.empty pass.postcond;
    prev_post_cond :=
      List.fold_left (fun a (id, _) -> StringSet.add id a)
        !prev_post_cond ivs
  in

  (* Check consistency *)
  let consistency () =
    if !Arg.consistency then
      let unconsistent =
        List.filter
          (fun (id, _) -> not (StringSet.mem id !current_conds))
          pass.precond in
      if unconsistent != [] then
        OManager.warning ~wclass:WarningClass.phandler_consistency
          "The following conditions are inconsistent : %s"
          (fst
             (List.fold_left
                (fun (s, p) (id, _) ->
                   (Printf.sprintf "%s%s%s" s p id), ", ")
                ("", "") unconsistent
             ));
      current_conds :=
        StringSet.filter
          (fun id -> not (List.exists (fun iv -> iv.id = id) pass.invariant))
          !current_conds;
      current_conds :=
        List.fold_left
          (fun acc pcond -> StringSet.add (fst pcond) acc)
          !current_conds
          pass.postcond;
  in

  (* Store passname for tracking specific interal logs *)
  PassTracker.set_current_passname passname;

  let run =
    match !Arg.unmarshal with
    | Some passname' when passname = passname' ->
        (* loading the environment from the disk *)
        OManager.verbose "%s: unmarshalling env for @{<bright>%s@}" !title passname;
        let env,options = PassTracker.unmarshal ~passname in
        let printers = RegisterPrinter.retrieve passname in
        `run ({env = env; options=options; printers=printers; trackers = (fun _ -> [])})
    | Some _ ->
        (* we are loading one pass, but not this one *)
        `skip
    | None ->
        (* normal compilation *)
        (match !Arg.marshals with
         | None -> PassTracker.marshal ~passname (env.env,env.options)
         | Some set ->
             if StringSet.mem passname set then
               PassTracker.marshal ~passname (env.env,env.options));
        `run env in

  match run with
  | `skip ->
      OManager.verbose "%s: skipping @{<bright>%s@}" !title passname;
      Obj.magic env
  | `run env -> (

  (* Pre traitment *)
  if not BuildInfos.is_release && not (passname = "") then
    OManager.verbose "%s: %s" !title passname;
  consistency ();
  pre_perform_invariant env;
  perform_conds "Pre" env pass.precond;
  OManager.flush_errors ();

  (* Pass *)
  let chrono = Chrono.make () in
  Chrono.start chrono;
  let env2 =
    let save_bck, get_bck =
      let bck = ref "No backtrace" in
      ((fun () -> bck := (Printexc.get_backtrace ())),
       (fun _ -> !bck)) in
    try (
      try let env = pass.f env in flush_internal_errors (); env with
      | (CompilerInternalError internal_errors) as e ->
          save_bck ();
          let internal_errors = HdList.unwrap internal_errors in
          OManager.printf "An internal error has occurred@\n";
          List.iter
            (function (_, msg) ->
               OManager.printf "Message:@\n@{<magenta>%s@}@\n" msg) internal_errors;
          let internal_errors = List.uniq_unsorted ~cmp:(Pervasives.compare @** fst) internal_errors in
          List.iter (fun (id, _) ->
          begin
            match id with
            | Some id ->
                OManager.printf (
                  "The condition '@{<bright>%s@}' has been declared violated by @{<bright>%s@}@\n"
                ) id passname;
                (match List.find_opt (fun (s , _) -> id = s ) pass.precond with
                 | None ->
                     OManager.printf "'@{<bright>%s@}' is not a precondition of '@{<bright>%s@}'@\n This pass is incoherent !!@\n" id passname
                 | Some precond ->
                     let is_warn_error = Option.default_map false (fun w -> WarningClass.is_warn w && WarningClass.is_warn_error w)
                       (StringMap.find_opt id !cond_map) in
                     let fatal_inconsistency () = OManager.printf (
                       "No error during the execution of the condition checker.@\n"^^
                       "The corresponding '@{<bright>--warn-error @{<red>%s@}@}' was activated.@\n"^^
                       "@{<red>FATAL INCONSISTENCY !@}@\n"^^
                       "@[<2>@{<bright>Hint@}:@\nPlease fix the coherence between the check impl., the pass impl., and the cond semantic.@]@\n" )
                       id
                     in
                     if Hashtbl.mem checked_conds id
                     then
                       begin
                         OManager.printf "'%s' has already been tested as precondition of pass '%s'@\n" id passname;
                         if is_warn_error
                         then fatal_inconsistency ()
                       end
                     else
                       begin
                         OManager.printf "Checking %s@\n" id;
                         check_cond env.env precond;
                         if is_warn_error && not (List.exists (fun w -> String.compare id (WarningClass.get_name w) = 0) (OManager.warn_error_status ()))
                         then fatal_inconsistency ()
                       end
                )
            | None -> ()
          end;
            ) internal_errors;
          raise e
      | e ->
          save_bck ();
          OManager.serror "An internal error has occurred during the pass @{<bright>%s@}" passname;
          raise e )
    with
    | e ->
        (* Print all that is possible - TODO add the possibility to
make filter *)
        (match e with CompilerInternalError _ -> () | _ -> OManager.printf "@[<2>Uncaught Exception:@\n@{<magenta>%s@}@]@\n" (Printexc.to_string e));
        OManager.printf "@[<2>Backtrace:@\n%s@]@\n" (get_bck ());
        PassTracker.internal ~filename:"backtrace" (fun fmt _ -> Format.fprintf fmt "%s@\nBacktrace:\n%s@\n" (Printexc.to_string e) (get_bck ())) ();
        OManager.printf "Saving the current environment for report (opatrack) ...@.";
        Arg.prints := None;
        perform_print env;
        Arg.tracks := None;
        perform_track env;
        OManager.flush_errors ();
        exit 1
  in
  Chrono.stop chrono;
  let c = Chrono.read chrono in
  let () =
    if not BuildInfos.is_release && count_time then PassTracker.time ~passname c
  in

  (* Post traitment *)
  perform_print env2;
  perform_track env2;
  post_perform_invariant env2;
  perform_conds "Post" env2 pass.postcond;
  OManager.flush_errors ();
  #<If:TESTING> ()
  #<Else>
    if not BuildInfos.is_release && count_time && c > 0.2 then
      OManager.verbose "duration %s" (Date.pretty_duration c)
  #<End>;
  env2
    )

let (|+>) env (passname, pass) = handler passname pass env

let if_handler
    ?(if_ = fun ~options:_ _ -> true)
    name pass env =
  if if_ ~options:env.options env.env then
    handler name pass env
  else env

let (|?>) env (if_, passname, pass) =
  if_handler ~if_ passname pass env

let alt_handler if_ (name1, pass1) (name2, pass2) env =
  if if_ ~options:env.options env.env then
    handler name1 pass1 env
  else
    handler name2 pass2 env

let (<?>) env (if_, (name1, pass1), (name2, pass2)) =
  alt_handler if_ (name1, pass1) (name2, pass2) env

let switch_handler switch pass env =
  let switcher = switch ~options:env.options env.env in
  let name, pass = pass switcher in
  handler name pass env

let (|?|) env (switch, pass) =
  switch_handler switch pass env

let return env = env.env

let (|>) = InfixOperator.(|>)

let register_printer = RegisterPrinter.register

(* Old handlers *********************************)
let old_if_handler
    ?(if_ = fun ~options:_ _ -> true)
    ?(precond = []) ?(postcond = [])
    name pass env =
  if if_ ~options:env.options env.env then
    let pass =
      make_pass ~precond ~postcond
        (fun env ->
           { env with
               env = pass ~options:env.options env.env })
    in
    handler name pass env
  else env

(* This handler should be no used because create env with default
printer... *)
let old_handler
    ?(precond = []) ?(postcond = [])
    name pass env =
  let pass =
    make_pass ~precond ~postcond
      (fun e ->
         make_env e.options (pass ~options:e.options e.env)) in
  handler name pass env


let (&) if1 if2 = fun ~options env -> if1 ~options env && if2 ~options env
let (or) if1 if2 = fun ~options env -> if1 ~options env || if2 ~options env
let neg if_ = fun ~options env -> not (if_ ~options env)
Something went wrong with that request. Please try again.