File: driver.ml

package info (click to toggle)
ppxlib 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 1,284 kB
  • sloc: ml: 17,184; sh: 149; makefile: 36; python: 36
file content (1330 lines) | stat: -rw-r--r-- 44,638 bytes parent folder | download
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
(*$ open Ppxlib_cinaps_helpers $*)
open Import
open Utils

module Arg = Caml.Arg

let exe_name = Caml.Filename.basename Caml.Sys.executable_name

let args = ref []

let add_arg key spec ~doc = args := (key, spec, doc) :: !args

let loc_fname = ref None
let perform_checks = ref Options.perform_checks
let perform_checks_on_extensions = ref Options.perform_checks_on_extensions
let perform_locations_check = ref Options.perform_locations_check
let debug_attribute_drop = ref false
let apply_list = ref None
let preprocessor = ref None
let no_merge = ref false
let request_print_passes = ref false
let request_print_transformations = ref false
let use_color = ref true
let diff_command = ref Options.diff_command
let pretty = ref false
let styler = ref None
let output_metadata_filename = ref None
let corrected_suffix = ref ".ppx-corrected"

module Lint_error = struct
  type t = Location.t * string

  let of_string loc s = (loc, s)
end

module Cookies = struct
  type t = Migrate_parsetree.Driver.cookies

  let get t name pattern =
    Option.map (Migrate_parsetree.Driver.get_cookie t name (module Ppxlib_ast.Selected_ast))
      ~f:(fun e ->
        Ast_pattern.parse pattern e.pexp_loc e Fn.id)

  let set t name expr =
    Migrate_parsetree.Driver.set_cookie t name (module Ppxlib_ast.Selected_ast) expr

  let handlers = ref []
  let add_handler f = handlers := !handlers @ [f]

  let add_simple_handler name pattern ~f =
    add_handler (fun t -> f (get t name pattern))

  let acknoledge_cookies t =
    List.iter !handlers ~f:(fun f -> f t)

  let post_handlers = ref []
  let add_post_handler f = post_handlers := !post_handlers @ [f]

  let call_post_handlers t =
    List.iter !post_handlers ~f:(fun f -> f t)
end

module Transform = struct
  type t =
    { name            : string
    ; aliases         : string list
    ; impl            : (Parsetree.structure -> Parsetree.structure) option
    ; intf            : (Parsetree.signature -> Parsetree.signature) option
    ; lint_impl       : (Parsetree.structure -> Lint_error.t list) option
    ; lint_intf       : (Parsetree.signature -> Lint_error.t list) option
    ; preprocess_impl : (Parsetree.structure -> Parsetree.structure) option
    ; preprocess_intf : (Parsetree.signature -> Parsetree.signature) option
    ; enclose_impl    : (Location.t option -> Parsetree.structure * Parsetree.structure) option
    ; enclose_intf    : (Location.t option -> Parsetree.signature * Parsetree.signature) option
    ; rules           : Context_free.Rule.t list
    ; registered_at   : Caller_id.t
    }

  let has_name t name =
    (String.equal name t.name) || (List.exists ~f:(String.equal name) t.aliases)

  let all : t list ref = ref []

  let print_caller_id oc (caller_id : Caller_id.t) =
    match caller_id with
    | None -> output_string oc "<unknown location>"
    | Some loc -> Printf.fprintf oc "%s:%d" loc.filename loc.line_number
  ;;

  let register ?(extensions=[]) ?(rules=[]) ?enclose_impl ?enclose_intf
        ?impl ?intf ?lint_impl ?lint_intf ?preprocess_impl ?preprocess_intf
        ?(aliases=[]) name =
    let rules =
      List.map extensions ~f:Context_free.Rule.extension @ rules
    in
    let caller_id = Caller_id.get ~skip:[Caml.__FILE__] in
    begin match List.filter !all ~f:(fun ct -> has_name ct name) with
    | [] -> ()
    | ct :: _ ->
      Printf.eprintf "Warning: code transformation %s registered twice.\n" name;
      Printf.eprintf "  - first time was at %a\n" print_caller_id ct.registered_at;
      Printf.eprintf "  - second time is at %a\n" print_caller_id caller_id;
    end;
    let ct =
      { name
      ; aliases
      ; rules
      ; enclose_impl
      ; enclose_intf
      ; impl
      ; intf
      ; lint_impl
      ; preprocess_impl
      ; preprocess_intf
      ; lint_intf
      ; registered_at = caller_id
      }
    in
    all := ct :: !all
  ;;

  let rec last prev l =
    match l with
    | [] -> prev
    | x :: l -> last x l
  ;;

  let loc_of_list ~get_loc l =
    match l with
    | [] -> None
    | x :: l ->
      let first : Location.t = get_loc x in
      let last = get_loc (last x l) in
      Some { first with loc_end = last.loc_end }
  ;;

  let merge_into_generic_mappers t ~hook ~expect_mismatch_handler ~tool_name =
    let { rules; enclose_impl; enclose_intf; impl; intf; _ } = t in
    let map =
      new Context_free.map_top_down rules
        ~generated_code_hook:hook
        ~expect_mismatch_handler
    in
    let gen_header_and_footer context whole_loc f =
      let header, footer = f whole_loc in
      (match whole_loc with
      | Some (loc : Location.t) ->
        let loc_header = { loc with loc_end   = loc.loc_start } in
        let loc_footer = { loc with loc_start = loc.loc_end   } in
        (match header with [] -> () | _ -> hook.f context loc_header (Many header));
        (match footer with [] -> () | _ -> hook.f context loc_footer (Many footer))
      | None ->
        match header @ footer with
        | [] -> ()
        | l ->
          let pos =
            { Lexing.
              pos_fname = ""
            ; pos_lnum  = 1
            ; pos_bol   = 0
            ; pos_cnum  = 0
            }
          in
          let loc = { Location. loc_start = pos; loc_end = pos; loc_ghost = false } in
          hook.f context loc (Many l));
      (header, footer)
    in
    let map_impl st_with_attrs =
      let st =
        let attrs, st =
          List.split_while st_with_attrs ~f:(function
            | { pstr_desc = Pstr_attribute _; _ } -> true
            | _ -> false)
        in
        let header, footer =
          match enclose_impl with
          | None   -> ([], [])
          | Some f ->
            let whole_loc = loc_of_list st ~get_loc:(fun st -> st.Parsetree.pstr_loc) in
            gen_header_and_footer Structure_item whole_loc f
        in
        let file_path = File_path.get_default_path_str st in
        let base_ctxt = Expansion_context.Base.top_level ~tool_name ~file_path in
        let attrs = map#structure base_ctxt attrs in
        let st = map#structure base_ctxt st in
        List.concat [ attrs; header; st; footer ]
      in
      match impl with
      | None -> st
      | Some f -> f st
    in
    let map_intf sg_with_attrs =
      let sg =
        let attrs, sg =
          List.split_while sg_with_attrs ~f:(function
            | { psig_desc = Psig_attribute _; _ } -> true
            | _ -> false)
        in
        let header, footer =
          match enclose_intf with
          | None   -> ([], [])
          | Some f ->
            let whole_loc = loc_of_list sg ~get_loc:(fun sg -> sg.Parsetree.psig_loc) in
            gen_header_and_footer Signature_item whole_loc f
        in
        let file_path = File_path.get_default_path_sig sg in
        let base_ctxt = Expansion_context.Base.top_level ~tool_name ~file_path in
        let attrs = map#signature base_ctxt attrs in
        let sg = map#signature base_ctxt sg in
        List.concat [ attrs; header; sg; footer ]
      in
      match intf with
      | None -> sg
      | Some f -> f sg
    in
    { t with
      impl = Some map_impl
    ; intf = Some map_intf
    }

  let builtin_of_context_free_rewriters ~hook ~rules ~enclose_impl ~enclose_intf =
    merge_into_generic_mappers ~hook
      { name = "<builtin:context-free>"
      ; aliases = []
      ; impl = None
      ; intf = None
      ; lint_impl = None
      ; lint_intf = None
      ; preprocess_impl = None
      ; preprocess_intf = None
      ; enclose_impl
      ; enclose_intf
      ; rules
      ; registered_at = Caller_id.get ~skip:[]
      }

  let partition_transformations ts =
    (`Linters
       (List.filter_map ts ~f:(fun t ->
          if Option.is_some t.lint_impl || Option.is_some t.lint_intf then
            Some
              { name = Printf.sprintf "<lint:%s>" t.name
              ; aliases = []
              ; impl = None
              ; intf = None
              ; lint_impl = t.lint_impl
              ; lint_intf = t.lint_intf
              ; enclose_impl = None
              ; enclose_intf = None
              ; preprocess_impl = None
              ; preprocess_intf = None
              ; rules = []
              ; registered_at = t.registered_at
              }
          else
            None)),
     `Preprocess
       (List.filter_map ts ~f:(fun t ->
          if Option.is_some t.preprocess_impl || Option.is_some t.preprocess_intf
          then
            Some
              { name = Printf.sprintf "<preprocess:%s>" t.name
              ; aliases = []
              ; impl = t.preprocess_impl
              ; intf = t.preprocess_intf
              ; lint_impl = None
              ; lint_intf = None
              ; enclose_impl = None
              ; enclose_intf = None
              ; preprocess_impl = None
              ; preprocess_intf = None
              ; rules = []
              ; registered_at = t.registered_at
              }
          else
            None)),
     `Rest
       (List.map ts ~f:(fun t ->
          { t with
            lint_impl = None
          ; lint_intf = None
          ; preprocess_impl = None
          ; preprocess_intf = None
          })))
end

let register_transformation = Transform.register

let register_code_transformation ~name ?(aliases=[]) ~impl ~intf =
  register_transformation name ~impl ~intf ~aliases
;;

let register_transformation_using_ocaml_current_ast ?impl ?intf ?aliases name =
  let impl = Option.map impl ~f:(Ppxlib_ast.Selected_ast.of_ocaml_mapper Structure) in
  let intf = Option.map intf ~f:(Ppxlib_ast.Selected_ast.of_ocaml_mapper Signature) in
  register_transformation ?impl ?intf ?aliases name

let debug_dropped_attribute name ~old_dropped ~new_dropped =
  let print_diff what a b =
    let diff =
      List.filter a ~f:(fun (name : _ Loc.t) ->
        not (List.exists b ~f:(fun (name' : _ Location.loc) -> name.txt == name'.txt)))
    in
    if not (List.is_empty diff) then begin
      Printf.eprintf "The following attributes %s after applying %s:\n"
        what name;
      List.iter diff ~f:(fun { Location. txt; loc } ->
        Caml.Format.eprintf "- %a: %s\n" Location.print loc txt);
      Caml.Format.eprintf "@."
    end
  in
  print_diff "disappeared" new_dropped old_dropped;
  print_diff "reappeared"  old_dropped new_dropped
;;

let get_whole_ast_passes ~hook ~expect_mismatch_handler ~tool_name =
  let cts =
    match !apply_list with
    | None -> List.rev !Transform.all
    | Some names ->
      List.map names ~f:(fun name ->
        List.find !Transform.all ~f:(fun (ct : Transform.t) ->
          Transform.has_name ct name))
  in
  let (`Linters linters, `Preprocess preprocess, `Rest cts) =
    Transform.partition_transformations cts in
  (* Allow only one preprocessor to assure deterministic order *)
  if (List.length preprocess) > 1 then begin
    let pp = String.concat ~sep:", " (List.map preprocess ~f:(fun t -> t.name)) in
    let err = Printf.sprintf "At most one preprocessor is allowed, while got: %s" pp in
    failwith err
  end;
  let cts =
    if !no_merge then
      List.map cts ~f:(Transform.merge_into_generic_mappers ~hook ~tool_name
                         ~expect_mismatch_handler)
    else begin
      let get_enclosers ~f =
        List.filter_map cts ~f:(fun (ct : Transform.t) ->
          match f ct with
          | None -> None
          | Some x -> Some (ct.name, x))
        (* Sort them to ensure deterministic ordering *)
        |> List.sort ~cmp:(fun (a, _) (b, _) -> String.compare a b)
        |> List.map ~f:snd
      in

      let rules =
        List.map cts ~f:(fun (ct : Transform.t) -> ct.rules) |> List.concat
      and impl_enclosers =
        get_enclosers ~f:(fun ct -> ct.enclose_impl)
      and intf_enclosers =
        get_enclosers ~f:(fun ct -> ct.enclose_intf)
      in
      match rules, impl_enclosers, intf_enclosers with
      | [], [], [] -> cts
      | _              ->
        let merge_encloser = function
          | [] -> None
          | enclosers -> Some (fun loc ->
            let headers, footers =
              List.map enclosers ~f:(fun f -> f loc)
              |> List.split
            in
            let headers = List.concat headers in
            let footers = List.concat (List.rev footers) in
            (headers, footers))
        in
        Transform.builtin_of_context_free_rewriters ~rules ~hook ~expect_mismatch_handler
          ~enclose_impl:(merge_encloser impl_enclosers)
          ~enclose_intf:(merge_encloser intf_enclosers)
          ~tool_name
        :: cts
    end
  in linters @ preprocess @ List.filter cts ~f:(fun (ct : Transform.t) ->
    match ct.impl, ct.intf with
    | None, None -> false
    | _          -> true)
;;

let apply_transforms
      ~tool_name ~field ~lint_field ~dropped_so_far ~hook ~expect_mismatch_handler x =
  let cts = get_whole_ast_passes ~tool_name ~hook ~expect_mismatch_handler in
  let x, _dropped, lint_errors =
  List.fold_left cts ~init:(x, [], [])
    ~f:(fun (x, dropped, lint_errors) (ct : Transform.t) ->
      let lint_errors =
        match lint_field ct with
        | None -> lint_errors
        | Some f -> lint_errors @ f x
      in
      match field ct with
      | None -> (x, dropped, lint_errors)
      | Some f ->
        let x = f x in
        let dropped =
          if !debug_attribute_drop then begin
            let new_dropped = dropped_so_far x in
            debug_dropped_attribute ct.name ~old_dropped:dropped ~new_dropped;
            new_dropped
          end else
            []
        in
        (x, dropped, lint_errors))
  in
  (x, List.map lint_errors ~f:(fun (loc, s) -> Common.attribute_of_warning loc s))
;;

(* +-----------------------------------------------------------------+
   | Actual rewriting of structure/signatures                        |
   +-----------------------------------------------------------------+ *)

(* We want driver registered plugins to work with omp driver and vice-versa. To
   simplify things we do as follow:

   - we register driver as a single omp driver plugin
   - driver calls the omp driver rewriting functions, which will apply everything

   The registration with omp driver is at the end of the file.
*)

module C = struct
  type t =
    { hook                    : Context_free.Generated_code_hook.t
    ; expect_mismatch_handler : Context_free.Expect_mismatch_handler.t
    }

  type Migrate_parsetree.Driver.extra += T of t

  let default =
    { hook                    = Context_free.Generated_code_hook.nop
    ; expect_mismatch_handler = Context_free.Expect_mismatch_handler.nop
    }

  let find (config : Migrate_parsetree.Driver.config) =
    List.find_map config.extras ~f:(function
      | T config -> Some config
      | _ -> None)
    |> Option.value ~default
end

let config ~hook ~expect_mismatch_handler =
  Migrate_parsetree.Driver.make_config ()
    ~tool_name:"ppxlib_driver"
    ~extras:[C.T { hook
                 ; expect_mismatch_handler
                 }]

let as_ppx_config () =
  Migrate_parsetree.Driver.make_config ()
    ~tool_name:(Ocaml_common.Ast_mapper.tool_name ())
    ~include_dirs:!Ocaml_common.Clflags.include_dirs
    ~load_path:(Compiler_specifics.get_load_path ())
    ~debug:!Ocaml_common.Clflags.debug
    ?for_package:!Ocaml_common.Clflags.for_package

let print_passes () =
  let tool_name = "ppxlib_driver" in
  let hook = Context_free.Generated_code_hook.nop in
  let expect_mismatch_handler = Context_free.Expect_mismatch_handler.nop in
  let cts = get_whole_ast_passes ~hook ~expect_mismatch_handler ~tool_name in
  if !perform_checks then
    Printf.printf "<builtin:freshen-and-collect-attributes>\n";
  List.iter cts ~f:(fun ct -> Printf.printf "%s\n" ct.Transform.name);
  if !perform_checks then
    begin
      Printf.printf "<builtin:check-unused-attributes>\n";
      if !perform_checks_on_extensions
      then Printf.printf "<builtin:check-unused-extensions>\n"
    end
;;

(*$*)
let real_map_structure config cookies st =
  let { C. hook; expect_mismatch_handler } = C.find config in
  Cookies.acknoledge_cookies cookies;
  if !perform_checks then begin
    Attribute.reset_checks ();
    Attribute.collect#structure st
  end;
  let st, lint_errors =
    apply_transforms st
      ~tool_name:config.Migrate_parsetree.Driver.tool_name
      ~field:(fun (ct : Transform.t) -> ct.impl)
      ~lint_field:(fun (ct : Transform.t) -> ct.lint_impl)
      ~dropped_so_far:Attribute.dropped_so_far_structure ~hook ~expect_mismatch_handler
  in
  let st =
    match lint_errors with
    | [] -> st
    | _  ->
      List.map lint_errors ~f:(fun ({ attr_name = { loc; _ }; _} as attr) ->
        Ast_builder.Default.pstr_attribute ~loc attr)
      @ st
  in
  Cookies.call_post_handlers cookies;
  if !perform_checks then begin
    (* TODO: these two passes could be merged, we now have more passes for
       checks than for actual rewriting. *)
    Attribute.check_unused#structure st;
    if !perform_checks_on_extensions then Extension.check_unused#structure st;
    Attribute.check_all_seen ();
    if !perform_locations_check then
      let open Location_check in
      ignore (
        (enforce_invariants !loc_fname)#structure
          st Non_intersecting_ranges.empty : Non_intersecting_ranges.t)
  end;
  st
;;

let map_structure_gen st ~config : Migrate_parsetree.Driver.some_structure =
  Migrate_parsetree.Driver.rewrite_structure
    config
    (module Ppxlib_ast.Selected_ast)
    st

let map_structure st =
  map_structure_gen st ~config:(as_ppx_config ())

(*$ str_to_sig _last_text_block *)
let real_map_signature config cookies sg =
  let { C. hook; expect_mismatch_handler } = C.find config in
  Cookies.acknoledge_cookies cookies;
  if !perform_checks then begin
    Attribute.reset_checks ();
    Attribute.collect#signature sg
  end;
  let sg, lint_errors =
    apply_transforms sg
      ~tool_name:config.Migrate_parsetree.Driver.tool_name
      ~field:(fun (ct : Transform.t) -> ct.intf)
      ~lint_field:(fun (ct : Transform.t) -> ct.lint_intf)
      ~dropped_so_far:Attribute.dropped_so_far_signature ~hook ~expect_mismatch_handler
  in
  let sg =
    match lint_errors with
    | [] -> sg
    | _  ->
      List.map lint_errors ~f:(fun ({ attr_name = { loc; _ }; _} as attr) ->
        Ast_builder.Default.psig_attribute ~loc attr)
      @ sg
  in
  Cookies.call_post_handlers cookies;
  if !perform_checks then begin
    (* TODO: these two passes could be merged, we now have more passes for
       checks than for actual rewriting. *)
    Attribute.check_unused#signature sg;
    if !perform_checks_on_extensions then Extension.check_unused#signature sg;
    Attribute.check_all_seen ();
    if !perform_locations_check then
      let open Location_check in
      ignore (
        (enforce_invariants !loc_fname)#signature
          sg Non_intersecting_ranges.empty : Non_intersecting_ranges.t)
  end;
  sg
;;

let map_signature_gen sg ~config : Migrate_parsetree.Driver.some_signature =
  Migrate_parsetree.Driver.rewrite_signature
    config
    (module Ppxlib_ast.Selected_ast)
    sg

let map_signature sg =
  map_signature_gen sg ~config:(as_ppx_config ())

(*$*)

(* +-----------------------------------------------------------------+
   | Entry points                                                    |
   +-----------------------------------------------------------------+ *)

let mapper =
  let module Js = Ppxlib_ast.Selected_ast in
  (*$*)
  let structure _ st =
    Js.of_ocaml Structure st
    |> map_structure
    |> Migrate_parsetree.Driver.migrate_some_structure
         (module Migrate_parsetree.OCaml_current)
  in
  (*$ str_to_sig _last_text_block *)
  let signature _ sg =
    Js.of_ocaml Signature sg
    |> map_signature
    |> Migrate_parsetree.Driver.migrate_some_signature
         (module Migrate_parsetree.OCaml_current)
  in
  (*$*)
  { Ocaml_common.Ast_mapper.default_mapper with structure; signature }
;;

let as_ppx_rewriter_main argv =
  let argv = Caml.Sys.executable_name :: argv in
  let usage =
    Printf.sprintf "%s [extra_args] <infile> <outfile>" exe_name
  in
  match
    Arg.parse_argv (Array.of_list argv) (Arg.align (List.rev !args))
      (fun _ -> raise (Arg.Bad "anonymous arguments not accepted"))
      usage
  with
  | exception Arg.Bad  msg -> Printf.eprintf "%s" msg; Caml.exit 2
  | exception Arg.Help msg -> Printf.eprintf "%s" msg; Caml.exit 0
  | () -> mapper

let run_as_ppx_rewriter () =
  perform_checks := false;
  Ocaml_common.Ast_mapper.run_main as_ppx_rewriter_main;
  Caml.exit 0

let string_contains_binary_ast s =
  let test magic_number =
    String.is_prefix s ~prefix:(String.sub magic_number ~pos:0 ~len:9)
  in
  test Ast_magic.ast_intf_magic_number ||
  test Ast_magic.ast_impl_magic_number

type pp_error = { filename : string; command_line : string }
exception Pp_error of pp_error

let report_pp_error e =
  let buff = Buffer.create 128 in
  let ppf = Caml.Format.formatter_of_buffer buff in
  Caml.Format.fprintf ppf "Error while running external preprocessor@.\
                           Command line: %s@." e.command_line;
  Caml.Format.pp_print_flush ppf ();
  Buffer.contents buff

let () =
  Location.Error.register_error_of_exn
    (function
      | Pp_error e ->
        Some (Location.Error.make ~loc:(Location.in_file e.filename) ~sub:[]
                (report_pp_error e))
      | _ -> None)

let remove_no_error fn =
  try Caml.Sys.remove fn with Sys_error _ -> ()

let protectx x ~f ~finally =
  match f x with
  | v -> finally x; v
  | exception e -> finally x; raise e
;;

let with_preprocessed_file fn ~f =
  match !preprocessor with
  | None -> f fn
  | Some pp ->
    protectx (Caml.Filename.temp_file "ocamlpp" "")
      ~finally:remove_no_error
      ~f:(fun tmpfile ->
        let comm =
          Printf.sprintf "%s %s > %s"
            pp (if String.equal fn "-" then "" else Caml.Filename.quote fn)
            (Caml.Filename.quote tmpfile)
        in
        if Caml.Sys.command comm <> 0 then
          raise (Pp_error { filename = fn
                          ; command_line = comm
                          });
        f tmpfile)

let with_preprocessed_input fn ~f =
  with_preprocessed_file fn ~f:(fun fn ->
    if String.equal fn "-" then
      f stdin
    else
      In_channel.with_file fn ~f)
;;

let relocate_mapper = object
  inherit [string * string] Ast_traverse.map_with_context

  method! position (old_fn, new_fn) pos =
    if String.equal pos.pos_fname old_fn then
      { pos with pos_fname = new_fn }
    else
      pos
end

(* Set the input name globally. This is used by some ppx rewriters
   such as bisect_ppx. *)
let set_input_name name =
  Ocaml_common.Location.input_name := name

let load_input (kind : Kind.t) fn input_name ~relocate ic =
  set_input_name input_name;
  match Migrate_parsetree.Ast_io.from_channel ic with
  | Ok (ast_input_name, ast) ->
    let ast = Intf_or_impl.of_ast_io ast in
    if not (Kind.equal kind (Intf_or_impl.kind ast)) then
      Location.raise_errorf ~loc:(Location.in_file fn)
        "File contains a binary %s AST but an %s was expected"
        (Kind.describe (Intf_or_impl.kind ast))
        (Kind.describe kind);
    if String.equal ast_input_name input_name || not relocate then begin
      set_input_name ast_input_name;
      (ast_input_name, ast)
    end else
      (input_name,
       Intf_or_impl.map_with_context ast relocate_mapper
         (ast_input_name, input_name))

  | Error (Unknown_version _) ->
    Location.raise_errorf ~loc:(Location.in_file fn)
      "File is a binary ast for an unknown version of OCaml"
  | Error (Not_a_binary_ast prefix_read_from_file) ->
    (* To test if a file is an AST file, we have to read the first few bytes of the
       file. If it is not, we have to parse these bytes and the rest of the file as
       source code.

       The compiler just does [seek_on 0] in this case, however this doesn't work when
       the input is a pipe.

       What we do instead is create a lexing buffer from the input channel and pre-fill
       it with what we read to do the test. *)
    let lexbuf = Lexing.from_channel ic in
    let len = String.length prefix_read_from_file in
    Bytes.blit_string ~src:prefix_read_from_file ~src_pos:0 ~dst:lexbuf.lex_buffer ~dst_pos:0
      ~len;
    lexbuf.lex_buffer_len <- len;
    lexbuf.lex_curr_p <-
      { pos_fname = input_name
      ; pos_lnum  = 1
      ; pos_bol   = 0
      ; pos_cnum  = 0
      };
    Lexer.skip_hash_bang lexbuf;
    match kind with
    | Intf -> input_name, Intf (Parse.interface      lexbuf)
    | Impl -> input_name, Impl (Parse.implementation lexbuf)
;;

let load_source_file fn =
  let s = In_channel.read_all fn in
  if string_contains_binary_ast s then
    Location.raise_errorf ~loc:(Location.in_file fn)
      "ppxlib_driver: cannot use -reconcile with binary AST files";
  s
;;

type output_mode =
  | Pretty_print
  | Dump_ast
  | Dparsetree
  | Reconcile of Reconcile.mode
  | Null

(*$*)
let extract_cookies_str st =
  match st with
  | { pstr_desc = Pstr_attribute {attr_name={txt = "ocaml.ppx.context"; _}; _}; _ } as prefix
    :: st ->
    let prefix = Ppxlib_ast.Selected_ast.to_ocaml Structure [prefix] in
    assert (List.is_empty
              (Ocaml_common.Ast_mapper.drop_ppx_context_str ~restore:true prefix));
    st
  | _ -> st

let add_cookies_str st =
  let prefix =
    Ocaml_common.Ast_mapper.add_ppx_context_str ~tool_name:"ppxlib_driver" []
    |> Ppxlib_ast.Selected_ast.of_ocaml Structure
  in
  prefix @ st

(*$ str_to_sig _last_text_block *)
let extract_cookies_sig sg =
  match sg with
  | { psig_desc = Psig_attribute {attr_name={txt = "ocaml.ppx.context"; _}; _}; _ } as prefix
    :: sg ->
    let prefix = Ppxlib_ast.Selected_ast.to_ocaml Signature [prefix] in
    assert (List.is_empty
              (Ocaml_common.Ast_mapper.drop_ppx_context_sig ~restore:true prefix));
    sg
  | _ -> sg

let add_cookies_sig sg =
  let prefix =
    Ocaml_common.Ast_mapper.add_ppx_context_sig ~tool_name:"ppxlib_driver" []
    |> Ppxlib_ast.Selected_ast.of_ocaml Signature
  in
  prefix @ sg

(*$*)

let extract_cookies (ast : Intf_or_impl.t) : Intf_or_impl.t =
  match ast with
  | Intf x -> Intf (extract_cookies_sig x)
  | Impl x -> Impl (extract_cookies_str x)

let add_cookies (ast : Intf_or_impl.t) : Intf_or_impl.t =
  match ast with
  | Intf x -> Intf (add_cookies_sig x)
  | Impl x -> Impl (add_cookies_str x)

let corrections = ref []

let add_to_list r x = r := x :: !r

let register_correction ~loc ~repl =
  add_to_list corrections
    (Reconcile.Replacement.make_text ()
       ~start:loc.loc_start
       ~stop:loc.loc_end
       ~repl)

let process_file_hooks = ref []

let register_process_file_hook f =
  add_to_list process_file_hooks f

module File_property = struct
  type 'a t =
    { name         : string
    ; mutable data : 'a option
    ; sexp_of_t    : 'a -> Sexp.t
    }

  type packed = T : _ t -> packed

  let all = ref []

  let register t = add_to_list all (T t)

  let reset_all () =
    List.iter !all ~f:(fun (T t) -> t.data <- None)

  let dump_and_reset_all () =
    List.filter_map (List.rev !all) ~f:(fun (T t) ->
      match t.data with
      | None -> None
      | Some v ->
        t.data <- None;
        Some (t.name, t.sexp_of_t v))
end

module Create_file_property(Name : sig val name : string end)(T : Sexpable.S) = struct
  let t : _ File_property.t =
    { name      = Name.name
    ; data      = None
    ; sexp_of_t = T.sexp_of_t
    }

  let () = File_property.register t

  let set x = t.data <- Some x
end

let process_file (kind : Kind.t) fn ~input_name ~relocate ~output_mode ~embed_errors ~output =
  File_property.reset_all ();
  List.iter (List.rev !process_file_hooks) ~f:(fun f -> f ());
  corrections := [];
  let replacements = ref [] in
  let hook : Context_free.Generated_code_hook.t =
    match output_mode with
    | Reconcile (Using_line_directives | Delimiting_generated_blocks) ->
      { f = fun context (loc : Location.t) generated ->
          add_to_list replacements
            (Reconcile.Replacement.make ()
               ~context:(Extension context)
               ~start:loc.loc_start
               ~stop:loc.loc_end
               ~repl:generated)
      }
    | _ ->
      Context_free.Generated_code_hook.nop
  in
  let expect_mismatch_handler : Context_free.Expect_mismatch_handler.t =
    { f = fun context (loc : Location.t) generated ->
        add_to_list corrections
          (Reconcile.Replacement.make ()
             ~context:(Floating_attribute context)
             ~start:loc.loc_start
             ~stop:loc.loc_end
             ~repl:(Many generated))
    }
  in

  let input_name, ast =
    try
      let input_name, ast =
        with_preprocessed_input fn ~f:(load_input kind fn input_name ~relocate)
      in
      let ast = extract_cookies ast in
      let config = config ~hook ~expect_mismatch_handler in
      match ast with
      | Intf x -> input_name, Some_intf_or_impl.Intf (map_signature_gen x ~config)
      | Impl x -> input_name, Some_intf_or_impl.Impl (map_structure_gen x ~config)
    with exn when embed_errors ->
    match Location.Error.of_exn exn with
    | None -> raise exn
    | Some error ->
      let loc = Location.none in
      let ext = Location.Error.to_extension error in
      let open Ast_builder.Default in
      let ast = match kind with
        | Intf ->
           Some_intf_or_impl.Intf
             (Sig ((module Ppxlib_ast.Selected_ast),
                   [ psig_extension ~loc ext [] ]))
        | Impl ->
           Some_intf_or_impl.Impl
             (Str ((module Ppxlib_ast.Selected_ast),
                   [ pstr_extension ~loc ext [] ]))
      in
      input_name, ast
    in

    Option.iter !output_metadata_filename ~f:(fun fn ->
      let metadata = File_property.dump_and_reset_all () in
      Out_channel.write_all fn
        ~data:(
          List.map metadata ~f:(fun (s, sexp) ->
            Sexp.to_string_hum (Sexp.List [Atom s; sexp]) ^ "\n")
          |> String.concat ~sep:""));

    let input_contents = lazy (load_source_file fn) in
    let corrected = fn ^ !corrected_suffix in
    let mismatches_found =
      match !corrections with
      | [] ->
        if Caml.Sys.file_exists corrected then Caml.Sys.remove corrected;
        false
      | corrections ->
        Reconcile.reconcile corrections ~contents:(Lazy.force input_contents)
          ~output:(Some corrected) ~input_filename:fn ~input_name ~target:Corrected
          ?styler:!styler ~kind;
        true
    in

    (match output_mode with
     | Null -> ()
     | Pretty_print ->
       with_output output ~binary:false ~f:(fun oc ->
         let ppf = Caml.Format.formatter_of_out_channel oc in
         let ast = Intf_or_impl.of_some_intf_or_impl ast in
         (match ast with
          | Intf ast -> Pprintast.signature ppf ast
          | Impl ast -> Pprintast.structure ppf ast);
         let null_ast =
           match ast with
           | Intf [] | Impl [] -> true
           | _ -> false
         in
         if not null_ast then Caml.Format.pp_print_newline ppf ())
     | Dump_ast ->
       with_output output ~binary:true ~f:(fun oc ->
         let ast = Some_intf_or_impl.to_ast_io ast ~add_ppx_context:true in
         Migrate_parsetree.Ast_io.to_channel oc input_name ast)
     | Dparsetree ->
       with_output output ~binary:false ~f:(fun oc ->
         let ppf = Caml.Format.formatter_of_out_channel oc in
         let ast = Intf_or_impl.of_some_intf_or_impl ast in
         let ast = add_cookies ast in
         (match ast with
          | Intf ast -> Sexp.pp_hum ppf (Ast_traverse.sexp_of#signature ast)
          | Impl ast -> Sexp.pp_hum ppf (Ast_traverse.sexp_of#structure ast));
         Caml.Format.pp_print_newline ppf ())
     | Reconcile mode ->
       Reconcile.reconcile !replacements ~contents:(Lazy.force input_contents) ~output
         ~input_filename:fn ~input_name ~target:(Output mode) ?styler:!styler
         ~kind);

    if mismatches_found &&
       (match !diff_command with
        | Some  "-" -> false
        | _ -> true) then begin
      Ppxlib_print_diff.print () ~file1:fn ~file2:corrected ~use_color:!use_color
        ?diff_command:!diff_command;
      Caml.exit 1
    end
;;

let output_mode = ref Pretty_print
let output = ref None
let kind = ref None
let input = ref None
let embed_errors = ref false
let set_input fn =
  match !input with
  | None -> input := Some fn
  | Some _ -> raise (Arg.Bad "too many input files")

let set_kind k =
  match !kind with
  | Some k' when not (Kind.equal k k') ->
    raise (Arg.Bad "must specify at most one of -impl or -intf")
  | _ -> kind := Some k
;;

let set_output_mode mode =
  match !output_mode, mode with
  | Pretty_print, _ -> output_mode := mode
  | _, Pretty_print -> assert false
  | Dump_ast   , Dump_ast
  | Dparsetree , Dparsetree -> ()
  | Reconcile a, Reconcile b when Poly.equal a b -> ()
  | x, y ->
    let arg_of_output_mode = function
      | Pretty_print -> assert false
      | Dump_ast                              -> "-dump-ast"
      | Dparsetree                            -> "-dparsetree"
      | Reconcile Using_line_directives       -> "-reconcile"
      | Reconcile Delimiting_generated_blocks -> "-reconcile-with-comments"
      | Null                                  -> "-null"
    in
    raise (Arg.Bad (Printf.sprintf
                      "%s and %s are incompatible"
                      (arg_of_output_mode x) (arg_of_output_mode y)))
;;

let print_transformations () =
  List.iter !Transform.all ~f:(fun (ct : Transform.t) ->
    Printf.printf "%s\n" ct.name);
;;

let parse_apply_list s =
  let names = if String.equal s "" then [] else String.split_on_char s ~sep:',' in
  List.iter names ~f:(fun name ->
    if not (List.exists !Transform.all ~f:(fun (ct : Transform.t) ->
      Transform.has_name ct name)) then
      raise (Caml.Arg.Bad (Printf.sprintf "code transformation '%s' does not exist" name)));
  names

type mask =
  { mutable apply      : string list option
  ; mutable dont_apply : string list option
  }

let mask =
  { apply      = None
  ; dont_apply = None
  }

let handle_apply s =
  if Option.is_some mask.apply then
    raise (Arg.Bad "-apply called too many times");
  (* This is not strictly necessary but it's more intuitive *)
  if Option.is_some mask.dont_apply then
    raise (Arg.Bad "-apply must be called before -dont-apply");
  mask.apply <- Some (parse_apply_list s)

let handle_dont_apply s =
  if Option.is_some mask.dont_apply then
    raise (Arg.Bad "-apply called too many times");
  mask.dont_apply <- Some (parse_apply_list s)

let interpret_mask () =
  if Option.is_some mask.apply || Option.is_some mask.dont_apply then begin
    let selected_transform_name ct =
      let is_candidate =
        match mask.apply with
        | None -> true
        | Some names -> List.exists names ~f:(Transform.has_name ct)
      in
      let is_selected =
        match mask.dont_apply with
        | None -> is_candidate
        | Some names ->
          is_candidate
          && not (List.exists names ~f:(Transform.has_name ct))
      in
      if is_selected then
        Some ct.name
      else
        None
    in
    apply_list := Some (List.filter_map !Transform.all ~f:selected_transform_name)
  end

let shared_args =
  [ "-loc-filename", Arg.String (fun s -> loc_fname := Some s),
    "<string> File name to use in locations"
  ; "-reserve-namespace", Arg.String Name.Reserved_namespaces.reserve,
    "<string> Mark the given namespace as reserved"
  ; "-no-check", Arg.Clear perform_checks,
    " Disable checks (unsafe)"
  ; "-check", Arg.Set perform_checks,
    " Enable checks"
  ; "-no-check-on-extensions", Arg.Clear perform_checks_on_extensions,
    " Disable checks on extension point only"
  ; "-check-on-extensions", Arg.Set perform_checks_on_extensions,
    " Enable checks on extension point only"
  ; "-no-locations-check", Arg.Clear perform_locations_check,
    " Disable locations check only"
  ; "-locations-check", Arg.Set perform_locations_check,
    " Enable locations check only"
  ; "-apply", Arg.String handle_apply,
    "<names> Apply these transformations in order (comma-separated list)"
  ; "-dont-apply", Arg.String handle_dont_apply,
    "<names> Exclude these transformations"
  ; "-no-merge", Arg.Set no_merge,
    " Do not merge context free transformations (better for debugging rewriters)"
  ]

let () =
  List.iter shared_args ~f:(fun (key, spec, doc) -> add_arg key spec ~doc)

let set_cookie s =
  match String.lsplit2 s ~on:'=' with
  | None ->
    raise (Arg.Bad "invalid cookie, must be of the form \"<name>=<expr>\"")
  | Some (name, value) ->
    let lexbuf = Lexing.from_string value in
    lexbuf.Lexing.lex_curr_p <-
      { Lexing.
        pos_fname = "<command-line>"
      ; pos_lnum  = 1
      ; pos_bol   = 0
      ; pos_cnum  = 0
      };
    let expr = Parse.expression lexbuf in
    Migrate_parsetree.Driver.set_global_cookie name
      (module Ppxlib_ast.Selected_ast) expr

let as_pp () =
  set_output_mode Dump_ast;
  embed_errors := true

let standalone_args =
  [ "-as-ppx", Arg.Unit (fun () -> raise (Arg.Bad "-as-ppx must be the first argument")),
    " Run as a -ppx rewriter (must be the first argument)"
  ; "--as-ppx", Arg.Unit (fun () -> raise (Arg.Bad "--as-ppx must be the first argument")),
    " Same as -as-ppx"
  ; "-as-pp", Arg.Unit as_pp,
    " Shorthand for: -dump-ast -embed-errors"
  ; "--as-pp", Arg.Unit as_pp,
    " Same as -as-pp"
  ; "-o", Arg.String (fun s -> output := Some s),
    "<filename> Output file (use '-' for stdout)"
  ; "-", Arg.Unit (fun () -> set_input "-"),
    " Read input from stdin"
  ; "-dump-ast", Arg.Unit (fun () -> set_output_mode Dump_ast),
    " Dump the marshaled ast to the output file instead of pretty-printing it"
  ; "--dump-ast", Arg.Unit (fun () -> set_output_mode Dump_ast),
    " Same as -dump-ast"
  ; "-dparsetree", Arg.Unit (fun () -> set_output_mode Dparsetree),
    " Print the parsetree (same as ocamlc -dparsetree)"
  ; "-embed-errors", Arg.Set embed_errors,
    " Embed errors in the output AST (default: true when -dump-ast, false otherwise)"
  ; "-null", Arg.Unit (fun () -> set_output_mode Null),
    " Produce no output, except for errors"
  ; "-impl", Arg.Unit (fun () -> set_kind Impl),
    "<file> Treat the input as a .ml file"
  ; "--impl", Arg.Unit (fun () -> set_kind Impl),
    "<file> Same as -impl"
  ; "-intf", Arg.Unit (fun () -> set_kind Intf),
    "<file> Treat the input as a .mli file"
  ; "--intf", Arg.Unit (fun () -> set_kind Intf),
    "<file> Same as -intf"
  ; "-debug-attribute-drop", Arg.Set debug_attribute_drop,
    " Debug attribute dropping"
  ; "-print-transformations", Arg.Set request_print_transformations,
    " Print linked-in code transformations, in the order they are applied"
  ; "-print-passes", Arg.Set request_print_passes,
    " Print the actual passes over the whole AST in the order they are applied"
  ; "-ite-check",
    Arg.Unit (fun () ->
      Printf.eprintf "Warning: the -ite-check flag is deprecated \
               and has no effect.\n%!";
      Extra_warnings.care_about_ite_branch := true),
    " (no effect -- kept for compatibility)"
  ; "-pp", Arg.String (fun s -> preprocessor := Some s),
    "<command>  Pipe sources through preprocessor <command> (incompatible with -as-ppx)"
  ; "-reconcile", Arg.Unit (fun () -> set_output_mode (Reconcile Using_line_directives)),
    " (WIP) Pretty print the output using a mix of the input source \
     and the generated code"
  ; "-reconcile-with-comments",
    Arg.Unit (fun () -> set_output_mode (Reconcile Delimiting_generated_blocks)),
    " (WIP) same as -reconcile but uses comments to enclose the generated code"
  ; "-no-color", Arg.Clear use_color,
    " Don't use colors when printing errors"
  ; "-diff-cmd", Arg.String (fun s -> diff_command := Some s),
    " Diff command when using code expectations (use - to disable diffing)"
  ; "-pretty", Arg.Set pretty,
    " Instruct code generators to improve the prettiness of the generated code"
  ; "-styler", Arg.String (fun s -> styler := Some s),
    " Code styler"
  ; "-cookie", Arg.String set_cookie,
    "NAME=EXPR Set the cookie NAME to EXPR"
  ; "--cookie", Arg.String set_cookie,
    " Same as -cookie"
  ; "-output-metadata", Arg.String (fun s -> output_metadata_filename := Some s),
    "FILE Where to store the output metadata"
  ; "-corrected-suffix", Arg.Set_string corrected_suffix,
    "SUFFIX Suffix to happend to corrected files"
  ]
;;

let get_args ?(standalone_args=standalone_args) () =
  let args = standalone_args @ List.rev !args in
  let my_arg_names =
    List.rev_map args ~f:(fun (name, _, _) -> name)
    |> String.Set.of_list
  in
  let omp_args =
    (* Filter out arguments that we override *)
    List.filter (Migrate_parsetree.Driver.registered_args ())
      ~f:(fun (name, _, _) ->
          not (String.Set.mem name my_arg_names))
  in
  args @ omp_args
;;

let standalone_main () =
  let usage =
    Printf.sprintf "%s [extra_args] [<files>]" exe_name
  in
  let args = get_args () in
  Migrate_parsetree.Driver.reset_args ();
  Arg.parse (Arg.align args) set_input usage;
  interpret_mask ();
  if !request_print_transformations then begin
    print_transformations ();
    Caml.exit 0;
  end;
  if !request_print_passes then begin
    print_passes ();
    Caml.exit 0;
  end;
  match !input with
  | None    ->
    Printf.eprintf "%s: no input file given\n%!" exe_name;
    Caml.exit 2
  | Some fn ->
    let kind =
      match !kind with
      | Some k -> k
      | None ->
        match Kind.of_filename fn with
        | Some k -> k
        | None ->
          Printf.eprintf "%s: don't know what to do with '%s', use -impl or -intf.\n"
            exe_name fn;
          Caml.exit 2
    in
    let input_name, relocate =
      match !loc_fname with
      | None    -> fn, false
      | Some fn -> fn, true
    in
    process_file kind fn ~input_name ~relocate ~output_mode:!output_mode ~output:!output
      ~embed_errors:!embed_errors
;;

let standalone_run_as_ppx_rewriter () =
  let n = Array.length Caml.Sys.argv in
  let usage = Printf.sprintf "%s -as-ppx [extra_args] <infile> <outfile>" exe_name in
  if n < 4 then begin
    Printf.eprintf "Usage: %s\n%!" usage;
    Caml.exit 2
  end;
  let argv = Array.make (n - 3) "" in
  argv.(0) <- Caml.Sys.argv.(0);
  for i = 1 to (n - 4) do
    argv.(i) <- Caml.Sys.argv.(i + 1)
  done;
  let standalone_args =
    List.map standalone_args ~f:(fun (arg, spec, _doc) ->
      (arg, spec, " Unused with -as-ppx"))
  in
  let args = get_args ~standalone_args () in
  Migrate_parsetree.Driver.reset_args ();
  match
    Arg.parse_argv argv (Arg.align args)
      (fun _ -> raise (Arg.Bad "anonymous arguments not accepted"))
      usage
  with
  | exception Arg.Bad  msg -> Printf.eprintf "%s" msg; Caml.exit 2
  | exception Arg.Help msg -> Printf.eprintf "%s" msg; Caml.exit 0
  | () ->
    interpret_mask ();
    Ocaml_common.Ast_mapper.apply
      ~source:Caml.Sys.argv.(n - 2) ~target:Caml.Sys.argv.(n - 1) mapper
;;

let standalone () =
  Compiler_specifics.read_clflags_from_env ();
  try
    if Array.length Caml.Sys.argv >= 2 &&
       match Caml.Sys.argv.(1) with
       | "-as-ppx" | "--as-ppx" -> true
       | _ -> false
    then
      standalone_run_as_ppx_rewriter ()
    else
      standalone_main ();
    Caml.exit 0
  with exn ->
    Location.report_exception Caml.Format.err_formatter exn;
    Caml.exit 1
;;

let pretty () = !pretty

let () =
  Migrate_parsetree.Driver.register
    ~name:"ppxlib_driver"
    (* This doesn't take arguments registered by rewriters. It's not worth supporting
       them, since [--cookie] is a much better replacement for passing parameters to
       individual rewriters. *)
    ~args:shared_args
    (module Ppxlib_ast.Selected_ast)
    (fun config cookies ->
       let module A = Ppxlib_ast.Selected_ast.Ast.Ast_mapper in
       let structure _ st = real_map_structure config cookies st in
       let signature _ sg = real_map_signature config cookies sg in
       { A.default_mapper with structure; signature })

let enable_checks () =
  (* We do not enable the locations check here, we currently require that one
     to be specifically enabled. *)
  perform_checks := true;
  perform_checks_on_extensions := true

let enable_location_check () =
  perform_locations_check := true

let disable_location_check () =
  perform_locations_check := false

let map_structure st =
  map_structure st
  |> Migrate_parsetree.Driver.migrate_some_structure
       (module Ppxlib_ast.Selected_ast)