(*
+ * 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.
module VT0 = Visitor_ast0_types
(* 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 is_ifdef name =
String.length name > 2 && String.uppercase name = name
-let ident context old_metas table minus i =
+let rec 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 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
+ 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
+ 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
- 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"
-
+ 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.DisjId(_,id_list,_,_) ->
+ List.iter (ident context old_metas table minus) id_list
+ | Ast0.OptIdent(_) | Ast0.UniqueIdent(_) ->
+ failwith "unexpected code"
+
(* --------------------------------------------------------------------- *)
(* Expression *)
check_table table minus name
| Ast0.MetaExpr(name,_,_,_,_) | Ast0.MetaErr(name,_,_) ->
check_table table minus name
- | Ast0.MetaExprList(name,None,_) ->
- check_table table minus name
- | Ast0.MetaExprList(name,Some lenname,_) ->
+ | Ast0.MetaExprList(name,Ast0.MetaListLen lenname,_) ->
check_table table minus name;
check_table table minus lenname
+ | Ast0.MetaExprList(name,_,_) ->
+ check_table table minus name
| Ast0.DisjExpr(_,exps,_,_) ->
List.iter (expression context old_metas table minus) exps
| Ast0.NestExpr(_,exp_dots,_,w,_) ->
Type_cocci.ConstVol(_,ty) | Type_cocci.SignedT(_,Some ty)
| Type_cocci.Pointer(ty)
| Type_cocci.FunctionPointer(ty) | Type_cocci.Array(ty) -> get_type_name ty
+ | Type_cocci.EnumName(Type_cocci.MV(nm,_,_)) -> Some nm
+ | Type_cocci.StructUnionName(_,Type_cocci.MV(nm,_,_)) -> Some nm
| Type_cocci.MetaType(nm,_,_) -> Some nm
| _ -> None
check_table table minus name
| Ast0.DisjType(_,types,_,_) ->
List.iter (typeC old_metas table minus) types
- | Ast0.EnumName(en,id) -> ident GLOBAL old_metas table minus id
+ | Ast0.EnumName(en,Some id) -> ident GLOBAL old_metas table minus id
+ | Ast0.EnumDef(ty,lb,ids,rb) ->
+ typeC old_metas table minus ty;
+ dots (expression GLOBAL old_metas table minus) ids
| Ast0.StructUnionName(su,Some id) -> ident GLOBAL old_metas table minus id
| Ast0.StructUnionDef(ty,lb,decls,rb) ->
typeC old_metas table minus ty;
and declaration context old_metas table minus d =
match Ast0.unwrap d with
- Ast0.Init(stg,ty,id,eq,ini,sem) ->
+ Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_) ->
+ check_table table minus name
+ | Ast0.MetaFieldList(name,Ast0.MetaListLen lenname,_) ->
+ check_table table minus name;
+ check_table table minus lenname
+ | Ast0.MetaFieldList(name,_,_) ->
+ check_table table minus name
+ | Ast0.Init(stg,ty,id,eq,ini,sem) ->
(match Ast0.unwrap ini with
Ast0.InitExpr exp ->
typeC old_metas table minus ty;
Ast0.MetaInit(name,_) ->
check_table table minus name
| Ast0.InitExpr(exp) -> expression ID old_metas table minus exp
- | Ast0.InitList(lb,initlist,rb) ->
+ | Ast0.InitList(lb,initlist,rb,ordered) ->
dots (initialiser old_metas table minus) initlist
| Ast0.InitGccExt(designators,eq,ini) ->
List.iter (designator old_metas table minus) designators;
typeC old_metas table minus ty
| Ast0.MetaParam(name,_) ->
check_table table minus name
- | Ast0.MetaParamList(name,None,_) ->
- check_table table minus name
- | Ast0.MetaParamList(name,Some lenname,_) ->
+ | Ast0.MetaParamList(name,Ast0.MetaListLen lenname,_) ->
check_table table minus name;
check_table table minus lenname
+ | Ast0.MetaParamList(name,_,_) ->
+ check_table table minus name
| _ -> () (* no metavariable subterms *)
and parameter_list old_metas table minus =
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.Undef(def,id) ->
+ ident GLOBAL old_metas table minus id
+ | Ast0.Define(def,id,params,body) ->
ident GLOBAL old_metas table minus id;
+ define_parameters old_metas table minus params;
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 *)
+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
Ast0.Default(def,colon,code) ->
dots (statement old_metas table minus) code
| Ast0.Case(case,exp,colon,code) ->
+ expression GLOBAL old_metas table minus exp;
dots (statement old_metas table minus) code
| Ast0.DisjCase(_,case_lines,_,_) ->
List.iter (case_line old_metas table minus) case_lines
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
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