Debian Codegraph @

tools/spgen/source/detect_patch.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 V0 = Visitor_ast0
9 module VT0 = Visitor_ast0_types
10 module IntMap = Common.IntMap
11
12 (* ------------------------------------------------------------------------- *)
13
14 (* Detects whether a rule is a */+/- rule or not.
15 *
16 * Generates a disjunction map for a rule, which maps each disjunction within
17 * the rule to a list of bools indicating whether each disjunction case has
18 * */+/-.
19 * (this is useful for determining whether a rule uses 'pattern matching'.)
20 *)
21
22 (* ------------------------------------------------------------------------- *)
23 (* HELPERS *)
24
25 (* merges two disjunction maps.
26 * runs through all keys in both maps, unlikely to be a problem since there
27 * won't be many keys (each key representing one disjunction)
28 *)
29 let merge =
30 let fn key aopt bopt =
31 match aopt, bopt with
32 | Some a, Some b -> Some (List.map2 (||) a b)
33 | Some a, None | None, Some a -> Some a
34 | None, None -> None in
35 IntMap.merge fn
36
37 (* mcodes contain the actual information about */+/-. *)
38 let mcode = function
39 | (x, a, info, Ast0.CONTEXT _, pos, _) -> (false, IntMap.empty)
40 | (x, a, info, Ast0.MINUS _, pos, _) -> (true, IntMap.empty)
41 | (x, a, info, Ast0.PLUS _, pos, _) -> (true, IntMap.empty)
42 | (x, a, info, Ast0.MIXED _, pos, _) ->
43 failwith "detect_patch: mixed not supported"
44
45 (* Disjunction handler.
46 * takes left and right parenthesis mcodes, list of pipe separator mcodes,
47 * the list of different cases and one function to handle each case.
48 * Parentheses and pipes are always context mode.
49 * Returns
50 * (whether there is a patch in any of the disjunction cases,
51 * a mapping of the beginning line number of the disj to a list of
52 * bools indicating whether each of the disjunctions are a patch or no)
53 *)
54 let handle_disj lp rp pipelist clist cfn =
55 let index = Ast0.get_mcode_line lp in
56
57 let disj_patches (is_patch, acc_list, acc_map) case =
58 let (case_is_p, case_map) = cfn case in
59 let p = is_patch || case_is_p in
60 let dp = case_is_p :: acc_list in
61 let dps = merge case_map acc_map in
62 (p, dp, dps) in
63
64 (* contains_patch is a bool denoting whether the whole disj contains a patch
65 * disj_patch is a list of bools, each bool representing a disj case
66 * acc are the accumulated disjunctions within the disjunction
67 *)
68 let (contains_patch, disj_patch, acc) =
69 List.fold_left disj_patches (false, [], IntMap.empty) clist in
70
71 (contains_patch, IntMap.add index (List.rev disj_patch) acc)
72
73
74 (* ------------------------------------------------------------------------- *)
75 (* THE COMBINER *)
76
77 let patch_combiner =
78 let bind (x, m1) (y, m2) = (x || y, IntMap.fold IntMap.add m1 m2) in
79 let option_default = (false, IntMap.empty) in
80
81 (* apply the passed function, do nothing else *)
82 let donothing r k e = k e in
83
84 let meta_mcode = mcode in
85 let string_mcode = mcode in
86 let const_mcode = mcode in
87 let simpleAssign_mcode = mcode in
88 let opAssign_mcode = mcode in
89 let fix_mcode = mcode in
90 let unary_mcode = mcode in
91 let arithOp_mcode = mcode in
92 let logicalOp_mcode = mcode in
93 let cv_mcode = mcode in
94 let sign_mcode = mcode in
95 let struct_mcode = mcode in
96 let storage_mcode = mcode in
97 let inc_mcode = mcode in
98
99 let dotsexprfn = donothing in
100 let dotsinitfn = donothing in
101 let dotsparamfn = donothing in
102 let dotstemplateparamfn = donothing in
103 let dotsdeclfn = donothing in
104 let dotsfieldfn = donothing in
105 let dotsenumdeclfn = donothing in
106 let dotscasefn = donothing in
107 let dotsdefparfn = donothing in
108 let assignOpfn = donothing in
109 let binaryOpfn = donothing in
110 let initfn = donothing in
111 let enumdeclfn = donothing in
112 let paramfn = donothing in
113 let templateparamfn = donothing in
114 let forinfofn = donothing in
115 let string_fragmentfn = donothing in
116 let attributefn = donothing in
117 let attr_argfn = donothing in
118 let topfn = donothing in
119 let dotsstmtfn = donothing in
120
121 let identfn c fn v =
122 match Ast0.unwrap v with
123 | Ast0.DisjId(lp, idlist, pipelist, rp) ->
124 handle_disj lp rp pipelist idlist c.VT0.combiner_rec_ident
125 | _ -> fn v in
126
127 let exprfn c fn v =
128 match Ast0.unwrap v with
129 | Ast0.DisjExpr(lp, exprlist, pipelist, rp) ->
130 handle_disj lp rp pipelist exprlist c.VT0.combiner_rec_expression
131 | _ -> fn v in
132
133 let tyfn c fn v =
134 match Ast0.unwrap v with
135 | Ast0.DisjType(lp, tylist, pipelist, rp) ->
136 handle_disj lp rp pipelist tylist c.VT0.combiner_rec_typeC
137 | _ -> fn v in
138
139 let declfn c fn v =
140 match Ast0.unwrap v with
141 | Ast0.DisjDecl(lp, decllist, pipelist, rp) ->
142 handle_disj lp rp pipelist decllist c.VT0.combiner_rec_declaration
143 | _ -> fn v in
144
145 let fieldfn c fn v =
146 match Ast0.unwrap v with
147 | Ast0.DisjField(lp, decllist, pipelist, rp) ->
148 handle_disj lp rp pipelist decllist c.VT0.combiner_rec_field
149 | _ -> fn v in
150
151 let casefn c fn v =
152 match Ast0.unwrap v with
153 | Ast0.DisjCase(lp, caselist, pipelist, rp) ->
154 handle_disj lp rp pipelist caselist c.VT0.combiner_rec_case_line
155 | _ -> fn v in
156
157 let stmtfn c fn v =
158 match Ast0.unwrap v with
159 | Ast0.Disj(lp, sdlist, pipelist, rp) ->
160 handle_disj lp rp pipelist sdlist c.VT0.combiner_rec_statement_dots
161 | _ -> fn v in
162
163 V0.combiner_default bind option_default
164 ~meta_mcode:meta_mcode ~string_mcode:string_mcode ~const_mcode:const_mcode
165 ~simpleAssign_mcode:simpleAssign_mcode ~opAssign_mcode:opAssign_mcode
166 ~fixOp_mcode:fix_mcode ~unaryOp_mcode:unary_mcode ~arithOp_mcode:arithOp_mcode
167 ~logicalOp_mcode:logicalOp_mcode ~cv_mcode:cv_mcode ~sign_mcode:sign_mcode
168 ~struct_mcode:struct_mcode ~storage_mcode:storage_mcode ~inc_mcode:inc_mcode
169 ~dotsexpr:dotsexprfn ~dotsinit:dotsinitfn ~dotsparam:dotsparamfn
170 ~dotstemplateparam:dotstemplateparamfn ~dotsstmt:dotsstmtfn ~dotsdecl:dotsdeclfn
171 ~dotsfield:dotsfieldfn ~dotsenumdecl:dotsenumdeclfn ~dotscase:dotscasefn
172 ~dotsdefpar:dotsdefparfn ~ident:identfn ~expr:exprfn ~assignOp:assignOpfn
173 ~binaryOp:binaryOpfn ~ty:tyfn ~init:initfn ~param:paramfn ~template_param:templateparamfn
174 ~decl:declfn ~field:fieldfn ~enumdecl:enumdeclfn ~stmt:stmtfn ~forinfo:forinfofn
175 ~case:casefn ~string_fragment:string_fragmentfn ~attribute:attributefn
176 ~attr_arg:attr_argfn ~top:topfn ()
177
178
179 (* ------------------------------------------------------------------------- *)
180 (* ENTRY POINT *)
181
182 (* (true if contains */+/-, disjunction map) *)
183 type t = bool * bool list Common.IntMap.t
184
185 let make = function
186 | Ast0.InitialScriptRule _
187 | Ast0.FinalScriptRule _
188 | Ast0.ScriptRule _ -> (false, IntMap.empty)
189 | Ast0.CocciRule ((minus,_,_),(plus,_),_,_) ->
190 let handle_toplvl (is_patch, disj) tpl =
191 let (r1, r2) = patch_combiner.VT0.combiner_rec_top_level tpl in
192 (is_patch || r1, merge disj r2) in
193 let rule = List.fold_left handle_toplvl (false, IntMap.empty) in
194 let (p1,p2) = rule plus in
195 let (m1,m2) = rule minus in
196 (p1 || m1, merge p2 m2)
197
198 let make_statement_dots s = patch_combiner.VT0.combiner_rec_statement_dots s
199
200 let is_patch (t,_) = t
201
202 let get_disj_patch index (_,t) =
203 try
204 IntMap.find index t
205 with Not_found ->
206 let i = string_of_int index in
207 failwith ("detect_patch: Could not find disjunction starting on line " ^ i)
208
209 let filter_patch_rules =
210 let rec get fn = function
211 | x::xs ->
212 let ((is_patch, disj_map) as dp) = make x in
213 if is_patch
214 then get (fun a -> fn ((x, dp)::a)) xs
215 else get fn xs
216 | [] -> fn [] in
217 get (fun x -> x)