Skip to content

Conversation

@fantazio
Copy link
Collaborator

@fantazio fantazio commented Dec 2, 2025

Fix #18

Reproducing the issue's example now works as expected:

steps to reproduce:

$ cat dune-project
(lang dune 2.0)
(package (name dead))
$ cat dune
(library (public_name dead) (modules lib_mod) (wrapped true))
(executable (public_name dead_bin) (modules dead_bin) (libraries dead))
$ cat lib_mod.ml
let dead () = print_endline "dead code"
let live () = print_endline "live code"
$ cat lib_mod.mli
val dead : unit -> unit
val live : unit -> unit
$ cat dead_bin.ml
let () = Dead.Lib_mod.live ()
(* let () = Lib_mod.live () *)

The dead_code_analyzer provides the following reports:

$ dune build
$ dead_code_analyzer _build/default/
Scanning files...
 [DONE]

.> UNUSED EXPORTED VALUES:
=========================
/home/fantasio/proj/wrapped/_build/default/lib_mod.mli:1: dead

Nothing else to report in this section
--------------------------------------------------------------------------------


.> UNUSED METHODS:
=================

Nothing else to report in this section
--------------------------------------------------------------------------------


.> UNUSED CONSTRUCTORS/RECORD FIELDS:
====================================

Nothing else to report in this section
--------------------------------------------------------------------------------

Key Changes

  • Use the module name found in the .cm[it] rather than the unit of the filename. DeadType.fields rely on them.
  • Use the "compilation unit" of the sourcepath when possible instead of the one of the .cm[it] file. This is compared with the unit of the source locations in DeadCommon.export.
  • Improve the sourcepath lookup on .cmi by checking all the locations found in them and selecting the one with the source filename the closest to the cmi filename.

With this change, the tool gives results on Frama-C (#23)

+ Extract file_infos
+ Add in-code documentation
+ Use sourcepath to compute compilation unit name
+ Use modname rather than filename for the current compilation unit's
  module
The `sourcepath` was extracted by using the location of the 1st
signature item found in the `cmi_sign`. As exposed by the
`examples/using_make/advanced/incl.ml` test, it would lead to the wrong
compilation unit associated with the module (here `mod.ml` for `Incl`),
resulting in both FP and FN.

Now, the `sourcepath` is the "best candidate" found in all the signature
items. The best candidate is defined in order as:
1. An exact match between the location's sourcefile's compilation unit
   and the cmi's estimated compilation unit.
2. The former is a suffix of the latter.
3. The former is a prefix of the latter
4. Mismatch is still better than no loc.

In case a new candidate would rank the same as the current best one, the
new one is kept.

As a side dev, a new module `Utils` is being defined. The long term goal
is to clear out `DeadCommon` and better organize its code.
@fantazio fantazio merged commit 6294e18 into LexiFi:master Dec 12, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrapped libraries support

1 participant