File: comm_assoc.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 (77 lines) | stat: -rw-r--r-- 2,468 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
67
68
69
70
71
72
73
74
75
76
77
(*
 * 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
 *)

(* searches for E op ..., for any commutative and associative binary
operator.  When this satisfies the isomorphism conditions (ie all minus, or
context for the op and ...), then this is converted to Nested(E,op).
Nested is not used before this phase. *)

module Ast = Ast_cocci
module Ast0 = Ast0_cocci
module V0 = Visitor_ast0
module VT0 = Visitor_ast0_types
module U = Unparse_ast0

let comm_assoc_arith =
  [Ast.Plus; Ast.Mul; Ast.And; Ast.Or]

let comm_assoc_logical =
  [Ast.AndLog; Ast.OrLog]

let is_comm_assoc op0 = match Ast0.unwrap op0 with
  | Ast0.Arith op -> List.mem (Ast0.unwrap_mcode op) comm_assoc_arith
  | Ast0.Logical op -> List.mem (Ast0.unwrap_mcode op) comm_assoc_logical
  | Ast0.MetaBinary _ -> false

let is_minus e =
  match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false

let is_context e =
  !Flag.sgrep_mode2 || (* everything is context for sgrep *)
  (match Ast0.get_mcodekind e with
    Ast0.CONTEXT(cell) -> true
  | _ -> false)

let nopos mc = (Ast0.get_pos mc) = []

let nopos_op op0 = match Ast0.unwrap op0 with
  | Ast0.Arith op -> nopos op
  | Ast0.Logical op -> nopos op
  | Ast0.MetaBinary(mv,_,_) -> nopos mv

let process_binops rule_name =
  let expr r k e1 =
    let e = k e1 in
    match Ast0.unwrap e with
      Ast0.Binary(left,op,right)
      when is_comm_assoc op ->
	(match Ast0.unwrap right with
	  Ast0.Edots(d,None) ->
	    if (is_minus e || (is_context op && is_context right))
		&& nopos_op op && nopos d
	    (* keep dots to record required modif *)
	    then Ast0.rewrap e (Ast0.Nested(left,op,right))
	    else
	      (Common.pr2
		 (Printf.sprintf
		    "%s: position variables or mixed modifs interfere with comm_assoc iso\n%s"
		    rule_name
		    (U.unparse_x_to_string U.expression e1));
	       e)
	| Ast0.Edots(d,_) ->
	    (Common.pr2
	       (Printf.sprintf
		  "%s: whencode interferes with comm_assoc iso\n%s" rule_name
		  (U.unparse_x_to_string U.expression e1));
	     e)
	| _ -> e)
    | _ -> e in
  V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}

let comm_assoc rule rule_name dropped_isos =
  if List.mem "comm_assoc" dropped_isos
  then rule
  else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule