|
| 1 | +open Cil |
| 2 | + |
| 3 | +(* Finds definition of a user-defined type *) |
| 4 | +let find_def name file = |
| 5 | + BatList.filter_map |
| 6 | + (function |
| 7 | + | GType (info, loc) -> |
| 8 | + if String.compare name info.tname = 0 then Some ("", loc, name, -1) |
| 9 | + else None |
| 10 | + | GCompTag (info, loc) -> |
| 11 | + if String.compare name info.cname = 0 then Some ("", loc, name, -1) |
| 12 | + else None |
| 13 | + | GEnumTag (info, loc) -> |
| 14 | + if String.compare name info.ename = 0 then Some ("", loc, name, -1) |
| 15 | + else None |
| 16 | + | _ -> None) |
| 17 | + file.globals |
| 18 | + |
| 19 | +(* Finds all definition of user-defined types *) |
| 20 | +let find_def_all file = |
| 21 | + BatList.filter_map |
| 22 | + (function |
| 23 | + | GType (info, loc) -> Some ("", loc, info.tname, -1) |
| 24 | + | GCompTag (info, loc) -> Some ("", loc, info.cname, -1) |
| 25 | + | GEnumTag (info, loc) -> Some ("", loc, info.ename, -1) |
| 26 | + | _ -> None) |
| 27 | + file.globals |
| 28 | + |
| 29 | +let find_in_globals list name = |
| 30 | + BatList.filter_map |
| 31 | + (function |
| 32 | + | GVar (info, _, _) -> |
| 33 | + if |
| 34 | + String.compare name |
| 35 | + (String.trim (Pretty.sprint ~width:1 (d_type () info.vtype))) |
| 36 | + = 0 |
| 37 | + then Some info.vid |
| 38 | + else None |
| 39 | + | _ -> None) |
| 40 | + list |
| 41 | + |
| 42 | +let find_in_varinfos list name = |
| 43 | + BatList.filter_map |
| 44 | + (fun info -> |
| 45 | + if |
| 46 | + String.compare name |
| 47 | + (String.trim (Pretty.sprint ~width:1 (d_type () info.vtype))) |
| 48 | + = 0 |
| 49 | + then Some info.vid |
| 50 | + else None) |
| 51 | + list |
| 52 | + |
| 53 | +let find_fundec globals name = |
| 54 | + let gfun = |
| 55 | + BatList.find_opt |
| 56 | + (function |
| 57 | + | GFun (fundec, _) -> String.compare fundec.svar.vname name = 0 |
| 58 | + | _ -> false) |
| 59 | + globals |
| 60 | + in |
| 61 | + match gfun with Some (GFun (fundec, _)) -> Some fundec | _ -> None |
| 62 | + |
| 63 | +let find_typevar_uses_in_fun list funname file = |
| 64 | + List.flatten |
| 65 | + @@ List.map (fun x -> FuncVar.find_uses_in_fun "" x funname file false) list |
| 66 | + |
| 67 | +(* Finds uses of a datatype in a function *) |
| 68 | +let find_uses_in_fun typename funname file = |
| 69 | + match find_fundec file.globals funname with |
| 70 | + | None -> [] |
| 71 | + | Some f -> |
| 72 | + find_typevar_uses_in_fun |
| 73 | + ( find_in_globals file.globals typename |
| 74 | + @ find_in_varinfos f.slocals typename |
| 75 | + @ find_in_varinfos f.sformals typename ) |
| 76 | + f.svar.vname file |
| 77 | + |
| 78 | +(* Finds all uses of a datatype in all functions *) |
| 79 | +let find_uses typename file = |
| 80 | + let list = FuncVar.find_uses_all file false in |
| 81 | + List.filter (fun (_, _, typ, _) -> String.compare typ typename = 0) list |
| 82 | + |
| 83 | +(* Finds all uses of a datatype in conditions *) |
| 84 | +let find_uses_in_cond typename file = |
| 85 | + let list = FuncVar.find_uses_in_cond_all file false in |
| 86 | + List.filter (fun (_, _, typ, _) -> String.compare typ typename = 0) list |
| 87 | + |
| 88 | +(* Finds all uses of a datatype in non-conditions *) |
| 89 | +let find_uses_in_noncond typename file = |
| 90 | + let list = FuncVar.find_uses_in_noncond_all file false in |
| 91 | + List.filter (fun (_, _, typ, _) -> String.compare typ typename = 0) list |
0 commit comments