Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_cocci / single_statement.ml
index 94f5a16..cc0d180 100644 (file)
@@ -1,5 +1,7 @@
 (*
- * Copyright 2010, INRIA, University of Copenhagen
+ * Copyright 2012, INRIA
+ * Julia Lawall, Gilles Muller
+ * Copyright 2010-2011, INRIA, University of Copenhagen
  * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
  * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
  * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
@@ -80,10 +82,10 @@ let modif_before x =
   | Ast0.MINUS mc ->
       (match !mc with
        (* do better for the common case of replacing a stmt by another one *)
-       ([[Ast.StatementTag(s)]],ti) ->
+       ((Ast.REPLACEMENT([[Ast.StatementTag(s)]],c)) as old,ti) ->
          (match Ast.unwrap s with
            Ast.IfThen(_,_,_) -> true (* potentially dangerous *)
-         | _ -> mc := ([[Ast.StatementTag(s)]],ti); false)
+         | _ -> mc := (old,ti); false)
       |        (_,_) -> true)
   | Ast0.CONTEXT mc | Ast0.MIXED mc ->
       (match !mc with
@@ -97,11 +99,11 @@ let modif_after x =
   | Ast0.MINUS mc ->
       (match !mc with
        (* do better for the common case of replacing a stmt by another one *)
-       ([[Ast.StatementTag(s)]],ti) ->
+       ((Ast.REPLACEMENT([[Ast.StatementTag(s)]],count)) as old,ti) ->
          (match Ast.unwrap s with
            Ast.IfThen(_,_,_) -> true (* potentially dangerous *)
-         | _ -> mc := ([[Ast.StatementTag(s)]],ti); false)
-      |        (l,_) when any_statements l -> true
+         | _ -> mc := (old,ti); false)
+      |        (Ast.REPLACEMENT(l,_),_) when any_statements l -> true
       |        (l,ti) -> mc := (l,ti); false)
   | Ast0.CONTEXT mc | Ast0.MIXED mc ->
       (match !mc with
@@ -114,12 +116,24 @@ let rec left_ident i =
   modif_before i or
   match Ast0.unwrap i with
     Ast0.Id(name) -> modif_before_mcode name
-  | Ast0.MetaId(name,_,_) -> modif_before_mcode name
+  | Ast0.MetaId(name,_,_,_) -> modif_before_mcode name
   | Ast0.MetaFunc(name,_,_) -> modif_before_mcode name
   | Ast0.MetaLocalFunc(name,_,_) -> modif_before_mcode name
+  | Ast0.DisjId(_,id_list,_,_) -> List.exists left_ident id_list
   | Ast0.OptIdent(id) -> left_ident id
   | Ast0.UniqueIdent(id) -> left_ident id
 
+let rec right_ident i =
+  modif_after i or
+  match Ast0.unwrap i with
+    Ast0.Id(name) -> modif_after_mcode name
+  | Ast0.MetaId(name,_,_,_) -> modif_after_mcode name
+  | Ast0.MetaFunc(name,_,_) -> modif_after_mcode name
+  | Ast0.MetaLocalFunc(name,_,_) -> modif_after_mcode name
+  | Ast0.DisjId(_,id_list,_,_) -> List.exists right_ident id_list
+  | Ast0.OptIdent(id) -> right_ident id
+  | Ast0.UniqueIdent(id) -> right_ident id
+
 (* --------------------------------------------------------------------- *)
 (* Expression *)
 
@@ -130,6 +144,7 @@ let rec left_expression e =
   | Ast0.Constant(const) -> modif_before_mcode const
   | Ast0.FunCall(fn,lp,args,rp) -> left_expression fn
   | Ast0.Assignment(left,op,right,_) -> left_expression left
+  | Ast0.Sequence(left,op,right) -> left_expression left
   | Ast0.CondExpr(exp1,why,exp2,colon,exp3) -> left_expression exp1
   | Ast0.Postfix(exp,op) -> left_expression exp
   | Ast0.Infix(exp,op) -> modif_before_mcode op
@@ -144,6 +159,7 @@ let rec left_expression e =
   | Ast0.SizeOfExpr(szf,exp) -> modif_before_mcode szf
   | Ast0.SizeOfType(szf,lp,ty,rp) -> modif_before_mcode szf
   | Ast0.TypeExp(ty) -> left_typeC ty
+  | Ast0.Constructor(lp,ty,rp,init) -> modif_before_mcode lp
   | Ast0.MetaErr(name,_,_) -> modif_before_mcode name
   | Ast0.MetaExpr(name,_,ty,_,_) -> modif_before_mcode name
   | Ast0.MetaExprList(name,_,_) -> modif_before_mcode name
@@ -154,6 +170,7 @@ let rec left_expression e =
   | Ast0.Edots(dots,_) | Ast0.Ecircles(dots,_) | Ast0.Estars(dots,_) -> false
   | Ast0.OptExp(exp) -> left_expression exp
   | Ast0.UniqueExp(exp) -> left_expression exp
+  | Ast0.AsExpr _ -> failwith "not possible"
 
 (* --------------------------------------------------------------------- *)
 (* Types *)
@@ -170,6 +187,7 @@ and left_typeC t =
   | Ast0.FunctionType(None,lp1,params,rp1) -> modif_before_mcode lp1
   | Ast0.Array(ty,lb,size,rb) -> left_typeC ty
   | Ast0.EnumName(kind,name) -> modif_before_mcode kind
+  | Ast0.EnumDef(ty,lb,ids,rb) -> left_typeC ty
   | Ast0.StructUnionName(kind,name) -> modif_before_mcode kind
   | Ast0.StructUnionDef(ty,lb,decls,rb) -> left_typeC ty
   | Ast0.TypeName(name) -> modif_before_mcode name
@@ -177,6 +195,8 @@ and left_typeC t =
   | Ast0.DisjType(lp,types,mids,rp) -> List.exists left_typeC types
   | Ast0.OptType(ty) -> left_typeC ty
   | Ast0.UniqueType(ty) -> left_typeC ty
+  | Ast0.AsType _ -> failwith "not possible"
+
 
 (* --------------------------------------------------------------------- *)
 (* Variable declaration *)
@@ -186,32 +206,40 @@ and left_typeC t =
 and left_declaration d =
   modif_before d or
   match Ast0.unwrap d with
-    Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_) -> modif_before_mcode name
+    Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_)
+  | Ast0.MetaFieldList(name,_,_) ->
+      modif_before_mcode name
   | Ast0.Init(Some stg,ty,id,eq,ini,sem) -> modif_before_mcode stg
   | Ast0.Init(None,ty,id,eq,ini,sem) -> left_typeC ty
   | Ast0.UnInit(Some stg,ty,id,sem) -> modif_before_mcode stg
   | Ast0.UnInit(None,ty,id,sem) -> left_typeC ty
   | Ast0.MacroDecl(name,lp,args,rp,sem) -> left_ident name
+  | Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem) -> left_ident name
   | Ast0.TyDecl(ty,sem) -> left_typeC ty
   | Ast0.Typedef(stg,ty,id,sem) -> modif_before_mcode stg
   | Ast0.DisjDecl(_,decls,_,_) -> List.exists left_declaration decls
   | Ast0.Ddots(dots,_) -> false
   | Ast0.OptDecl(decl) -> left_declaration decl
   | Ast0.UniqueDecl(decl) -> left_declaration decl
+  | Ast0.AsDecl _ -> failwith "not possible"
 
 and right_declaration d =
   modif_before d or
   match Ast0.unwrap d with
-    Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_) -> modif_before_mcode name
+    Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_)
+  | Ast0.MetaFieldList(name,_,_) ->
+      modif_before_mcode name
   | Ast0.Init(_,ty,id,eq,ini,sem) -> modif_after_mcode sem
   | Ast0.UnInit(_,ty,id,sem) -> modif_after_mcode sem
   | Ast0.MacroDecl(name,lp,args,rp,sem) -> modif_after_mcode sem
+  | Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem) -> modif_after_mcode sem
   | Ast0.TyDecl(ty,sem) -> modif_after_mcode sem
   | Ast0.Typedef(stg,ty,id,sem) -> modif_after_mcode sem
   | Ast0.DisjDecl(_,decls,_,_) -> List.exists right_declaration decls
   | Ast0.Ddots(dots,_) -> false
   | Ast0.OptDecl(decl) -> right_declaration decl
   | Ast0.UniqueDecl(decl) -> right_declaration decl
+  | Ast0.AsDecl _ -> failwith "not possible"
 
 (* --------------------------------------------------------------------- *)
 (* Top-level code *)
@@ -223,7 +251,8 @@ and left_statement s =
       (* irrelevant *) false
   | Ast0.Decl(_,decl) -> left_declaration decl
   | Ast0.Seq(lbrace,body,rbrace) -> modif_before_mcode lbrace
-  | Ast0.ExprStatement(exp,sem) -> left_expression exp
+  | Ast0.ExprStatement(Some exp,sem) -> left_expression exp
+  | Ast0.ExprStatement(None,sem) -> modif_before_mcode sem
   | Ast0.IfThen(iff,lp,exp,rp,branch1,(info,aft)) -> modif_before_mcode iff
   | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,(info,aft)) ->
       modif_before_mcode iff
@@ -252,9 +281,11 @@ and left_statement s =
   | Ast0.TopInit(init) -> false (* can only be replaced by an init *)
   | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) -> false
   | Ast0.Include(inc,s) -> modif_before_mcode inc
+  | Ast0.Undef(def,id) -> modif_before_mcode def
   | Ast0.Define(def,id,params,body) -> modif_before_mcode def
   | Ast0.OptStm(re) -> left_statement re
   | Ast0.UniqueStm(re) -> left_statement re
+  | Ast0.AsStmt _ -> failwith "not possible"
 
 and right_statement s =
   modif_after s or
@@ -292,9 +323,11 @@ and right_statement s =
   | Ast0.TopInit(init) -> false (* can only be replaced by an init *)
   | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) -> false
   | Ast0.Include(inc,s) -> modif_after_mcode s
+  | Ast0.Undef(def,id) -> right_ident id
   | Ast0.Define(def,id,params,body) -> right_dots right_statement body
   | Ast0.OptStm(re) -> right_statement re
   | Ast0.UniqueStm(re) -> right_statement re
+  | Ast0.AsStmt _ -> failwith "not possible"
 
 (* --------------------------------------------------------------------- *)
 
@@ -309,10 +342,10 @@ let rec adding_something s =
     Ast0.MINUS(mc) ->
       (match !mc with
        (* do better for the common case of replacing a stmt by another one *)
-       ([[Ast.StatementTag(s)]],ti) ->
+       ((Ast.REPLACEMENT([[Ast.StatementTag(s)]],c)) as old,ti) ->
          (match Ast.unwrap s with
            Ast.IfThen(_,_,_) -> true (* potentially dangerous *)
-         | _ -> mc := ([[Ast.StatementTag(s)]],ti); false)
+         | _ -> mc := (old,ti); false)
       |        (_,_) -> true)
   | Ast0.CONTEXT(mc) ->
       let (text,tinfo1,tinfo2) = !mc in
@@ -329,7 +362,7 @@ and contains_only_minus =
   let mcodekind = function
       Ast0.MINUS(mc) ->
        (match !mc with
-         ([],_) -> true
+         (Ast.NOREPLACEMENT,_) -> true
        | _ -> false)
     | Ast0.CONTEXT(mc) -> false
     | _ -> false in
@@ -342,6 +375,13 @@ and contains_only_minus =
       Ast0.DOTS([]) | Ast0.CIRCLES([]) | Ast0.STARS([]) -> true
     | _ -> k e in
 
+  let identifier r k e =
+    mcodekind (Ast0.get_mcodekind e) &&
+    match Ast0.unwrap e with
+      Ast0.DisjId(starter,id_list,mids,ender) ->
+       List.for_all r.VT0.combiner_rec_ident id_list
+    | _ -> k e in
+
   let expression r k e =
     mcodekind (Ast0.get_mcodekind e) &&
     match Ast0.unwrap e with
@@ -380,7 +420,7 @@ and contains_only_minus =
   V0.flat_combiner bind option_default
     mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
     dots dots dots dots dots dots
-    donothing expression typeC donothing donothing declaration
+    identifier expression typeC donothing donothing declaration
     statement case_line donothing
 
 
@@ -407,7 +447,14 @@ let add_braces orig_s =
     match Ast0.get_mcodekind s with
       Ast0.MINUS(mc) ->
        let (text,tinfo) = !mc in
-       Ast0.MINUS(ref([Ast.mkToken "{"]::text@[[Ast.mkToken "}"]],tinfo))
+       let inner_text =
+         match text with
+           Ast.NOREPLACEMENT -> [[Ast.mkToken "{}"]]
+         | Ast.REPLACEMENT(anythings,Ast.ONE) ->
+             [Ast.mkToken "{"]::anythings@[[Ast.mkToken "}"]]
+         | Ast.REPLACEMENT(anythings,Ast.MANY) ->
+             failwith "++ not supported when braces must be added" in
+       Ast0.MINUS(ref(Ast.REPLACEMENT(inner_text,Ast.ONE),tinfo))
     | Ast0.CONTEXT(mc) ->
        let (text,tinfo1,tinfo2) = !mc in
        let new_text =
@@ -465,7 +512,8 @@ let all_minus s =
 
 let rec unchanged_minus s =
   match Ast0.get_mcodekind s with
-    Ast0.MINUS(mc) -> (match !mc with ([],_) -> true | _ -> false)
+    Ast0.MINUS(mc) ->
+      (match !mc with (Ast.NOREPLACEMENT,_) -> true | _ -> false)
   | _ -> false
 
 let rec do_branch s =
@@ -580,6 +628,7 @@ let rec statement dots_before dots_after s =
   | Ast0.TopInit(init) -> s
   | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) -> s
   | Ast0.Include(inc,string) -> s (* doesn't affect the need for braces *)
+  | Ast0.Undef(def,id) -> s (* same as include *)
   | Ast0.Define(def,id,params,body) -> s (* same as include *)
   | Ast0.OptStm(re) ->
       Ast0.rewrap s
@@ -587,6 +636,7 @@ let rec statement dots_before dots_after s =
   | Ast0.UniqueStm(re) ->
       Ast0.rewrap s
        (Ast0.UniqueStm(statement dots_before dots_after re))
+  | Ast0.AsStmt _ -> failwith "not possible"
 
 and case_line c =
   Ast0.rewrap c
@@ -624,7 +674,7 @@ and statement_dots dots_before dots_after d =
 let top_level t =
   Ast0.rewrap t
     (match Ast0.unwrap t with
-      Ast0.DECL(stmt_dots) -> Ast0.DECL(statement true true stmt_dots)
+      Ast0.NONDECL(stmt_dots) -> Ast0.NONDECL(statement true true stmt_dots)
     | Ast0.CODE(stmt_dots) -> Ast0.CODE(statement_dots true true stmt_dots)
     | t -> t)