Debian Codegraph @

tools/spgen/source/context_rule.ml

raw
1 (*
2 * This file is part of Coccinelle, licensed under the terms of the GPL v2.
3 * See copyright.txt in the Coccinelle source code for more information.
4 * The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
5 *)
6
7 module Ast0 = Ast0_cocci
8 module Ast = Ast_cocci
9 module MV = Meta_variable
10
11 (* ------------------------------------------------------------------------- *)
12
13 (* Generates a context mode rule with metapositions and stars!
14 * May generate an extra disjunction rule if the original rule calls for it.
15 *)
16
17 (* ------------------------------------------------------------------------- *)
18 (* CONTEXT RULE GENERATION FUNCTIONS *)
19
20 type t = (Rule_header.t * Rule_body.t) list
21
22 let generate ~context_mode ~disj_map ~new_name ~rule_names ~rule =
23
24 match rule with
25 | Ast0.InitialScriptRule (nm,_,_,_,_,_)
26 | Ast0.FinalScriptRule (nm,_,_,_,_,_)
27 | Ast0.ScriptRule (nm,_,_,_,_,_,_) ->
28 failwith
29 ("Internal error: Can't generate a context rule for a script rule! " ^
30 "The rule is: " ^ nm)
31
32 | Ast0.CocciRule((minus_rule,_,(isos,drop_isos,deps,old_nm,exists)),_,_,_) ->
33
34 let context_nm = Globals.get_context_name ~context_mode new_name in
35 let disj_nm = Globals.get_disj_name new_name in
36
37 let meta_vars =
38 (* the mv is either local, inherited from a non-*/+/- rule, or
39 * inherited from a */+/- rule. In the last case, it should be inherited
40 * from the * version and not the +/- version of the rule.
41 * We can do it the following way, since we know that:
42 * 1) an inherited rule is always named
43 * 2) a named rule will have the same name in the generated script
44 * (unlike number rules, which can have names set by config)
45 * 3) if the current local rule is in context_mode, so was the inherited
46 * rule since context_mode is set on a per-script basis. Ie. we can
47 * safely use get_context_name.
48 *)
49 let inherit_name mv =
50 let r = MV.get_rule mv in
51 if List.mem r rule_names
52 then MV.inherit_rule ~force:true ~new_rule:(Globals.get_context_name ~context_mode r) mv
53 else mv
54 in
55 List.map inherit_name (MV.extract ~minus_rule ~rule_name:old_nm)
56 in
57
58 let deps = Globals.add_context_dependency ~context_mode deps in
59 let rh_fn = Rule_header.generate ~isos ~drop_isos ~deps ~meta_vars in
60
61 let (pos, (context_body, disj)) =
62 Rule_body.generate ~context_mode ~disj_map ~minus_rule in
63 let _ = if pos = [] then failwith
64 ("MEGA ERROR: Congratulations! You managed to write a Coccinelle " ^
65 "rule that spgen was unable to add a position to! The rule is \"" ^
66 old_nm ^ "\".") in
67
68 (* the added position metavariables in local scope (for headers) *)
69 let pos_mv = List.map (MV.make ~typ:"position ") pos in
70
71 (* the added position metavariables in inherited scope (for scripts) *)
72 let pos_inh = List.map (MV.inherit_rule ~new_rule:context_nm) pos_mv in
73
74 match disj with
75 | None ->
76
77 let context_header =
78 rh_fn ~exists ~rule_name:context_nm ~meta_pos:pos_mv in
79 ([(context_header, context_body)], pos_inh)
80
81 | Some disj_body ->
82
83 (* context rule has no stars, therefore "exists" in header *)
84 let context_header =
85 rh_fn ~rule_name:context_nm ~exists:Ast.Exists ~meta_pos:pos_mv in
86 let disj_header =
87 rh_fn ~rule_name:disj_nm ~exists ~meta_pos:pos_inh in
88 ([(context_header, context_body); (disj_header, disj_body)], pos_inh)
89
90 let print out l =
91 List.iter (fun (rh,rb) -> Rule_header.print out rh; Rule_body.print out rb) l