| 1 | (* |
| 2 | * Copyright 2012, INRIA |
| 3 | * Julia Lawall, Gilles Muller |
| 4 | * Copyright 2010-2011, INRIA, University of Copenhagen |
| 5 | * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix |
| 6 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen |
| 7 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix |
| 8 | * This file is part of Coccinelle. |
| 9 | * |
| 10 | * Coccinelle is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, according to version 2 of the License. |
| 13 | * |
| 14 | * Coccinelle is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * The authors reserve the right to distribute this or future versions of |
| 23 | * Coccinelle under other licenses. |
| 24 | *) |
| 25 | |
| 26 | |
| 27 | # 0 "./adjust_pragmas.ml" |
| 28 | (* Find a directive or comment at the end of a statement. Things with aft |
| 29 | given None, because they can accomodate their own directives or comments *) |
| 30 | |
| 31 | module Ast0 = Ast0_cocci |
| 32 | module Ast = Ast_cocci |
| 33 | module V0 = Visitor_ast0 |
| 34 | module VT0 = Visitor_ast0_types |
| 35 | |
| 36 | let call_right processor data s cont = |
| 37 | match processor data with |
| 38 | None -> None |
| 39 | | Some(pragmas,data) -> Some (pragmas,Ast0.rewrap s (cont data)) |
| 40 | |
| 41 | let left_mcode (a,b,info,mcodekind,d,e) = |
| 42 | match (info.Ast0.strings_before,mcodekind) with |
| 43 | ([],_) | (_,Ast0.PLUS _) -> None |
| 44 | | (l,_) -> Some(l,(a,b,{info with Ast0.strings_before = []},mcodekind,d,e)) |
| 45 | |
| 46 | let right_mcode (a,b,info,mcodekind,d,e) = |
| 47 | match (info.Ast0.strings_after,mcodekind) with |
| 48 | ([],_) | (_,Ast0.PLUS _) -> None |
| 49 | | (l,_) -> Some(l,(a,b,{info with Ast0.strings_after = []},mcodekind,d,e)) |
| 50 | |
| 51 | let update_before pragmas (info,x,adj) = |
| 52 | ({info with Ast0.strings_before = pragmas @ info.Ast0.strings_before}, |
| 53 | Ast0.PLUS Ast.ONE,adj)(*not sure what the arg should be... one seems safe*) |
| 54 | |
| 55 | let update_after pragmas (info,x) = |
| 56 | ({info with Ast0.strings_after = info.Ast0.strings_after @ pragmas}, |
| 57 | Ast0.PLUS Ast.ONE) (*not sure what the arg should be... one seems safe*) |
| 58 | |
| 59 | let rec right_decl d = |
| 60 | match Ast0.unwrap d with |
| 61 | Ast0.MetaDecl(name,pure) -> |
| 62 | call_right right_mcode name d |
| 63 | (function name -> Ast0.MetaDecl(name,pure)) |
| 64 | | Ast0.MetaField(name,pure) -> |
| 65 | call_right right_mcode name d |
| 66 | (function name -> Ast0.MetaField(name,pure)) |
| 67 | | Ast0.MetaFieldList(name,lenname,pure) -> |
| 68 | call_right right_mcode name d |
| 69 | (function name -> Ast0.MetaFieldList(name,lenname,pure)) |
| 70 | | Ast0.AsDecl(decl,asdecl) -> failwith "not possible" |
| 71 | | Ast0.Init(Some stg,ty,id,eq,ini,sem) -> |
| 72 | call_right right_mcode sem d |
| 73 | (function sem -> Ast0.Init(Some stg,ty,id,eq,ini,sem)) |
| 74 | | Ast0.Init(None,ty,id,eq,ini,sem) -> |
| 75 | call_right right_mcode sem d |
| 76 | (function sem -> Ast0.Init(None,ty,id,eq,ini,sem)) |
| 77 | | Ast0.UnInit(Some stg,ty,id,sem) -> |
| 78 | call_right right_mcode sem d |
| 79 | (function sem -> Ast0.UnInit(Some stg,ty,id,sem)) |
| 80 | | Ast0.UnInit(None,ty,id,sem) -> |
| 81 | call_right right_mcode sem d |
| 82 | (function sem -> Ast0.UnInit(None,ty,id,sem)) |
| 83 | | Ast0.MacroDecl(name,lp,args,rp,sem) -> |
| 84 | call_right right_mcode sem d |
| 85 | (function sem -> Ast0.MacroDecl(name,lp,args,rp,sem)) |
| 86 | | Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem) -> |
| 87 | call_right right_mcode sem d |
| 88 | (function sem -> Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem)) |
| 89 | | Ast0.TyDecl(ty,sem) -> |
| 90 | call_right right_mcode sem d |
| 91 | (function sem -> Ast0.TyDecl(ty,sem)) |
| 92 | | Ast0.Typedef(stg,ty,id,sem) -> |
| 93 | call_right right_mcode sem d |
| 94 | (function sem -> Ast0.Typedef(stg,ty,id,sem)) |
| 95 | | Ast0.DisjDecl(starter,decls,mids,ender) -> None |
| 96 | | Ast0.Ddots(dots,whencode) -> None |
| 97 | | Ast0.OptDecl(decl) -> |
| 98 | call_right right_decl decl d (function decl -> Ast0.OptDecl(decl)) |
| 99 | | Ast0.UniqueDecl(decl) -> |
| 100 | call_right right_decl decl d (function decl -> Ast0.UniqueDecl(decl)) |
| 101 | |
| 102 | let rec right_statement s = |
| 103 | match Ast0.unwrap s with |
| 104 | Ast0.FunDecl(bef,fi,name,lp,params,rp,lbrace,body,rbrace) -> None |
| 105 | | Ast0.Decl(bef,decl) -> |
| 106 | call_right right_decl decl s |
| 107 | (function decl -> Ast0.Decl(bef,decl)) |
| 108 | | Ast0.Seq(lbrace,body,rbrace) -> |
| 109 | call_right right_mcode rbrace s |
| 110 | (function rbrace -> Ast0.Seq(lbrace,body,rbrace)) |
| 111 | | Ast0.ExprStatement(exp,sem) -> |
| 112 | call_right right_mcode sem s |
| 113 | (function sem -> Ast0.ExprStatement(exp,sem)) |
| 114 | | Ast0.IfThen(iff,lp,exp,rp,branch1,aft) -> None |
| 115 | | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,aft) -> None |
| 116 | | Ast0.While(whl,lp,exp,rp,body,aft) -> None |
| 117 | | Ast0.Do(d,body,whl,lp,exp,rp,sem) -> |
| 118 | call_right right_mcode sem s |
| 119 | (function sem -> Ast0.Do(d,body,whl,lp,exp,rp,sem)) |
| 120 | | Ast0.For(fr,lp,first,e2,sem2,e3,rp,body,aft) -> None |
| 121 | | Ast0.Iterator(nm,lp,args,rp,body,aft) -> None |
| 122 | | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) -> |
| 123 | call_right right_mcode rb s |
| 124 | (function rb -> Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb)) |
| 125 | | Ast0.Break(br,sem) -> |
| 126 | call_right right_mcode sem s |
| 127 | (function sem -> Ast0.Break(br,sem)) |
| 128 | | Ast0.Continue(cont,sem) -> |
| 129 | call_right right_mcode sem s |
| 130 | (function sem -> Ast0.Continue(cont,sem)) |
| 131 | | Ast0.Label(l,dd) -> |
| 132 | call_right right_mcode dd s |
| 133 | (function dd -> Ast0.Label(l,dd)) |
| 134 | | Ast0.Goto(goto,l,sem) -> |
| 135 | call_right right_mcode sem s |
| 136 | (function sem -> Ast0.Goto(goto,l,sem)) |
| 137 | | Ast0.Return(ret,sem) -> |
| 138 | call_right right_mcode sem s |
| 139 | (function sem -> Ast0.Return(ret,sem)) |
| 140 | | Ast0.ReturnExpr(ret,exp,sem) -> |
| 141 | call_right right_mcode sem s |
| 142 | (function sem -> Ast0.ReturnExpr(ret,exp,sem)) |
| 143 | | Ast0.MetaStmt(name,pure) -> |
| 144 | call_right right_mcode name s |
| 145 | (function name -> Ast0.MetaStmt(name,pure)) |
| 146 | | Ast0.MetaStmtList(name,pure) -> |
| 147 | call_right right_mcode name s |
| 148 | (function name -> Ast0.MetaStmtList(name,pure)) |
| 149 | | Ast0.AsStmt(stm,asstm) -> failwith "not possible" |
| 150 | | Ast0.Disj(starter,statement_dots_list,mids,ender) -> None |
| 151 | | Ast0.Nest(starter,stmt_dots,ender,whn,multi) -> None |
| 152 | (* the following are None, because they can't be adjacent to an aft node *) |
| 153 | | Ast0.Exp(exp) -> None |
| 154 | | Ast0.TopExp(exp) -> None |
| 155 | | Ast0.Ty(ty) -> None |
| 156 | | Ast0.TopInit(init) -> None |
| 157 | | Ast0.Dots(d,whn) -> None |
| 158 | | Ast0.Circles(d,whn) -> None |
| 159 | | Ast0.Stars(d,whn) -> None |
| 160 | | Ast0.Include(inc,name) -> |
| 161 | call_right right_mcode name s |
| 162 | (function name -> Ast0.Include(inc,name)) |
| 163 | | Ast0.Undef(def,id) -> |
| 164 | (* nothing available for ident, and not sure code can appear |
| 165 | here anyway *) |
| 166 | None |
| 167 | | Ast0.Define(def,id,params,body) -> |
| 168 | call_right right_statement_dots body s |
| 169 | (function body -> Ast0.Define(def,id,params,body)) |
| 170 | | Ast0.OptStm(re) -> |
| 171 | call_right right_statement re s (function re -> Ast0.OptStm(re)) |
| 172 | | Ast0.UniqueStm(re) -> |
| 173 | call_right right_statement re s (function re -> Ast0.UniqueStm(re)) |
| 174 | |
| 175 | and right_statement_dots sd = |
| 176 | match Ast0.unwrap sd with |
| 177 | Ast0.DOTS([]) -> failwith "empty statement dots" |
| 178 | | Ast0.DOTS(s::r) -> |
| 179 | call_right right_statement s sd |
| 180 | (function s -> Ast0.DOTS(List.rev(s::r))) |
| 181 | | _ -> failwith "circles and stars not supported" |
| 182 | |
| 183 | let rec left_ty t = |
| 184 | match Ast0.unwrap t with |
| 185 | Ast0.ConstVol(cv,ty) -> |
| 186 | call_right left_mcode cv t (function cv -> Ast0.ConstVol(cv,ty)) |
| 187 | | Ast0.BaseType(ty,strings) -> |
| 188 | (match strings with |
| 189 | [] -> failwith "empty strings in type" |
| 190 | | s::r -> |
| 191 | call_right left_mcode s t (function s -> Ast0.BaseType(ty,s::r))) |
| 192 | | Ast0.Signed(sign,ty) -> |
| 193 | call_right left_mcode sign t (function sign -> Ast0.Signed(sign,ty)) |
| 194 | | Ast0.Pointer(ty,star) -> |
| 195 | call_right left_ty ty t (function ty -> Ast0.Pointer(ty,star)) |
| 196 | | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> |
| 197 | call_right left_ty ty t |
| 198 | (function ty -> Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2)) |
| 199 | | Ast0.FunctionType(Some ty,lp1,params,rp1) -> |
| 200 | call_right left_ty ty t |
| 201 | (function ty -> Ast0.FunctionType(Some ty,lp1,params,rp1)) |
| 202 | | Ast0.FunctionType(None,lp1,params,rp1) -> |
| 203 | call_right left_mcode lp1 t |
| 204 | (function lp1 -> Ast0.FunctionType(None,lp1,params,rp1)) |
| 205 | | Ast0.Array(ty,lb,size,rb) -> |
| 206 | call_right left_ty ty t (function ty -> Ast0.Array(ty,lb,size,rb)) |
| 207 | | Ast0.EnumName(kind,name) -> |
| 208 | call_right left_mcode kind t (function kind -> Ast0.EnumName(kind,name)) |
| 209 | | Ast0.EnumDef(ty,lb,ids,rb) -> |
| 210 | call_right left_ty ty t |
| 211 | (function ty -> Ast0.EnumDef(ty,lb,ids,rb)) |
| 212 | | Ast0.StructUnionName(kind,name) -> |
| 213 | call_right left_mcode kind t |
| 214 | (function kind -> Ast0.StructUnionName(kind,name)) |
| 215 | | Ast0.StructUnionDef(ty,lb,decls,rb) -> |
| 216 | call_right left_ty ty t |
| 217 | (function ty -> Ast0.StructUnionDef(ty,lb,decls,rb)) |
| 218 | | Ast0.TypeName(name) -> |
| 219 | call_right left_mcode name t (function name -> Ast0.TypeName(name)) |
| 220 | | Ast0.MetaType(name,x) -> |
| 221 | call_right left_mcode name t (function name -> Ast0.MetaType(name,x)) |
| 222 | | Ast0.AsType(ty,asty) -> failwith "not possible" |
| 223 | | Ast0.DisjType(starter,types,mids,ender) -> None |
| 224 | | Ast0.OptType(ty) -> |
| 225 | call_right left_ty ty t (function ty -> Ast0.OptType(ty)) |
| 226 | | Ast0.UniqueType(ty) -> |
| 227 | call_right left_ty ty t (function ty -> Ast0.UniqueType(ty)) |
| 228 | |
| 229 | let rec left_ident i = |
| 230 | match Ast0.unwrap i with |
| 231 | Ast0.Id(name) -> |
| 232 | call_right left_mcode name i (function name -> Ast0.Id(name)) |
| 233 | | Ast0.MetaId(name,a,b,c) -> |
| 234 | call_right left_mcode name i (function name -> Ast0.MetaId(name,a,b,c)) |
| 235 | | Ast0.MetaFunc(name,a,b) -> |
| 236 | call_right left_mcode name i (function name -> Ast0.MetaFunc(name,a,b)) |
| 237 | | Ast0.MetaLocalFunc(name,a,b) -> |
| 238 | call_right left_mcode name i |
| 239 | (function name -> Ast0.MetaLocalFunc(name,a,b)) |
| 240 | | Ast0.DisjId(starter,ids,mids,ender) -> None |
| 241 | | Ast0.OptIdent(id) -> |
| 242 | call_right left_ident id i (function id -> Ast0.OptIdent(id)) |
| 243 | | Ast0.UniqueIdent(id) -> |
| 244 | call_right left_ident id i (function id -> Ast0.UniqueIdent(id)) |
| 245 | | Ast0.AsIdent(id,asid) -> failwith "not possible" |
| 246 | |
| 247 | let left_fundecl name fninfo = |
| 248 | let fncall_right processor data cont = |
| 249 | match processor data with |
| 250 | None -> None |
| 251 | | Some(pragmas,data) -> Some (pragmas,cont data,name) in |
| 252 | match fninfo with |
| 253 | [] -> |
| 254 | (match left_ident name with |
| 255 | None -> None |
| 256 | | Some(pragmas,name) -> Some(pragmas,fninfo,name)) |
| 257 | | (Ast0.FStorage sto)::x -> |
| 258 | fncall_right left_mcode sto (function sto -> (Ast0.FStorage sto)::x) |
| 259 | | (Ast0.FType ty)::x -> |
| 260 | fncall_right left_ty ty (function ty -> (Ast0.FType ty)::x) |
| 261 | | (Ast0.FInline inl)::x -> |
| 262 | fncall_right left_mcode inl (function inl -> (Ast0.FInline inl)::x) |
| 263 | | (Ast0.FAttr atr)::x -> |
| 264 | fncall_right left_mcode atr (function atr -> (Ast0.FAttr atr)::x) |
| 265 | |
| 266 | let rec left_decl decl = |
| 267 | match Ast0.unwrap decl with |
| 268 | Ast0.MetaDecl(name,pure) -> |
| 269 | call_right right_mcode name decl |
| 270 | (function name -> Ast0.MetaDecl(name,pure)) |
| 271 | | Ast0.MetaField(name,pure) -> |
| 272 | call_right right_mcode name decl |
| 273 | (function name -> Ast0.MetaField(name,pure)) |
| 274 | | Ast0.MetaFieldList(name,lenname,pure) -> |
| 275 | call_right right_mcode name decl |
| 276 | (function name -> Ast0.MetaFieldList(name,lenname,pure)) |
| 277 | | Ast0.AsDecl(decl,asdecl) -> failwith "not possible" |
| 278 | | Ast0.Init(Some stg,ty,id,eq,ini,sem) -> |
| 279 | call_right left_mcode stg decl |
| 280 | (function stg -> Ast0.Init(Some stg,ty,id,eq,ini,sem)) |
| 281 | | Ast0.Init(None,ty,id,eq,ini,sem) -> |
| 282 | call_right left_ty ty decl |
| 283 | (function ty -> Ast0.Init(None,ty,id,eq,ini,sem)) |
| 284 | | Ast0.UnInit(Some stg,ty,id,sem) -> |
| 285 | call_right left_mcode stg decl |
| 286 | (function stg -> Ast0.UnInit(Some stg,ty,id,sem)) |
| 287 | | Ast0.UnInit(None,ty,id,sem) -> |
| 288 | call_right left_ty ty decl |
| 289 | (function ty -> Ast0.UnInit(None,ty,id,sem)) |
| 290 | | Ast0.MacroDecl(name,lp,args,rp,sem) -> |
| 291 | call_right left_ident name decl |
| 292 | (function name -> Ast0.MacroDecl(name,lp,args,rp,sem)) |
| 293 | | Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem) -> |
| 294 | call_right left_ident name decl |
| 295 | (function name -> Ast0.MacroDeclInit(name,lp,args,rp,eq,ini,sem)) |
| 296 | | Ast0.TyDecl(ty,sem) -> |
| 297 | call_right left_ty ty decl (function ty -> Ast0.TyDecl(ty,sem)) |
| 298 | | Ast0.Typedef(stg,ty,id,sem) -> |
| 299 | call_right left_mcode stg decl |
| 300 | (function stg -> Ast0.Typedef(stg,ty,id,sem)) |
| 301 | | Ast0.DisjDecl(starter,decls,mids,ender) -> None |
| 302 | | Ast0.Ddots(dots,whencode) -> None |
| 303 | | Ast0.OptDecl(d) -> |
| 304 | call_right left_decl d decl (function decl -> Ast0.OptDecl(decl)) |
| 305 | | Ast0.UniqueDecl(d) -> |
| 306 | call_right left_decl d decl (function decl -> Ast0.UniqueDecl(decl)) |
| 307 | |
| 308 | let process = |
| 309 | let statement r k s = |
| 310 | let s = k s in |
| 311 | Ast0.rewrap s |
| 312 | (match Ast0.unwrap s with |
| 313 | Ast0.FunDecl(bef,fi,name,lp,params,rp,lbrace,body,rbrace) -> |
| 314 | (match left_fundecl name fi with |
| 315 | None -> Ast0.unwrap s |
| 316 | | Some (pragmas,fi,name) -> |
| 317 | Ast0.FunDecl |
| 318 | (update_after pragmas bef, |
| 319 | fi,name,lp,params,rp,lbrace,body,rbrace)) |
| 320 | | Ast0.Decl(bef,decl) -> |
| 321 | (match left_decl decl with |
| 322 | None -> Ast0.unwrap s |
| 323 | | Some (pragmas,decl) -> |
| 324 | Ast0.Decl(update_after pragmas bef,decl)) |
| 325 | | Ast0.IfThen(iff,lp,exp,rp,branch1,aft) -> |
| 326 | (match right_statement branch1 with |
| 327 | None -> Ast0.unwrap s |
| 328 | | Some (pragmas,branch1) -> |
| 329 | Ast0.IfThen |
| 330 | (iff,lp,exp,rp,branch1,update_before pragmas aft)) |
| 331 | | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,aft) -> |
| 332 | (match right_statement branch2 with |
| 333 | None -> Ast0.unwrap s |
| 334 | | Some (pragmas,branch2) -> |
| 335 | Ast0.IfThenElse |
| 336 | (iff,lp,exp,rp,branch1,els,branch2, |
| 337 | update_before pragmas aft)) |
| 338 | | Ast0.While(whl,lp,exp,rp,body,aft) -> |
| 339 | (match right_statement body with |
| 340 | None -> Ast0.unwrap s |
| 341 | | Some (pragmas,body) -> |
| 342 | Ast0.While(whl,lp,exp,rp,body,update_before pragmas aft)) |
| 343 | | Ast0.For(fr,lp,first,e2,sem2,e3,rp,body,aft) -> |
| 344 | (match right_statement body with |
| 345 | None -> Ast0.unwrap s |
| 346 | | Some (pragmas,body) -> |
| 347 | Ast0.For |
| 348 | (fr,lp,first,e2,sem2,e3,rp,body, |
| 349 | update_before pragmas aft)) |
| 350 | | Ast0.Iterator(nm,lp,args,rp,body,aft) -> |
| 351 | (match right_statement body with |
| 352 | None -> Ast0.unwrap s |
| 353 | | Some (pragmas,body) -> |
| 354 | Ast0.Iterator(nm,lp,args,rp,body,update_before pragmas aft)) |
| 355 | | _ -> Ast0.unwrap s) in |
| 356 | |
| 357 | let res = V0.rebuilder |
| 358 | {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in |
| 359 | |
| 360 | List.map res.VT0.rebuilder_rec_top_level |