Release coccinelle-0.2.3rc4
[bpt/coccinelle.git] / parsing_cocci / free_vars.ml
index 55cd94b..3efc01e 100644 (file)
@@ -1,4 +1,6 @@
 (*
+ * Copyright 2010, 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
  * This file is part of Coccinelle.
@@ -86,24 +88,49 @@ let collect_refs include_constraints =
   let astfvident recursor k i =
     bind (k i)
       (match Ast.unwrap i with
-       Ast.MetaId(name,_,_,_) | Ast.MetaFunc(name,_,_,_)
-      | Ast.MetaLocalFunc(name,_,_,_) -> [metaid name]
+       Ast.MetaId(name,idconstraint,_,_) | Ast.MetaFunc(name,idconstraint,_,_)
+      | Ast.MetaLocalFunc(name,idconstraint,_,_) ->
+         let metas =
+           if include_constraints
+           then
+             match idconstraint with
+               Ast.IdNegIdSet (_,metas) -> metas
+             | _ -> []
+           else [] in
+         bind (List.rev metas) [metaid name]
       | _ -> option_default) in
 
   let rec type_collect res = function
       TC.ConstVol(_,ty) | TC.Pointer(ty) | TC.FunctionPointer(ty)
     | TC.Array(ty) -> type_collect res ty
-    | TC.MetaType(tyname,_,_) -> bind [tyname] res
+    | TC.MetaType(tyname,_,_) ->
+       bind [tyname] res
     | TC.SignedT(_,Some ty) -> type_collect res ty
     | ty -> res in
 
   let astfvexpr recursor k e =
     bind (k e)
       (match Ast.unwrap e with
-       Ast.MetaExpr(name,_,_,Some type_list,_,_) ->
+       Ast.MetaExpr(name,constraints,_,Some type_list,_,_) ->
          let types = List.fold_left type_collect option_default type_list in
-         bind [metaid name] types
-      | Ast.MetaErr(name,_,_,_) | Ast.MetaExpr(name,_,_,_,_,_) -> [metaid name]
+         let extra =
+           if include_constraints
+           then
+             match constraints with
+               Ast.SubExpCstrt l -> l
+             | _ -> []
+           else [] in
+         bind extra (bind [metaid name] types)
+      | Ast.MetaErr(name,constraints,_,_)
+      | Ast.MetaExpr(name,constraints,_,_,_,_) ->
+         let extra =
+           if include_constraints
+           then
+             match constraints with
+               Ast.SubExpCstrt l -> l
+             | _ -> []
+           else [] in
+         bind extra [metaid name]
       | Ast.MetaExprList(name,None,_,_) -> [metaid name]
       | Ast.MetaExprList(name,Some (lenname,_,_),_,_) ->
          [metaid name;metaid lenname]
@@ -196,14 +223,17 @@ let collect_saved =
   let astfvident recursor k i =
     bind (k i)
       (match Ast.unwrap i with
-       Ast.MetaId(name,_,TC.Saved,_) | Ast.MetaFunc(name,_,TC.Saved,_)
-      | Ast.MetaLocalFunc(name,_,TC.Saved,_) -> [metaid name]
+       Ast.MetaId(name,_,TC.Saved,_)
+      | Ast.MetaFunc(name,_,TC.Saved,_)
+      | Ast.MetaLocalFunc(name,_,TC.Saved,_) ->
+         [metaid name]
       | _ -> option_default) in
 
   let rec type_collect res = function
       TC.ConstVol(_,ty) | TC.Pointer(ty) | TC.FunctionPointer(ty)
     | TC.Array(ty) -> type_collect res ty
-    | TC.MetaType(tyname,TC.Saved,_) -> bind [tyname] res
+    | TC.MetaType(tyname,TC.Saved,_) ->
+       bind [tyname] res
     | TC.SignedT(_,Some ty) -> type_collect res ty
     | ty -> res in
 
@@ -437,7 +467,8 @@ let classify_variables metavar_decls minirules used_after =
     match Ast.unwrap e with
       Ast.MetaId(name,constraints,_,_) ->
        let (unitary,inherited) = classify name in
-       Ast.rewrap e (Ast.MetaId(name,constraints,unitary,inherited))
+       Ast.rewrap e
+         (Ast.MetaId(name,constraints,unitary,inherited))
     | Ast.MetaFunc(name,constraints,_,_) ->
        let (unitary,inherited) = classify name in
        Ast.rewrap e (Ast.MetaFunc(name,constraints,unitary,inherited))
@@ -682,8 +713,8 @@ let collect_astfvs rules =
       [] -> []
     | (metavars, rule)::rules ->
         match rule with
-          Ast.ScriptRule (_,_,_,_)
-       | Ast.InitialScriptRule (_,_) | Ast.FinalScriptRule (_,_) ->
+          Ast.ScriptRule (_,_,_,_,_)
+       | Ast.InitialScriptRule (_,_,_,_) | Ast.FinalScriptRule (_,_,_,_) ->
            (* bound stays as is because script rules have no names, so no
               inheritance is possible *)
            rule::(loop bound rules)
@@ -749,6 +780,7 @@ their point of definition. *)
 
 
 let collect_top_level_used_after metavar_rule_list =
+  let drop_virt = List.filter (function ("virtual",_) -> false | _ -> true) in
   let (used_after,used_after_lists) =
     List.fold_right
       (function (metavar_list,r) ->
@@ -759,12 +791,14 @@ let collect_top_level_used_after metavar_rule_list =
              used_after in
          let free_vars =
             match r with
-              Ast.ScriptRule (_,_,mv,_) ->
-                List.map (function (_,(r,v)) -> (r,v)) mv
-            | Ast.InitialScriptRule (_,_) | Ast.FinalScriptRule (_,_) -> []
+              Ast.ScriptRule (_,_,_,mv,_) ->
+                drop_virt(List.map (function (_,(r,v),_) -> (r,v)) mv)
+            | Ast.InitialScriptRule (_,_,_,_)
+           | Ast.FinalScriptRule (_,_,_,_) -> []
             | Ast.CocciRule (_,_,rule,_,_) ->
-               Common.union_set (nub (collect_all_rule_refs rule))
-                 (collect_in_plus metavar_list rule) in
+               drop_virt
+                 (Common.union_set (nub (collect_all_rule_refs rule))
+                    (collect_in_plus metavar_list rule)) in
          let inherited =
            List.filter (function x -> not (List.mem x locally_defined))
              free_vars in
@@ -867,8 +901,8 @@ let collect_used_after metavar_rule_list =
     (function (metavars,r) ->
       function used_after ->
         match r with
-          Ast.ScriptRule (_,_,_,_)
-       | Ast.InitialScriptRule (_,_) | Ast.FinalScriptRule (_,_) ->
+          Ast.ScriptRule (_,_,_,_,_)
+       | Ast.InitialScriptRule (_,_,_,_) | Ast.FinalScriptRule (_,_,_,_) ->
            ([], [used_after], [], [])
         | Ast.CocciRule (name, rule_info, minirules, _,_) ->
           collect_local_used_after metavars minirules used_after