Coccinelle release 0.2.5-rc4
[bpt/coccinelle.git] / tools / bridge.ml
index 6e3037c..666acf0 100644 (file)
@@ -1,3 +1,27 @@
+(*
+ * 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.
+ *
+ * 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.
+ *)
+
+
 let drop_spaces s =
   String.concat "" (Str.split (Str.regexp "[ ]+") s)
 
@@ -21,12 +45,12 @@ let parse_line fp l n =
                (match List.rev splitted with
                  new_tag::info ->
                    let rest = loop new_tag rest in
-                   (tag,String.concat "\\ " info)::rest
+                   (tag,String.concat "\\ " (List.rev info))::rest
                |       _ -> failwith "bad element")
            | _ -> failwith "no data" in
          Some (cocci,loop (drop_spaces first) others)
       | _ -> failwith (Printf.sprintf "bad line: %s" l)
-    
+
 let collect_lines fp i =
   let lines = ref [] in
   let ln = ref 0 in
@@ -56,6 +80,7 @@ let process_fp fl =
   !lines
 
 (* --------------------------------------------------------------------- *)
+(* same info, different categories *)
 
 let discard_ambiguous lines =
   let rec loop = function
@@ -138,7 +163,7 @@ let command s =
   let _ = Sys.command s in
   ()
 
-let created = ref ([] : (string * (int ref * out_channel)) list)
+let created = ref ([] : (string * (string list ref * out_channel)) list)
 
 let mktag n = Printf.sprintf "x%d" n
 
@@ -155,12 +180,17 @@ let process_line env (cocci,tags) =
        try List.assoc resdir !created
        with Not_found ->
          begin
-           command
-             (Printf.sprintf "/bin/rm -r -f %s; mkdir %s" resdir resdir);
+           if Sys.file_exists resdir
+           then
+             command
+               (Printf.sprintf
+                  "test %s -nt %s && /bin/rm -r -f %s && mkdir %s"
+                  cocci_file resdir resdir resdir)
+           else command (Printf.sprintf "mkdir %s" resdir);
            let files = Printf.sprintf "%s/files" resdir in
            let o = open_out files in
            Printf.fprintf o "all: real_all\n\n";
-           let cell = ((ref 0),o) in
+           let cell = ((ref []),o) in
            created := (resdir,cell) :: !created;
            cell
          end in
@@ -183,18 +213,18 @@ let process_line env (cocci,tags) =
       List.iter
        (function (tag,tagval) ->
          command
-           (Printf.sprintf "sed s/%s/%s/ %s > %s_out; cp %s_out %s"
+           (Printf.sprintf "sed s+%s+%s+ %s > %s_out; cp %s_out %s"
               tag tagval temp_file temp_file temp_file temp_file))
        tags;
       command
        (Printf.sprintf "mv %s %s/%s.cocci" temp_file resdir first_tag_val);
-      Printf.fprintf o "%s:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
-       (mktag !n) first_tag_val;
-      n := !n + 1)
+      Printf.fprintf o "%s.out:\n\tmono_spatch_linux %s.cocci ${ARGS}\n\n"
+       first_tag_val first_tag_val;
+      n := (first_tag_val^".out") :: !n)
     files
-    
+
 (* --------------------------------------------------------------------- *)
-    
+
 let rec mkenv = function
     [] -> []
   | [_] -> failwith "required arguments: file (category x cocci file)*"
@@ -206,10 +236,13 @@ let rec mkenv = function
 let rec upto = function
     0 -> []
   | n -> (mktag (n-1)) :: (upto (n-1))
-         
+
 let _ =
+  let (no_ors,args) =
+    List.partition (function "-no_ors" -> true | _ -> false)
+      (Array.to_list Sys.argv) in
   let (file,fp,env) =
-    match List.tl(Array.to_list Sys.argv) with
+    match List.tl args with
       file::env ->
        let rec loop prev = function
            [] ->
@@ -233,13 +266,15 @@ let _ =
   let fp = List.fold_left (@) [] (List.map process_fp fp) in
   let i = open_in file in
   let lines = collect_lines fp i in
-  let lines = collect_ors fp lines in
+  let lines = if no_ors = [] then collect_ors fp lines else lines in
   close_in i;
   let lines = discard_ambiguous lines in
   List.iter (process_line env) lines;
   List.iter
     (function (resdir,(n,o)) ->
       Printf.fprintf o "real_all: %s\n"
-       (String.concat " " (List.rev (upto !n)));
+       (String.concat " " (List.rev !n));
+      Printf.fprintf o "\tcat %s > completed\n"
+       (String.concat " " (List.rev !n));
       close_out o)
     !created