Debian Codegraph @

tools/spgen/source/rule_body.ml

raw
1 (*
2 * This file is part of Coccinelle, lincensed 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 Ast = Ast_cocci
8 module Ast0 = Ast0_cocci
9 module V0 = Visitor_ast0
10 module VT0 = Visitor_ast0_types
11 module S = Ast_tostring
12 module Snap = Snapshot
13 module PG = Position_generator
14 module DG = Disj_generator
15
16 (* ------------------------------------------------------------------------- *)
17
18 (* Generates the rule body of a context rule.
19 *
20 * Main logic for starring lines:
21 * CONTEXT ( * ): put the stars where they were in the original rule. Do not
22 * change rule layout, only add positions.
23 *
24 * PATCH (+/-): If a statement dots contain any minus transformations, put the
25 * star where the minus is. If not, put a star where a position was generated.
26 * All lines are always non-starred, unless there turns out to be
27 * an added metaposition (from the position generator) on that same line, in
28 * which case the whole line becomes star mode. A position is added if it is
29 * in Ast0.PLUS context, since natural positions are NEVER in PLUS context.
30 *
31 * Uses a (Snapshot.t -> Snapshot.t) combiner. Snapshot is a state type that
32 * contains the generated rule and state information. So each rule component
33 * gets turned into a function that modifies the state. The result is a
34 * composite function that takes an initial (presumably empty) snapshot and
35 * turns it into a snapshot that contains a full generated rule.
36 *)
37
38 (* ------------------------------------------------------------------------- *)
39 (* HELPERS *)
40
41 (* Function composition *)
42 let ( >> ) f g x = g (f x)
43
44 (* Continuously apply fn ('b -> 'a -> 'a) with lst ('b list) and start ('a). *)
45 let reduce fn lst start = List.fold_left (fun a b -> fn b a) start lst
46
47 (* print helpers for context rules (which are really just string lists) *)
48 let print_newl out = output_string out "\n"
49
50 let printfn out x =
51 List.iter (fun x -> output_string out x; print_newl out) x;
52 print_newl out
53
54
55 (* ------------------------------------------------------------------------- *)
56 (* FUNCTIONS TO HANDLE SPECIAL CASES *)
57
58 let starrify_line a = Snap.set_mode_star ~arity:a
59
60 (* metapositions are represented as lists of Ast0.anythings.
61 * PATCH MODE: if a position is in plus mode ("added"), ie. made by the
62 * position generator, it indicates an important line which should be starred.
63 * Mutual recursion because meta anythings can have metas themselves ...
64 *)
65 let rec add_positions ~context_mode lst =
66 reduce (add_pos ~context_mode) lst
67
68 and add_pos ~context_mode = function
69
70 (* these are the added/generated positions (hence the PLUS mode) *)
71 | Ast0.MetaPosTag(Ast0.MetaPos(((_,nm),arity,_,Ast0.PLUS _,_,_),_,_)) ->
72 let default = Snap.add_with_arity ("@"^nm) arity in
73 if context_mode then
74 default
75 else
76 starrify_line arity
77 >> default
78 | Ast0.MetaPosTag(Ast0.MetaPos(((_,nm),arity,_,_,p,_),_,_))
79
80 (* extracting the node is equivalent to calling Ast0.unwrap *)
81 | Ast0.ExprTag
82 {Ast0.node = Ast0.MetaExpr(((_,nm),arity,_,_,p,_),_,_,_,_,_); _}
83 | Ast0.StmtTag {Ast0.node = Ast0.MetaStmt(((_,nm),arity,_,_,p,_),_,_); _}
84 | Ast0.DeclTag {Ast0.node = Ast0.MetaDecl(((_,nm),arity,_,_,p,_),_,_); _}
85 | Ast0.IdentTag {Ast0.node = Ast0.MetaId(((_,nm),arity,_,_,p,_),_,_,_); _}
86 | Ast0.TypeCTag {Ast0.node = Ast0.MetaType(((_,nm),arity,_,_,p,_),_,_); _} ->
87 Snap.add_with_arity ("@"^nm) arity
88 >> add_positions ~context_mode !p
89 | _ -> failwith "add_pos only supported for metavariables."
90
91 (* renders the mcode as a string in the map and updates the line number.
92 * context_mode means that the stars are put where the minuses are.
93 *)
94 let mcode ~context_mode ~tostring_fn (x, a, info, mc, pos, _) =
95
96 let default ~add_star =
97 Snap.skip ~rule_line:(info.Ast0.pos_info.Ast0.line_start)
98 >> (if add_star then starrify_line a else (fun a -> a))
99 >> Snap.add info.Ast0.whitespace
100 >> Snap.add_with_arity (tostring_fn x) a
101 >> add_positions ~context_mode !pos in
102
103 match mc with
104 | Ast0.MINUS _ -> default ~add_star:context_mode
105 | Ast0.CONTEXT _ -> default ~add_star:false
106 | _ -> failwith "plus and mixed not allowed, should be the minus ast0..."
107
108 (* Handle Ast0_cocci.whenmodes. Primary purpose is to handle WhenModifiers
109 * and WhenNotTrue/False which are not parameterised in the visitor.
110 *)
111 let whencodes ~strfn ~exprfn ~notfn ~alwaysfn l =
112 let add_whens = function
113 | Ast0.WhenNot(whenmc, notequalmc, a) ->
114 strfn whenmc
115 >> strfn notequalmc
116 >> notfn a
117 | Ast0.WhenAlways(whenmc, equalmc, a) ->
118 strfn whenmc
119 >> strfn equalmc
120 >> alwaysfn a
121 | Ast0.WhenModifier(whenmc, a) ->
122 strfn whenmc
123 >> Snap.add (" " ^ (S.whenmodifier_tostring a))
124 | Ast0.WhenNotTrue(whenmc, notequalmc, expr) ->
125 strfn whenmc
126 >> strfn notequalmc
127 >> Snap.add " true"
128 >> exprfn expr
129 | Ast0.WhenNotFalse(whenmc, equalmc, expr) ->
130 strfn whenmc
131 >> strfn equalmc
132 >> Snap.add " false"
133 >> exprfn expr in
134 Snap.do_whencode (reduce add_whens l)
135
136
137 (* This is where the magic happens!
138 * Inserts stars/positions into the statements of a statement_dots.
139 * Only give positions and stars to statements if they are the first in a dots
140 * or come immediately after a nest, dots, disjunction, or metastatement.
141 *)
142 let star_dotsstmtfn ~context_mode stmtfn stmtdots =
143
144 (* inserts position into statement where structurally appropriate *)
145 let star_stmtfn stmt snp =
146 let _ = assert (not (Snap.no_gen snp)) in
147 match PG.statement_pos stmt snp with
148 | Some (stmt, snp) -> stmtfn stmt snp
149 | None -> stmtfn stmt snp in
150
151 (* returns true if the statement can potentially cover large amounts of
152 * code/requires special handling and therefore should not be starred.
153 *)
154 let do_not_star x =
155 match Ast0.unwrap x with
156 | Ast0.Nest _
157 | Ast0.Dots _
158 | Ast0.Disj _
159 | Ast0.MetaStmt _ -> true
160 | _ -> false in
161
162 (* increase line number if not in context_mode (if context_mode, we don't
163 * want to modify layout, only add positions).
164 *)
165 let inc_line = if context_mode then (fun x -> x) else Snap.inc_line in
166
167 (* puts stars and positions in statements that come after one of the cases
168 * in do_not_star. Insert newline after a do_not_star case.
169 *)
170 let rec insert_stars star_current fn =
171 let starfn = if star_current then star_stmtfn else stmtfn in
172 function
173 | [] -> fn
174 | [x] ->
175 if do_not_star x
176 then fn >> stmtfn x
177 else fn >> starfn x
178 | x::xs ->
179 if do_not_star x
180 then insert_stars true (fn >> stmtfn x >> inc_line) xs
181 else insert_stars false (fn >> starfn x) xs in
182
183 insert_stars true (fun x -> x) (Ast0.unwrap stmtdots)
184
185
186 (* ------------------------------------------------------------------------- *)
187 (* THE COMBINER *)
188
189 (* The type of the combiner is (Snapshot.t -> Snapshot.t) which enables us to
190 * pass states from token to token. We need the states to keep track of our
191 * current context and for proper line formatting.
192 * The state also contains the generated rule.
193 *)
194 let rec gen_combiner ~context_mode =
195 let bind x y = x >> y in (* do x then apply y to the result *)
196 let option_default = (fun x -> x) in
197
198 (* apply the passed function, do nothing else *)
199 let donothing r k e = k e in
200
201 let mcode a = mcode ~context_mode ~tostring_fn:a in
202 let meta_mcode = mcode S.meta_tostring in
203 let string_mcode = mcode (fun x -> x) in
204 let const_mcode = mcode S.constant_tostring in
205 let simpleAssign_mcode = mcode (fun x -> x) in
206 let opAssign_mcode = mcode S.arith_tostring in
207 let fix_mcode = mcode S.fix_tostring in
208 let unary_mcode = mcode S.unary_tostring in
209 let arithOp_mcode = mcode S.arith_tostring in
210 let logicalOp_mcode = mcode S.logic_tostring in
211 let cv_mcode = mcode S.const_vol_tostring in
212 let sign_mcode = mcode S.sign_tostring in
213 let struct_mcode = mcode S.struct_union_tostring in
214 let storage_mcode = mcode S.storage_tostring in
215 let inc_mcode = mcode S.inc_file_tostring in
216
217 let dotsexprfn = donothing in
218 let dotsinitfn = donothing in
219 let dotsparamfn = donothing in
220 let dotstemplateparamfn = donothing in
221 let dotsdeclfn = donothing in
222 let dotsfieldfn = donothing in
223 let dotsenumdeclfn = donothing in
224 let dotscasefn = donothing in
225 let dotsdefparfn = donothing in
226 let assignOpfn = donothing in
227 let binaryOpfn = donothing in
228 let tyfn = donothing in
229 let initfn = donothing in
230 let enumdeclfn = donothing in
231 let paramfn = donothing in
232 let templateparamfn = donothing in
233 let forinfofn = donothing in
234 let casefn = donothing in
235 let string_fragmentfn = donothing in
236 let attributefn = donothing in
237 let attr_argfn = donothing in
238
239 (* Universal special cases, regardless of no_gen mode:
240 * Disjunctions with SmPL style pattern-matching may need to be split into
241 * two rules.
242 *)
243 let identfn _ c_identfn ident =
244 match Ast0.unwrap ident with
245 | Ast0.DisjId _ ->
246 DG.generate_ident
247 ~strfn:string_mcode ~identfn:c_identfn ~ident ~at_top:false
248 | _ -> c_identfn ident in
249
250 let exprfn _ c_exprfn expr =
251 match Ast0.unwrap expr with
252 | Ast0.DisjExpr _ ->
253 DG.generate_expression
254 ~strfn:string_mcode ~exprfn:c_exprfn ~expr ~at_top:false
255 | _ -> c_exprfn expr in
256
257 let declfn _ c_declfn decl =
258 match Ast0.unwrap decl with
259 | Ast0.DisjDecl _ ->
260 DG.generate_declaration
261 ~strfn:string_mcode ~declfn:c_declfn ~decl ~at_top:false
262 | _ -> c_declfn decl in
263
264 let fieldfn _ c_fieldfn field =
265 match Ast0.unwrap field with
266 | Ast0.DisjField _ ->
267 DG.generate_field
268 ~strfn:string_mcode ~fieldfn:c_fieldfn ~field ~at_top:false
269 | _ -> c_fieldfn field in
270
271 let stmtfn combiner c_stmtfn stmt =
272 let c_dotsstmtfn = combiner.VT0.combiner_rec_statement_dots in
273 let c_substmtfn = combiner.VT0.combiner_rec_statement in
274 let c_exprfn = combiner.VT0.combiner_rec_expression in
275 let whncodes = whencodes
276 ~strfn:string_mcode ~exprfn:c_exprfn ~notfn:c_dotsstmtfn
277 ~alwaysfn:c_substmtfn in
278 let inc_star = if context_mode then (fun x -> x) else Snap.inc_star in
279
280 match Ast0.unwrap stmt with
281
282 (* nest and dots are explicitly written out rather than
283 * letting the visitor handle them. Otherwise whencodes would be ignored.
284 * (whencodes are difficult to parameterise in the visitor due to typing).
285 *
286 * nest, dots, and metastatements can represent code slices
287 * of arbitrary length and should therefore not be starred, so if their
288 * current line is starred, put them on a new line (inc_star).
289 *)
290 | Ast0.Nest(starter,stmt_dots,ender,whn,multi) ->
291 inc_star
292 >> string_mcode starter
293 >> whncodes whn
294 >> c_dotsstmtfn stmt_dots
295 >> string_mcode ender
296
297 | Ast0.Dots(dots,whn) ->
298 inc_star
299 >> string_mcode dots
300 >> whncodes whn
301
302 | Ast0.MetaStmt _ ->
303 inc_star
304 >> c_stmtfn stmt
305
306 | Ast0.Disj _ ->
307 DG.generate_statement
308 ~stmtdotsfn:c_dotsstmtfn ~strfn:string_mcode ~stmtfn:c_substmtfn
309 ~stmt ~at_top:false
310 | _ -> c_stmtfn stmt in
311
312 (* positions and stars are added here!!! *)
313 let dotsstmtfn c c_dotsstmtfn dotsstmt =
314 let stmtfn = c.VT0.combiner_rec_statement in
315 (fun snp ->
316 if Snap.no_gen snp (* add no positions; this is relevant in whencodes *)
317 then c_dotsstmtfn dotsstmt snp
318 else star_dotsstmtfn ~context_mode stmtfn dotsstmt snp) in
319
320 (* detect if disj is the only thing, in which case we don't want to split
321 * the disjunction rule.
322 * TODO: better detection of when to set at_top! for example, should not
323 * split if the only other stmts are unstarrable.
324 * (this includes the case where no statement outside the disjunction has a
325 * minus, since the pos generator will ignore them due to minus inside disj!)
326 *)
327 let topfn c c_topfn top =
328 match Ast0.unwrap top with
329 | Ast0.CODE stmtdots ->
330 (* detects if any of the statements in here contain minuses in which
331 * case we put the stars where the minuses are.
332 * NOTE: uses only minus rule, so does not detect plus slices. This is
333 * exactly what we want to happen as plus slices are not in generated
334 * rule!
335 *)
336 let detect_patch = Detect_patch.make_statement_dots stmtdots in
337 let has_minuses = Detect_patch.is_patch detect_patch in
338 let c =
339 if context_mode
340 then c
341 else gen_combiner ~context_mode:has_minuses in
342 (match Ast0.unwrap stmtdots with
343 | [{Ast0.node = Ast0.Disj _; _} as x] ->
344 DG.generate_statement
345 ~stmtdotsfn:c.VT0.combiner_rec_statement_dots
346 ~stmtfn:c.VT0.combiner_rec_statement
347 ~strfn:string_mcode ~stmt:x ~at_top:true
348 | _ -> c.VT0.combiner_rec_statement_dots stmtdots
349 )
350 | _ -> c_topfn top
351 in
352
353 V0.combiner_default bind option_default
354 ~meta_mcode:meta_mcode ~string_mcode:string_mcode ~const_mcode:const_mcode
355 ~simpleAssign_mcode:simpleAssign_mcode ~opAssign_mcode:opAssign_mcode
356 ~fixOp_mcode:fix_mcode ~unaryOp_mcode:unary_mcode ~arithOp_mcode:arithOp_mcode
357 ~logicalOp_mcode:logicalOp_mcode ~cv_mcode:cv_mcode ~sign_mcode:sign_mcode
358 ~struct_mcode:struct_mcode ~storage_mcode:storage_mcode ~inc_mcode:inc_mcode
359 ~dotsexpr:dotsexprfn ~dotsinit:dotsinitfn ~dotsparam:dotsparamfn
360 ~dotstemplateparam:dotstemplateparamfn ~dotsstmt:dotsstmtfn ~dotsdecl:dotsdeclfn
361 ~dotsfield:dotsfieldfn ~dotsenumdecl:dotsenumdeclfn ~dotscase:dotscasefn
362 ~dotsdefpar:dotsdefparfn ~ident:identfn ~expr:exprfn ~assignOp:assignOpfn
363 ~binaryOp:binaryOpfn ~ty:tyfn ~init:initfn ~param:paramfn
364 ~template_param:templateparamfn ~decl:declfn ~field:fieldfn
365 ~enumdecl:enumdeclfn ~stmt:stmtfn ~forinfo:forinfofn ~case:casefn
366 ~string_fragment:string_fragmentfn ~attribute:attributefn ~attr_arg:attr_argfn
367 ~top:topfn ()
368
369 (* ------------------------------------------------------------------------- *)
370 (* ENTRY POINT *)
371
372 type t = string list
373
374 (* Creates a context mode rule for the input rule.
375 * Returns list of added metapositions and the new rule.
376 *)
377 let generate ~context_mode ~disj_map ~minus_rule =
378 let snp = Snap.make ~disj_map in
379 let combiner = gen_combiner ~context_mode in
380 let final = reduce combiner.VT0.combiner_rec_top_level minus_rule snp in
381 (Snap.get_positions final, Snap.get_result final)
382
383 let print = printfn