Release coccinelle-0.2.0
[bpt/coccinelle.git] / parsing_cocci / get_constants2.ml
index 93e9ea6..aaed6e9 100644 (file)
@@ -1,23 +1,23 @@
 (*
-* Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
-* Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller
-* This file is part of Coccinelle.
-* 
-* Coccinelle is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, according to version 2 of the License.
-* 
-* Coccinelle is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-* 
-* You should have received a copy of the GNU General Public License
-* along with Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
-* 
-* The authors reserve the right to distribute this or future versions of
-* Coccinelle under other licenses.
-*)
+ * 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.
+ *
+ * Coccinelle is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, according to version 2 of the License.
+ *
+ * Coccinelle is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The authors reserve the right to distribute this or future versions of
+ * Coccinelle under other licenses.
+ *)
 
 
 module Ast = Ast_cocci
@@ -47,7 +47,7 @@ wanted *)
 type combine =
     And of combine list | Or of combine list | Elem of string | False | True
 
-let interpret strict x =
+let interpret_glimpse strict x =
   let rec loop = function
       Elem x -> x
     | And [x] -> loop x
@@ -63,12 +63,45 @@ let interpret strict x =
     True -> None
   | False when strict ->
       failwith "False should not be in the final result.  Perhaps your rule doesn't contain any +/-/* code"
-  | _ -> Some (loop x)
+  | _ -> Some [(loop x)]
+
+let interpret_google strict x =
+  (* convert to dnf *)
+  let rec dnf = function
+      Elem x -> [x]
+    | Or l -> List.fold_left Common.union_set [] (List.map dnf l)
+    | And l ->
+       let l = List.map dnf l in
+       List.fold_left
+         (function prev ->
+           function cur ->
+             List.fold_left Common.union_set []
+               (List.map
+                  (function x ->
+                    List.map (function y -> Printf.sprintf "%s %s" x y) prev)
+                  cur))
+         [] l
+    | True -> ["True"]
+    | False ->
+       if strict
+       then failwith "False should not be in the final result.  Perhaps your rule doesn't contain any +/-/* code"
+       else ["False"] in
+  match x with
+    True -> None
+  | False when strict ->
+      failwith "False should not be in the final result.  Perhaps your rule doesn't contain any +/-/* code"
+  | _ -> Some (dnf x)
+
+let interpret strict x =
+  match !Flag.scanner with
+    Flag.Glimpse -> interpret_glimpse strict x
+  | Flag.Google _ -> interpret_google strict x
+  | _ -> failwith "not possible"
 
 let combine2c x =
   match interpret false x with
     None -> "None"
-  | Some x -> x
+  | Some x -> String.concat " || " x
 
 let norm = function
     And l -> And (List.sort compare l)
@@ -199,12 +232,10 @@ let do_get_constants constants keywords env neg_pos =
          (match Ast.unwrap_mcode const with
            Ast.String s -> constants s
          | Ast.Char "\\0" -> option_default (* glimpse doesn't like it *)
-         | Ast.Char s -> constants s
+         | Ast.Char s -> option_default (* probably not chars either *)
          (* the following were eg keywords "1", but not good for glimpse *)
-         | Ast.Int "0" -> option_default (* glimpse doesn't like it *)
-         | Ast.Int "1" -> option_default (* glimpse doesn't like it *)
-         | Ast.Int s -> constants s
-         | Ast.Float s -> constants s)
+         | Ast.Int s -> option_default (* glimpse doesn't index integers *)
+         | Ast.Float s -> option_default (* probably not floats either *))
     | Ast.MetaExpr(name,_,_,Some type_list,_,_) ->
        let types = List.fold_left type_collect option_default type_list in
        bind (k e) (bind (minherited name) types)
@@ -377,9 +408,9 @@ let get_plus_constants =
        [] l in
     match mcodekind with
       Ast.MINUS(_,_,_,anythings) -> recurse anythings
-    | Ast.CONTEXT(_,Ast.BEFORE(a)) -> recurse a
-    | Ast.CONTEXT(_,Ast.AFTER(a)) -> recurse a
-    | Ast.CONTEXT(_,Ast.BEFOREAFTER(a1,a2)) ->
+    | Ast.CONTEXT(_,Ast.BEFORE(a,_)) -> recurse a
+    | Ast.CONTEXT(_,Ast.AFTER(a,_)) -> recurse a
+    | Ast.CONTEXT(_,Ast.BEFOREAFTER(a1,a2,_)) ->
        Common.union_set (recurse a1) (recurse a2)
     | _ -> [] in
 
@@ -400,6 +431,7 @@ let rec dependencies env = function
   | Ast.AndDep (d1,d2) -> build_and (dependencies env d1) (dependencies env d2)
   | Ast.OrDep (d1,d2) -> build_or (dependencies env d1) (dependencies env d2)
   | Ast.NoDep -> True
+  | Ast.FailDep -> False
 
 (* ------------------------------------------------------------------------ *)
 
@@ -457,37 +489,39 @@ let rule_fn tls in_plus env neg_pos =
     (False,in_plus) (List.combine tls neg_pos)
 
 let get_constants rules neg_pos_vars =
-  if not !Flag.use_glimpse
-  then None
-  else
-    let (info,_,_,_) =
-      List.fold_left
-       (function (rest_info,in_plus,env,locals(*dom of env*)) ->
-          function
-              (Ast.ScriptRule (_,deps,mv,_),_) ->
-               let extra_deps =
-                 List.fold_left
-                   (function prev ->
-                     function (_,(rule,_)) -> Ast.AndDep (Ast.Dep rule,prev))
-                   deps mv in
-               (match dependencies env extra_deps with
-                 False -> (rest_info, in_plus, env, locals)
-               | dependencies ->
-                   (build_or dependencies rest_info, in_plus, env, locals))
-            | (Ast.InitialScriptRule (_,_),_)
-           | (Ast.FinalScriptRule (_,_),_) -> (rest_info,in_plus,env,locals)
-            | (Ast.CocciRule (nm,(dep,_,_),cur,_,_),neg_pos_vars) ->
-               let (cur_info,cur_plus) =
-                 rule_fn cur in_plus ((nm,True)::env) neg_pos_vars in
-               if List.for_all all_context.V.combiner_top_level cur
-               then (rest_info,cur_plus,(nm,cur_info)::env,nm::locals)
-               else
+  match !Flag.scanner with
+    Flag.NoScanner -> None
+  | Flag.Glimpse | Flag.Google _ ->
+      let (info,_,_,_) =
+       List.fold_left
+         (function (rest_info,in_plus,env,locals(*dom of env*)) ->
+            function
+               (Ast.ScriptRule (_,deps,mv,_),_) ->
+                 let extra_deps =
+                   List.fold_left
+                     (function prev ->
+                       function (_,(rule,_)) ->
+                         Ast.AndDep (Ast.Dep rule,prev))
+                     deps mv in
+                 (match dependencies env extra_deps with
+                   False -> (rest_info, in_plus, env, locals)
+                 | dependencies ->
+                     (build_or dependencies rest_info, in_plus, env, locals))
+              | (Ast.InitialScriptRule (_,_),_)
+             | (Ast.FinalScriptRule (_,_),_) -> (rest_info,in_plus,env,locals)
+              | (Ast.CocciRule (nm,(dep,_,_),cur,_,_),neg_pos_vars) ->
+                 let (cur_info,cur_plus) =
+                   rule_fn cur in_plus ((nm,True)::env) neg_pos_vars in
+                 if List.for_all all_context.V.combiner_top_level cur
+                 then (rest_info,cur_plus,(nm,cur_info)::env,nm::locals)
+                 else
               (* no constants if dependent on another rule; then we need to
                  find the constants of that rule *)
-                 match dependencies env dep with
-                   False -> (rest_info,cur_plus,env,locals)
-                 | dependencies ->
-                     (build_or (build_and dependencies cur_info) rest_info,
-                      cur_plus,env,locals))
-       (False,[],[],[]) (List.combine (rules : Ast.rule list) neg_pos_vars) in
-    interpret true info
+                   match dependencies env dep with
+                     False -> (rest_info,cur_plus,env,locals)
+                   | dependencies ->
+                       (build_or (build_and dependencies cur_info) rest_info,
+                        cur_plus,env,locals))
+         (False,[],[],[])
+         (List.combine (rules : Ast.rule list) neg_pos_vars) in
+      interpret true info