Skip to content
This repository
Browse code

sgrep: support printing multi metavariables with -pvar

Summary: Matthieu wanted that.

Test Plan:
$ ~/pfff/sgrep -e 'bar(X,MANYARGS)' -pvar X,MANYARGS *
many_args.php:4: 'a_string':$x,$y

Reviewers: matthieu, pieter, julienv

Reviewed By: pieter

CC: srenfro

Differential Revision: https://phabricator.fb.com/D586880

Task ID: 1766646
  • Loading branch information...
commit 69ed7457024465e8ae86c1be0ee386ed07a61c51 1 parent 4a1a082
Yoann Padioleau authored
1  lang_php/parsing/lib_parsing_php.mli
@@ -32,6 +32,7 @@ type match_format =
32 32
   | OneLine
33 33
 
34 34
 val print_match: ?format:match_format -> Ast_php.tok list -> unit
  35
+val join_with_space_if_needed: string list -> string
35 36
 
36 37
 (*x: lib_parsing_php.mli *)
37 38
 val get_funcalls_any         : Ast_php.any -> string list
34  main_sgrep.ml
@@ -63,17 +63,27 @@ let print_match mvars mvar_binding tokens_matched_code =
63 63
   (match mvars with
64 64
   | [] ->
65 65
       Lib_parsing_php.print_match ~format:!match_format tokens_matched_code
66  
-
67  
-  | [x] ->
68  
-      (match Common.assoc_option x mvar_binding with
69  
-      | Some any ->
70  
-          let ii = Lib_parsing_php.ii_of_any any in
71  
-          Lib_parsing_php.print_match  ~format:Lib_parsing_php.OneLine ii
72  
-      | None ->
73  
-          failwith (spf "the metavariable '%s' was not binded" x)
74  
-      )
75  
-  | x::y::xs ->
76  
-      failwith "multiple metavariables not yet handled. mailto:pad@facebook.com"
  66
+  | xs ->
  67
+      (* similar to the code of Lib_parsing_php.print_match, maybe could
  68
+       * factorize code a bit.
  69
+       *)
  70
+      let (mini, maxi) = 
  71
+        Lib_parsing_php.min_max_ii_by_pos tokens_matched_code in
  72
+      let (file, line) = 
  73
+        Ast.file_of_info mini, Ast.line_of_info mini in
  74
+
  75
+      let strings_metavars =
  76
+        xs +> List.map (fun x ->
  77
+          match Common.assoc_option x mvar_binding with
  78
+          | Some any ->
  79
+              Lib_parsing_php.ii_of_any any
  80
+              +> List.map Ast.str_of_info 
  81
+              +> Lib_parsing_php.join_with_space_if_needed
  82
+          | None ->
  83
+              failwith (spf "the metavariable '%s' was not binded" x)
  84
+          )
  85
+      in
  86
+      pr (spf "%s:%d: %s" file line (Common.join ":" strings_metavars));
77 87
   );
78 88
   tokens_matched_code +> List.iter (fun x -> Common.push2 x _matching_tokens)
79 89
 
@@ -204,7 +214,7 @@ let options () =
204 214
     " print matches on one line, in normalized form";
205 215
 
206 216
     "-pvar", Arg.String (fun s -> mvars := Common.split "," s),
207  
-    " <metavar> print the metavariable, not the matched code";
  217
+    " <metavars> print the metavariables, not the matched code";
208 218
 
209 219
     "-gen_layer", Arg.String (fun s -> layer_file := Some s),
210 220
     " <file> save result in a pfff layer file";
2  tests/php/sgrep/many_args.php
... ...
@@ -1,5 +1,5 @@
1 1
 <?php
2 2
 
3 3
 function foo() {
4  
-  bar(1, 2, 3);
  4
+  bar('a_string', $x, $y);
5 5
 }

0 notes on commit 69ed745

Please sign in to comment.
Something went wrong with that request. Please try again.