File: stmtlist.ml

package info (click to toggle)
coccinelle 1.0.8.deb-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 26,148 kB
  • sloc: ml: 136,392; ansic: 23,594; sh: 2,189; makefile: 2,157; perl: 1,576; lisp: 840; python: 823; awk: 70; csh: 12
file content (66 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(*
 * This file is part of Coccinelle, licensed under the terms of the GPL v2.
 * See copyright.txt in the Coccinelle source code for more information.
 * The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
 *)

(* Goal: Move a MetaStmtList, if any, up into { and replace by ... *)
(* Only applied to - code *)

module Ast = Ast_cocci
module V = Visitor_ast

let adjust_brace_and_body lbrace body =
  let body_element =
    match Ast.unwrap body with
      [x] ->
	(match Ast.unwrap x with
	  Ast.Atomic(y) ->
	    (match Ast.unwrap y with
	      Ast.MetaStmtList(name,lenname,cstr,keep,inherited) ->
		Some(name,lenname,cstr,keep,inherited,body,x,y)
	    | _ -> None)
	| _ -> None)
    | _ -> None in
  match body_element with
    Some(name,lenname,cstr,keep,inherited,body,x,y) ->
      let newbody =
	(* dropping x *)
	Ast.rewrap y (Ast.Dots(Ast.rewrap_mcode name "...",[],[],[])) in
      let name = Ast.make_mcode(Ast.unwrap_mcode(name)) in
      let meta =
	Ast.make_term (Ast.MetaStmtList(name,lenname,cstr,keep,inherited)) in
      let body = Ast.rewrap body [newbody] in
      (Ast.make_term(Ast.AsRe(lbrace,meta)),body)
  | None -> (lbrace,body)

let statement r k s =
  let s = k s in
  match Ast.unwrap s with
    Ast.Seq(lbrace,body,rbrace) ->
      let (lbrace,body) = adjust_brace_and_body lbrace body in
      Ast.rewrap s (Ast.Seq(lbrace,body,rbrace))
  | Ast.FunDecl(header,lbrace,body,rbrace,endinfo) ->
      let (lbrace,body) = adjust_brace_and_body lbrace body in
      Ast.rewrap s (Ast.FunDecl(header,lbrace,body,rbrace,endinfo))
  | _ -> s

let mcode mc = mc
let donothing r k e = k e

let stmtlist_rebuilder =
  V.rebuilder
    mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
    mcode mcode
    donothing donothing donothing donothing donothing donothing
    donothing donothing donothing donothing donothing donothing
    donothing donothing donothing donothing donothing donothing
    donothing donothing donothing donothing statement donothing donothing
    donothing

let stmtlist rule =
  match rule with
    Ast.ScriptRule _ | Ast.InitialScriptRule _ | Ast.FinalScriptRule _ -> rule
  | Ast.CocciRule (nm, rule_info, r, is_exp,ruletype) ->
      let r = List.map stmtlist_rebuilder.V.rebuilder_top_level r in
      Ast.CocciRule (nm, rule_info, r, is_exp,ruletype)