views:

665

answers:

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.

+1  A: 

See some of the answers here.

Ulrich Gerhardt
+8  A: 

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.

John Nolan
+2  A: 

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.

Lars Truijens
Maybe the remove can be an optional question in such a utility?
mm2010
Units where only the initialization/finalization sections are important are quite rare. In general, these sections are used to initialize code for the unit itself, not for the generic application. Furthermore, a tool like ICARUS does check for these sections or can be made to analyze those sections and check if they're important.
Workshop Alex
CnPack has an option to skip units with initialization
mjustin
+6  A: 

You might want to take a look at at CnPack.

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

Lieven
Does it do an automatic remove if desired?
mm2010
+1 for CnPack - it has some smart options which avoid common pitfalls like units with initialization sections
mjustin
@mm2010, it has. As a matter of fact, it doesn't have a "show me what you are going to remove" option, only a report "after" the action of what was removed. Although I never had any problems with it, I recommend you have your source files backed up in a source control system.
Lieven
It should be noted that CNPack uses cleaner only analyzes units used in the DPR, as far as I could tell it doesn't check the uses of the individual units. So it's not all that useful if your company policy is to go out of your way to delete that info.
Peter Turner
Newer versions of cnPack display a pretty treeview showing all intended operations, and iirc you can override (by unchecking) the actions.
mjustin
+1  A: 

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:

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

mjustin
+1  A: 

Peganza has similar tools: http://www.peganza.com/

Marco van de Voort
+2  A: 

Remember that the linker will strip anything that can't be called. If no use is made of a unit and it has no initialization or finalization it's going to be completely stripped anyway. Anything referenced in the initialization or finalization will stay but in general this shouldn't bring in much anyway.

It's possible for a unit to consist of only initialization so full-auto stripping is limited anyway. Units which merely change system behavior in some fashion might very well have no references and yet be needed. (The replacement memory manager with good leak tracking comes to mind. While it does have things you can reference there's no need to, just use it and get the leak report. Nice double-check for your code.)

Loren Pechtel