2 * Copyright 2005-2010, 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.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
45 (* Reorganize the top level of a rule to be a list of either top-level
46 declarations or code dots. A function declaration is always considered top
47 level. A statement is always considered code dots. A variable declaration
48 is ambiguous. We use the heuristic that if there are code dots somewhere
49 else, then the variable declaration is at top level, otherwise it applies
50 both at top level and to all code. *)
52 (* This is assumed to be done before compute_lines, and thus the info on a
53 complex term is assumed to be Ast0.default_info *)
55 module Ast0
= Ast0_cocci
59 match Ast0.unwrap x
with Ast0.Circles
(_
) -> true | _
-> false in
61 match Ast0.unwrap x
with Ast0.Stars
(_
) -> true | _
-> false in
62 if List.exists
circle l
63 then Ast0.wrap
(Ast0.CIRCLES
(l
))
64 else if List.exists
star l
65 then Ast0.wrap
(Ast0.STARS
(l
))
66 else Ast0.wrap
(Ast0.DOTS
(l
))
69 let statements = ref false in
70 let rec loop = function
73 (match Ast0.unwrap x
with
75 (match Ast0.unwrap code
with
77 let (front
,rest
) = loop xs
in
81 let (front
,rest
) = loop xs
in
88 then ([Ast0.wrap
(Ast0.CODE
(top_dots code
))],rest
)
92 match Ast0.unwrap d
with
93 Ast0.Decl
(bef
,x
) -> Ast0.wrap
(Ast0.DECL
(d
))
94 | _
-> failwith
"impossible")
98 let rec scan_top_decl = function
100 | ((topdecl
::rest
) as all
) ->
101 (match Ast0.unwrap topdecl
with
102 Ast0.OTHER
(_
) -> ([],all
)
103 | _
-> let (front
,rest
) = scan_top_decl rest
in (topdecl
::front
,rest
))
107 match Ast0.unwrap l
with
108 Ast0.DECL
(_
) -> "decl"
109 | Ast0.CODE
(_
) -> "code"
110 | Ast0.FILEINFO
(_
,_
) -> "fileinfo"
111 | Ast0.ERRORWORDS
(_
) -> "errorwords"
112 | Ast0.OTHER
(_
) -> "other"
114 let rec top_level l
=
115 match scan_code l
with
118 (match scan_top_decl rest
with
119 (top_decls
,[]) -> code
@top_decls
120 | (top_decls
,rest
) -> code
@ top_decls
@ (top_level rest
))