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 match Ast0.unwrap s
with
50 Ast0.Decl
(_
,e
) -> true
53 let isonly f l
= match Ast0.undots l
with [s
] -> f s
| _
-> false
54 let isall f l
= List.for_all
(isonly f
) l
56 let rec is_toplevel s
=
57 match Ast0.unwrap s
with
58 Ast0.Decl
(_
,e
) -> true
59 | Ast0.FunDecl
(_
,_
,_
,_
,_
,_
,_
,_
,_
) -> true
60 | Ast0.Disj
(_
,stmts
,_
,_
) -> isall is_toplevel stmts
61 | Ast0.ExprStatement
(Some fc
,_
) -> false
62 | Ast0.Include
(_
,_
) -> true
63 | Ast0.Undef
(_
,_
) -> true
64 | Ast0.Define
(_
,_
,_
,_
) -> true
67 let scan_code must_be_code l
=
68 let rec loop = function
71 (match Ast0.unwrap x
with
73 let (front
,rest
) = loop xs
in
80 | [x
] when is_decl x
&& must_be_code
->
81 ([Ast0.wrap
(Ast0.NONDECL x
)],rest
)
82 | _
when List.for_all
is_toplevel code
->
83 ([Ast0.wrap
(Ast0.TOPCODE
(top_dots code
))],rest
)
85 ([Ast0.wrap
(Ast0.CODE
(top_dots code
))],rest
))
87 let rec scan_top_decl = function
89 | ((topdecl
::rest
) as all
) ->
90 (match Ast0.unwrap topdecl
with
91 Ast0.OTHER
(_
) -> ([],all
)
93 let (front
,rest
) = scan_top_decl rest
94 in (topdecl
::front
,rest
))
98 match Ast0.unwrap l
with
99 Ast0.NONDECL
(_
) -> "decl"
100 | Ast0.CODE
(_
) -> "code"
101 | Ast0.TOPCODE
(_
) -> "code"
102 | Ast0.FILEINFO
(_
,_
) -> "fileinfo"
103 | Ast0.ERRORWORDS
(_
) -> "errorwords"
104 | Ast0.OTHER
(_
) -> "other"
106 let rec top_level must_be_code l
=
107 match scan_code must_be_code l
with
110 (match scan_top_decl rest
with
111 (top_decls
,[]) -> code
@top_decls
112 | (top_decls
,rest
) -> code
@ top_decls
@ (top_level must_be_code rest
))
117 match Ast0.unwrap tl
with
118 Ast0.TOPCODE x
-> Ast0.rewrap tl
(Ast0.CODE x
)