forked from facebookarchive/pfff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_spatch.ml
630 lines (523 loc) · 18.8 KB
/
main_spatch.ml
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
(*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*)
open Common
open Ast_php
module Ast = Ast_php
module V = Visitor_php
module Ast2 = Ast_js
module V2 = Visitor_js
module PI = Parse_info
open Parse_info
(*****************************************************************************)
(* Purpose *)
(*****************************************************************************)
(*
* A syntactical patch for PHP.
*
* opti: git grep xxx | xargs spatch_php ...
*
*
* Alternatives:
* - you could also just write a sgrep that put a special mark to the
* place where it matched and then do the transformation using an
* emacs macro leveraging those marks.
*)
(*****************************************************************************)
(* Flags *)
(*****************************************************************************)
let verbose = ref true
let apply_patch = ref false
let spatch_file = ref ""
(* action mode *)
let action = ref ""
(*****************************************************************************)
(* Wrappers *)
(*****************************************************************************)
let pr2 s =
if !verbose then Common.pr2 s
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
(* Some helper functions when using the low-level transformation API of pfff.
* You should use the spatch DSL if you can.
*)
type transformation = {
(* Works by side effect on the tokens in the AST, see the transfo field.
* It returns a boolean indicating whether any transformation was done.
*)
trans_func: Ast_php.program -> bool;
(* optimisation; if a file does not contain certain keywords we don't
* even have to parse it
*)
grep_keywords: string list option;
}
let apply_transfo transfo xs =
let files = Lib_parsing_php.find_php_files_of_dir_or_files xs in
let pbs = ref [] in
(* xhp and transformation was not mixing well, but now it's better
* thanks to builtin xhp support
*)
Flag_parsing_php.show_parsing_error := false;
Flag_parsing_php.verbose_lexing := false;
let nbfiles = List.length files in
Common.execute_and_show_progress ~show_progress:true nbfiles (fun k ->
files +> List.iter (fun file ->
let file = Common.relative_to_absolute file in
pr2 (spf "processing: %s" file);
k();
let worth_trying =
match transfo.grep_keywords with
| None -> true
| Some xs -> Common.contain_any_token_with_egrep xs file
in
if not worth_trying then ()
else
try (
let (ast2, _stat) = Parse_php.parse file in
let ast = Parse_php.program_of_program2 ast2 in
Lib_parsing_php.print_warning_if_not_correctly_parsed ast file;
let was_modified = transfo.trans_func ast in
(* old:
* let patch = Patch.generate_patch !edition_cmds
* ~filename_in_project:file file in
* patch |> List.iter pr
*)
if was_modified then begin
let s = Unparse_php.string_of_program2_using_tokens ast2 in
let tmpfile = Common.new_temp_file "trans" ".php" in
Common.write_file ~file:tmpfile s;
let diff = Common.cmd_to_list (spf "diff -u %s %s" file tmpfile) in
diff |> List.iter pr;
if !apply_patch
then Common.write_file ~file:file s;
end
) with exn ->
Common.push2 (spf "PB with %s, exn = %s" file (Common.exn_to_s exn)) pbs;
));
!pbs +> List.iter Common.pr2
(*****************************************************************************)
(* Main action *)
(*****************************************************************************)
(*
./spatch -c tests/php/spatch/foo.spatch tests/php/spatch/foo.php
*)
(* just to test the backend part of spatch *)
let (dumb_spatch_pattern: Ast_php.expr) =
(* ./pfff -dump_php_ml tests/php/spatch/1.php *)
let i_1 = {
PI.token =
PI.OriginTok(
{ PI.str = "1"; charpos = 6; line = 2; column = 0;
file = "tests/php/spatch/1.php";
});
(* the spatch is to replace every 1 by 42 *)
PI.transfo = PI.Replace (PI.AddStr "42");
PI.comments = ();
}
in
let t_1 = Ast.noType () in
(Sc(C(Int(("1", i_1)))), t_1)
(*
* Here is an example of a spatch file:
*
* foo(2,
* - bar(2)
* + foobar(4)
* )
*
* This will replace all calls to bar(2) by foobar(4) when
* the function call is the second argument of a call to
* foo where its first argument is 2.
*
* Algorithm to parse a spatch file:
* - take lines of the file, index the lines
* - replace the + lines by an empty line and remember in a line_env
* the line and its index
* - remove the - in the first column and remember in a line_env
* that is was a minus line
* - unlines the filtered lines into a new string
* - call the PHP expr parser on this new string
* - go through all tokens and adjust its transfo field using the
* information in line_env
*)
type line_kind =
| Context
| Plus of string
| Minus
let parse_spatch file =
let xs = Common.cat file +> Common.index_list_1 in
let hline_env = Hashtbl.create 11 in
let ys = xs +> List.map (fun (s, lineno) ->
match s with
(* ugly: for now I strip the space after the + because.
* at some point we need to parse this stuff and
* add the correct amount of indentation when it's processing
* a token.
*)
| _ when s =~ "^\\+[ \t]*\\(.*\\)" ->
let rest_line = Common.matched1 s in
Hashtbl.add hline_env lineno (Plus rest_line);
""
| _ when s =~ "^\\-\\(.*\\)" ->
let rest_line = Common.matched1 s in
Hashtbl.add hline_env lineno Minus;
rest_line
| _ ->
Hashtbl.add hline_env lineno Context;
s
)
in
let spatch_without_patch_annot = Common.unlines ys in
(* pr2 spatch_without_patch_annot; *)
let pattern =
Common.save_excursion Flag_parsing_php.sgrep_mode true (fun () ->
(* ugly *)
if spatch_without_patch_annot =~ "^[ \t]*<"
then Parse_php.xhp_expr_of_string spatch_without_patch_annot
else Parse_php.expr_of_string spatch_without_patch_annot
)
in
(* need adjust the tokens in it now *)
let toks = Lib_parsing_php.ii_of_any (Expr pattern) in
(* adjust with Minus info *)
toks +> List.iter (fun tok ->
let line = Ast.line_of_info tok in
(* ugly: right now expr_of_string introduce an extra <?php at
* the beginning which shifts line number by 1 so have
* to compensate back here.
*)
let line = line - 1 in
let annot = Hashtbl.find hline_env line in
(match annot with
| Context -> ()
| Minus -> tok.PI.transfo <- PI.Remove;
| Plus _ ->
(* normally impossible since we removed the elements in the
* plus line, except the newline. should assert it's only newline
*)
()
);
);
(* adjust with the Plus info. We need to annotate the last token
* on the preceding line or next line.
* e.g. on
* foo(2,
* + 42,
* 3)
* we could either put the + on the ',' of the first line (as an AddAfter)
* or on the + on the '3' of the thirdt line (as an AddBefore).
* The preceding and next line could also be a minus line itself.
* Also it could be possible to have multiple + line in which
* case we want to concatenate them together.
*
* TODO: for now I just associate it with the previous line ...
*)
let grouped_by_lines =
toks +> Common.group_by_mapped_key (fun tok -> Ast.line_of_info tok) in
let rec aux xs =
match xs with
| (line, toks_at_line)::rest ->
(* ugly *)
let line = line - 1 in
(* if the next line was a +, then associate with the last token
* on this line
*)
(match Common.hfind_option (line+1) hline_env with
| None ->
(* probably because was last line *)
()
| Some (Plus toadd) ->
(* todo? what if there is no token on this line ? *)
let last_tok = Common.last toks_at_line in
(match last_tok.transfo with
| Remove -> last_tok.transfo <- Replace (AddStr toadd)
| NoTransfo -> last_tok.transfo <- AddAfter (AddStr toadd)
| _ -> raise Impossible
)
| Some _ -> ()
);
aux rest
| [] -> ()
in
aux grouped_by_lines;
(* both the ast (here pattern) and the tokens share the same
* reference so by modifying the tokens we actually also modifed
* the AST.
*)
pattern
let spatch pattern file =
let was_modifed = ref false in
(* quite similar to what we do in main_sgrep.ml *)
let (ast2, _stat) = Parse_php.parse file in
let ast = Parse_php.program_of_program2 ast2 in
Lib_parsing_php.print_warning_if_not_correctly_parsed ast file;
let visitor = V.mk_visitor { V.default_visitor with
(* for now handle only expression patching *)
V.kexpr = (fun (k, _) x ->
let matches_with_env =
Matching_php.match_e_e pattern x
in
if matches_with_env = []
then k x
else begin
was_modifed := true;
Transforming_php.transform_e_e pattern x
(* TODO, maybe could get multiple matching env *)
(List.hd matches_with_env)
end
);
}
in
visitor (Program ast);
if !was_modifed
then Some (Unparse_php.string_of_program2_using_tokens ast2)
else None
let main_action xs =
if Common.null_string !spatch_file
then failwith "I need a semantic patch file; use -c";
let spatch_file = !spatch_file in
(* old: let pattern = dumb_spatch_pattern in *)
let pattern = parse_spatch spatch_file in
let files = Lib_parsing_php.find_php_files_of_dir_or_files xs in
files +> Common.index_list_and_total +> List.iter (fun (file, i, total) ->
pr2 (spf "processing: %s (%d/%d)" file i total);
let resopt = spatch pattern file in
resopt +> Common.do_option (fun (s) ->
let tmpfile = Common.new_temp_file "trans" ".php" in
Common.write_file ~file:tmpfile s;
let diff = Common.cmd_to_list (spf "diff -u %s %s" file tmpfile) in
diff |> List.iter pr;
if !apply_patch
then Common.write_file ~file:file s;
)
)
(*****************************************************************************)
(* Extra actions *)
(*****************************************************************************)
(* -------------------------------------------------------------------------*)
(* to test *)
(* -------------------------------------------------------------------------*)
(* see also demos/simple_refactoring.ml *)
let simple_transfo xs =
let files = Lib_parsing_php.find_php_files_of_dir_or_files xs in
Flag_parsing_php.show_parsing_error := false;
Flag_parsing_php.verbose_lexing := false;
files +> List.iter (fun file ->
pr2 (spf "processing: %s" file);
let (ast2, _stat) = Parse_php.parse file in
let ast = Parse_php.program_of_program2 ast2 in
let hook = { V.default_visitor with
V.klvalue = (fun (k, _) x ->
match Ast.untype x with
| FunCallSimple((Name ("foo", info_foo)), (lp, args, rp)) ->
pr2 "found match";
let ii = Lib_parsing_php.ii_of_any (Lvalue x) in
ii |> List.iter (fun info ->
info.transfo <- Remove
);
info_foo.transfo <- Replace (AddStr "1");
()
| _ -> k x
);
}
in
(V.mk_visitor hook) (Program ast);
let s = Unparse_php.string_of_program2_using_tokens ast2 in
let tmpfile = Common.new_temp_file "trans" ".php" in
Common.write_file ~file:tmpfile s;
let diff = Common.cmd_to_list (spf "diff -u %s %s" file tmpfile) in
diff |> List.iter pr;
);
()
(* -------------------------------------------------------------------------*)
(* An example of an XHP transformation *)
(* -------------------------------------------------------------------------*)
(*
* The transformation below can be encoded via this syntactical patch:
*
* <ui:section-header
* - border=X
* ></ui:section-header>
*
* There are nevertheless currenty a few limitations in spatch which
* will cause this syntactical patch to fail to transform all the
* relevant code (e.g. because of some XHP isomorphisms not yet
* handled by spatch). In the mean time the code below uses the low-level
* transformation API of pfff to express the same transformation;
* it can be used as a reference for spatch to compare with.
*)
let remove_border_attribute ast =
let was_modified = ref false in
(* $ ./pfff -dump_php_ml tests/php/spatch/border_var.php
*
* [StmtList(
* [ExprStmt(
* (Assign((Var(DName(("a", i_1)), Ref(NoScope)), tlval_2), i_3,
* (XhpHtml(
* Xhp((["ui"; "section-header"], i_4),
* [(("href", i_5), i_6,
* XhpAttrString(i_7, [EncapsString(("foo", i_8))], i_9));
* (("border", i_10), i_11,
* XhpAttrString(i_12, [EncapsString(("1", i_13))], i_14))],
* i_15, [XhpText(("
* This is nuts
* ", i_16))],
* (Some(["ui"; "section-header"]), i_17))),
* t_18)),
* t_19), i_20)]); FinalDef(i_21)]
*)
let visitor = V.mk_visitor { V.default_visitor with
V.kxhp_html = (fun (k, _) x ->
match x with
| Xhp ( (["ui"; "section-header"], _), attributes, _, _, _)
| XhpSingleton ( (["ui"; "section-header"], _), attributes, _) ->
attributes +> List.iter (fun attr ->
match attr with
| (("border", _tok_border), _tok_equal, _xhp_attr_value) ->
was_modified := true;
let tokens = Lib_parsing_php.ii_of_any (XhpAttribute attr) in
tokens +> List.iter (fun tok ->
tok.transfo <- Remove;
);
| _ -> ()
);
(* recurse *)
k x
(* recurse *)
| _ -> k x
);
}
in
visitor (Program ast);
!was_modified
let remove_border_attribute_transfo = {
trans_func = remove_border_attribute;
grep_keywords = Some ["border"];
}
(*---------------------------------------------------------------------------*)
(* regression testing *)
(*---------------------------------------------------------------------------*)
let unittest_spatch () =
let testdir = Filename.concat Config.path "tests/php/spatch/" in
let expfiles = Common.glob (testdir ^ "*.exp") in
expfiles +> List.iter (fun expfile ->
(* todo: this regexp should just be .*? but ocaml regexp do not
* have the greedy feature :( Also note that expfile is a fullpath
* so it can contains /, hence this ugly regexp
*)
if expfile =~ "\\([a-zA-Z_/]+\\)\\([0-9]*\\)\\.exp$" then begin
let (prefix, variant) = Common.matched2 expfile in
let spatchfile = prefix ^ ".spatch" in
let phpfile = prefix ^ variant ^ ".php" in
pr2 (spf "Testing %s on %s expecting %s"
(Filename.basename spatchfile)
(Filename.basename phpfile)
(Filename.basename expfile));
let pattern = parse_spatch spatchfile in
let resopt = spatch pattern phpfile in
let file_res =
match resopt with
| None -> phpfile
| Some s ->
let tmpfile = Common.new_temp_file "spatch_test" ".php" in
Common.write_file ~file:tmpfile s;
tmpfile
in
let diff = Common.cmd_to_list (spf "diff -u %s %s" file_res expfile) in
diff |> List.iter pr;
if List.length diff > 1
then failwith (spf "PB with %s" expfile);
end
else failwith ("wrong format for expfile: " ^ expfile)
)
(*---------------------------------------------------------------------------*)
(* the command line flags *)
(*---------------------------------------------------------------------------*)
let spatch_extra_actions () = [
(* see also demos/simple_refactoring.ml *)
"-simple_transfo", " <files_or_dirs>",
Common.mk_action_n_arg (simple_transfo);
"-remove_border_attribute", " <files_or_dirs>",
Common.mk_action_n_arg (apply_transfo remove_border_attribute_transfo);
"-test", "",
Common.mk_action_0_arg unittest_spatch;
]
(*****************************************************************************)
(* The options *)
(*****************************************************************************)
let all_actions () =
spatch_extra_actions()++
[]
let options () =
[
"-c", Arg.Set_string spatch_file,
" <spatch_file>";
"-apply_patch", Arg.Set apply_patch,
" ";
"-verbose", Arg.Set verbose,
" ";
] ++
(* Flag_parsing_php.cmdline_flags_pp () ++ *)
Common.options_of_actions action (all_actions()) ++
Common.cmdline_flags_devel () ++
Common.cmdline_flags_verbose () ++
Common.cmdline_flags_other () ++
[
"-version", Arg.Unit (fun () ->
Common.pr2 (spf "spatch_php version: %s" Config.version);
exit 0;
),
" guess what";
(* this can not be factorized in Common *)
"-date", Arg.Unit (fun () ->
Common.pr2 "version: $Date: 2010/04/25 00:44:57 $";
raise (Common.UnixExit 0)
),
" guess what";
] ++
[]
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let main () =
Common_extra.set_link ();
let usage_msg =
"Usage: " ^ Common.basename Sys.argv.(0) ^
" [options] <file or dir> " ^ "\n" ^ "Options are:"
in
(* does side effect on many global flags *)
let args = Common.parse_options (options()) usage_msg Sys.argv in
(* must be done after Arg.parse, because Common.profile is set by it *)
Common.profile_code "Main total" (fun () ->
(match args with
(* --------------------------------------------------------- *)
(* actions, useful to debug subpart *)
(* --------------------------------------------------------- *)
| xs when List.mem !action (Common.action_list (all_actions())) ->
Common.do_action !action xs (all_actions())
| _ when not (Common.null_string !action) ->
failwith ("unrecognized action or wrong params: " ^ !action)
(* --------------------------------------------------------- *)
(* main entry *)
(* --------------------------------------------------------- *)
| x::xs ->
main_action (x::xs)
(* --------------------------------------------------------- *)
(* empty entry *)
(* --------------------------------------------------------- *)
| [] ->
Common.usage usage_msg (options());
failwith "too few arguments"
)
)
(*****************************************************************************)
let _ =
Common.main_boilerplate (fun () ->
main ();
)