Debian Codegraph @

tools/spgen/source/snapshot.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 Ast0 = Ast0_cocci
8 module StringSet = Set.Make (String)
9 module IntMap = Common.IntMap
10
11 (* ------------------------------------------------------------------------- *)
12
13 (* Types used to represent internal state during context rule generation.
14 * We cannot pass the states from function to function in the visitor due to
15 * the function signatures already having been decided for us.
16 * Basically glorified global variables (but with limited access and scope, so
17 * not quite!).
18 *
19 * The generated rule is represented by a (mode * string) IntMap. Mapping line
20 * number to mode ( * or nothing) and the string contents.
21 *
22 * During execution, there are quite many cases dictated by the three internal
23 * flags:
24 * - disj_mode (add to disj result)
25 * - no_gen_mode (do not generate positions) ALSO TRIGGERED BY WHENCODES!
26 * - freeze_pos (don't increment position counter).
27 *
28 * An overview of the logic:
29 * State | Add to disj result| Generate position| Incr pos counter
30 * ---------------------------------------------------------------------------
31 * normal | NO | YES | YES
32 * normal whencodes | NO | NO | x
33 * disj context | NO | NO | x
34 * disj patch | YES | YES | NO
35 * disj patch whencode| YES | NO | x
36 * after special disj | YES | YES | YES
37 *
38 *)
39
40
41 (* ------------------------------------------------------------------------- *)
42 (* ARITY *)
43
44 (* Arity denotes whether a match is optional (OPT) or required (NONE).
45 * Mostly the same as Ast0_cocci, but does not allow unique matches. *)
46
47 type arity = OPT | NONE
48
49 let tostring_arity = function
50 | OPT -> "?"
51 | NONE -> ""
52
53 (* converts Ast0.arity to local arity *)
54 let to_a = function
55 | Ast0.OPT -> OPT
56 | Ast0.NONE -> NONE
57
58 (* ------------------------------------------------------------------------- *)
59 (* MODE *)
60
61 (* Mode denotes whether a line should be preprended with * or nothing. *)
62
63 type mode = Star of arity | Context of arity
64
65 let tostring_mode = function
66 | Star a -> (tostring_arity a) ^ "* "
67 | Context a -> (tostring_arity a) ^ ""
68
69 let is_star = function
70 | Star _ -> true
71 | Context _ -> false
72
73 let set_arity m a = match m, a with
74 | Context _, Some a -> Context a
75 | Star _, Some a -> Star a
76 | m, None -> m
77
78
79 (* ------------------------------------------------------------------------- *)
80 (* STATE *)
81
82 (* Wrapper for state variables. *)
83 type t =
84 {
85 result : (mode * string) IntMap.t; (* maps line number to content *)
86 current_mode : mode; (* whether current line is in context or star mode *)
87 current_line : int; (* current line number (for hashtable indexing) *)
88 rule_line : int; (* current line number in the original rule *)
89 whencode_nest : int; (* number of levels of whencode nests *)
90 pos_counter : int; (* number of added metapositions *)
91 positions : StringSet.t; (* names of added metapositions *)
92 disj_map : Detect_patch.t; (* maps line number to disj patch detect *)
93 disj_result : (mode * string) IntMap.t option; (* generated disj rule *)
94 disj_mode : bool; (* flag for adding content to disj rule *)
95 no_gen_mode : bool; (* flag for not generating positions *)
96 freeze_pos : bool * bool; (* flag for not incrementing the pos counter *)
97 (* the second part means a pos was added during *)
98 }
99
100 (* Constructor.
101 * Note: disj_map stays invariant throughout the processing of the rule ...
102 *)
103 let make ~disj_map =
104 {
105 result = IntMap.empty;
106 current_mode = Context NONE;
107 current_line = 0;
108 rule_line = 0;
109 whencode_nest = 0;
110 pos_counter = 0;
111 positions = StringSet.empty;
112 disj_result = None;
113 disj_map;
114 disj_mode = false;
115 no_gen_mode = false;
116 freeze_pos = false, false;
117 }
118
119 (* ------------------------------------------------------------------------- *)
120 (* STATE: MODE AND LINE FUNCTIONS *)
121
122 let set_mode m snp = { snp with current_mode = m }
123 let set_mode_star ~arity = set_mode (Star (to_a arity))
124 let set_mode_context ~arity = set_mode (Context (to_a arity))
125
126 let set_rule_line l snp = { snp with rule_line = l }
127 let inc_current_line snp = { snp with current_line = snp.current_line + 1 }
128
129 (* New lines default to Context, they have to be explicitly set to Star. *)
130 let inc_line snp = inc_current_line (set_mode_context ~arity:Ast0.NONE snp)
131 let inc_star snp = if is_star snp.current_mode then inc_line snp else snp
132
133 (* if input number exceeds the current rule line number, increase the internal
134 * line number.
135 *)
136 let skip ~rule_line snp =
137 let snp = if rule_line > snp.rule_line then inc_line snp else snp in
138 set_rule_line rule_line snp
139
140
141 (* ------------------------------------------------------------------------- *)
142 (* STATE: ADDING CONTENT TO MAP FUNCTIONS *)
143
144 (* Functions for modifying the generated rule (represented as map that maps
145 * line number to mode and contents (string)).
146 *
147 * NOTE: can be changed to hashtbl if needed. Just also need to
148 * - make the record field mutable.
149 * - in get_result, need to sort by line number when extracting the lines.
150 *)
151
152 (* add the value in v to the entry that has i as key *)
153 let add_map (v : string) (i : int) (m : mode) (r : (mode * string) IntMap.t) =
154 if IntMap.mem i r then
155 let (_, cur) = IntMap.find i r in
156 IntMap.add i (m, cur ^ v) r
157 else
158 IntMap.add i (m, v) r
159
160 (* add the value in v to the current line entry, possibly changing arity. *)
161 let add_result (v : string) (a : arity option) (snp : t) =
162 let (r, i, m) =
163 (snp.result, snp.current_line, set_arity snp.current_mode a) in
164 if snp.disj_mode then begin
165 match snp.disj_result with
166 | Some d ->
167 {
168 snp with
169 result = add_map v i m r;
170 disj_result = Some (add_map v i m d)
171 }
172 | None ->
173 {
174 snp with
175 result = add_map v i m r
176 }
177 end else
178 { snp with result = add_map v i m r }
179
180 (* add to current line *)
181 let add_with_arity value arity = add_result value (Some (to_a arity))
182 let add value = add_result value None
183
184
185 (* ------------------------------------------------------------------------- *)
186 (* STATE: POSITION FUNCTIONS *)
187
188 (* generate a position and add it to the internal list.
189 * return the name of the new position and the modified snapshot.
190 *)
191 let add_position snp =
192 let pos_name = Globals.get_pos_name() in
193 let newpos = pos_name ^ (string_of_int snp.pos_counter) in
194 let newsnp =
195 if fst snp.freeze_pos
196 then
197 {
198 snp with
199 positions = StringSet.add newpos snp.positions;
200 freeze_pos = (true, true) (* set dirty bit, because adding position *)
201 }
202 else
203 {
204 snp with
205 pos_counter = snp.pos_counter + 1;
206 positions = StringSet.add newpos snp.positions;
207 } in
208 (newpos, newsnp)
209
210 (* set the freeze position flag to b.
211 * if we're ending a freeze period AND one position was added during this time
212 * (aka the dirty flag), then we need to increment the counter.
213 * TODO: nested freezes in e.g. nested disjunctions are VERY error-prone.
214 *)
215 let set_freeze_pos b snp =
216 let (freez,dirty) = snp.freeze_pos in
217 if freez && dirty && not(b) then
218 {
219 snp with
220 freeze_pos = (b,false);
221 pos_counter = snp.pos_counter + 1;
222 }
223 else
224 { snp with freeze_pos = (b,false) }
225
226 (* do fn (t -> t) while position incrementing is frozen.
227 * this is e.g. used in disjunctions where we want all cases to have the
228 * same position.
229 *)
230 let do_freeze_pos fn snp =
231 let (current,_) = snp.freeze_pos in
232 set_freeze_pos current (fn (set_freeze_pos true snp))
233
234
235 (* ------------------------------------------------------------------------- *)
236 (* STATE: DISJUNCTION FUNCTIONS *)
237
238 (* get the bool list for the disjunction starting at line l. *)
239 let get_disj l snp = Detect_patch.get_disj_patch l snp.disj_map
240
241 (* start generation of disjunction rule, copy the existing generated rule *)
242 let init_disj_result snp =
243 match snp.disj_result with
244 | Some s -> snp
245 | None -> { snp with disj_result = Some snp.result }
246
247 let set_disj_mode b snp = { snp with disj_mode = b }
248
249
250 (* ------------------------------------------------------------------------- *)
251 (* STATE: WHENCODES FUNCTIONS *)
252
253 (* We never want to add stars or positions to whencodes, so we keep track of
254 * whether we are currently inside a whencode (and they may be nested).
255 *)
256
257 let inc_whencode snp = { snp with whencode_nest = snp.whencode_nest + 1 }
258 let dec_whencode snp = { snp with whencode_nest = snp.whencode_nest - 1 }
259 let in_whencode snp = snp.whencode_nest <> 0
260
261 let do_whencode fn snp = dec_whencode (fn (inc_whencode snp))
262
263
264 (* ------------------------------------------------------------------------- *)
265 (* STATE: NO GEN *)
266
267 (* if no_gen flag is set, unparse the AST0 as normal, don't insert positions
268 * or forced newlines.
269 *)
270 let set_no_gen b snp = { snp with no_gen_mode = b }
271
272 (* do not add positions or newlines if ... *)
273 let no_gen snp = in_whencode snp || snp.no_gen_mode
274
275
276 (* ------------------------------------------------------------------------- *)
277 (* STATE: GETTERS *)
278
279 let get_positions snp = StringSet.elements snp.positions
280
281 (* Returns the map as a string list, sorted by key and the same for
282 * the disjunction result if any.
283 *)
284 let get_result snp =
285 let transform x no_mode =
286 List.map (fun (_,(c,s)) -> if no_mode then s else (tostring_mode c) ^ s)
287 (IntMap.bindings x) in
288 match snp.disj_result with
289 | Some d -> (transform snp.result true, Some (transform d false))
290 | None -> (transform snp.result false, None)