2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 #
0 "./yes_pycocci.ml"
29 * Copyright 2012, INRIA
30 * Julia Lawall, Gilles Muller
31 * Copyright 2010-2011, INRIA, University of Copenhagen
32 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
33 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
34 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
35 * This file is part of Coccinelle.
37 * Coccinelle is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, according to version 2 of the License.
41 * Coccinelle is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
54 #
0 "./yes_pycocci.ml"
59 module StringMap
= Map.Make
(String
)
61 exception Pycocciexception
63 let python_support = true
65 (* ------------------------------------------------------------------- *)
66 (* The following definitions are from
67 http://patches.ubuntu.com/by-release/extracted/debian/c/coccinelle/0.1.5dbs-2/01-system-pycaml
68 as well as _pycocci_setargs *)
70 let _pycocci_none () =
71 let builtins = pyeval_getbuiltins
() in
72 pyobject_getitem
(builtins, pystring_fromstring
"None")
74 let _pycocci_true () =
75 let builtins = pyeval_getbuiltins
() in
76 pyobject_getitem
(builtins, pystring_fromstring
"True")
78 let _pycocci_false () =
79 let builtins = pyeval_getbuiltins
() in
80 pyobject_getitem
(builtins, pystring_fromstring
"False")
82 let _pycocci_tuple6 (a
,b
,c
,d
,e
,f
) =
83 pytuple_fromarray
([|a
; b
; c
; d
; e
; f
|])
85 (* ------------------------------------------------------------------- *)
87 let check_return_value msg v
=
88 if v
=*= (pynull
()) then
90 Common.pr2
("while " ^ msg ^
":");
91 raise Pycocciexception
)
93 let check_int_return_value msg v
=
96 Common.pr2
("while " ^ msg ^
":");
97 raise Pycocciexception
)
100 let initialised = ref false
102 let coccinelle_module = ref (_pycocci_none ())
103 let cocci_file_name = ref ""
105 (* dealing with python modules loaded *)
106 let module_map = ref (StringMap.add
"__main__" (_pycocci_none ()) StringMap.empty
)
108 let get_module module_name
=
109 StringMap.find module_name
(!module_map)
111 let is_module_loaded module_name
=
113 let _ = get_module module_name
in
115 with Not_found
-> false
117 let load_module module_name
=
118 if not
(is_module_loaded module_name
) then
119 (* let _ = Sys.command("python3 -c 'import " ^ module_name ^ "'") in *)
120 let m = pyimport_importmodule module_name
in
121 check_return_value ("importing module " ^ module_name
) m;
122 (module_map := (StringMap.add module_name
m (!module_map));
124 else get_module module_name
125 (* end python module handling part *)
127 (* python interaction *)
129 let last_period = String.rindex fqn '
.'
in
130 let module_name = String.sub fqn
0 last_period in
131 let class_name = String.sub fqn
(last_period + 1) (String.length fqn
- last_period - 1) in
132 (module_name, class_name)
134 let pycocci_get_class_type fqn
=
135 let (module_name, class_name) = split_fqn fqn
in
136 let m = get_module module_name in
137 let attr = pyobject_getattrstring
(m, class_name) in
138 check_return_value "obtaining a python class type" attr;
141 let pycocci_instantiate_class fqn args
=
142 let class_type = pycocci_get_class_type fqn
in
144 pyeval_callobjectwithkeywords
(class_type, args
, pynull
()) in
145 check_return_value "instantiating a python class" obj;
148 (* end python interaction *)
150 let inc_match = ref true
151 let exited = ref false
153 let include_match v
=
154 let truth = pyobject_istrue
(pytuple_getitem
(v
, 1)) in
155 check_int_return_value "testing include_match" truth;
156 inc_match := truth != 0;
163 let build_method (mname
, camlfunc
, args
) pymodule classx classdict
=
164 let cmx = pymethod_new
(pywrap_closure camlfunc
, args
, classx
) in
165 let v = pydict_setitemstring
(classdict
, mname
, cmx) in
166 check_int_return_value ("building python method " ^ mname
) v;
169 let build_class cname parent methods pymodule
=
170 let cd = pydict_new
() in
171 check_return_value "creating a new python dictionary" cd;
172 let cx = pyclass_new
(pytuple_fromsingle
(pycocci_get_class_type parent
), cd,
173 pystring_fromstring cname
) in
174 check_return_value "creating a new python class" cx;
175 List.iter
(function meth
-> build_method meth pymodule
cx cd) methods
;
176 let v = pydict_setitemstring
(pymodule_getdict pymodule
, cname
, cx) in
177 check_int_return_value ("adding python class " ^ cname
) v;
180 let the_environment = ref []
182 let has_environment_binding name
=
183 let a = pytuple_toarray name
in
184 let (rule
, name
) = (Array.get
a 1, Array.get
a 2) in
185 let orule = pystring_asstring rule
in
186 let oname = pystring_asstring name
in
187 let e = List.exists
(function (x
,y
) -> orule =*= x
&& oname =$
= y
)
189 if e then _pycocci_true () else _pycocci_false ()
191 let pyoutputinstance = ref (_pycocci_none ())
192 let pyoutputdict = ref (_pycocci_none ())
194 let get_cocci_file args
=
195 pystring_fromstring
(!cocci_file_name)
197 (* initialisation routines *)
198 let _pycocci_setargs argv0
=
200 pysequence_list
(pytuple_fromsingle
(pystring_fromstring argv0
)) in
201 let sys_mod = load_module "sys" in
202 pyobject_setattrstring
(sys_mod, "argv", argv)
204 let pycocci_init () =
206 if not
!initialised then (
208 (* use python_path_base as default (overridable) dir for coccilib *)
209 let python_path_base = Printf.sprintf
"%s/coccinelle" (Unix.getenv
"HOME") in
210 let python_path = try Unix.getenv
"PYTHONPATH" ^
":" ^
python_path_base
211 with Not_found
-> python_path_base in
212 Unix.putenv
"PYTHONPATH" python_path;
213 let _ = if not
(py_isinitialized
() != 0) then
214 (if !Flag.show_misc
then Common.pr2
"Initializing python\n%!";
218 let argv0 = Sys.executable_name
in
219 let _ = _pycocci_setargs argv0 in
221 coccinelle_module := (pymodule_new
"coccinelle");
222 module_map := StringMap.add
"coccinelle" !coccinelle_module !module_map;
223 let _ = load_module "coccilib.elems" in
224 let _ = load_module "coccilib.output" in
226 let module_dictionary = pyimport_getmoduledict
() in
227 coccinelle_module := pymodule_new
"coccinelle";
228 let mx = !coccinelle_module in
229 let (cd, cx) = build_class "Cocci" (!Flag.pyoutput
)
230 [("exit", sp_exit, (pynull
()));
231 ("include_match", include_match, (pynull
()));
232 ("has_env_binding", has_environment_binding, (pynull
()))] mx in
233 pyoutputinstance := cx;
235 let v1 = pydict_setitemstring
(module_dictionary, "coccinelle", mx) in
236 check_int_return_value "adding coccinelle python module" v1;
237 let mypystring = pystring_fromstring
!cocci_file_name in
238 let v2 = pydict_setitemstring
(cd, "cocci_file", mypystring) in
239 check_int_return_value "adding python field cocci_file" v2;
243 (*let _ = pycocci_init ()*)
244 (* end initialisation routines *)
246 let added_variables = ref []
248 let build_classes env
=
249 let _ = pycocci_init () in
252 the_environment := env
;
253 let mx = !coccinelle_module in
254 let dict = pymodule_getdict
mx in
257 "include_match" | "has_env_binding" | "exit" -> ()
259 let v = pydict_delitemstring
(dict,name
) in
260 check_int_return_value ("removing " ^ name ^
" from python coccinelle module") v)
262 added_variables := [];
265 let build_variable name
value =
266 let mx = !coccinelle_module in
267 added_variables := name
:: !added_variables;
268 check_int_return_value ("build python variable " ^ name
)
269 (pydict_setitemstring
(pymodule_getdict
mx, name
, value))
271 let get_variable name
=
272 let mx = !coccinelle_module in
274 (pyobject_str
(pydict_getitemstring
(pymodule_getdict
mx, name
)))
276 let contains_binding e (_,(r
,m),_) =
278 let _ = List.find
(function ((re
, rm
), _) -> r
=*= re
&& m =$
= rm
) e in
280 with Not_found
-> false
282 let construct_variables mv
e =
283 let find_binding (r
,m) =
285 let elem = List.find
(function ((re
,rm
),_) -> r
=*= re
&& m =$
= rm
) e in
287 with Not_found
-> None
290 (* Only string in this representation, so no point
291 let instantiate_Expression(x) =
292 let str = pystring_fromstring (Pycocci_aux.exprrep x) in
293 pycocci_instantiate_class "coccilib.elems.Expression"
294 (pytuple_fromsingle (str))
298 (* Only string in this representation, so no point
299 let instantiate_Identifier(x) =
300 let str = pystring_fromstring x in
301 pycocci_instantiate_class "coccilib.elems.Identifier"
302 (pytuple_fromsingle (str))
306 let instantiate_term_list py printer lst
=
307 let (str,elements
) = printer lst
in
308 let str = pystring_fromstring
str in
311 (Array.of_list
(List.map pystring_fromstring
elements)) in
313 pycocci_instantiate_class "coccilib.elems.TermList"
314 (pytuple_fromarray
(Array.of_list
[str;elements])) in
315 let _ = build_variable py
repr in () in
317 List.iter
(function (py
,(r
,m),_) ->
318 match find_binding (r
,m) with
320 (* | Some (_, Ast_c.MetaExprVal (expr,_)) ->
321 let expr_repr = instantiate_Expression(expr) in
322 let _ = build_variable py expr_repr in
324 (* | Some (_, Ast_c.MetaIdVal (id,_)) ->
325 let id_repr = instantiate_Identifier(id) in
326 let _ = build_variable py id_repr in
328 | Some
(_, Ast_c.MetaExprListVal
(exprlist
)) ->
329 instantiate_term_list py
Pycocci_aux.exprlistrep exprlist
330 | Some
(_, Ast_c.MetaParamListVal
(paramlist
)) ->
331 instantiate_term_list py
Pycocci_aux.paramlistrep paramlist
332 | Some
(_, Ast_c.MetaInitListVal
(initlist
)) ->
333 instantiate_term_list py
Pycocci_aux.initlistrep initlist
334 | Some
(_, Ast_c.MetaFieldListVal
(fieldlist
)) ->
335 instantiate_term_list py
Pycocci_aux.fieldlistrep fieldlist
336 | Some
(_, Ast_c.MetaPosValList l
) ->
339 (function (fname
,current_element
,(line
,col
),(line_end
,col_end
)) ->
340 pycocci_instantiate_class "coccilib.elems.Location"
342 (pystring_fromstring fname
,pystring_fromstring current_element
,
343 pystring_fromstring
(Printf.sprintf
"%d" line
),
344 pystring_fromstring
(Printf.sprintf
"%d" col
),
345 pystring_fromstring
(Printf.sprintf
"%d" line_end
),
346 pystring_fromstring
(Printf.sprintf
"%d" col_end
)))) l
in
347 let pylocs = pytuple_fromarray
(Array.of_list
locs) in
348 let _ = build_variable py
pylocs in
350 | Some
(_,binding
) ->
353 (pystring_fromstring
(Pycocci_aux.stringrep binding
)) in
359 let construct_script_variables mv
=
364 "initial value: consider using coccinelle.varname" in
365 let _ = build_variable py
str in
369 let retrieve_script_variables mv
=
370 List.map
(function (_,py
) -> get_variable py
) mv
372 let set_coccifile cocci_file
=
373 cocci_file_name := cocci_file
;
376 let pyrun_simplestring s
=
377 let res = Pycaml.pyrun_simplestring s
in
378 check_int_return_value ("running simple python string: " ^ s
) res;
381 let py_isinitialized () =
382 Pycaml.py_isinitialized ()
386 Pycaml.py_finalize ()