Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I identify and get rid of unused units in the "uses clause" in Delphi 7?

This should reduce the executable size quite a bit in some of my very large projects. I am sure there would be other benefits too.

EDIT: Is there perhaps a utility that will scan the project and remove redundant ones automatically? I do have 100s of projects and "automatic remove" would be first prize although if I have to I will go the manual way with the help of identifying utilities.

like image 316
Johan Bresler Avatar asked May 19 '09 09:05

Johan Bresler


4 Answers

You might want to take a look at at CnPack.

CnPack includes a Uses cleaner wizard wich hasn't failed me yet.

like image 113
Lieven Keersmaekers Avatar answered Nov 06 '22 15:11

Lieven Keersmaekers


I used to use Icarus that gave me a report of unused uses entries.

From the web page

ICARUS parses Delphi or Borland Pascal source code and generates a Uses Report. This report will help you remove unneeded units from your uses lists. You will also know which units that can be moved from the interface uses list to the implementation uses list.

Removing unused uses references has multiple benefits: Cleaner code to maintain, no need to bother about code that is not used Code from initialization and finalization sections in unused units is not linked in Compilation runs smoother and quicker

ICARUS is very easy to use, just select a source file, set a few options, and start analyzing. Or let the built-in wizard guide you through this process.

ICARUS is FREEWARE.

like image 22
Johnno Nolan Avatar answered Nov 06 '22 13:11

Johnno Nolan


Such a tool can never analyze if the initialization or finalization sections of a used unit need to be executed or not. Removing units from the uses could result in not running initialization and finalization sections and therefore result in bugs.

like image 2
Lars Truijens Avatar answered Nov 06 '22 15:11

Lars Truijens


If your code can be compiled with Free Pascal, the Free Pascal compiler also will emit hints about unused units in your code.

See also this SO entry with additional information about possible problems, like RTTI based methods:

https://stackoverflow.com/questions/605977/tools-to-detect-dead-code-in-delphi2007-or-above/

like image 2
mjn Avatar answered Nov 06 '22 14:11

mjn