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.
31 module StringMap
= Map.Make
(String
)
33 exception Pycocciexception
35 let python_support = true
37 (* ------------------------------------------------------------------- *)
38 (* The following definitions are from
39 http://patches.ubuntu.com/by-release/extracted/debian/c/coccinelle/0.1.5dbs-2/01-system-pycaml
40 as well as _pycocci_setargs *)
42 let _pycocci_none () =
43 let builtins = pyeval_getbuiltins
() in
44 pyobject_getitem
(builtins, pystring_fromstring
"None")
46 let _pycocci_true () =
47 let builtins = pyeval_getbuiltins
() in
48 pyobject_getitem
(builtins, pystring_fromstring
"True")
50 let _pycocci_false () =
51 let builtins = pyeval_getbuiltins
() in
52 pyobject_getitem
(builtins, pystring_fromstring
"False")
54 let _pycocci_tuple6 (a
,b
,c
,d
,e
,f
) =
55 pytuple_fromarray
([|a
; b
; c
; d
; e
; f
|])
57 (* ------------------------------------------------------------------- *)
59 let check_return_value v
=
60 if v
=*= (pynull
()) then
62 raise Pycocciexception
)
64 let check_int_return_value v
=
67 raise Pycocciexception
)
70 let initialised = ref false
72 let coccinelle_module = ref (_pycocci_none ())
73 let cocci_file_name = ref ""
75 (* dealing with python modules loaded *)
76 let module_map = ref (StringMap.add
"__main__" (_pycocci_none ()) StringMap.empty
)
78 let get_module module_name
=
79 StringMap.find module_name
(!module_map)
81 let is_module_loaded module_name
=
83 let _ = get_module module_name
in
85 with Not_found
-> false
87 let load_module module_name
=
88 if not
(is_module_loaded module_name
) then
89 let m = pyimport_importmodule module_name
in
91 (module_map := (StringMap.add module_name
m (!module_map));
93 else get_module module_name
94 (* end python module handling part *)
96 (* python interaction *)
98 let last_period = String.rindex fqn '
.'
in
99 let module_name = String.sub fqn
0 last_period in
100 let class_name = String.sub fqn
(last_period + 1) (String.length fqn
- last_period - 1) in
101 (module_name, class_name)
103 let pycocci_get_class_type fqn
=
104 let (module_name, class_name) = split_fqn fqn
in
105 let m = get_module module_name in
106 let attr = pyobject_getattrstring
(m, class_name) in
107 check_return_value attr;
110 let pycocci_instantiate_class fqn args
=
111 let class_type = pycocci_get_class_type fqn
in
113 pyeval_callobjectwithkeywords
(class_type, args
, pynull
()) in
114 check_return_value obj;
117 (* end python interaction *)
119 let inc_match = ref true
120 let exited = ref false
122 let include_match v
=
123 let truth = pyobject_istrue
(pytuple_getitem
(v
, 1)) in
124 check_int_return_value truth;
125 inc_match := truth != 0;
132 let build_method (mname
, camlfunc
, args
) pymodule classx classdict
=
133 let cmx = pymethod_new
(pywrap_closure camlfunc
, args
, classx
) in
134 let v = pydict_setitemstring
(classdict
, mname
, cmx) in
135 check_int_return_value v;
138 let build_class cname parent methods pymodule
=
139 let cd = pydict_new
() in
140 check_return_value cd;
141 let cx = pyclass_new
(pytuple_fromsingle
(pycocci_get_class_type parent
), cd,
142 pystring_fromstring cname
) in
143 check_return_value cx;
144 List.iter
(function meth
-> build_method meth pymodule
cx cd) methods
;
145 let v = pydict_setitemstring
(pymodule_getdict pymodule
, cname
, cx) in
146 check_int_return_value v;
149 let the_environment = ref []
151 let has_environment_binding name
=
152 let a = pytuple_toarray name
in
153 let (rule
, name
) = (Array.get
a 1, Array.get
a 2) in
154 let orule = pystring_asstring rule
in
155 let oname = pystring_asstring name
in
156 let e = List.exists
(function (x
,y
) -> orule =*= x
&& oname =$
= y
)
158 if e then _pycocci_true () else _pycocci_false ()
160 let pyoutputinstance = ref (_pycocci_none ())
161 let pyoutputdict = ref (_pycocci_none ())
163 let get_cocci_file args
=
164 pystring_fromstring
(!cocci_file_name)
166 (* initialisation routines *)
167 let _pycocci_setargs argv0
=
169 pysequence_list
(pytuple_fromsingle
(pystring_fromstring argv0
)) in
170 let sys_mod = load_module "sys" in
171 pyobject_setattrstring
(sys_mod, "argv", argv)
173 let pycocci_init () =
175 if not
!initialised then (
177 Unix.putenv
"PYTHONPATH"
178 (Printf.sprintf
"%s/coccinelle" (Unix.getenv
"HOME"));
179 let _ = if not
(py_isinitialized
() != 0) then
180 (if !Flag.show_misc
then Common.pr2
"Initializing python\n%!";
184 let argv0 = Printf.sprintf
"%s%sspatch" (Sys.getcwd
()) (match Sys.os_type
with "Win32" -> "\\" | _ -> "/") in
185 let _ = _pycocci_setargs argv0 in
187 coccinelle_module := (pymodule_new
"coccinelle");
188 module_map := StringMap.add
"coccinelle" !coccinelle_module !module_map;
189 let _ = load_module "coccilib.elems" in
190 let _ = load_module "coccilib.output" in
192 let module_dictionary = pyimport_getmoduledict
() in
193 coccinelle_module := pymodule_new
"coccinelle";
194 let mx = !coccinelle_module in
195 let (cd, cx) = build_class "Cocci" (!Flag.pyoutput
)
196 [("exit", sp_exit, (pynull
()));
197 ("include_match", include_match, (pynull
()));
198 ("has_env_binding", has_environment_binding, (pynull
()))] mx in
199 pyoutputinstance := cx;
201 let v1 = pydict_setitemstring
(module_dictionary, "coccinelle", mx) in
202 check_int_return_value v1;
203 let mypystring = pystring_fromstring
!cocci_file_name in
204 let v2 = pydict_setitemstring
(cd, "cocci_file", mypystring) in
205 check_int_return_value v2;
209 (*let _ = pycocci_init ()*)
210 (* end initialisation routines *)
212 let added_variables = ref []
214 let build_classes env
=
215 let _ = pycocci_init () in
218 the_environment := env
;
219 let mx = !coccinelle_module in
220 let dict = pymodule_getdict
mx in
223 "include_match" | "has_env_binding" | "exit" -> ()
225 let v = pydict_delitemstring
(dict,name
) in
226 check_int_return_value v)
228 added_variables := [];
231 let build_variable name
value =
232 let mx = !coccinelle_module in
233 added_variables := name
:: !added_variables;
234 check_int_return_value
235 (pydict_setitemstring
(pymodule_getdict
mx, name
, value))
237 let get_variable name
=
238 let mx = !coccinelle_module in
240 (pyobject_str
(pydict_getitemstring
(pymodule_getdict
mx, name
)))
242 let contains_binding e (_,(r
,m),_) =
244 let _ = List.find
(function ((re
, rm
), _) -> r
=*= re
&& m =$
= rm
) e in
246 with Not_found
-> false
248 let construct_variables mv
e =
249 let find_binding (r
,m) =
251 let elem = List.find
(function ((re
,rm
),_) -> r
=*= re
&& m =$
= rm
) e in
253 with Not_found
-> None
256 (* Only string in this representation, so no point
257 let instantiate_Expression(x) =
258 let str = pystring_fromstring (Pycocci_aux.exprrep x) in
259 pycocci_instantiate_class "coccilib.elems.Expression"
260 (pytuple_fromsingle (str))
264 (* Only string in this representation, so no point
265 let instantiate_Identifier(x) =
266 let str = pystring_fromstring x in
267 pycocci_instantiate_class "coccilib.elems.Identifier"
268 (pytuple_fromsingle (str))
272 let instantiate_term_list py printer lst
=
273 let (str,elements
) = printer lst
in
274 let str = pystring_fromstring
str in
277 (Array.of_list
(List.map pystring_fromstring
elements)) in
279 pycocci_instantiate_class "coccilib.elems.TermList"
280 (pytuple_fromarray
(Array.of_list
[str;elements])) in
281 let _ = build_variable py
repr in () in
283 List.iter
(function (py
,(r
,m),_) ->
284 match find_binding (r
,m) with
286 (* | Some (_, Ast_c.MetaExprVal (expr,_)) ->
287 let expr_repr = instantiate_Expression(expr) in
288 let _ = build_variable py expr_repr in
290 (* | Some (_, Ast_c.MetaIdVal (id,_)) ->
291 let id_repr = instantiate_Identifier(id) in
292 let _ = build_variable py id_repr in
294 | Some
(_, Ast_c.MetaExprListVal
(exprlist
)) ->
295 instantiate_term_list py
Pycocci_aux.exprlistrep exprlist
296 | Some
(_, Ast_c.MetaParamListVal
(paramlist
)) ->
297 instantiate_term_list py
Pycocci_aux.paramlistrep paramlist
298 | Some
(_, Ast_c.MetaInitListVal
(initlist
)) ->
299 instantiate_term_list py
Pycocci_aux.initlistrep initlist
300 | Some
(_, Ast_c.MetaFieldListVal
(fieldlist
)) ->
301 instantiate_term_list py
Pycocci_aux.fieldlistrep fieldlist
302 | Some
(_, Ast_c.MetaPosValList l
) ->
305 (function (fname
,current_element
,(line
,col
),(line_end
,col_end
)) ->
306 pycocci_instantiate_class "coccilib.elems.Location"
308 (pystring_fromstring fname
,pystring_fromstring current_element
,
309 pystring_fromstring
(Printf.sprintf
"%d" line
),
310 pystring_fromstring
(Printf.sprintf
"%d" col
),
311 pystring_fromstring
(Printf.sprintf
"%d" line_end
),
312 pystring_fromstring
(Printf.sprintf
"%d" col_end
)))) l
in
313 let pylocs = pytuple_fromarray
(Array.of_list
locs) in
314 let _ = build_variable py
pylocs in
316 | Some
(_,binding
) ->
319 (pystring_fromstring
(Pycocci_aux.stringrep binding
)) in
325 let construct_script_variables mv
=
330 "initial value: consider using coccinelle.varname" in
331 let _ = build_variable py
str in
335 let retrieve_script_variables mv
=
336 List.map
(function (_,py
) -> get_variable py
) mv
338 let set_coccifile cocci_file
=
339 cocci_file_name := cocci_file
;
342 let pyrun_simplestring s
=
343 let res = Pycaml.pyrun_simplestring s
in
344 check_int_return_value res;
347 let py_isinitialized () =
348 Pycaml.py_isinitialized ()
352 Pycaml.py_finalize ()