File: run_ocamlcocci.ml

package info (click to toggle)
coccinelle 1.0.8.deb-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 26,148 kB
  • sloc: ml: 136,392; ansic: 23,594; sh: 2,189; makefile: 2,157; perl: 1,576; lisp: 840; python: 823; awk: 70; csh: 12
file content (120 lines) | stat: -rw-r--r-- 4,288 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
open Common

let string_value = function
    Ast_c.MetaPosValList l ->
      let locs =
	List.map
	  (function
	      (fname,current_element,current_element_pos,
	       (line,col),(line_end,col_end)) ->
		 let (current_element_line,current_element_col,
		      current_element_line_end,current_element_col_end) =
		   match current_element_pos with
		     Some
		       ((current_element_line,current_element_col),
			(current_element_line_end,current_element_col_end)) ->
			  (current_element_line,current_element_col,
			   current_element_line_end,current_element_col_end)
		   | None -> (-1,-1,-1,-1) in
		{ Coccilib.current_element = current_element;
		  Coccilib.current_element_line = current_element_line;
		  Coccilib.current_element_col = current_element_col;
		  Coccilib.current_element_line_end = current_element_line_end;
		  Coccilib.current_element_col_end = current_element_col_end;
		  Coccilib.file = fname;
		  Coccilib.line = line;
		  Coccilib.col = col;
		  Coccilib.line_end = line_end;
		  Coccilib.col_end = col_end }) l in
      Coccilib.Pos locs
  | Ast_c.MetaComValList l ->
      Coccilib.Com
	(List.map
	   (function (bef,mid,aft) ->
	     let com_strings l =
	       List.rev
		 (List.fold_left
		    (fun prev cur ->
		      match cur with
			(Token_c.TComment,_) -> (Token_c.str_of_token cur) :: prev
		      | (Token_c.TCommentCpp _,_) -> (Token_c.str_of_token cur) :: prev
		      | _ -> prev)
		    [] l) in
	     (com_strings bef,com_strings mid,com_strings aft))
	   l)
  | Ast_c.MetaListlenVal n -> Coccilib.Int n
  | v -> Coccilib.Str (Ocamlcocci_aux.stringrep v)

let string_binding vl = function
    None -> []
  | Some _ -> [string_value vl]

let ast_binding vl = function
    None -> []
  | Some _ ->
      [match vl with
	Ast_c.MetaIdVal id | Ast_c.MetaFuncVal id
      | Ast_c.MetaLocalFuncVal id ->
	  Coccilib.Str id
      | Ast_c.MetaAssignOpVal op -> Coccilib.AssignOp op
      | Ast_c.MetaBinaryOpVal op -> Coccilib.BinaryOp op
      | Ast_c.MetaExprVal(expr,_,_) -> Coccilib.Expr expr
      | Ast_c.MetaExprListVal arglist -> Coccilib.ExprList arglist
      | Ast_c.MetaParamVal param -> Coccilib.Param param
      | Ast_c.MetaParamListVal paramlist -> Coccilib.ParamList paramlist
      | Ast_c.MetaDParamListVal paramlist -> Coccilib.DParamList paramlist

      | Ast_c.MetaTypeVal ty -> Coccilib.Type ty
      | Ast_c.MetaInitVal init -> Coccilib.Init init
      | Ast_c.MetaInitListVal init -> Coccilib.InitList init
      | Ast_c.MetaDeclVal(decl,_) -> Coccilib.Decl decl
      | Ast_c.MetaFieldVal field -> Coccilib.Field field
      | Ast_c.MetaFieldListVal field -> Coccilib.FieldList field
      | Ast_c.MetaStmtVal(stm,_,_) -> Coccilib.Stmt stm
      | Ast_c.MetaStmtListVal(stm,_) -> Coccilib.StmtList stm
      | Ast_c.MetaFragListVal frags -> Coccilib.FragList frags
      | Ast_c.MetaFmtVal fmt -> Coccilib.Fmt fmt
      | Ast_c.MetaNoVal -> failwith "no value for script metavariable"

      | Ast_c.MetaPosVal _ | Ast_c.MetaPosValList _ | Ast_c.MetaComValList _
      | Ast_c.MetaListlenVal _ ->
	  failwith "not associated with a declared metavariable"]

let run mv ve script_vars name code =
  (* set up variables *)
  let find_binding (r,m) =
    try
      let elem =
	List.find (function ((re,rm),_) -> r = re && m = rm) ve in
      Some elem
    with Not_found -> None in

  let args =
    List.concat
      (List.map
	 (function ((str_name,ast_name),(r,m),_,init) ->
	   match find_binding (r,m) with
	     None ->
	       (match init with
		 Ast_cocci.NoMVInit -> failwith "no value for ocaml metavars"
	       | Ast_cocci.MVInitString s -> [Coccilib.Str s]
	       | Ast_cocci.MVInitPosList -> [Coccilib.Pos []])
	   | Some (_,vl) ->
	       (string_binding vl str_name) @ (ast_binding vl ast_name))
	 mv) in

  let script_args = List.map (function _ -> ref Ast_c.MetaNoVal) script_vars in

  (* call the function *)
  Coccilib.include_match true;
  Coccilib.exited := false;
  let fn =
    try Hashtbl.find Coccilib.fcts name
    with Not_found -> failwith (Printf.sprintf "%s not found" name) in
  fn args script_args;
  List.map (function x -> !x) script_args

let run_constraint ocamlname args =
  let args = List.map string_value args in
  let fn = Hashtbl.find Coccilib.bool_fcts ocamlname in
  fn args