1 (* Copyright (C) 2006, 2007 Yoann Padioleau
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License (GPL)
5 * version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * file license.txt for more details.
12 * This file was part of Coccinelle.
16 module Flag_engine
= Flag_matcher
17 (*****************************************************************************)
18 (* The functor argument *)
19 (*****************************************************************************)
21 (* info passed recursively in monad in addition to binding *)
23 optional_storage_iso
: bool;
24 optional_qualifier_iso
: bool;
25 value_format_iso
: bool;
28 module XMATCH
= struct
30 (* ------------------------------------------------------------------------*)
31 (* Combinators history *)
32 (* ------------------------------------------------------------------------*)
35 * type ('a, 'b) matcher = 'a -> 'b -> bool
37 * version1: same but with a global variable holding the current binding
39 * - can have multiple possibilities
41 * - sometimes have to undo, cos if start match, then it binds,
42 * and if later it does not match, then must undo the first binds.
43 * ex: when match parameters, can try to match, but then we found far
44 * later that the last argument of a function does not match
45 * => have to uando the binding !!!
46 * (can handle that too with a global, by saving the
47 * global, ... but sux)
48 * => better not use global
51 * type ('a, 'b) matcher = binding -> 'a -> 'b -> binding list
53 * Empty list mean failure (let matchfailure = []).
54 * To be able to have pretty code, have to use partial application
55 * powa, and so the type is in fact
58 * type ('a, 'b) matcher = 'a -> 'b -> binding -> binding list
60 * Then by defining the correct combinators, can have quite pretty code (that
61 * looks like the clean code of version0).
63 * opti: return a lazy list of possible matchs ?
65 * version4: type tin = Lib_engine.metavars_binding
68 (* ------------------------------------------------------------------------*)
69 (* Standard type and operators *)
70 (* ------------------------------------------------------------------------*)
74 binding
: Lib_engine.metavars_binding
;
75 binding0
: Lib_engine.metavars_binding
; (* inherited bindings *)
77 (* 'x is a ('a * 'b) but in fact dont care about 'b, we just tag the SP *)
78 (* opti? use set instead of list *)
79 type 'x tout
= ('x
* Lib_engine.metavars_binding
) list
81 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
84 let (>>=) m1 m2
= fun tin
->
86 let xxs = xs +> List.map
(fun ((a
,b
), binding
) ->
87 m2 a b
{tin
with binding
= binding
}
91 (* Je compare les bindings retournés par les differentes branches.
92 * Si la deuxieme branche amene a des bindings qui sont deja presents
93 * dans la premiere branche, alors je ne les accepte pas.
95 * update: still useful now that julia better handle Exp directly via
96 * ctl tricks using positions ?
98 let (>|+|>) m1 m2
= fun tin
->
107 let list_bindings_already = List.map snd
res1 in
109 (res2 +> List.filter
(fun (x
, binding
) ->
111 (list_bindings_already +> List.exists
(fun already
->
112 Lib_engine.equal_binding binding already
))
118 let (>||>) m1 m2
= fun tin
->
125 (* opti? use set instead of list *)
129 let return res
= fun tin
->
132 let fail = fun tin
->
135 let (>&&>) f m
= fun tin
->
141 let mode = Cocci_vs_c.PatternMode
143 (* ------------------------------------------------------------------------*)
145 (* ------------------------------------------------------------------------*)
146 let cocciExp = fun expf expa node
-> fun tin
->
148 let globals = ref [] in
151 Visitor_c.default_visitor_c
with
152 Visitor_c.kexpr
= (fun (k
, bigf) expb
->
153 match expf expa expb tin
with
154 | [] -> (* failed *) k expb
156 globals := xs @ !globals;
157 if not
!Flag_engine.disallow_nested_exps
then k expb
(* CHOICE *)
160 * push2 expr globals; k expr
162 * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e)
168 Visitor_c.vk_node
bigf node
;
169 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
173 (* same as cocciExp, but for expressions in an expression, not expressions
175 let cocciExpExp = fun expf expa expb
-> fun tin
->
177 let globals = ref [] in
180 Visitor_c.default_visitor_c
with
181 Visitor_c.kexpr
= (fun (k
, bigf) expb
->
182 match expf expa expb tin
with
183 | [] -> (* failed *) k expb
185 globals := xs @ !globals;
186 if not
!Flag_engine.disallow_nested_exps
then k expb
(* CHOICE *)
189 * push2 expr globals; k expr
191 * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e)
197 Visitor_c.vk_expr
bigf expb
;
198 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
202 let cocciTy = fun expf expa node
-> fun tin
->
204 let globals = ref [] in
206 Visitor_c.default_visitor_c
with
207 Visitor_c.ktype
= (fun (k
, bigf) expb
->
208 match expf expa expb tin
with
209 | [] -> (* failed *) k expb
210 | xs -> globals := xs @ !globals);
214 Visitor_c.vk_node
bigf node
;
215 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
219 let cocciInit = fun expf expa node
-> fun tin
->
221 let globals = ref [] in
223 Visitor_c.default_visitor_c
with
224 Visitor_c.kini
= (fun (k
, bigf) expb
->
225 match expf expa expb tin
with
226 | [] -> (* failed *) k expb
227 | xs -> globals := xs @ !globals);
231 Visitor_c.vk_node
bigf node
;
232 !globals +> List.map
(fun ((a
, _exp
), binding
) ->
237 (* ------------------------------------------------------------------------*)
238 (* Distribute mcode *)
239 (* ------------------------------------------------------------------------*)
240 let tag_mck_pos mck posmck
=
242 | Ast_cocci.PLUS
-> Ast_cocci.PLUS
243 | Ast_cocci.CONTEXT
(pos
, xs) ->
244 assert (pos
= Ast_cocci.NoPos
|| pos
= Ast_cocci.DontCarePos
);
245 Ast_cocci.CONTEXT
(posmck
, xs)
246 | Ast_cocci.MINUS
(pos
, xs) ->
247 assert (pos
= Ast_cocci.NoPos
|| pos
= Ast_cocci.DontCarePos
);
248 Ast_cocci.MINUS
(posmck
, xs)
251 let tag_mck_pos_mcode (x
,info
,mck
,pos
) posmck stuff
= fun tin
->
252 [((x
, info
, tag_mck_pos mck posmck
, pos
),stuff
), tin
.binding
]
255 let distrf (ii_of_x_f
) =
256 fun mcode x
-> fun tin
->
257 let (max
, min
) = Lib_parsing_c.max_min_by_pos
(ii_of_x_f x
)
259 let posmck = Ast_cocci.FixPos
(min
, max
) (* subtil: and not max, min !!*)
261 tag_mck_pos_mcode mcode
posmck x tin
263 let distrf_e = distrf (Lib_parsing_c.ii_of_expr
)
264 let distrf_args = distrf (Lib_parsing_c.ii_of_args
)
265 let distrf_type = distrf (Lib_parsing_c.ii_of_type
)
266 let distrf_param = distrf (Lib_parsing_c.ii_of_param
)
267 let distrf_params = distrf (Lib_parsing_c.ii_of_params
)
268 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini
)
269 let distrf_node = distrf (Lib_parsing_c.ii_of_node
)
270 let distrf_struct_fields = distrf (Lib_parsing_c.ii_of_struct_fields
)
271 let distrf_cst = distrf (Lib_parsing_c.ii_of_cst
)
272 let distrf_define_params = distrf (Lib_parsing_c.ii_of_define_params
)
275 (* ------------------------------------------------------------------------*)
276 (* Constraints on metavariable values *)
277 (* ------------------------------------------------------------------------*)
278 let check_constraints matcher constraints exp
= fun f tin
->
279 let rec loop = function
280 [] -> f
() tin
(* success *)
282 match matcher c exp tin
with
283 [] (* failure *) -> loop cs
284 | _
(* success *) -> fail tin
in
287 let check_pos_constraints constraints pvalu f tin
=
290 let success = [[]] in
292 (* relies on the fact that constraints on pos variables must refer to
293 inherited variables *)
294 (match Common.optionise
(fun () -> tin
.binding0
+> List.assoc c
) with
296 if Cocci_vs_c.equal_metavarval exp valu'
297 then success else failure
299 (* if the variable is not there, it puts no constraints *)
300 (* not sure this is still useful *)
302 constraints pvalu f tin
304 (* ------------------------------------------------------------------------*)
306 (* ------------------------------------------------------------------------*)
307 (* pre: if have declared a new metavar that hide another one, then
308 * must be passed with a binding that deleted this metavar
310 * Here we dont use the keep argument of julia. cf f(X,X), J'ai
311 * besoin de garder le X en interne, meme si julia s'en fout elle du
312 * X et qu'elle a mis X a DontSaved.
314 let check_add_metavars_binding strip _keep inherited
= fun (k
, valu
) tin
->
317 match Common.optionise
(fun () -> tin
.binding0
+> List.assoc k
) with
319 if Cocci_vs_c.equal_metavarval valu valu'
320 then Some tin
.binding
324 match Common.optionise
(fun () -> tin
.binding
+> List.assoc k
) with
326 if Cocci_vs_c.equal_metavarval valu valu'
327 then Some tin
.binding
333 Ast_c.MetaIdVal a
-> Ast_c.MetaIdVal a
334 | Ast_c.MetaFuncVal a
-> Ast_c.MetaFuncVal a
335 | Ast_c.MetaLocalFuncVal a
-> Ast_c.MetaLocalFuncVal a
(*more?*)
336 | Ast_c.MetaExprVal a
->
339 then Lib_parsing_c.al_expr a
340 else Lib_parsing_c.semi_al_expr a
)
341 | Ast_c.MetaExprListVal a
->
342 Ast_c.MetaExprListVal
344 then Lib_parsing_c.al_arguments a
345 else Lib_parsing_c.semi_al_arguments a
)
347 | Ast_c.MetaStmtVal a
->
350 then Lib_parsing_c.al_statement a
351 else Lib_parsing_c.semi_al_statement a
)
352 | Ast_c.MetaTypeVal a
->
355 then Lib_parsing_c.al_type a
356 else Lib_parsing_c.semi_al_type a
)
358 | Ast_c.MetaListlenVal a
-> Ast_c.MetaListlenVal a
360 | Ast_c.MetaParamVal a
-> failwith
"not handling MetaParamVal"
361 | Ast_c.MetaParamListVal a
->
362 Ast_c.MetaParamListVal
364 then Lib_parsing_c.al_params a
365 else Lib_parsing_c.semi_al_params a
)
367 | Ast_c.MetaPosVal
(pos1
,pos2
) -> Ast_c.MetaPosVal
(pos1
,pos2
)
368 | Ast_c.MetaPosValList l
-> Ast_c.MetaPosValList l
369 in Some
(tin
.binding
+> Common.insert_assoc
(k
, valu'
))
371 let envf keep inherited
= fun (k
, valu, get_max_min
) f tin
->
372 let x = Ast_cocci.unwrap_mcode k
in
373 match check_add_metavars_binding true keep inherited
(x, valu) tin
with
375 let new_tin = {tin
with binding
= binding
} in
376 (match Ast_cocci.get_pos_var k
with
377 Ast_cocci.MetaPos
(name
,constraints
,per
,keep
,inherited
) ->
379 let (file
,current_element
,min
,max
) = get_max_min
() in
380 Ast_c.MetaPosValList
[(file
,current_element
,min
,max
)] in
381 (* check constraints. success means that there is a match with
382 one of the constraints, which will ultimately result in
384 check_pos_constraints constraints
pvalu
386 (* constraints are satisfied, now see if we are compatible
387 with existing bindings *)
389 let x = Ast_cocci.unwrap_mcode name
in
391 check_add_metavars_binding false keep inherited
(x, pvalu)
394 f
() {new_tin with binding
= binding
}
397 | Ast_cocci.NoMetaPos
-> f
() new_tin)
400 (* ------------------------------------------------------------------------*)
401 (* Environment, allbounds *)
402 (* ------------------------------------------------------------------------*)
403 (* all referenced inherited variables have to be bound. This would
404 * be naturally checked for the minus or context ones in the
405 * matching process, but have to check the plus ones as well. The
406 * result of get_inherited contains all of these, but the potential
407 * redundant checking for the minus and context ones is probably not
408 * a big deal. If it's a problem, could fix free_vars to distinguish
409 * between + variables and the other ones. *)
411 let (all_bound
: Ast_cocci.meta_name list
-> tin
-> bool) = fun l tin
->
412 l
+> List.for_all
(fun inhvar
->
413 match Common.optionise
(fun () -> tin
.binding0
+> List.assoc inhvar
) with
418 let optional_storage_flag f
= fun tin
->
419 f
(tin
.extra
.optional_storage_iso
) tin
421 let optional_qualifier_flag f
= fun tin
->
422 f
(tin
.extra
.optional_qualifier_iso
) tin
424 let value_format_flag f
= fun tin
->
425 f
(tin
.extra
.value_format_iso
) tin
428 (* ------------------------------------------------------------------------*)
430 (* ------------------------------------------------------------------------*)
431 let tokenf ia ib
= fun tin
->
432 let pos = Ast_c.info_to_fixpos ib
in
433 let posmck = Ast_cocci.FixPos
(pos, pos) in
434 let finish tin
= tag_mck_pos_mcode ia
posmck ib tin
in
435 match Ast_cocci.get_pos_var ia
with
436 Ast_cocci.MetaPos
(name
,constraints
,per
,keep
,inherited
) ->
437 let mpos = Lib_parsing_c.lin_col_by_pos
[ib
] in
438 let pvalu = Ast_c.MetaPosValList
[mpos] in
439 check_pos_constraints constraints
pvalu
441 (* constraints are satisfied, now see if we are compatible
442 with existing bindings *)
444 let x = Ast_cocci.unwrap_mcode name
in
446 check_add_metavars_binding false keep inherited
(x, pvalu) tin
448 Some binding
-> finish {tin
with binding
= binding
}
453 let tokenf_mck mck ib
= fun tin
->
454 let pos = Ast_c.info_to_fixpos ib
in
455 let posmck = Ast_cocci.FixPos
(pos, pos) in
456 [(tag_mck_pos mck
posmck, ib
), tin
.binding
]
460 (*****************************************************************************)
462 (*****************************************************************************)
463 module MATCH
= Cocci_vs_c.COCCI_VS_C
(XMATCH
)
466 let match_re_node2 dropped_isos a b binding0
=
470 optional_storage_iso
= not
(List.mem
"optional_storage" dropped_isos
);
471 optional_qualifier_iso
= not
(List.mem
"optional_qualifier" dropped_isos
);
472 value_format_iso
= not
(List.mem
"value_format" dropped_isos
);
475 XMATCH.binding0
= binding0
;
478 MATCH.rule_elem_node a b
tin
479 (* take only the tagged-SP, the 'a' *)
480 +> List.map
(fun ((a
,_b
), binding
) -> a
, binding
)
483 let match_re_node a b c d
=
484 Common.profile_code
"Pattern3.match_re_node"
485 (fun () -> match_re_node2 a b c d
)