2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 (* The plus fragments are converted to a list of lists of lists.
28 Innermost list: Elements have type anything. For any pair of successive
29 elements, n and n+1, the ending line of n is the same as the starting line
31 Middle lists: For any pair of successive elements, n and n+1, the ending
32 line of n is one less than the starting line of n+1.
33 Outer list: For any pair of successive elements, n and n+1, the ending
34 line of n is more than one less than the starting line of n+1. *)
36 (* For nests and disjs, we are relying on the fact that <... ...> ( | )
37 must appear on lines by themselves, meaning that the various + fragments
38 can't be contiguous to each other or to unrelated things. *)
40 module Ast
= Ast_cocci
41 module V
= Visitor_ast
43 (* --------------------------------------------------------------------- *)
46 Open
of Ast.anything
* int * int * int * int
47 | Closed
of (Ast.anything
* int * int * int * int) list
49 let 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)]
56 let mk_fullType x
= Ast.FullTypeTag x
57 let mk_baseType x
= Ast.BaseTypeTag x
58 let mk_structUnion x
= Ast.StructUnionTag x
59 let mk_sign x
= Ast.SignTag x
60 let mk_ident x
= Ast.IdentTag x
61 let mk_expression x
= Ast.ExpressionTag x
62 let mk_constant x
= Ast.ConstantTag x
63 let mk_unaryOp x
= Ast.UnaryOpTag x
64 let mk_assignOp x
= Ast.AssignOpTag x
65 let mk_fixOp x
= Ast.FixOpTag x
66 let mk_binaryOp x
= Ast.BinaryOpTag x
67 let mk_arithOp x
= Ast.ArithOpTag x
68 let mk_logicalOp x
= Ast.LogicalOpTag x
69 let mk_declaration x
= Ast.DeclarationTag x
70 let mk_storage x
= Ast.StorageTag x
71 let mk_rule_elem x
= Ast.Rule_elemTag x
72 let mk_const_vol x
= Ast.ConstVolTag x
73 let mk_token x
= Ast.Token x
75 let get_real_start = function
76 Open
(_
,line,_
,_
,_
) -> line
77 | _
-> failwith
"not possible"
79 let get_real_finish = function
80 Open
(_
,_
,line,_
,_
) -> line
81 | _
-> failwith
"not possible"
83 let get_start = function
84 Open
(_
,_
,_
,line,_
) -> line
85 | _
-> failwith
"not possible"
87 let get_finish = function
88 Open
(_
,_
,_
,_
,line) -> line
89 | _
-> failwith
"not possible"
91 let get_option fn
= function
95 (* --------------------------------------------------------------------- *)
96 (* --------------------------------------------------------------------- *)
97 (* Step 1: coalesce + terms, record starting and ending line numbers *)
100 let rec loop = function
102 | Open
(x
,start
,finish
,lstart
,lfinish
)::rest
->
103 (x
,start
,finish
,lstart
,lfinish
)::(loop rest
)
104 | (Closed l
)::rest
-> l
@ (loop rest
) in
107 let test term subterms
=
108 if List.for_all
(function Open
(_
,_
,_
,_
,_
) -> true | _
-> false) subterms
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
]
116 (* --------------------------------------------------------------------- *)
119 let dots recursor k dotlist
= [close (k dotlist
)]
121 (* --------------------------------------------------------------------- *)
124 let ident recursor k i
= test (Ast.IdentTag i
) (k i
)
126 (* --------------------------------------------------------------------- *)
129 let 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
)
138 (* --------------------------------------------------------------------- *)
141 and fullType recursor k ft
= test (Ast.FullTypeTag ft
) (k ft
)
143 and typeC recursor k t
= k t
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. *)
150 let declaration recursor k d
= test (Ast.DeclarationTag d
) (k d
)
152 (* --------------------------------------------------------------------- *)
155 let parameterTypeDef recursor k
= function
156 Ast.Pdots
(_
) -> [Closed
[]]
157 | Ast.Pcircles
(_
) -> [Closed
[]]
158 | p
-> test (Ast.ParameterTypeDefTag p
) (k p
)
160 (* --------------------------------------------------------------------- *)
163 let rec rule_elem recursor k re
= test (Ast.Rule_elemTag re
) (k re
)
165 let rec statement recursor k
= function
166 Ast.Disj
(stmt_dots_list
) ->
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
)
175 let rec meta recursor k m
= test (Ast.MetaTag m
) (k m
)
177 let top_level recursor k
= function
178 Ast.FILEINFO
(_
,_
) -> [Closed
[]]
179 | Ast.ERRORWORDS
(exps
) -> [Closed
[]]
180 | t
-> test (Ast.Code t
) (k t
)
182 let anything recursor k a
= failwith
"not called"
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
195 let rule code
= List.concat
(List.map
collect_tokens code
)
197 (* --------------------------------------------------------------------- *)
198 (* --------------------------------------------------------------------- *)
199 (* Step 2: find neighbors *)
201 let rec find_neighbors = function
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
)
210 ((((x1
,real_start1
,real_finish1
,start1
,finish1
)::
211 (x2
,real_start2
,real_finish2
,start2
,finish2
)::rest_inner
)::
214 else if finish1
+ 1 = start2
216 (([(x1
,real_start1
,real_finish1
,start1
,finish1
)]::
217 ((x2
,real_start2
,real_finish2
,start2
,finish2
)::rest_inner
)::
220 else [[(x1
,real_start1
,real_finish1
,start1
,finish1
)]]::rest
221 | _
-> [[[(x1
,real_start1
,real_finish1
,start1
,finish1
)]]])
222 (* rest must be [] *)
224 (* --------------------------------------------------------------------- *)
225 (* --------------------------------------------------------------------- *)
229 match close (rule ast
) with
230 Closed l
-> find_neighbors l
231 | _
-> failwith
"impossible"