Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / check_meta.ml
index daa2a2e..2d95149 100644 (file)
@@ -1,23 +1,45 @@
 (*
 (*
-* 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.
+ *)
+
+
+(*
+ * 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.
+ *)
 
 
 (* For minus fragment, checks that all of the identifier metavariables that
 
 
 (* For minus fragment, checks that all of the identifier metavariables that
@@ -28,13 +50,14 @@ fresh are used.  What is the issue about error variables? (don't remember) *)
 module Ast0 = Ast0_cocci
 module Ast = Ast_cocci
 module V0 = Visitor_ast0
 module Ast0 = Ast0_cocci
 module Ast = Ast_cocci
 module V0 = Visitor_ast0
+module VT0 = Visitor_ast0_types
 
 (* all fresh identifiers *)
 
 (* all fresh identifiers *)
-let fresh_table = (Hashtbl.create(50) : ((string * string), unit) Hashtbl.t)
+let fresh_table = (Hashtbl.create(50) : (Ast.meta_name, unit) Hashtbl.t)
 
 let warning s = Printf.fprintf stderr "warning: %s\n" s
 
 
 let warning s = Printf.fprintf stderr "warning: %s\n" s
 
-let promote name = (name,(),Ast0.default_info(),(),None)
+let promote name = (name,(),Ast0.default_info(),(),None,-1)
 
 (* --------------------------------------------------------------------- *)
 
 
 (* --------------------------------------------------------------------- *)
 
@@ -44,7 +67,7 @@ let find_loop table name =
     | x::xs -> (try Hashtbl.find x name with Not_found -> loop xs) in
   loop table
 
     | x::xs -> (try Hashtbl.find x name with Not_found -> loop xs) in
   loop table
 
-let check_table table minus (name,_,info,_,_) =
+let check_table table minus (name,_,info,_,_,_) =
   let rl = info.Ast0.pos_info.Ast0.line_start in
   if minus
   then
   let rl = info.Ast0.pos_info.Ast0.line_start in
   if minus
   then
@@ -82,31 +105,33 @@ let is_ifdef name =
 
 let ident context old_metas table minus i =
   match Ast0.unwrap i with
 
 let ident context old_metas table minus i =
   match Ast0.unwrap i with
-    Ast0.Id((name,_,info,_,_) : string Ast0.mcode) ->
-      let rl = info.Ast0.pos_info.Ast0.line_start in
-      let err =
-       if List.exists (function x -> x = name) old_metas
-           && (minus || Ast0.get_mcodekind i = Ast0.PLUS)
-       then
-         begin
-           warning
-             (Printf.sprintf
-                "line %d: %s, previously declared as a metavariable, is used as an identifier" rl name);
-             true
-         end
-       else false in
-      (match context with
-       ID ->
-         if not (is_ifdef name) && minus && not err(* warn only once per id *)
+      Ast0.Id((name,_,info,_,_,_) : string Ast0.mcode) ->
+       let rl = info.Ast0.pos_info.Ast0.line_start in
+       let is_plus i =
+         match Ast0.get_mcodekind i with Ast0.PLUS _ -> true | _ -> false in
+       let err =
+         if List.exists (function x -> x = name) old_metas
+           && (minus || is_plus i)
          then
          then
-           warning
-             (Printf.sprintf "line %d: should %s be a metavariable?" rl name)
-      | _ -> ())
-  | Ast0.MetaId(name,_,_) -> check_table table minus name
-  | Ast0.MetaFunc(name,_,_) -> check_table table minus name
-  | Ast0.MetaLocalFunc(name,_,_) -> check_table table minus name
-  | Ast0.OptIdent(_) | Ast0.UniqueIdent(_) ->
-      failwith "unexpected code"
+           begin
+             warning
+               (Printf.sprintf
+                  "line %d: %s, previously declared as a metavariable, is used as an identifier" rl name);
+             true
+           end
+         else false in
+         (match context with
+              ID ->
+                if not (is_ifdef name) && minus && not err(* warn only once per id *)
+                then
+                  warning
+                    (Printf.sprintf "line %d: should %s be a metavariable?" rl name)
+            | _ -> ())
+    | Ast0.MetaId(name,_,_) -> check_table table minus name
+    | Ast0.MetaFunc(name,_,_) -> check_table table minus name
+    | Ast0.MetaLocalFunc(name,_,_) -> check_table table minus name
+    | Ast0.OptIdent(_) | Ast0.UniqueIdent(_) ->
+       failwith "unexpected code"
 
 (* --------------------------------------------------------------------- *)
 (* Expression *)
 
 (* --------------------------------------------------------------------- *)
 (* Expression *)
@@ -333,8 +358,9 @@ and statement old_metas table minus s =
       ident GLOBAL old_metas table minus nm;
       dots (expression ID old_metas table minus) args;
       statement old_metas table minus body
       ident GLOBAL old_metas table minus nm;
       dots (expression ID old_metas table minus) args;
       statement old_metas table minus body
-  | Ast0.Switch(switch,lp,exp,rp,lb,cases,rb) ->
+  | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) ->
       expression ID old_metas table minus exp;
       expression ID old_metas table minus exp;
+      dots (statement old_metas table minus) decls;
       dots (case_line old_metas table minus) cases
   | Ast0.ReturnExpr(ret,exp,sem) -> expression ID old_metas table minus exp
   | Ast0.MetaStmt(name,_) ->     check_table table minus name
       dots (case_line old_metas table minus) cases
   | Ast0.ReturnExpr(ret,exp,sem) -> expression ID old_metas table minus exp
   | Ast0.MetaStmt(name,_) ->     check_table table minus name
@@ -362,12 +388,27 @@ and statement old_metas table minus s =
       parameter_list old_metas table minus params;
       dots (statement old_metas table minus) body
   | Ast0.Include(inc,s) -> () (* no metavariables possible *)
       parameter_list old_metas table minus params;
       dots (statement old_metas table minus) body
   | Ast0.Include(inc,s) -> () (* no metavariables possible *)
-  | Ast0.Define(def,id,_,body) ->
+  | Ast0.Define(def,id,params,body) ->
       ident GLOBAL old_metas table minus id;
       ident GLOBAL old_metas table minus id;
+      define_parameters old_metas table minus params;
       dots (statement old_metas table minus) body
       dots (statement old_metas table minus) body
+  | Ast0.Label(i,_) -> ident ID old_metas table minus i
   | Ast0.Goto(_,i,_) -> ident ID old_metas table minus i
   | _ -> () (* no metavariable subterms *)
 
   | Ast0.Goto(_,i,_) -> ident ID old_metas table minus i
   | _ -> () (* no metavariable subterms *)
 
+and define_param old_metas table minus p =
+  match Ast0.unwrap p with
+    Ast0.DParam(id) -> ident GLOBAL old_metas table minus id
+  | Ast0.DPComma(_) | Ast0.DPdots(_) | Ast0.DPcircles(_) ->
+      () (* no metavariable subterms *)
+  | Ast0.OptDParam(dp)    -> define_param old_metas table minus dp
+  | Ast0.UniqueDParam(dp) -> define_param old_metas table minus dp
+
+and define_parameters old_metas table minus x =
+  match Ast0.unwrap x with
+    Ast0.NoParams -> ()
+  | Ast0.DParams(lp,dp,rp) -> dots (define_param old_metas table minus) dp
+
 and fninfo old_metas table minus = function
     Ast0.FStorage(stg) -> ()
   | Ast0.FType(ty) -> typeC old_metas table minus ty
 and fninfo old_metas table minus = function
     Ast0.FStorage(stg) -> ()
   | Ast0.FType(ty) -> typeC old_metas table minus ty
@@ -387,6 +428,8 @@ and case_line old_metas table minus c =
       dots (statement old_metas table minus) code
   | Ast0.Case(case,exp,colon,code) ->
       dots (statement old_metas table minus) code
       dots (statement old_metas table minus) code
   | Ast0.Case(case,exp,colon,code) ->
       dots (statement old_metas table minus) code
+  | Ast0.DisjCase(_,case_lines,_,_) ->
+      List.iter (case_line old_metas table minus) case_lines
   | Ast0.OptCase(case) -> failwith "unexpected code"
 
 (* --------------------------------------------------------------------- *)
   | Ast0.OptCase(case) -> failwith "unexpected code"
 
 (* --------------------------------------------------------------------- *)
@@ -416,13 +459,13 @@ let positions table rules =
   let bind x y = () in
   let donothing r k e = k e in
   let fn =
   let bind x y = () in
   let donothing r k e = k e in
   let fn =
-    V0.combiner bind option_default
+    V0.flat_combiner bind option_default
       mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
       donothing donothing donothing donothing donothing donothing
       donothing donothing donothing donothing donothing donothing donothing
       donothing donothing in
 
       mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
       donothing donothing donothing donothing donothing donothing
       donothing donothing donothing donothing donothing donothing donothing
       donothing donothing in
 
-  List.iter fn.V0.combiner_top_level rules
+  List.iter fn.VT0.combiner_rec_top_level rules
 
 let dup_positions rules =
   let mcode x =
 
 let dup_positions rules =
   let mcode x =
@@ -441,33 +484,33 @@ let dup_positions rules =
     match Ast0.unwrap e with
       Ast0.DisjExpr(_,explist,_,_) ->
        List.fold_left Common.union_set option_default
     match Ast0.unwrap e with
       Ast0.DisjExpr(_,explist,_,_) ->
        List.fold_left Common.union_set option_default
-         (List.map r.V0.combiner_expression explist)
+         (List.map r.VT0.combiner_rec_expression explist)
     | _ -> k e in
 
   let typeC r k e = (* not sure relevent because "only after iso" *)
     match Ast0.unwrap e with
       Ast0.DisjType(_,types,_,_) ->
        List.fold_left Common.union_set option_default
     | _ -> k e in
 
   let typeC r k e = (* not sure relevent because "only after iso" *)
     match Ast0.unwrap e with
       Ast0.DisjType(_,types,_,_) ->
        List.fold_left Common.union_set option_default
-         (List.map r.V0.combiner_typeC types)
+         (List.map r.VT0.combiner_rec_typeC types)
     | _ -> k e in
 
   let declaration r k e =
     match Ast0.unwrap e with
       Ast0.DisjDecl(_,decls,_,_) ->
        List.fold_left Common.union_set option_default
     | _ -> k e in
 
   let declaration r k e =
     match Ast0.unwrap e with
       Ast0.DisjDecl(_,decls,_,_) ->
        List.fold_left Common.union_set option_default
-         (List.map r.V0.combiner_declaration decls)
+         (List.map r.VT0.combiner_rec_declaration decls)
     | _ -> k e in
 
   let statement r k e =
     match Ast0.unwrap e with
       Ast0.Disj(_,stmts,_,_) ->
        List.fold_left Common.union_set option_default
     | _ -> k e in
 
   let statement r k e =
     match Ast0.unwrap e with
       Ast0.Disj(_,stmts,_,_) ->
        List.fold_left Common.union_set option_default
-         (List.map r.V0.combiner_statement_dots stmts)
+         (List.map r.VT0.combiner_rec_statement_dots stmts)
     | _ -> k e in
 
   let donothing r k e = k e in
   let fn =
     | _ -> k e in
 
   let donothing r k e = k e in
   let fn =
-    V0.combiner bind option_default
+    V0.flat_combiner bind option_default
       mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
       donothing donothing donothing donothing donothing donothing
       donothing expression typeC donothing donothing declaration statement
       mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
       donothing donothing donothing donothing donothing donothing
       donothing expression typeC donothing donothing declaration statement
@@ -476,11 +519,12 @@ let dup_positions rules =
   let res =
     List.sort compare
       (List.fold_left Common.union_set option_default
   let res =
     List.sort compare
       (List.fold_left Common.union_set option_default
-        (List.map fn.V0.combiner_top_level rules)) in
+        (List.map fn.VT0.combiner_rec_top_level rules)) in
   let rec loop = function
       [] | [_] -> ()
     | ((rule,name) as x)::y::_ when x = y ->
   let rec loop = function
       [] | [_] -> ()
     | ((rule,name) as x)::y::_ when x = y ->
-       failwith (Printf.sprintf "duplicate use of %s.%s" rule name)
+       failwith
+         (Printf.sprintf "duplicate use of %s.%s" rule name)
     | _::xs -> loop xs in
   loop res
 
     | _::xs -> loop xs in
   loop res
 
@@ -489,7 +533,7 @@ let dup_positions rules =
 let make_table l =
   let table =
     (Hashtbl.create(List.length l) :
 let make_table l =
   let table =
     (Hashtbl.create(List.length l) :
-       ((string * string), bool ref) Hashtbl.t) in
+       (Ast.meta_name, bool ref) Hashtbl.t) in
   List.iter
     (function x -> Hashtbl.add table (Ast.get_meta_name x) (ref false)) l;
   table
   List.iter
     (function x -> Hashtbl.add table (Ast.get_meta_name x) (ref false)) l;
   table