Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_cocci / plus.ml
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
34e49164
C
27(* The plus fragments are converted to a list of lists of lists.
28Innermost list: Elements have type anything. For any pair of successive
29elements, n and n+1, the ending line of n is the same as the starting line
30of n+1.
31Middle lists: For any pair of successive elements, n and n+1, the ending
32line of n is one less than the starting line of n+1.
33Outer list: For any pair of successive elements, n and n+1, the ending
34line of n is more than one less than the starting line of n+1. *)
35
36(* For nests and disjs, we are relying on the fact that <... ...> ( | )
37must appear on lines by themselves, meaning that the various + fragments
38can't be contiguous to each other or to unrelated things. *)
39
40module Ast = Ast_cocci
41module V = Visitor_ast
42
43(* --------------------------------------------------------------------- *)
44
45type res =
46 Open of Ast.anything * int * int * int * int
47 | Closed of (Ast.anything * int * int * int * int) list
48
49let mcode fn = function
50 (term, Ast.PLUS(info)) ->
51 let line = info.Ast.line in
52 let lline = info.Ast.logical_line in
53 [Open (fn term,line,line,lline,lline)]
54 | _ -> [Closed []]
55
56let mk_fullType x = Ast.FullTypeTag x
57let mk_baseType x = Ast.BaseTypeTag x
58let mk_structUnion x = Ast.StructUnionTag x
59let mk_sign x = Ast.SignTag x
60let mk_ident x = Ast.IdentTag x
61let mk_expression x = Ast.ExpressionTag x
62let mk_constant x = Ast.ConstantTag x
63let mk_unaryOp x = Ast.UnaryOpTag x
64let mk_assignOp x = Ast.AssignOpTag x
65let mk_fixOp x = Ast.FixOpTag x
66let mk_binaryOp x = Ast.BinaryOpTag x
67let mk_arithOp x = Ast.ArithOpTag x
68let mk_logicalOp x = Ast.LogicalOpTag x
69let mk_declaration x = Ast.DeclarationTag x
70let mk_storage x = Ast.StorageTag x
71let mk_rule_elem x = Ast.Rule_elemTag x
72let mk_const_vol x = Ast.ConstVolTag x
73let mk_token x = Ast.Token x
74
75let get_real_start = function
76 Open (_,line,_,_,_) -> line
77 | _ -> failwith "not possible"
78
79let get_real_finish = function
80 Open (_,_,line,_,_) -> line
81 | _ -> failwith "not possible"
82
83let get_start = function
84 Open (_,_,_,line,_) -> line
85 | _ -> failwith "not possible"
86
87let get_finish = function
88 Open (_,_,_,_,line) -> line
89 | _ -> failwith "not possible"
90
91let get_option fn = function
92 None -> []
93 | Some x -> [fn x]
94
95(* --------------------------------------------------------------------- *)
96(* --------------------------------------------------------------------- *)
97(* Step 1: coalesce + terms, record starting and ending line numbers *)
98
99let rec close l =
100 let rec loop = function
101 [] -> []
102 | Open(x,start,finish,lstart,lfinish)::rest ->
103 (x,start,finish,lstart,lfinish)::(loop rest)
104 | (Closed l)::rest -> l @ (loop rest) in
105 Closed (loop l)
106
107let test term subterms =
108 if List.for_all (function Open(_,_,_,_,_) -> true | _ -> false) subterms
109 then [Open(term,
110 get_real_start (List.hd subterms),
111 get_real_finish (List.hd (List.rev subterms)),
112 get_start (List.hd subterms),
113 get_finish (List.hd (List.rev subterms)))]
114 else [close subterms]
115
116(* --------------------------------------------------------------------- *)
117(* Dots *)
118
119let dots recursor k dotlist = [close (k dotlist)]
120
121(* --------------------------------------------------------------------- *)
122(* Identifier *)
123
124let ident recursor k i = test (Ast.IdentTag i) (k i)
125
126(* --------------------------------------------------------------------- *)
127(* Expression *)
128
129let expression recursor k = function
130 Ast.DisjExpr(exps) ->
131 [close (List.concat(List.map recursor.V.combiner_expression exps))]
132 | Ast.Edots(_,_) -> [Closed []] (* must be context *)
133 | Ast.Ecircles(_,_) -> [Closed []] (* must be context *)
134 | Ast.Estars(_,_) -> [Closed []] (* must be context *)
135 | Ast.OptExp(_) | Ast.UniqueExp(_) | Ast.MultiExp(_) -> failwith "impossible"
136 | e -> test (Ast.ExpressionTag e) (k e)
137
138(* --------------------------------------------------------------------- *)
139(* Types *)
140
141and fullType recursor k ft = test (Ast.FullTypeTag ft) (k ft)
142
143and typeC recursor k t = k t
144
145(* --------------------------------------------------------------------- *)
146(* Variable declaration *)
147(* Even if the Cocci program specifies a list of declarations, they are
148 split out into multiple declarations of a single variable each. *)
149
150let declaration recursor k d = test (Ast.DeclarationTag d) (k d)
151
152(* --------------------------------------------------------------------- *)
153(* Parameter *)
154
155let parameterTypeDef recursor k = function
156 Ast.Pdots(_) -> [Closed []]
157 | Ast.Pcircles(_) -> [Closed []]
158 | p -> test (Ast.ParameterTypeDefTag p) (k p)
159
160(* --------------------------------------------------------------------- *)
161(* Top-level code *)
162
163let rec rule_elem recursor k re = test (Ast.Rule_elemTag re) (k re)
164
165let rec statement recursor k = function
166 Ast.Disj(stmt_dots_list) ->
167 [close
168 (List.concat
169 (List.map recursor.V.combiner_statement_dots stmt_dots_list))]
170 | Ast.Dots(_,_,_) -> [Closed []]
171 | Ast.Circles(_,_,_) -> [Closed []]
172 | Ast.Stars(_,_,_) -> [Closed []]
173 | s -> test (Ast.StatementTag s) (k s)
174
175let rec meta recursor k m = test (Ast.MetaTag m) (k m)
176
177let top_level recursor k = function
178 Ast.FILEINFO(_,_) -> [Closed []]
179 | Ast.ERRORWORDS(exps) -> [Closed []]
180 | t -> test (Ast.Code t) (k t)
181
182let anything recursor k a = failwith "not called"
183
184let collect_tokens =
185 let recursor =
186 V.combiner (@) []
187 (mcode mk_token) (mcode mk_constant) (mcode mk_assignOp) (mcode mk_fixOp)
188 (mcode mk_unaryOp) (mcode mk_binaryOp) (mcode mk_const_vol)
189 (mcode mk_baseType) (mcode mk_sign) (mcode mk_structUnion)
190 (mcode mk_storage) dots dots dots
191 ident expression fullType typeC parameterTypeDef declaration
192 rule_elem statement meta top_level anything in
193 recursor.V.combiner_top_level
194
195let rule code = List.concat(List.map collect_tokens code)
196
197(* --------------------------------------------------------------------- *)
198(* --------------------------------------------------------------------- *)
199(* Step 2: find neighbors *)
200
201let rec find_neighbors = function
202 [] -> []
203 | (x1,real_start1,real_finish1,start1,finish1)::rest ->
204 (match find_neighbors rest with
205 ((((x2,real_start2,real_finish2,start2,finish2)::
206 rest_inner)::rest_middle)::rest_outer)
207 as rest ->
208 if finish1 = start2
faf9a90c 209 then
34e49164
C
210 ((((x1,real_start1,real_finish1,start1,finish1)::
211 (x2,real_start2,real_finish2,start2,finish2)::rest_inner)::
212 rest_middle)::
213 rest_outer)
214 else if finish1 + 1 = start2
215 then
216 (([(x1,real_start1,real_finish1,start1,finish1)]::
217 ((x2,real_start2,real_finish2,start2,finish2)::rest_inner)::
218 rest_middle)::
219 rest_outer)
220 else [[(x1,real_start1,real_finish1,start1,finish1)]]::rest
221 | _ -> [[[(x1,real_start1,real_finish1,start1,finish1)]]])
222 (* rest must be [] *)
faf9a90c 223
34e49164
C
224(* --------------------------------------------------------------------- *)
225(* --------------------------------------------------------------------- *)
226(* Entry point *)
227
228let plus ast =
229 match close (rule ast) with
230 Closed l -> find_neighbors l
231 | _ -> failwith "impossible"