| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* This file is part of Frama-C. *) |
| 4 |
(* *) |
| 5 |
(* Copyright (C) 2007-2022 *) |
| 6 |
(* CEA (Commissariat à l'énergie atomique et aux énergies *) |
| 7 |
(* alternatives) *) |
| 8 |
(* *) |
| 9 |
(* you can redistribute it and/or modify it under the terms of the GNU *) |
| 10 |
(* Lesser General Public License as published by the Free Software *) |
| 11 |
(* Foundation, version 2.1. *) |
| 12 |
(* *) |
| 13 |
(* It is distributed in the hope that it will be useful, *) |
| 14 |
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *) |
| 15 |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *) |
| 16 |
(* GNU Lesser General Public License for more details. *) |
| 17 |
(* *) |
| 18 |
(* See the GNU Lesser General Public License version 2.1 *) |
| 19 |
(* for more details (enclosed in the file licenses/LGPLv2.1). *) |
| 20 |
(* *) |
| 21 |
(**************************************************************************) |
| 22 |
|
| 23 |
(* To avoid listing declarations several times, we use their locations |
| 24 |
as proxies. However, we cannot directly compare locations, since static |
| 25 |
(re-)definitions, as well as prototypes included several times, |
| 26 |
have locations which are physically different (pos_cnum) |
| 27 |
despite being semantically identical (same file/line/column). |
| 28 |
The module below provides a hash table using the equality function |
| 29 |
corresponding to our needs. |
| 30 |
*) |
| 31 |
module SemanticLocs : sig |
| 32 |
include Hashtbl.S with type key = Cil_datatype.Location.t |
| 33 |
val is_empty: 'a t -> bool |
| 34 |
val keys: 'a t -> key list (* sorted w.r.t. cmp_start_semantic *) |
| 35 |
val elements: 'a t -> (key * 'a) list (* sorted w.r.t. cmp_start_semantic *) |
| 36 |
end = |
| 37 |
struct |
| 38 |
include |
| 39 |
Hashtbl.Make(struct |
| 40 |
type t = Cil_datatype.Location.t |
| 41 |
let equal = Cil_datatype.Location.equal_start_semantic |
| 42 |
let hash (b, _e) = Hashtbl.hash (b.Filepath.pos_path, b.Filepath.pos_lnum) |
| 43 |
end) |
| 44 |
let is_empty tbl = length tbl = 0 |
| 45 |
let keys tbl = |
| 46 |
let l = fold (fun loc _ acc -> loc :: acc) tbl [] in |
| 47 |
List.sort Cil_datatype.Location.compare_start_semantic l |
| 48 |
let elements tbl = |
| 49 |
let l = fold (fun loc v acc -> (loc, v) :: acc) tbl [] in |
| 50 |
List.sort (fun (l1, _v1) (l2, _v2) -> |
| 51 |
Cil_datatype.Location.compare_start_semantic l1 l2) l |
| 52 |
end |
| 53 |
|
| 54 |
module Self = Plugin.Register |
| 55 |
(struct |
| 56 |
let name = "list-functions" |
| 57 |
let shortname = "list-functions" |
| 58 |
let help = "prints the list of function definitions and declarations, \ |
| 59 |
along with their locations and number of statements, \ |
| 60 |
in text or JSON format" |
| 61 |
end) |
| 62 |
|
| 63 |
module PrintLibc = |
| 64 |
Self.False |
| 65 |
(struct |
| 66 |
let option_name = "-list-functions-libc" |
| 67 |
let help = "whether to print functions located within Frama-C's libc \ |
| 68 |
directory. Default: false" |
| 69 |
end) |
| 70 |
|
| 71 |
module PrintDeclarations = |
| 72 |
Self.False |
| 73 |
(struct |
| 74 |
let option_name = "-list-functions-declarations" |
| 75 |
let help = "whether to print function declarations. Default: false" |
| 76 |
end) |
| 77 |
|
| 78 |
module Output = |
| 79 |
Self.Filepath |
| 80 |
(struct |
| 81 |
let option_name = "-list-functions-output" |
| 82 |
let arg_name = "filename" |
| 83 |
let existence = Filepath.Indifferent |
| 84 |
let file_kind = "json" |
| 85 |
let help = "where to save the output, in JSON format. If omitted', \ |
| 86 |
then output to stdout in text format instead" |
| 87 |
end) |
| 88 |
|
| 89 |
type funinfo = { |
| 90 |
name : string; |
| 91 |
declarations : unit SemanticLocs.t; |
| 92 |
definitions : int (*number of statements*) SemanticLocs.t; |
| 93 |
(* Note: only static functions can have multiple definitions *) |
| 94 |
} |
| 95 |
|
| 96 |
class stmt_count_visitor = |
| 97 |
object |
| 98 |
inherit Visitor.frama_c_inplace |
| 99 |
val count = ref 0 |
| 100 |
method! vstmt_aux _s = |
| 101 |
incr count; |
| 102 |
Cil.DoChildren |
| 103 |
method get = !count |
| 104 |
end |
| 105 |
|
| 106 |
(* To find good locations for declarations and definitions, we use different |
| 107 |
methods: |
| 108 |
- For declarations, the Cabs AST information is much better than the Cil |
| 109 |
one, which erases declarations when a definition is found; |
| 110 |
- For definitions, the information seems to be equivalent, so we use the |
| 111 |
one in Kernel_function. |
| 112 |
*) |
| 113 |
|
| 114 |
(* Due to the fact that the Cabs AST contains no fc_stdlib attributes, we use a |
| 115 |
location-based approach. *) |
| 116 |
let located_within_framac_libc loc = |
| 117 |
let pos = fst loc in |
| 118 |
Filepath.is_relative ~base_name:Fc_config.framac_libc pos.Filepath.pos_path |
| 119 |
|
| 120 |
class fun_cabs_visitor print_libc = object(self) |
| 121 |
inherit Cabsvisit.nopCabsVisitor |
| 122 |
|
| 123 |
val decls : (string, 'a SemanticLocs.t) Hashtbl.t = Hashtbl.create 7 |
| 124 |
method get_decls = decls |
| 125 |
|
| 126 |
method private get_single_name (_spec, (name, _, _, _)) = name |
| 127 |
method private get_name (name, _, _, _) = name |
| 128 |
|
| 129 |
method private add_loc table name loc = |
| 130 |
if print_libc || not (located_within_framac_libc loc) then |
| 131 |
let locs_table = |
| 132 |
try |
| 133 |
Hashtbl.find table name |
| 134 |
with |
| 135 |
| Not_found -> |
| 136 |
let t = SemanticLocs.create 1 in |
| 137 |
Hashtbl.replace table name t; |
| 138 |
t |
| 139 |
in |
| 140 |
SemanticLocs.replace locs_table loc () |
| 141 |
|
| 142 |
method! vdef def = |
| 143 |
let open Cabs in |
| 144 |
match def with |
| 145 |
| FUNDEF _ -> |
| 146 |
(* we will use Cil information anyway *) |
| 147 |
Cil.SkipChildren |
| 148 |
| DECDEF (_, (_, name_list), loc) -> |
| 149 |
List.iter |
| 150 |
(function |
| 151 |
| ((name, PROTO _, _, _), _) -> |
| 152 |
self#add_loc decls name loc |
| 153 |
| _ -> () |
| 154 |
) name_list; |
| 155 |
Cil.SkipChildren |
| 156 |
| _ -> |
| 157 |
Cil.DoChildren |
| 158 |
|
| 159 |
end |
| 160 |
|
| 161 |
let print_json (fp : Filepath.Normalized.t) funinfos_json = |
| 162 |
try |
| 163 |
let oc = open_out (fp:>string) in |
| 164 |
let fmt = Format.formatter_of_out_channel oc in |
| 165 |
Json.pp fmt funinfos_json; |
| 166 |
Format.fprintf fmt "@."; |
| 167 |
close_out oc; |
| 168 |
Self.debug "List written to: %a" Filepath.Normalized.pretty fp |
| 169 |
with Sys_error msg -> |
| 170 |
Self.abort "cannot write JSON to %a: %s" |
| 171 |
Filepath.Normalized.pretty fp msg |
| 172 |
|
| 173 |
let pp_semlocs fmt t = |
| 174 |
Format.fprintf fmt "%a" |
| 175 |
(Pretty_utils.pp_list ~sep:", " Cil_datatype.Location.pretty) |
| 176 |
(SemanticLocs.keys t) |
| 177 |
|
| 178 |
let pp_loc_size fmt loc_size = |
| 179 |
let (loc, size) = loc_size in |
| 180 |
Format.fprintf fmt "%a (%d statement%s)" |
| 181 |
Cil_datatype.Location.pretty loc size (if size <> 1 then "s" else "") |
| 182 |
|
| 183 |
let pp_definitions fmt defs = |
| 184 |
Format.fprintf fmt "%a" |
| 185 |
(Pretty_utils.pp_list ~sep:", " pp_loc_size) |
| 186 |
(SemanticLocs.elements defs) |
| 187 |
|
| 188 |
let print_text funinfos = |
| 189 |
List.iter (fun fi -> |
| 190 |
if PrintDeclarations.get () || |
| 191 |
not (SemanticLocs.is_empty fi.definitions) then |
| 192 |
begin |
| 193 |
Format.printf "%s:" fi.name; |
| 194 |
begin |
| 195 |
if not (SemanticLocs.is_empty fi.definitions) then |
| 196 |
Format.printf " defined at %a;" |
| 197 |
pp_definitions fi.definitions |
| 198 |
end; |
| 199 |
if PrintDeclarations.get () && |
| 200 |
not (SemanticLocs.is_empty fi.declarations) then |
| 201 |
Format.printf " declared at %a" pp_semlocs fi.declarations; |
| 202 |
if SemanticLocs.(is_empty fi.definitions && is_empty fi.declarations) |
| 203 |
then |
| 204 |
Format.printf " called but never declared nor defined"; |
| 205 |
Format.printf "@." |
| 206 |
end |
| 207 |
) funinfos |
| 208 |
|
| 209 |
let get_size kf = |
| 210 |
let stmt_count_vis = new stmt_count_visitor in |
| 211 |
ignore Visitor.(visitFramacKf (stmt_count_vis :> frama_c_inplace) kf); |
| 212 |
stmt_count_vis#get |
| 213 |
|
| 214 |
let definitions_with_size name = |
| 215 |
let kfs = |
| 216 |
List.filter Kernel_function.is_definition |
| 217 |
(Globals.Functions.find_all_by_orig_name name) |
| 218 |
in |
| 219 |
let defs_with_size = SemanticLocs.create (List.length kfs) in |
| 220 |
List.iter (fun kf -> |
| 221 |
let n = get_size kf in |
| 222 |
let loc = Kernel_function.get_location kf in |
| 223 |
SemanticLocs.add defs_with_size loc n |
| 224 |
) kfs; |
| 225 |
defs_with_size |
| 226 |
|
| 227 |
let json_string_of_loc loc = |
| 228 |
`String (Format.asprintf "%a" Cil_datatype.Location.pretty loc) |
| 229 |
|
| 230 |
let json_list_of_loc_tbl tbl = |
| 231 |
let keys = SemanticLocs.keys tbl in |
| 232 |
`List (List.map json_string_of_loc keys) |
| 233 |
|
| 234 |
let json_array_of_loc_size (loc, size) = |
| 235 |
`Assoc [("location", json_string_of_loc loc); ("statements", `Int size)] |
| 236 |
|
| 237 |
let json_list_of_loc_size_tbl tbl = |
| 238 |
let elements = SemanticLocs.elements tbl in |
| 239 |
`List (List.map json_array_of_loc_size elements) |
| 240 |
|
| 241 |
let run () = |
| 242 |
if List.length (File.get_all ()) < 1 then begin |
| 243 |
Self.abort "no input files"; |
| 244 |
end; |
| 245 |
let cabs_files = Ast.UntypedFiles.get () in |
| 246 |
let vis = new fun_cabs_visitor (PrintLibc.get ()) in |
| 247 |
List.iter (fun file -> |
| 248 |
ignore Cabsvisit.(visitCabsFile (vis :> nopCabsVisitor) file) |
| 249 |
) cabs_files; |
| 250 |
let decls = vis#get_decls in |
| 251 |
let defs_without_decls = Globals.Functions.fold (fun kf acc -> |
| 252 |
if Kernel_function.is_definition kf then begin |
| 253 |
let orig_name = (Kernel_function.get_vi kf).vorig_name in |
| 254 |
if Hashtbl.mem decls orig_name then acc |
| 255 |
else Datatype.String.Set.add orig_name acc |
| 256 |
end |
| 257 |
else acc |
| 258 |
) Datatype.String.Set.empty |
| 259 |
in |
| 260 |
let funinfos = |
| 261 |
Hashtbl.fold (fun name declarations acc -> |
| 262 |
let definitions = definitions_with_size name in |
| 263 |
let fi = { name; definitions; declarations } in |
| 264 |
fi :: acc |
| 265 |
) decls [] |
| 266 |
in |
| 267 |
(* add data for defined functions not present in 'decls' *) |
| 268 |
let funinfos = |
| 269 |
Datatype.String.Set.fold (fun orig_name acc -> |
| 270 |
if not (Hashtbl.mem decls orig_name) then begin |
| 271 |
let definitions = definitions_with_size orig_name in |
| 272 |
let fi = { name = orig_name; definitions; |
| 273 |
declarations = SemanticLocs.create 0 } |
| 274 |
in |
| 275 |
fi :: acc |
| 276 |
end else |
| 277 |
acc |
| 278 |
) defs_without_decls funinfos |
| 279 |
in |
| 280 |
let funinfos = List.sort |
| 281 |
(fun fi1 fi2 -> Extlib.compare_ignore_case fi1.name fi2.name) funinfos |
| 282 |
in |
| 283 |
let outfp = Output.get () in |
| 284 |
if Filepath.Normalized.is_empty outfp then |
| 285 |
print_text funinfos |
| 286 |
else |
| 287 |
let funinfos_json = `List (List.map (fun fi -> |
| 288 |
let definitions = |
| 289 |
if SemanticLocs.is_empty fi.definitions then [] |
| 290 |
else |
| 291 |
[("definitions", json_list_of_loc_size_tbl fi.definitions)] |
| 292 |
in |
| 293 |
let declarations = |
| 294 |
if SemanticLocs.is_empty fi.declarations then [] |
| 295 |
else |
| 296 |
[("declarations", json_list_of_loc_tbl fi.declarations)] |
| 297 |
in |
| 298 |
`Assoc [(fi.name, `Assoc (definitions @ declarations))] |
| 299 |
) funinfos) |
| 300 |
in |
| 301 |
print_json outfp funinfos_json |
| 302 |
|
| 303 |
let () = Db.Main.extend run |