16
votes

How to automatically remove unused units from uses section on all source files of the project on Delphi XE2?

P.S. IDE will work much faster after that.

5
The question is made more exact. Please cancel "on hold" flag. The question is very important for every large project on Delphi and the answer will be very useful for all developers.Dmitry
Can you define exactly what you mean by "unused unit". Do note that units with initialization code may have an impact on your program even if you refer to none of their exported symbols.David Heffernan
Automatically removing units isn't as simple as you think. Some units have initialization sections that may do something that's needed (for instance, calling CoInitialize to set up COM) even though nothing else in the unit appears to be called. (Yes, I know you can manually call CoInitialize yourself; the point is that you can't always tell automatically what the unit might be doing.)Ken White

5 Answers

18
votes

There is no way to fully automate this.

There are a few tools I'm aware of that take a wizard approach:

  • CnPack Uses Units Cleaner
  • Peganza's Pascal Analyzer (and it's sidekick icarus).
  • The Lazarus IDE has a "Unused Units" dialog in it's CodeTools package.

Peganza's tools simply show a report. CnPack will prompt to remove the unused units for you but you must confirm. Lazarus presents you with a list of unit's it thinks are unused and gives you the choice of removing some or all of them.

Why isn't it automated?

Because this is static analysis. Even the most sophisticated tools cannot determine with 100% certainty whether a specific line of code will be used at runtime let alone an entire unit. These tools have implemented their own parsers to accomplish this feat but they aren't fool proof.

In any case the main benefit of cleaning up the uses clause is removing visual clutter from both the source itself and the code completion feature. Yes there is some performance gained during compiling and certain IDE background operations will speed up slightly but I think you'll be disappointed if you think the IDE will miraculously speed up.

You'll see better IDE and compiler performance by:

  1. Breaking your projects into smaller pieces that can be worked on independently.
  2. Eliminating duplicate code.
  3. Disabling unneeded IDE packages.

I'm certainly not trying to dissuade you from removing unused unit references. As I said it will help unclutter your source. Just make sure you're doing it for the right reasons.

13
votes

We have a utility called the Delphi Unit Dependency Scanner (DUDS). It scans your entire project and builds a fully searchable tree of the units and dependencies. It can be very useful in finding unused units.

The application is Freeware and you can find it here.

Disclaimer-I am the author.

4
votes

Don't think I would want a tool that would automatically rip out unnecessary units in the Uses section...

but there are tools out there to identify them...look at Icarus...freeware that you can get at http://www.peganza.com/downloads.htm

4
votes

CnPack has "Use Cleaner..." option that I have used unit by unit basis without a problem. It also has the ability to do the entire project - which I haven't tried due to the size of the project.

-1
votes

Use reFind.exe utility provided since Delphi XE, use this command

reFind *.pas /X:unuse.txt

And unuse.txt is a text file with something like this:

#unuse Unit1
#unuse Unit2
#unuse Unit3

And that's it. It will remove the units in the uses clause taking care if the unit is the last one used and there is a ; after the unit.