Release coccinelle-0.2.2-rc1
[bpt/coccinelle.git] / parsing_cocci / parse_cocci.ml
index 209ab97..e78b7a0 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-2010, 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.
+ *)
 
 
 (* splits the entire file into minus and plus fragments, and parses each
@@ -46,6 +46,7 @@ let line_type2c tok =
   match line_type tok with
     D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ":-"
   | D.PLUS -> ":+"
+  | D.PLUSPLUS -> ":++"
   | D.CONTEXT | D.UNIQUE | D.OPT -> ""
 
 let token2c (tok,_) =
@@ -64,6 +65,7 @@ let token2c (tok,_) =
   | PC.TLocal -> "local"
   | PC.Tlist -> "list"
   | PC.TFresh -> "fresh"
+  | PC.TCppConcatOp -> "##"
   | PC.TPure -> "pure"
   | PC.TContext -> "context"
   | PC.TTypedef -> "typedef"
@@ -72,6 +74,7 @@ let token2c (tok,_) =
   | PC.TName -> "name"
   | PC.TRuleName str -> "rule_name-"^str
   | PC.TUsing -> "using"
+  | PC.TVirtual -> "virtual"
   | PC.TPathIsoFile str -> "path_iso_file-"^str
   | PC.TDisable -> "disable"
   | PC.TExtends -> "extends"
@@ -81,7 +84,6 @@ let token2c (tok,_) =
   | PC.TNever -> "never"
   | PC.TExists -> "exists"
   | PC.TForall -> "forall"
-  | PC.TReverse -> "reverse"
   | PC.TError -> "error"
   | PC.TWords -> "words"
   | PC.TGenerated -> "generated"
@@ -110,7 +112,8 @@ let token2c (tok,_) =
   | PC.Tconst(clt) -> "const"^(line_type2c clt)
   | PC.Tvolatile(clt) -> "volatile"^(line_type2c clt)
 
-  | PC.TPragma(s,_) -> s
+  | PC.TPragma(Ast.Noindent s,_) -> s
+  | PC.TPragma(Ast.Indent s,_)   -> s
   | PC.TIncludeL(s,clt) -> (pr "#include \"%s\"" s)^(line_type2c clt)
   | PC.TIncludeNL(s,clt) -> (pr "#include <%s>" s)^(line_type2c clt)
   | PC.TDefine(clt,_) -> "#define"^(line_type2c clt)
@@ -154,6 +157,8 @@ let token2c (tok,_) =
   | PC.TAnd (clt) -> "&"^(line_type2c clt)
   | PC.TEqEq(clt) -> "=="^(line_type2c clt)
   | PC.TNotEq(clt) -> "!="^(line_type2c clt)
+  | PC.TTildeEq(clt) -> "~="^(line_type2c clt)
+  | PC.TTildeExclEq(clt) -> "~!="^(line_type2c clt)
   | PC.TLogOp(op,clt) ->
       (match op with
        Ast.Inf -> "<"
@@ -262,6 +267,7 @@ let token2c (tok,_) =
   | PC.TIsoExpression -> "Expression"
   | PC.TIsoArgExpression -> "ArgExpression"
   | PC.TIsoTestExpression -> "TestExpression"
+  | PC.TIsoToTestExpression -> "ToTestExpression"
   | PC.TIsoStatement -> "Statement"
   | PC.TIsoDeclaration -> "Declaration"
   | PC.TIsoType -> "Type"
@@ -300,7 +306,7 @@ let plus_attachable only_plus (tok,_) =
   | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
 
   | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
-  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
   | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
   | PC.TDmOp(_,clt) | PC.TTilde (clt)
 
@@ -328,7 +334,7 @@ let plus_attachable only_plus (tok,_) =
 
   | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
   | PC.TPtVirg(clt) ->
-      if line_type clt = D.PLUS
+      if List.mem (line_type clt) [D.PLUS;D.PLUSPLUS]
       then PLUS
       else if only_plus then NOTPLUS
       else if line_type clt = D.CONTEXT then PLUS else NOTPLUS
@@ -365,7 +371,7 @@ let get_clt (tok,_) =
   | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
 
   | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
-  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
   | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
   | PC.TDmOp(_,clt) | PC.TTilde (clt)
 
@@ -466,6 +472,7 @@ let update_clt (tok,x) clt =
   | PC.TAnd (_) -> (PC.TAnd (clt),x)
   | PC.TEqEq(_) -> (PC.TEqEq(clt),x)
   | PC.TNotEq(_) -> (PC.TNotEq(clt),x)
+  | PC.TTildeEq(_) -> (PC.TTildeEq(clt),x)
   | PC.TLogOp(op,_) -> (PC.TLogOp(op,clt),x)
   | PC.TShOp(op,_) -> (PC.TShOp(op,clt),x)
   | PC.TPlus(_) -> (PC.TPlus(clt),x)
@@ -588,7 +595,7 @@ let split t clt =
   let (d,_,_,_,_,_,_,_) = clt in
   match d with
     D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ([t],[])
-  | D.PLUS -> ([],[t])
+  | D.PLUS | D.PLUSPLUS -> ([],[t])
   | D.CONTEXT | D.UNIQUE | D.OPT -> ([t],[t])
 
 let split_token ((tok,_) as t) =
@@ -596,11 +603,11 @@ let split_token ((tok,_) as t) =
     PC.TIdentifier | PC.TConstant | PC.TExpression | PC.TIdExpression
   | PC.TStatement | PC.TPosition | PC.TPosAny | PC.TInitialiser
   | PC.TFunction | PC.TTypedef | PC.TDeclarer | PC.TIterator | PC.TName
-  | PC.TType | PC.TParameter | PC.TLocal | PC.Tlist | PC.TFresh | PC.TPure
-  | PC.TContext | PC.TRuleName(_) | PC.TUsing | PC.TDisable | PC.TExtends
-  | PC.TPathIsoFile(_)
+  | PC.TType | PC.TParameter | PC.TLocal | PC.Tlist | PC.TFresh
+  | PC.TCppConcatOp | PC.TPure
+  | PC.TContext | PC.TRuleName(_) | PC.TUsing | PC.TVirtual | PC.TDisable
+  | PC.TExtends | PC.TPathIsoFile(_)
   | PC.TDepends | PC.TOn | PC.TEver | PC.TNever | PC.TExists | PC.TForall
-  | PC.TReverse
   | PC.TError | PC.TWords | PC.TGenerated | PC.TNothing -> ([t],[t])
 
   | PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
@@ -658,7 +665,7 @@ let split_token ((tok,_) as t) =
       split t clt
 
   | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
-  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+  | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TTildeExclEq(clt) | PC.TLogOp(_,clt)
   | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
   | PC.TDmOp(_,clt) | PC.TTilde (clt) -> split t clt
 
@@ -674,7 +681,8 @@ let split_token ((tok,_) as t) =
 
   | PC.TIso | PC.TRightIso
   | PC.TIsoExpression | PC.TIsoStatement | PC.TIsoDeclaration | PC.TIsoType
-  | PC.TIsoTopLevel | PC.TIsoArgExpression | PC.TIsoTestExpression ->
+  | PC.TIsoTopLevel | PC.TIsoArgExpression | PC.TIsoTestExpression
+  | PC.TIsoToTestExpression ->
       failwith "unexpected tokens"
   | PC.TScriptData s -> ([t],[t])
 
@@ -936,6 +944,42 @@ let rec translate_when_true_false = function
       (PC.TWhenFalse(clt),q)::x::(translate_when_true_false xs)
   | x::xs -> x :: (translate_when_true_false xs)
 
+(* ----------------------------------------------------------------------- *)
+
+let check_parentheses tokens =
+  let clt2line (_,line,_,_,_,_,_,_) = line in
+  let rec loop seen_open = function
+      [] -> tokens
+    | (PC.TOPar(clt),q) :: rest
+    | (PC.TDefineParam(clt,_,_,_),q) :: rest ->
+       loop (Common.Left (clt2line clt) :: seen_open) rest
+    | (PC.TOPar0(clt),q) :: rest ->
+       loop (Common.Right (clt2line clt) :: seen_open) rest
+    | (PC.TCPar(clt),q) :: rest ->
+       (match seen_open with
+         [] ->
+           failwith
+             (Printf.sprintf
+                "unexpected close parenthesis in line %d\n" (clt2line clt))
+       | Common.Left _ :: seen_open -> loop seen_open rest
+       | Common.Right open_line :: _ ->
+           failwith
+             (Printf.sprintf
+                "disjunction parenthesis in line %d column 0 matched to normal parenthesis on line %d\n" open_line (clt2line clt)))
+    | (PC.TCPar0(clt),q) :: rest ->
+       (match seen_open with
+         [] ->
+           failwith
+             (Printf.sprintf
+                "unexpected close parenthesis in line %d\n" (clt2line clt))
+       | Common.Right _ :: seen_open -> loop seen_open rest
+       | Common.Left open_line :: _ ->
+           failwith
+             (Printf.sprintf
+                "normal parenthesis in line %d matched to disjunction parenthesis on line %d column 0\n" open_line (clt2line clt)))
+    | x::rest -> loop seen_open rest in
+  loop [] tokens
+
 (* ----------------------------------------------------------------------- *)
 (* top level initializers: a sequence of braces followed by a dot *)
 
@@ -1065,7 +1109,7 @@ let minus_to_nothing l =
       let (d,_,_,_,_,_,_,_) = get_clt tok in
       (match d with
        D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> true
-      | D.PLUS -> false
+      | D.PLUS | D.PLUSPLUS -> false
       | D.CONTEXT | D.UNIQUE | D.OPT -> false)
     with _ -> false in
   let rec minus_loop = function
@@ -1191,7 +1235,8 @@ let prepare_tokens tokens =
   find_top_init
     (translate_when_true_false (* after insert_line_end *)
        (insert_line_end
-         (detect_types false (find_function_names (detect_attr tokens)))))
+         (detect_types false
+            (find_function_names (detect_attr (check_parentheses tokens))))))
 
 let prepare_mv_tokens tokens =
   detect_types false (detect_attr tokens)
@@ -1213,7 +1258,7 @@ let rec consume_minus_positions = function
 let any_modif rule =
   let mcode x =
     match Ast0.get_mcode_mcodekind x with
-      Ast0.MINUS _ | Ast0.PLUS -> true
+      Ast0.MINUS _ | Ast0.PLUS -> true
     | _ -> false in
   let donothing r k e = k e in
   let bind x y = x or y in
@@ -1226,6 +1271,15 @@ let any_modif rule =
       donothing donothing in
   List.exists fn.VT0.combiner_rec_top_level rule
 
+let eval_virt virt =
+  List.iter
+    (function x ->
+      if not (List.mem x virt)
+      then
+        failwith
+          (Printf.sprintf "unknown virtual rule %s\n" x))
+    !Flag.defined_virtual_rules
+
 let drop_last extra l = List.rev(extra@(List.tl(List.rev l)))
 
 let partition_either l =
@@ -1240,7 +1294,9 @@ let partition_either l =
 let get_metavars parse_fn table file lexbuf =
   let rec meta_loop acc (* read one decl at a time *) =
     let (_,tokens) =
-      tokens_all table file true lexbuf [PC.TArobArob;PC.TMPtVirg] in
+      Data.call_in_meta
+       (function _ ->
+         tokens_all table file true lexbuf [PC.TArobArob;PC.TMPtVirg]) in
     let tokens = prepare_mv_tokens tokens in
     match tokens with
       [(PC.TArobArob,_)] -> List.rev acc
@@ -1281,8 +1337,8 @@ let get_rule_name parse_fn starts_with_name get_tokens file prefix =
       | Ast.GeneratedRulename (nm,a,b,c,d,e) ->
           Ast.GeneratedRulename (check_name nm,a,b,c,d,e)
       | Ast.ScriptRulename(s,deps) -> Ast.ScriptRulename(s,deps)
-      | Ast.InitialScriptRulename(s) -> Ast.InitialScriptRulename(s)
-      | Ast.FinalScriptRulename(s) -> Ast.FinalScriptRulename(s)
+      | Ast.InitialScriptRulename(s,deps) -> Ast.InitialScriptRulename(s,deps)
+      | Ast.FinalScriptRulename(s,deps) -> Ast.FinalScriptRulename(s,deps)
     else
       Ast.CocciRulename(Some(mknm()),Ast.NoDep,[],[],Ast.Undetermined,false) in
   Data.in_rule_name := false;
@@ -1312,17 +1368,15 @@ let parse_iso file =
               | _ -> failwith "Script rules cannot appear in isomorphism rules"
               in
            Ast0.rule_name := rule_name;
-           Data.in_meta := true;
            let iso_metavars =
              match get_metavars PC.iso_meta_main table file lexbuf with
                (iso_metavars,[]) -> iso_metavars
-             | _ -> failwith "unexpected inheritance in iso" in
-           Data.in_meta := false;
+             | _ -> failwith "unexpected inheritance in iso" in
            (* get the rule *)
            let (more,tokens) =
              get_tokens
                [PC.TIsoStatement;PC.TIsoExpression;PC.TIsoArgExpression;
-                 PC.TIsoTestExpression;
+                 PC.TIsoTestExpression; PC.TIsoToTestExpression;
                  PC.TIsoDeclaration;PC.TIsoType;PC.TIsoTopLevel] in
            let next_start = List.hd(List.rev tokens) in
            let dummy_info = ("",(-1,-1),(-1,-1)) in
@@ -1370,7 +1424,44 @@ let parse_iso_files existing_isos iso_files extra_path =
   Data.in_iso := false;
   existing_isos@(List.concat (List.rev res))
 
+(* None = dependency not satisfied
+   Some dep = dependency satisfied or unknown and dep has virts optimized
+   away *)
+let eval_depend dep virt =
+  let rec loop dep =
+    match dep with
+      Ast.Dep req | Ast.EverDep req ->
+       if List.mem req virt
+       then
+         if List.mem req !Flag.defined_virtual_rules
+         then Some Ast.NoDep
+         else None
+       else Some dep
+    | Ast.AntiDep antireq | Ast.NeverDep antireq ->
+       if List.mem antireq virt
+       then
+         if not(List.mem antireq !Flag.defined_virtual_rules)
+         then Some Ast.NoDep
+         else None
+       else Some dep
+    | Ast.AndDep(d1,d2) ->
+       (match (loop d1, loop d2) with
+         (None,_) | (_,None) -> None
+       | (Some Ast.NoDep,x) | (x,Some Ast.NoDep) -> x
+       | (Some x,Some y) -> Some (Ast.AndDep(x,y)))
+    | Ast.OrDep(d1,d2) ->
+       (match (loop d1, loop d2) with
+         (None,None) -> None
+       | (Some Ast.NoDep,x) | (x,Some Ast.NoDep) -> Some Ast.NoDep
+       | (None,x) | (x,None) -> x
+       | (Some x,Some y) -> Some (Ast.OrDep(x,y)))
+    | Ast.NoDep | Ast.FailDep -> Some dep
+    in
+  loop dep
+
 let parse file =
+  Lexer_cocci.init();
+  let rec parse_loop file =
   let table = Common.full_charpos_to_pos file in
   Common.with_open_infile file (fun channel ->
   let lexbuf = Lexing.from_channel channel in
@@ -1383,8 +1474,28 @@ let parse file =
     (true,data) ->
       (match List.rev data with
        ((PC.TArobArob as x),_)::_ | ((PC.TArob as x),_)::_ ->
-         let iso_files =
-           parse_one "iso file names" PC.include_main file data in
+         let include_and_iso_files =
+           parse_one "include and iso file names" PC.include_main file data in
+
+         let (include_files,iso_files,virt) =
+           List.fold_left
+             (function (include_files,iso_files,virt) ->
+               function
+                   Data.Include s -> (s::include_files,iso_files,virt)
+                 | Data.Iso s -> (include_files,s::iso_files,virt)
+                 | Data.Virt l -> (include_files,iso_files,l@virt))
+             ([],[],[]) include_and_iso_files in
+
+         List.iter (function x -> Hashtbl.add Lexer_cocci.rule_names x ())
+           virt;
+
+         let (extra_iso_files, extra_rules, extra_virt) =
+           let rec loop = function
+               [] -> ([],[],[])
+             | (a,b,c)::rest ->
+                 let (x,y,z) = loop rest in
+                 (a::x,b::y,c::z) in
+           loop (List.map parse_loop include_files) in
 
           let parse_cocci_rule ruletype old_metas
              (rule_name, dependencies, iso, dropiso, exists, is_expression) =
@@ -1393,14 +1504,12 @@ let parse file =
                rule_name :: !Data.inheritable_positions;
 
             (* get metavariable declarations *)
-            Data.in_meta := true;
             let (metavars, inherited_metavars) =
-              get_metavars PC.meta_main table file lexbuf in
-            Data.in_meta := false;
-            Hashtbl.add Data.all_metadecls rule_name metavars;
-            Hashtbl.add Lexer_cocci.rule_names rule_name ();
-            Hashtbl.add Lexer_cocci.all_metavariables rule_name
-              (Hashtbl.fold
+             get_metavars PC.meta_main table file lexbuf in
+           Hashtbl.add Data.all_metadecls rule_name metavars;
+           Hashtbl.add Lexer_cocci.rule_names rule_name ();
+           Hashtbl.add Lexer_cocci.all_metavariables rule_name
+             (Hashtbl.fold
                 (fun key v rest -> (key,v)::rest)
                 Lexer_cocci.metavariables []);
 
@@ -1410,6 +1519,11 @@ let parse file =
             let (_, plus_tokens) =
              split_token_stream (minus_to_nothing tokens) in
 
+           (*
+              print_tokens "minus tokens" minus_tokens;
+              print_tokens "plus tokens" plus_tokens;
+           *)
+
            let minus_tokens = consume_minus_positions minus_tokens in
            let minus_tokens = prepare_tokens minus_tokens in
            let plus_tokens = prepare_tokens plus_tokens in
@@ -1436,7 +1550,9 @@ let parse file =
               Printf.printf "before plus parse\n";
            *)
            let plus_res =
-             if !Flag.sgrep_mode2
+             (* put ignore_patch_or_match with * case, which is less
+                constraining *)
+             if !Flag.sgrep_mode2 or !D.ignore_patch_or_match
              then (* not actually used for anything, except context_neg *)
                List.map
                  (Iso_pattern.rebuild_mcode None).VT0.rebuilder_rec_top_level
@@ -1460,16 +1576,27 @@ let parse file =
               (iso, dropiso, dependencies, rule_name, exists)),
               (plus_res, metavars), ruletype), metavars, tokens) in
 
+         let rec collect_script_tokens = function
+             [(PC.EOF,_)] | [(PC.TArobArob,_)] | [(PC.TArob,_)] -> ""
+           | (PC.TScriptData(s),_)::xs -> s^(collect_script_tokens xs)
+           | toks ->
+               List.iter
+                 (function x ->
+                   Printf.printf "%s\n" (token2c x))
+                 toks;
+               failwith "Malformed script rule" in
+
           let parse_script_rule language old_metas deps =
             let get_tokens = tokens_script_all table file false lexbuf in
 
               (* meta-variables *)
-            Data.in_meta := true;
             let metavars =
-             get_script_metavars PC.script_meta_main table file lexbuf in
-            Data.in_meta := false;
+             Data.call_in_meta
+               (function _ ->
+                 get_script_metavars PC.script_meta_main table file lexbuf) in
 
             let exists_in old_metas (py,(r,m)) =
+             r = "virtual" or
               let test (rr,mr) x =
                 let (ro,vo) = Ast.get_meta_name x in
                 ro = rr && vo = mr in
@@ -1487,53 +1614,72 @@ let parse file =
 
               (* script code *)
             let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
-            let data =
-              match List.hd tokens with
-                (PC.TScriptData(s),_) -> s
-              | (PC.TArobArob,_) | (PC.TArob,_) -> ""
-              | _ -> failwith "Malformed script rule" in
+            let data = collect_script_tokens tokens in
             (more,Ast0.ScriptRule(language, deps, metavars, data),[],tokens) in
 
-          let parse_if_script_rule k language =
+          let parse_if_script_rule k language _ deps =
             let get_tokens = tokens_script_all table file false lexbuf in
 
               (* script code *)
             let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
-            let data =
-              match List.hd tokens with
-                (PC.TScriptData(s),_) -> s
-              | (PC.TArobArob,_) | (PC.TArob,_) -> ""
-              | _ -> failwith "Malformed script rule" in
-            (more,k (language, data),[],tokens) in
+            let data = collect_script_tokens tokens in
+            (more,k (language, deps, data),[],tokens) in
 
          let parse_iscript_rule =
            parse_if_script_rule
-             (function (language,data) ->
-               Ast0.InitialScriptRule(language,data)) in
+             (function (language,deps,data) ->
+               Ast0.InitialScriptRule(language,deps,data)) in
 
          let parse_fscript_rule =
            parse_if_script_rule
-             (function (language,data) ->
-               Ast0.FinalScriptRule(language,data)) in
+             (function (language,deps,data) ->
+               Ast0.FinalScriptRule(language,deps,data)) in
+
+         let do_parse_script_rule fn l old_metas deps =
+           match eval_depend deps virt with
+             Some deps -> fn l old_metas deps
+           | None ->  fn l old_metas Ast.FailDep in
 
           let parse_rule old_metas starts_with_name =
             let rulename =
              get_rule_name PC.rule_name starts_with_name get_tokens file
                "rule" in
             match rulename with
-              Ast.CocciRulename (Some s, a, b, c, d, e) ->
-                parse_cocci_rule Ast.Normal old_metas (s, a, b, c, d, e)
-            | Ast.GeneratedRulename (Some s, a, b, c, d, e) ->
-               Data.in_generating := true;
-                let res =
-                 parse_cocci_rule Ast.Generated old_metas (s,a,b,c,d,e) in
-               Data.in_generating := false;
-               res
-            | Ast.ScriptRulename(l,deps) -> parse_script_rule l old_metas deps
-            | Ast.InitialScriptRulename(l) -> parse_iscript_rule l
-            | Ast.FinalScriptRulename(l) -> parse_fscript_rule l
-            | _ -> failwith "Malformed rule name"
-            in
+              Ast.CocciRulename (Some s, dep, b, c, d, e) ->
+               (match eval_depend dep virt with
+                 Some (dep) ->
+                   parse_cocci_rule Ast.Normal old_metas (s,dep,b,c,d,e)
+               | None ->
+                   D.ignore_patch_or_match := true;
+                    let res =
+                     parse_cocci_rule Ast.Normal old_metas
+                       (s, Ast.FailDep, b, c, d, e) in
+                   D.ignore_patch_or_match := false;
+                   res)
+            | Ast.GeneratedRulename (Some s, dep, b, c, d, e) ->
+               (match eval_depend dep virt with
+                 Some (dep) ->
+                   Data.in_generating := true;
+                   let res =
+                     parse_cocci_rule Ast.Normal old_metas (s,dep,b,c,d,e) in
+                   Data.in_generating := false;
+                   res
+               | None ->
+                   D.ignore_patch_or_match := true;
+                   Data.in_generating := true;
+                    let res =
+                     parse_cocci_rule Ast.Normal old_metas
+                       (s, Ast.FailDep, b, c, d, e) in
+                   D.ignore_patch_or_match := false;
+                   Data.in_generating := false;
+                   res)
+            | Ast.ScriptRulename(l,deps) ->
+               do_parse_script_rule parse_script_rule l old_metas deps
+            | Ast.InitialScriptRulename(l,deps) ->
+               do_parse_script_rule parse_iscript_rule l old_metas deps
+            | Ast.FinalScriptRulename(l,deps)   ->
+               do_parse_script_rule parse_fscript_rule l old_metas deps
+            | _ -> failwith "Malformed rule name" in
 
          let rec loop old_metas starts_with_name =
            (!Data.init_rule)();
@@ -1551,22 +1697,27 @@ let parse file =
             if more then
               rule::
              (loop (metavars @ old_metas) (gen_starts_with_name more tokens))
-            else [rule];
-
-            in
-
-         (iso_files, loop [] (x = PC.TArob))
+            else [rule] in
+
+         (List.fold_left
+            (function prev -> function cur -> Common.union_set cur prev)
+            iso_files extra_iso_files,
+          (* included rules first *)
+          List.fold_left (function prev -> function cur -> cur@prev)
+            (loop [] (x = PC.TArob)) (List.rev extra_rules),
+          List.fold_left (@) virt extra_virt (*no dups allowed*))
       |        _ -> failwith "unexpected code before the first rule\n")
   | (false,[(PC.TArobArob,_)]) | (false,[(PC.TArob,_)]) ->
-      ([],([] : Ast0.parsed_rule list))
+      ([],([] : Ast0.parsed_rule list),[] (*virtual rules*))
   | _ -> failwith "unexpected code before the first rule\n" in
-  res)
+  res) in
+  parse_loop file
 
 (* parse to ast0 and then convert to ast *)
 let process file isofile verbose =
   let extra_path = Filename.dirname file in
-  Lexer_cocci.init();
-  let (iso_files, rules) = parse file in
+  let (iso_files, rules, virt) = parse file in
+  eval_virt virt;
   let std_isos =
     match isofile with
       None -> []
@@ -1577,8 +1728,8 @@ let process file isofile verbose =
     List.map
       (function
           Ast0.ScriptRule (a,b,c,d) -> [([],Ast.ScriptRule (a,b,c,d))]
-       | Ast0.InitialScriptRule (a,b) -> [([],Ast.InitialScriptRule (a,b))]
-       | Ast0.FinalScriptRule (a,b) -> [([],Ast.FinalScriptRule (a,b))]
+       | Ast0.InitialScriptRule (a,b,c) -> [([],Ast.InitialScriptRule (a,b,c))]
+       | Ast0.FinalScriptRule (a,b,c) -> [([],Ast.FinalScriptRule (a,b,c))]
        | Ast0.CocciRule
            ((minus, metavarsm,
              (iso, dropiso, dependencies, rule_name, exists)),
@@ -1622,8 +1773,8 @@ let process file isofile verbose =
                     failwith
                       "bad list of reserved names - all must be at start" in
               let minus = Test_exps.process minus in
-              let minus = Compute_lines.compute_lines minus in
-              let plus = Compute_lines.compute_lines plus in
+              let minus = Compute_lines.compute_lines false minus in
+              let plus = Compute_lines.compute_lines false plus in
               let is_exp =
                 (* only relevant to Flag.make_hrule *)
                 (* doesn't handle multiple minirules properly, but since
@@ -1664,10 +1815,10 @@ let process file isofile verbose =
               let minus_ast =
                 Ast0toast.ast0toast rule_name dependencies dropped_isos
                   exists minus is_exp ruletype in
+
               match function_prototypes with
                 None -> [(extra_meta @ metavars, minus_ast)]
-              | Some mv_fp ->
-                  [(extra_meta @ metavars, minus_ast); mv_fp])
+              | Some mv_fp -> [(extra_meta @ metavars, minus_ast); mv_fp])
 (*          Ast0.CocciRule ((minus, metavarsm, (iso, dropiso, dependencies, rule_name, exists)), (plus, metavars))*)
       rules in
   let parsed = List.concat parsed in
@@ -1678,9 +1829,10 @@ let process file isofile verbose =
   then List.iter Pretty_print_cocci.unparse code;
 
   let grep_tokens =
-    Common.profile_code "get_constants"
-      (fun () -> Get_constants.get_constants code) in (* for grep *)
+    Common.profile_code "get_constants" (* for grep *)
+      (fun () -> Get_constants.get_constants code) in
   let glimpse_tokens2 =
-    Common.profile_code "get_glimpse_constants"
-      (fun () -> Get_constants2.get_constants code neg_pos) in(* for glimpse *)
+    Common.profile_code "get_glimpse_constants" (* for glimpse *)
+      (fun () -> Get_constants2.get_constants code neg_pos) in
+
   (metavars,code,fvs,neg_pos,ua,pos,grep_tokens,glimpse_tokens2)