2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
25 * Copyright (C) 2006, 2007 Ecole des Mines de Nantes
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License (GPL)
29 * version 2 as published by the Free Software Foundation.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * file license.txt for more details.
36 * This file was part of Coccinelle.
40 module Flag_engine
= Flag_matcher
41 (*****************************************************************************)
42 (* The functor argument *)
43 (*****************************************************************************)
45 (* info passed recursively in monad in addition to binding *)
47 optional_storage_iso
: bool;
48 optional_qualifier_iso
: bool;
49 value_format_iso
: bool;
52 module XMATCH
= struct
54 (* ------------------------------------------------------------------------*)
55 (* Combinators history *)
56 (* ------------------------------------------------------------------------*)
59 * type ('a, 'b) matcher = 'a -> 'b -> bool
61 * version1: same but with a global variable holding the current binding
63 * - can have multiple possibilities
65 * - sometimes have to undo, cos if start match, then it binds,
66 * and if later it does not match, then must undo the first binds.
67 * ex: when match parameters, can try to match, but then we found far
68 * later that the last argument of a function does not match
69 * => have to uando the binding !!!
70 * (can handle that too with a global, by saving the
71 * global, ... but sux)
72 * => better not use global
75 * type ('a, 'b) matcher = binding -> 'a -> 'b -> binding list
77 * Empty list mean failure (let matchfailure = []).
78 * To be able to have pretty code, have to use partial application
79 * powa, and so the type is in fact
82 * type ('a, 'b) matcher = 'a -> 'b -> binding -> binding list
84 * Then by defining the correct combinators, can have quite pretty code (that
85 * looks like the clean code of version0).
87 * opti: return a lazy list of possible matchs ?
89 * version4: type tin = Lib_engine.metavars_binding
92 (* ------------------------------------------------------------------------*)
93 (* Standard type and operators *)
94 (* ------------------------------------------------------------------------*)
98 binding
: Lib_engine.metavars_binding
;
99 binding0
: Lib_engine.metavars_binding
; (* inherited bindings *)
101 (* 'x is a ('a * 'b) but in fact dont care about 'b, we just tag the SP *)
102 (* opti? use set instead of list *)
103 type 'x tout
= ('x
* Lib_engine.metavars_binding
) list
105 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
108 let (>>=) m1 m2
= fun tin
->
110 let xxs = xs +> List.map
(fun ((a
,b
), binding
) ->
111 m2 a b
{tin
with binding
= binding
}
115 (* Je compare les bindings retournés par les differentes branches.
116 * Si la deuxieme branche amene a des bindings qui sont deja presents
117 * dans la premiere branche, alors je ne les accepte pas.
119 * update: still useful now that julia better handle Exp directly via
120 * ctl tricks using positions ?
122 let (>|+|>) m1 m2
= fun tin
->
131 let list_bindings_already = List.map snd
res1 in
133 (res2 +> List.filter
(fun (x
, binding
) ->
135 (list_bindings_already +> List.exists
(fun already
->
136 Lib_engine.equal_binding binding already
))
142 let (>||>) m1 m2
= fun tin
->
149 (* opti? use set instead of list *)
153 let return res
= fun tin
->
156 let fail = fun tin
->
159 let (>&&>) f m
= fun tin
->
165 let mode = Cocci_vs_c.PatternMode
167 (* ------------------------------------------------------------------------*)
169 (* ------------------------------------------------------------------------*)
170 let cocciExp = fun expf expa node
-> fun tin
->
172 let globals = ref [] in
175 Visitor_c.default_visitor_c
with
176 Visitor_c.kexpr
= (fun (k
, bigf) expb
->
177 match expf expa expb tin
with
178 | [] -> (* failed *) k expb
180 globals := xs @ !globals;
181 if not
!Flag_engine.disallow_nested_exps
then k expb
(* CHOICE *)
184 * push2 expr globals; k expr
186 * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e)
192 Visitor_c.vk_node
bigf node
;
193 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
197 (* same as cocciExp, but for expressions in an expression, not expressions
199 let cocciExpExp = fun expf expa expb
-> fun tin
->
201 let globals = ref [] in
204 Visitor_c.default_visitor_c
with
205 Visitor_c.kexpr
= (fun (k
, bigf) expb
->
206 match expf expa expb tin
with
207 | [] -> (* failed *) k expb
209 globals := xs @ !globals;
210 if not
!Flag_engine.disallow_nested_exps
then k expb
(* CHOICE *)
213 * push2 expr globals; k expr
215 * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e)
221 Visitor_c.vk_expr
bigf expb
;
222 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
226 let cocciTy = fun expf expa node
-> fun tin
->
228 let globals = ref [] in
230 Visitor_c.default_visitor_c
with
231 Visitor_c.ktype
= (fun (k
, bigf) expb
->
232 match expf expa expb tin
with
233 | [] -> (* failed *) k expb
234 | xs -> globals := xs @ !globals);
238 Visitor_c.vk_node
bigf node
;
239 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
243 let cocciInit = fun expf expa node
-> fun tin
->
245 let globals = ref [] in
247 Visitor_c.default_visitor_c
with
248 Visitor_c.kini
= (fun (k
, bigf) expb
->
249 match expf expa expb tin
with
250 | [] -> (* failed *) k expb
251 | xs -> globals := xs @ !globals);
255 Visitor_c.vk_node
bigf node
;
256 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
261 (* ------------------------------------------------------------------------*)
262 (* Distribute mcode *)
263 (* ------------------------------------------------------------------------*)
264 let tag_mck_pos mck posmck
=
266 | Ast_cocci.PLUS c
-> Ast_cocci.PLUS c
267 | Ast_cocci.CONTEXT
(pos
, xs) ->
268 assert (pos
=*= Ast_cocci.NoPos
|| pos
=*= Ast_cocci.DontCarePos
);
269 Ast_cocci.CONTEXT
(posmck
, xs)
270 | Ast_cocci.MINUS
(pos
, inst
, adj
, xs) ->
271 assert (pos
=*= Ast_cocci.NoPos
|| pos
=*= Ast_cocci.DontCarePos
);
272 Ast_cocci.MINUS
(posmck
, inst
, adj
, xs)
275 let tag_mck_pos_mcode (x
,info
,mck
,pos
) posmck stuff
= fun tin
->
276 [((x
, info
, tag_mck_pos mck posmck
, pos
),stuff
), tin
.binding
]
279 let distrf (ii_of_x_f
) =
280 fun mcode x
-> fun tin
->
281 let (max
, min
) = Lib_parsing_c.max_min_by_pos
(ii_of_x_f x
)
283 let posmck = Ast_cocci.FixPos
(min
, max
) (* subtil: and not max, min !!*)
285 tag_mck_pos_mcode mcode
posmck x tin
287 let distrf_e = distrf (Lib_parsing_c.ii_of_expr
)
288 let distrf_args = distrf (Lib_parsing_c.ii_of_args
)
289 let distrf_type = distrf (Lib_parsing_c.ii_of_type
)
290 let distrf_param = distrf (Lib_parsing_c.ii_of_param
)
291 let distrf_params = distrf (Lib_parsing_c.ii_of_params
)
292 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini
)
293 let distrf_node = distrf (Lib_parsing_c.ii_of_node
)
294 let distrf_struct_fields = distrf (Lib_parsing_c.ii_of_struct_fields
)
295 let distrf_cst = distrf (Lib_parsing_c.ii_of_cst
)
296 let distrf_define_params = distrf (Lib_parsing_c.ii_of_define_params
)
299 (* ------------------------------------------------------------------------*)
300 (* Constraints on metavariable values *)
301 (* ------------------------------------------------------------------------*)
302 let check_idconstraint matcher c id
= fun f tin
->
310 let check_constraints_ne matcher constraints exp
= fun f tin
->
311 let rec loop = function
312 [] -> f
() tin
(* success *)
314 match matcher c exp tin
with
315 [] (* failure *) -> loop cs
316 | _
(* success *) -> fail tin
in
319 let check_pos_constraints constraints pvalu f tin
=
322 let success = [[]] in
324 (* relies on the fact that constraints on pos variables must refer to
325 inherited variables *)
326 (match Common.optionise
(fun () -> tin
.binding0
+> List.assoc c
) with
328 if Cocci_vs_c.equal_inh_metavarval exp valu'
329 then success else failure
331 (* if the variable is not there, it puts no constraints *)
332 (* not sure this is still useful *)
334 constraints pvalu f tin
336 (* ------------------------------------------------------------------------*)
338 (* ------------------------------------------------------------------------*)
339 (* pre: if have declared a new metavar that hide another one, then
340 * must be passed with a binding that deleted this metavar
342 * Here we dont use the keep argument of julia. cf f(X,X), J'ai
343 * besoin de garder le X en interne, meme si julia s'en fout elle du
344 * X et qu'elle a mis X a DontSaved.
346 let check_add_metavars_binding strip _keep inherited
= fun (k
, valu
) tin
->
349 match Common.optionise
(fun () -> tin
.binding0
+> List.assoc k
) with
351 if Cocci_vs_c.equal_inh_metavarval valu valu'
352 then Some tin
.binding
356 match Common.optionise
(fun () -> tin
.binding
+> List.assoc k
) with
358 if Cocci_vs_c.equal_metavarval valu valu'
359 then Some tin
.binding
365 Ast_c.MetaIdVal a
-> Ast_c.MetaIdVal a
366 | Ast_c.MetaFuncVal a
-> Ast_c.MetaFuncVal a
367 | Ast_c.MetaLocalFuncVal a
-> Ast_c.MetaLocalFuncVal a
(*more?*)
368 | Ast_c.MetaExprVal a
->
371 then Lib_parsing_c.al_expr a
372 else Lib_parsing_c.semi_al_expr a
)
373 | Ast_c.MetaExprListVal a
->
374 Ast_c.MetaExprListVal
376 then Lib_parsing_c.al_arguments a
377 else Lib_parsing_c.semi_al_arguments a
)
379 | Ast_c.MetaStmtVal a
->
382 then Lib_parsing_c.al_statement a
383 else Lib_parsing_c.semi_al_statement a
)
384 | Ast_c.MetaTypeVal a
->
387 then Lib_parsing_c.al_type a
388 else Lib_parsing_c.semi_al_type a
)
390 | Ast_c.MetaInitVal a
->
393 then Lib_parsing_c.al_init a
394 else Lib_parsing_c.semi_al_init a
)
396 | Ast_c.MetaListlenVal a
-> Ast_c.MetaListlenVal a
398 | Ast_c.MetaParamVal a
-> failwith
"not handling MetaParamVal"
399 | Ast_c.MetaParamListVal a
->
400 Ast_c.MetaParamListVal
402 then Lib_parsing_c.al_params a
403 else Lib_parsing_c.semi_al_params a
)
405 | Ast_c.MetaPosVal
(pos1
,pos2
) -> Ast_c.MetaPosVal
(pos1
,pos2
)
406 | Ast_c.MetaPosValList l
-> Ast_c.MetaPosValList l
407 in Some
(tin
.binding
+> Common.insert_assoc
(k
, valu'
))
409 let envf keep inherited
= fun (k
, valu, get_max_min
) f tin
->
410 let x = Ast_cocci.unwrap_mcode k
in
411 match check_add_metavars_binding true keep inherited
(x, valu) tin
with
413 let new_tin = {tin
with binding
= binding
} in
414 (match Ast_cocci.get_pos_var k
with
415 Ast_cocci.MetaPos
(name
,constraints
,per
,keep
,inherited
) ->
417 let (file
,current_element
,min
,max
) = get_max_min
() in
418 Ast_c.MetaPosValList
[(file
,current_element
,min
,max
)] in
419 (* check constraints. success means that there is a match with
420 one of the constraints, which will ultimately result in
422 check_pos_constraints constraints
pvalu
424 (* constraints are satisfied, now see if we are compatible
425 with existing bindings *)
427 let x = Ast_cocci.unwrap_mcode name
in
429 check_add_metavars_binding false keep inherited
(x, pvalu)
432 f
() {new_tin with binding
= binding
}
435 | Ast_cocci.NoMetaPos
-> f
() new_tin)
438 (* ------------------------------------------------------------------------*)
439 (* Environment, allbounds *)
440 (* ------------------------------------------------------------------------*)
441 (* all referenced inherited variables have to be bound. This would
442 * be naturally checked for the minus or context ones in the
443 * matching process, but have to check the plus ones as well. The
444 * result of get_inherited contains all of these, but the potential
445 * redundant checking for the minus and context ones is probably not
446 * a big deal. If it's a problem, could fix free_vars to distinguish
447 * between + variables and the other ones. *)
449 let (all_bound
: Ast_cocci.meta_name list
-> tin
-> bool) = fun l tin
->
450 l
+> List.for_all
(fun inhvar
->
451 match Common.optionise
(fun () -> tin
.binding0
+> List.assoc inhvar
) with
456 let optional_storage_flag f
= fun tin
->
457 f
(tin
.extra
.optional_storage_iso
) tin
459 let optional_qualifier_flag f
= fun tin
->
460 f
(tin
.extra
.optional_qualifier_iso
) tin
462 let value_format_flag f
= fun tin
->
463 f
(tin
.extra
.value_format_iso
) tin
466 (* ------------------------------------------------------------------------*)
468 (* ------------------------------------------------------------------------*)
469 let tokenf ia ib
= fun tin
->
470 let pos = Ast_c.info_to_fixpos ib
in
471 let posmck = Ast_cocci.FixPos
(pos, pos) in
472 let finish tin
= tag_mck_pos_mcode ia
posmck ib tin
in
473 match Ast_cocci.get_pos_var ia
with
474 Ast_cocci.MetaPos
(name
,constraints
,per
,keep
,inherited
) ->
475 let mpos = Lib_parsing_c.lin_col_by_pos
[ib
] in
476 let pvalu = Ast_c.MetaPosValList
[mpos] in
477 check_pos_constraints constraints
pvalu
479 (* constraints are satisfied, now see if we are compatible
480 with existing bindings *)
482 let x = Ast_cocci.unwrap_mcode name
in
484 check_add_metavars_binding false keep inherited
(x, pvalu) tin
486 Some binding
-> finish {tin
with binding
= binding
}
491 let tokenf_mck mck ib
= fun tin
->
492 let pos = Ast_c.info_to_fixpos ib
in
493 let posmck = Ast_cocci.FixPos
(pos, pos) in
494 [(tag_mck_pos mck
posmck, ib
), tin
.binding
]
498 (*****************************************************************************)
500 (*****************************************************************************)
501 module MATCH
= Cocci_vs_c.COCCI_VS_C
(XMATCH
)
504 let match_re_node2 dropped_isos a b binding0
=
508 optional_storage_iso
= not
(List.mem
"optional_storage" dropped_isos
);
509 optional_qualifier_iso
= not
(List.mem
"optional_qualifier" dropped_isos
);
510 value_format_iso
= not
(List.mem
"value_format" dropped_isos
);
513 XMATCH.binding0
= binding0
;
516 MATCH.rule_elem_node a b
tin
517 (* take only the tagged-SP, the 'a' *)
518 +> List.map
(fun ((a
,_b
), binding
) -> a
, binding
)
521 let match_re_node a b c d
=
522 Common.profile_code
"Pattern3.match_re_node"
523 (fun () -> match_re_node2 a b c d
)