package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file sgrep_generic.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
(* Yoann Padioleau
 *
 * Copyright (C) 2019 r2c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library 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 file
 * license.txt for more details.
 *)
open Common

open Ast_generic
module Ast = Ast_generic
module V = Visitor_ast

module GG = Generic_vs_generic

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* See https://github.com/facebook/pfff/wiki/Sgrep 
 *
 *)

(*****************************************************************************)
(* Type *)
(*****************************************************************************)

(* right now only Expr and Stmt are supported *)
type pattern = Ast.any

type ('a, 'b) matcher = 'a -> 'b ->
  Metavars_generic.metavars_binding list

 
let match_e_e pattern e = 
  let env = GG.empty_environment () in
  GG.m_expr pattern e env

let match_st_st pattern e = 
  let env = GG.empty_environment () in
  GG.m_stmt pattern e env

(* for unit testing *)
let match_any_any pattern e = 
  let env = GG.empty_environment () in
  GG.m_any pattern e env

(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)

let sgrep_ast ~hook pattern ast =
  let hook =
    match pattern with
    (* depending on the pattern, we visit every relevant nodes
     * and try the pattern on it.
     *)

    | E pattern_expr ->
        { V.default_visitor with
          V.kexpr = (fun (k, _) x ->
            let matches_with_env = match_e_e pattern_expr  x in
            if matches_with_env = []
            then k x
            else begin
              (* could also recurse to find nested matching inside
               * the matched code itself.
               *)
              let matched_tokens = Lib_ast.ii_of_any (E x) in
              matches_with_env |> List.iter (fun env ->
                hook env matched_tokens
              )
            end
          );
        }

    | S pattern ->
        { V.default_visitor with
          V.kstmt = (fun (k, _) x ->
            let matches_with_env = match_st_st pattern x in
            if matches_with_env = []
            then k x
            else begin
              (* could also recurse to find nested matching inside
               * the matched code itself.
               *)
              let matched_tokens = Lib_ast.ii_of_any (S x) in
              matches_with_env |> List.iter (fun env ->
                hook env matched_tokens
              )
            end
          );
        }

    | _ -> failwith (spf "pattern not yet supported:" )
  in
  (* TODO? opti: dont analyze certain ASTs if it does not contain
   * certain constants that interect with the pattern?
   * But this requires to analyze the pattern to extract those
   * constants (name of function, field, etc.)
   *)

  (V.mk_visitor hook) (Pr ast)
OCaml

Innovation. Community. Security.