1 (* Reorganize the top level of a rule to be a list of either top-level
2 declarations or code dots. A function declaration is always considered top
3 level. A statement is always considered code dots. A variable declaration
4 is ambiguous. We use the heuristic that if there are code dots somewhere
5 else, then the variable declaration is at top level, otherwise it applies
6 both at top level and to all code. *)
8 (* This is assumed to be done before compute_lines, and thus the info on a
9 complex term is assumed to be Ast0.default_info *)
11 module Ast0
= Ast0_cocci
15 match Ast0.unwrap x
with Ast0.Circles
(_
) -> true | _
-> false in
17 match Ast0.unwrap x
with Ast0.Stars
(_
) -> true | _
-> false in
18 if List.exists
circle l
19 then Ast0.wrap
(Ast0.CIRCLES
(l
))
20 else if List.exists
star l
21 then Ast0.wrap
(Ast0.STARS
(l
))
22 else Ast0.wrap
(Ast0.DOTS
(l
))
25 let statements = ref false in
26 let rec loop = function
29 (match Ast0.unwrap x
with
31 (match Ast0.unwrap code
with
33 let (front
,rest
) = loop xs
in
37 let (front
,rest
) = loop xs
in
44 then ([Ast0.wrap
(Ast0.CODE
(top_dots code
))],rest
)
48 match Ast0.unwrap d
with
49 Ast0.Decl
(bef
,x
) -> Ast0.wrap
(Ast0.DECL
(d
))
50 | _
-> failwith
"impossible")
54 let rec scan_top_decl = function
56 | ((topdecl
::rest
) as all
) ->
57 (match Ast0.unwrap topdecl
with
58 Ast0.OTHER
(_
) -> ([],all
)
59 | _
-> let (front
,rest
) = scan_top_decl rest
in (topdecl
::front
,rest
))
63 match Ast0.unwrap l
with
64 Ast0.DECL
(_
) -> "decl"
65 | Ast0.CODE
(_
) -> "code"
66 | Ast0.FILEINFO
(_
,_
) -> "fileinfo"
67 | Ast0.ERRORWORDS
(_
) -> "errorwords"
68 | Ast0.OTHER
(_
) -> "other"
71 match scan_code l
with
74 (match scan_top_decl rest
with
75 (top_decls
,[]) -> code
@top_decls
76 | (top_decls
,rest
) -> code
@ top_decls
@ (top_level rest
))