2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
25 (* Reorganize the top level of a rule to be a list of either top-level
26 declarations or code dots. A function declaration is always considered top
27 level. A statement is always considered code dots. A variable declaration
28 is ambiguous. We use the heuristic that if there are code dots somewhere
29 else, then the variable declaration is at top level, otherwise it applies
30 both at top level and to all code. *)
32 (* This is assumed to be done before compute_lines, and thus the info on a
33 complex term is assumed to be Ast0.default_info *)
35 module Ast0
= Ast0_cocci
39 match Ast0.unwrap x
with Ast0.Circles
(_
) -> true | _
-> false in
41 match Ast0.unwrap x
with Ast0.Stars
(_
) -> true | _
-> false in
42 if List.exists
circle l
43 then Ast0.wrap
(Ast0.CIRCLES
(l
))
44 else if List.exists
star l
45 then Ast0.wrap
(Ast0.STARS
(l
))
46 else Ast0.wrap
(Ast0.DOTS
(l
))
49 let statements = ref false in
50 let rec loop = function
53 (match Ast0.unwrap x
with
55 (match Ast0.unwrap code
with
57 let (front
,rest
) = loop xs
in
61 let (front
,rest
) = loop xs
in
68 then ([Ast0.wrap
(Ast0.CODE
(top_dots code
))],rest
)
72 match Ast0.unwrap d
with
73 Ast0.Decl
(bef
,x
) -> Ast0.wrap
(Ast0.DECL
(d
))
74 | _
-> failwith
"impossible")
78 let rec scan_top_decl = function
80 | ((topdecl
::rest
) as all
) ->
81 (match Ast0.unwrap topdecl
with
82 Ast0.OTHER
(_
) -> ([],all
)
83 | _
-> let (front
,rest
) = scan_top_decl rest
in (topdecl
::front
,rest
))
87 match Ast0.unwrap l
with
88 Ast0.DECL
(_
) -> "decl"
89 | Ast0.CODE
(_
) -> "code"
90 | Ast0.FILEINFO
(_
,_
) -> "fileinfo"
91 | Ast0.ERRORWORDS
(_
) -> "errorwords"
92 | Ast0.OTHER
(_
) -> "other"
95 match scan_code l
with
98 (match scan_top_decl rest
with
99 (top_decls
,[]) -> code
@top_decls
100 | (top_decls
,rest
) -> code
@ top_decls
@ (top_level rest
))