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