Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_cocci / top_level.ml
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
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.
9 *
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.
13 *
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.
18 *
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/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
34e49164
C
27(* Reorganize the top level of a rule to be a list of either top-level
28declarations or code dots. A function declaration is always considered top
29level. A statement is always considered code dots. A variable declaration
30is ambiguous. We use the heuristic that if there are code dots somewhere
31else, then the variable declaration is at top level, otherwise it applies
32both at top level and to all code. *)
33
34(* This is assumed to be done before compute_lines, and thus the info on a
35complex term is assumed to be Ast0.default_info *)
36
37module Ast0 = Ast0_cocci
38
39let top_dots l =
40 let circle x =
41 match Ast0.unwrap x with Ast0.Circles(_) -> true | _ -> false in
42 let star x =
43 match Ast0.unwrap x with Ast0.Stars(_) -> true | _ -> false in
44 if List.exists circle l
45 then Ast0.wrap (Ast0.CIRCLES(l))
46 else if List.exists star l
47 then Ast0.wrap (Ast0.STARS(l))
48 else Ast0.wrap (Ast0.DOTS(l))
49
65038c61
C
50let rec is_decl s =
51 match Ast0.unwrap s with
52 Ast0.Decl(_,e) -> true
53 | _ -> false
54
55let isonly f l = match Ast0.undots l with [s] -> f s | _ -> false
56let isall f l = List.for_all (isonly f) l
57
58let rec is_toplevel s =
59 match Ast0.unwrap s with
60 Ast0.Decl(_,e) -> true
61 | Ast0.FunDecl(_,_,_,_,_,_,_,_,_) -> true
62 | Ast0.Disj(_,stmts,_,_) -> isall is_toplevel stmts
63 | Ast0.ExprStatement(Some fc,_) -> false
64 | Ast0.Include(_,_) -> true
65 | Ast0.Undef(_,_) -> true
66 | Ast0.Define(_,_,_,_) -> true
67 | _ -> false
68
69let scan_code must_be_code l =
34e49164
C
70 let rec loop = function
71 [] -> ([],[])
72 | (x::xs) as all ->
65038c61
C
73 (match Ast0.unwrap x with
74 (Ast0.OTHER(code)) ->
75 let (front,rest) = loop xs in
76 (code::front,rest)
77 | _ -> ([],all)) in
34e49164
C
78 match loop l with
79 ([],_) as res -> res
80 | (code,rest) ->
65038c61
C
81 (match code with
82 | [x] when is_decl x && must_be_code ->
83 ([Ast0.wrap(Ast0.NONDECL x)],rest)
84 | _ when List.for_all is_toplevel code ->
85 ([Ast0.wrap(Ast0.TOPCODE(top_dots code))],rest)
86 | _ ->
87 ([Ast0.wrap(Ast0.CODE(top_dots code))],rest))
34e49164
C
88
89let rec scan_top_decl = function
90 [] -> ([],[])
91 | ((topdecl::rest) as all) ->
92 (match Ast0.unwrap topdecl with
93 Ast0.OTHER(_) -> ([],all)
65038c61
C
94 | _ ->
95 let (front,rest) = scan_top_decl rest
96 in (topdecl::front,rest))
34e49164
C
97
98(* for debugging *)
99let l2c l =
100 match Ast0.unwrap l with
65038c61 101 Ast0.NONDECL(_) -> "decl"
34e49164 102 | Ast0.CODE(_) -> "code"
65038c61 103 | Ast0.TOPCODE(_) -> "code"
34e49164
C
104 | Ast0.FILEINFO(_,_) -> "fileinfo"
105 | Ast0.ERRORWORDS(_) -> "errorwords"
106 | Ast0.OTHER(_) -> "other"
107
65038c61
C
108let rec top_level must_be_code l =
109 match scan_code must_be_code l with
34e49164
C
110 (code,[]) -> code
111 | (code,rest) ->
112 (match scan_top_decl rest with
113 (top_decls,[]) -> code@top_decls
65038c61
C
114 | (top_decls,rest) -> code @ top_decls @ (top_level must_be_code rest))
115
116let clean l =
117 List.map
118 (function tl ->
119 match Ast0.unwrap tl with
120 Ast0.TOPCODE x -> Ast0.rewrap tl (Ast0.CODE x)
121 | _ -> tl)
122 l