6dd7eed02edb22d68582e59eeefe7b7233cb465c
[bpt/coccinelle.git] / parsing_cocci / top_level.ml
1 (*
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.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
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. *)
31
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 *)
34
35 module Ast0 = Ast0_cocci
36
37 let top_dots l =
38 let circle x =
39 match Ast0.unwrap x with Ast0.Circles(_) -> true | _ -> false in
40 let star x =
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))
47
48 let scan_code l =
49 let statements = ref false in
50 let rec loop = function
51 [] -> ([],[])
52 | (x::xs) as all ->
53 (match Ast0.unwrap x with
54 (Ast0.OTHER(code)) ->
55 (match Ast0.unwrap code with
56 Ast0.Decl(_) ->
57 let (front,rest) = loop xs in
58 (code::front,rest)
59 | _ ->
60 statements := true;
61 let (front,rest) = loop xs in
62 (code::front,rest))
63 | _ -> ([],all)) in
64 match loop l with
65 ([],_) as res -> res
66 | (code,rest) ->
67 if !statements = true
68 then ([Ast0.wrap(Ast0.CODE(top_dots code))],rest)
69 else
70 (List.map
71 (function d ->
72 match Ast0.unwrap d with
73 Ast0.Decl(bef,x) -> Ast0.wrap (Ast0.DECL(d))
74 | _ -> failwith "impossible")
75 code,
76 rest)
77
78 let rec scan_top_decl = function
79 [] -> ([],[])
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))
84
85 (* for debugging *)
86 let l2c l =
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"
93
94 let rec top_level l =
95 match scan_code l with
96 (code,[]) -> code
97 | (code,rest) ->
98 (match scan_top_decl rest with
99 (top_decls,[]) -> code@top_decls
100 | (top_decls,rest) -> code @ top_decls @ (top_level rest))