Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Dead code removal / cross references
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: e@f...
Subject: Re: Dead code removal / cross references
>called/used ? which modules are used by this one ? is this #open
>really necessary ?) and detect and remove the useless #open and the
>dead code. Does such a tool exists ?

Here is a small piece of what you need...

(* usedmodu.ml *)
(* Copyright ) e, 1994. All rights reserved.
        This code may be freely distributed as long as this notice remains.
*)

(* To read a file, and return the list of the modules used *)

let letter_p c =
  let x = int_of_char c in
    ((x > 0x40) & (x < 0x5b)) or ((x > 0x60) & (x < 0x7b))
;;

let identchp c =
  let x = int_of_char c in
    ((x > 0x40) & (x < 0x5b))
 or ((x > 0x60) & (x < 0x7b))
 or ((x > 0x2f) & (x < 0x3a))
;;

let sm ic =
  let buf = create_string 1024 in
  let mds = ref [] in
  let addm i =
    let m = (sub_string buf 0 i) in
      if mem m !mds
        then ()
        else mds := (m :: !mds) in
  let
  rec s0 () = (* eat whitespace *)
    while (*
          match input_char ic with
            ` ` | `\n` | `\r` | `\t` -> true
          | c -> set_nth_char buf 0 c; false
          *)
          let c = input_char ic in
            if (letter_p c) or (c = `#`)
            then begin set_nth_char buf 0 c; false end
            else true
    do () done;
    s1 1
  and s1 i = (* get token *)
    match input_char ic with
    (*
      ` ` | `\n` | `\r` | `\t` -> s5 i
    | *)
      `_` -> begin
               match input_char ic with
                  `_` -> s2 i
               | c -> let j = (succ i) in
                        set_nth_char buf i `_`;
                        set_nth_char buf j c;
                        s1 (succ j)
             end
    | c -> if identchp c
           then begin set_nth_char buf i c; s1 (succ i) end
           else s5 i
  and s2 i = (* record module, eat non-whsp *)
    addm i;
    while match input_char ic with
            ` ` | `\n` | `\r` | `\t` -> false
          | c -> true
    do () done;
    s0 ()
  and s5 i = (* look for #open *)
    if (i = 5) &
       ((nth_char buf 0) = `#`) &
       ((nth_char buf 1) = `o`) &
       ((nth_char buf 2) = `p`) &
       ((nth_char buf 3) = `e`) &
       ((nth_char buf 4) = `n`)
    then
      begin
        while (input_char ic) != `"` do () done;
        let rec f i =
                  let c = input_char ic in
                    if c = `"`
                    then addm i
                    else begin set_nth_char buf i c; f (succ i) end in
           f 0;
        s0 ()
      end
    else s0 ()
  in
  try
    s0 ()
  with End_of_file ->
    rev !mds
;;

let modules_used_by_file filename =
  let ic = open_in filename in
  let res = sm ic in
  close_in ic;
  res
;;

(* ********************************************* *)