text
stringlengths
12
786k
let last = ref [ ]
let met = Hashtbl . create 512
let eom ( ) = List . iter ( fun f -> f ( ) ) ! later ; later := [ ] ; Hashtbl . reset met
let add lab expr loc last_loc nb_occur = let has_val = match expr . exp_desc with | Texp_construct ( _ , { cstr_name = " None " ; _ } , _ ) -> false | _ -> true in let occur = let occur = if not ( Hashtbl . mem nb_occur lab ) then Hashtbl . add nb_occur lab 1 else Hashtbl . f...
let rec process loc args = List . iter ( function | ( _ , Some e ) -> check e | _ -> ( ) ) args ; if is_ghost loc then ( ) else begin let nb_occur = Hashtbl . create 256 in let last_loc = ! last_loc in let add lab expr = add lab expr loc last_loc nb_occur in let add = function | ( ...
let node_build loc expr = let rec loop loc expr = match expr . exp_desc with | Texp_function { arg_label = lab ; cases = [ { c_lhs = { pat_type ; _ } ; c_rhs = exp ; _ } ] ; _ } -> DeadType . check_style pat_type expr . exp_loc . Location . loc_start ; begin match lab wi...
let wrap f x y = if DeadFlag . ( ! optn . print || ! opta . print ) then f x y else ( )
let process val_loc args = wrap process val_loc args
let bad_files = ref [ ]
let main_files = Hashtbl . create 256
let rec collect_export ( ? mod_type = false ) path u stock = function | Sig_value ( id , ( { Types . val_loc ; val_type ; _ } as value ) ) when not val_loc . Location . loc_ghost && stock == decs -> if ! DeadFlag . exported . DeadFlag . print then export path u stock id val_lo...
let rec treat_exp exp args = match exp . exp_desc with | Texp_apply ( exp , in_args ) -> treat_exp exp ( in_args @ args ) | Texp_ident ( _ , _ , { Types . val_loc = { Location . loc_start = loc ; _ } ; _ } ) | Texp_field ( _ , _ , { lbl_loc = { Location . l...
let value_binding super self x = let old_later = ! DeadArg . later in DeadArg . later := [ ] ; incr depth ; let open Asttypes in begin match x with | { vb_pat = { pat_desc = Tpat_var ( _ , { loc = { Location . loc_start = loc1 ; loc_ghost = false ; _ } ; _ } ) ; _ }...
let structure_item super self i = let open Asttypes in begin match i . str_desc with | Tstr_type ( _ , l ) when ! DeadFlag . typ . DeadFlag . print -> List . iter DeadType . tstr l | Tstr_module { mb_name = { txt ; _ } ; _ } -> mods := txt :: ! mods ; DeadMod . defined :=...
let pat super self p = let pat_loc = p . pat_loc . Location . loc_start in let u s = let err = ( ! current_src , pat_loc , Printf . sprintf " unit pattern % s " s ) in style := err :: ! style in let open Asttypes in if DeadType . is_unit p . pat_type && ! DeadFlag . style . DeadFl...
let expr super self e = let rec extra = function | [ ] -> ( ) | ( Texp_coerce ( _ , typ ) , _ , _ ) :: l -> DeadObj . coerce e typ . ctyp_type ; extra l | _ :: l -> extra l in extra e . exp_extra ; let exp_loc = e . exp_loc . Location . loc_start in let open Ident in...
let collect_references = let super = Tast_mapper . default in let wrap f loc self x = let l = ! last_loc in let ll = ( loc x ) . Location . loc_start in if ll <> Lexing . dummy_pos then last_loc := ll ; let prev_last_class = ! DeadObj . last_class in let r = f self x in DeadObj . last_class...
let starts_with prefix s = let len = String . length prefix in if len > String . length s then false else String . sub s 0 len = prefix
let ends_with suffix s = let len = String . length suffix in let s_len = String . length s in if len > s_len then false else String . sub s ( s_len - len ) len = suffix
let remove_wrap name = let n = String . length name in let rec loop i = if i < n - 1 then if name . [ i ] == ' _ ' && name . [ i + 1 ] == ' _ ' then String . uncapitalize_ascii ( String . sub name ( i + 2 ) ( n - i - 2 ) ) else loop ( i + 1 ) else name in lo...
let kind fn = if not ( Sys . file_exists fn ) then begin prerr_endline ( " Warning : ' " ^ fn ^ " ' not found " ) ; ` Ignore end else if not ( DeadFlag . is_excluded fn ) then begin let good_exts base exts = let open DeadFlag in let good_ext base ext = let fn = base ^ ext in Sys ...
let regabs fn = current_src := fn ; hashtbl_add_unique_to_list abspath ( unit fn ) fn ; if ! DeadCommon . declarations then hashtbl_add_unique_to_list main_files ( unit fn ) ( )
let read_interface fn src = let open Cmi_format in try regabs src ; let u = unit fn in if ! DeadFlag . exported . DeadFlag . print || ! DeadFlag . obj . DeadFlag . print || ! DeadFlag . typ . DeadFlag . print then let f = collect_export [ Ident . create ( String . capitalize_ascii u ...
let assoc decs ( loc1 , loc2 ) = let fn1 = loc1 . Lexing . pos_fname and fn2 = loc2 . Lexing . pos_fname in let is_implem fn = fn . [ String . length fn - 1 ] <> ' i ' in let has_iface fn = fn . [ String . length fn - 1 ] = ' i ' || ( unit fn = unit ! current_src &...
let clean references loc = let fn = loc . Lexing . pos_fname in if ( fn . [ String . length fn - 1 ] <> ' i ' && unit fn = unit ! current_src ) then LocHash . remove references loc
let eom loc_dep = DeadArg . eom ( ) ; List . iter ( assoc decs ) loc_dep ; List . iter ( assoc DeadType . decs ) ! DeadType . dependencies ; if Sys . file_exists ( ! current_src ^ " i " ) then begin let clean = List . iter ( fun ( loc1 , loc2 ) -> clean references l...
let rec load_file fn = match kind fn with | ` Iface src when ! DeadCommon . declarations -> last_loc := Lexing . dummy_pos ; if ! DeadFlag . verbose then Printf . eprintf " Scanning % s \ n " %! fn ; read_interface fn src | ` Implem src -> let open Cmt_format in last_loc := Lexing . du...
let analyze_opt_args ( ) = List . iter ( fun f -> f ( ) ) ! DeadArg . last ; let all = ref [ ] in let tbl = Hashtbl . create 256 in let dec_loc loc = Hashtbl . mem main_files ( unit loc . Lexing . pos_fname ) in let analyze = fun ( loc , lab , has_val , call_site ) -...
let report_opt_args s l = let opt = if s = " NEVER " then ! DeadFlag . optn else ! DeadFlag . opta in let percent = percent opt in let rec report_opt_args nb_call = let open DeadFlag in let l = List . filter ( fun ( _ , _ , slot , ratio , _ ) -> let ratio = 1 . . - ratio in if...
let report_unused_exported ( ) = report_basic decs " UNUSED EXPORTED VALUES " ! DeadFlag . exported
let report_style ( ) = section " CODING STYLE " ; if ! style <> [ ] then begin style := List . fast_sort compare ! style ; let change = let ( fn , _ , _ ) = List . hd ! style in dir fn in List . iter ( fun ( fn , l , s ) -> if change fn then print_newline ( ) ; p...
let parse ( ) = let update_all print ( ) = DeadFlag . ( update_style ( ( if print = " all " then " " + else " " ) - ^ " all " ) ; update_basic " - E " DeadFlag . exported print ; update_basic " - M " obj print ; update_basic " - T " typ print ; update_o...
let ( ) = try parse ( ) ; DeadCommon . declarations := false ; let oldstyle = ! DeadFlag . style in DeadFlag . update_style " - all " ; List . iter load_file ! DeadFlag . directories ; DeadFlag . style := oldstyle ; Printf . eprintf " [ DONE ] \ n \ n " ; %! let o...
module LocSet = Set . Make ( struct type t = Lexing . position let compare = compare end )
module LocHash = struct include Hashtbl . Make ( struct type t = Lexing . position let hash x = let s = Filename . basename x . Lexing . pos_fname in Hashtbl . hash ( x . Lexing . pos_cnum , s ) let equal x y = x = y end ) let find_set h k = try find h k with Not_found -> LocSet . em...
let abspath : ( string , string ) Hashtbl . t = Hashtbl . create 256
let decs : ( Lexing . position , string * string ) Hashtbl . t = Hashtbl . create 256
let incl : ( Lexing . position , string * string ) Hashtbl . t = Hashtbl . create 256
let references : LocSet . t LocHash . t = LocHash . create 256
let fields : ( string , Lexing . position ) Hashtbl . t = Hashtbl . create 256
let style : ( string * Lexing . position * string ) list ref = ref [ ]
let last_loc = ref Lexing . dummy_pos
let current_src = ref " "
let mods : string list ref = ref [ ]
let depth = ref ( - 1 )
let declarations = ref true
let _variant = " : variant " :
let unit fn = try Filename . chop_extension ( Filename . basename fn ) with Invalid_argument _ -> fn
let is_ghost loc = loc . Lexing . pos_lnum <= 0 || loc . Lexing . pos_cnum - loc . Lexing . pos_bol < 0 || loc . Lexing . pos_fname = _none || loc . Lexing . pos_fname = " "
let check_underscore name = not ! DeadFlag . underscore || name . [ 0 ] <> ' _ '
let hashtbl_find_list hashtbl key = Hashtbl . find_all hashtbl key
let hashtbl_add_to_list hashtbl key elt = Hashtbl . add hashtbl key elt
let hashtbl_add_unique_to_list hashtbl key elt = if not ( List . mem elt ( hashtbl_find_list hashtbl key ) ) then Hashtbl . add hashtbl key elt
let rec hashtbl_remove_list hashtbl key = if Hashtbl . mem hashtbl key then begin Hashtbl . remove hashtbl key ; hashtbl_remove_list hashtbl key end
let hashtbl_replace_list hashtbl key l = hashtbl_remove_list hashtbl key ; List . iter ( fun elt -> hashtbl_add_to_list hashtbl key elt ) l
let hashtbl_merge_unique_list tbl1 key1 tbl2 key2 = List . iter ( fun elt -> hashtbl_add_unique_to_list tbl1 key1 elt ) ( hashtbl_find_list tbl2 key2 )
let find_path fn ( ? sep = ' ' ) / l = List . find ( fun path -> let lp = String . length path and lf = String . length fn in ( lp > lf && path . [ lp - lf - 1 ] = sep || lp = lf ) && String . sub path ( lp - lf ) lf = fn ) l
let find_abspath fn = find_path fn ( hashtbl_find_list abspath ( unit fn ) )
let exported ( flag : DeadFlag . basic ref ) loc = let fn = loc . Lexing . pos_fname in ! flag . DeadFlag . print && LocHash . find_set references loc |> LocSet . cardinal <= ! flag . DeadFlag . threshold && ( flag == DeadFlag . typ || ! DeadFlag . internal || fn . [ String ...
let section ( ? sub = false ) title = Printf . printf " % s % s :\ n % s \ n " ( if sub then " . >-> " else " . " ) > title ( String . make ( ( if sub then 5 else 2 ) + String . length title + 1 ) ( if sub then ' ' ~ else ' ' ) ) =
let separator ( ) = print_endline " Nothing else to report in this section " ; Printf . printf " % s \ n \ n \ n " ( String . make 80 ' ' ) -
let prloc ( ? call_site = false ) ? fn ( loc : Lexing . position ) = let file = loc . Lexing . pos_fname in let line = loc . Lexing . pos_lnum in let col = loc . Lexing . pos_cnum - loc . Lexing . pos_bol in begin match fn with | Some s -> print_string s | _ -> match find_abspath...
type opt_arg = { mutable with_val : Lexing . position list ; mutable without_val : Lexing . position list ; }
let opt_args : ( Lexing . position * string * bool * Lexing . position ) list ref = ref [ ]
module VdNode = struct type t = ( string list * Lexing . position option ) let vd_nodes = LocHash . create 256 let parents = LocHash . create 256 let get loc = assert ( not ( is_ghost loc ) ) ; try ( LocHash . find vd_nodes loc ) with Not_found -> let r = ( [ ] , None ) in ...
let export ( ? sep = " . " ) path u stock id loc = let value = String . concat " . " ( List . rev_map Ident . name path ) ^ sep ^ id . Ident . name in if not loc . Location . loc_ghost && ( u = unit loc . Location . loc_start . Lexing . pos_fname || u == _include )...
let abs loc = match find_abspath loc . Lexing . pos_fname with | s -> s | exception Not_found -> loc . Lexing . pos_fname
let dir first = let prev = ref @@ Filename . dirname first in fun s -> let s = Filename . dirname s in ! prev <> s && ( prev := s ; true )
let rec check_length len = function | [ ] -> len = 0 | _ :: l when len > 0 -> check_length ( len - 1 ) l | _ -> false
let pretty_print_call ( ) = let ghost = ref false in function | loc when not ( is_ghost loc ) -> prloc ~ call_site : true loc |> print_newline | _ when ! ghost -> ( ) | _ -> print_endline " ~ ghost " ; ~ ghost := true
let percent ( opt : DeadFlag . opt ) base = let open DeadFlag in 1 . . - ( float_of_int base ) . * ( 1 . . - opt . threshold . percentage ) . / 10 .
let report s ( ~ opt : DeadFlag . opt ) ( ? extra = " Called " ) l continue nb_call pretty_print reporter = if nb_call = 0 || l <> [ ] then begin section ~ sub ( : nb_call <> 0 ) @@ ( if nb_call = 0 then s else if DeadFlag . ( opt . threshold . optional ) = ` Both |...
let report_basic ? folder decs title ( flag : DeadFlag . basic ) = let folder = match folder with | Some folder -> folder | None -> fun nb_call -> fun loc ( fn , path ) acc -> let rec cut_main s pos = if pos = String . length s then s else if s . [ pos ] = ' . ' then String . su...
module DeadLexiFi = struct let sig_value : ( Types . value_description -> unit ) ref = ref ( fun _ -> ( ) ) let export_type : ( Lexing . position -> string -> unit ) ref = ref ( fun _ _ -> ( ) ) let type_ext : ( Typedtree . core_type -> unit ) ref = ref ( fun _ -> (...
type threshold = { exceptions : int ; percentage : float ; optional : [ ` Percent | ` Both ] }
type opt = { print : bool ; call_sites : bool ; threshold : threshold }
let opta = ref { print = false ; call_sites = false ; threshold = { exceptions = 0 ; percentage = 1 . ; optional = ` Percent } ; }
let optn = ref { print = false ; call_sites = false ; threshold = { exceptions = 0 ; percentage = 1 . ; optional = ` Percent } ; }
let update_opt opt s = let threshold s = let len = String . length s in if len > 5 && String . sub s 0 5 = " both " : then begin let limits = String . sub s 5 ( String . length s - 5 ) in let thr = let rec loop s pos len = if len = String . length s then s else if s . [ pos ] =...
type style = { opt_arg : bool ; unit_pat : bool ; seq : bool ; binding : bool }
let style = ref { opt_arg = false ; unit_pat = false ; seq = false ; binding = false ; }
let update_style s = let rec aux = function | ( b , " opt " ) :: l -> style := { ! style with opt_arg = b } ; aux l | ( b , " unit " ) :: l -> style := { ! style with unit_pat = b } ; aux l | ( b , " seq " ) :: l -> style := { ! style with seq = b } ; ...
type basic = { print : bool ; call_sites : bool ; threshold : int }
let exported : basic ref = ref ( { print = true ; call_sites = false ; threshold = 0 } : basic )
let obj = ref ( { print = true ; call_sites = false ; threshold = 0 ; } : basic )
let typ : basic ref = ref ( { print = true ; call_sites = false ; threshold = 0 } : basic )
let update_basic opt ( flag : basic ref ) = function | " all " -> flag := { ! flag with print = true } | " nothing " -> flag := { ! flag with print = false } | s -> flag := { ! flag with print = true } ; let threshold = let len = String . length s in if len > 6 && Strin...
let verbose = ref false
let set_verbose ( ) = verbose := true
let underscore = ref true
let set_underscore ( ) = underscore := false
let internal = ref false
let set_internal ( ) = internal := true
let normalize_path s = let rec split_path s = let open Filename in if s = current_dir_name || s = dirname s then [ s ] else ( basename s ) :: ( split_path ( dirname s ) ) in let rec norm_path = function | [ ] -> [ ] | x :: ( ( y :: _ ) as yss ) when x = y && x = Filename ...
let exclude , is_excluded = let tbl = Hashtbl . create 10 in let exclude s = Hashtbl . replace tbl ( normalize_path s ) ( ) in let is_excluded s = Hashtbl . mem tbl ( normalize_path s ) in exclude , is_excluded
let directories : string list ref = ref [ ]
let dyn_rec = ref [ ]
let str = Hashtbl . create 256
let used = ref [ ]
let field_link = Hashtbl . create 256