17

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.

Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • 4
    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 Jul 22 '13 at 19:12
  • 1
    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 Jul 22 '13 at 20:30
  • 3
    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 Jul 22 '13 at 20:55

5 Answers5

18

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.

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117
  • 1
    i used CnWizards - but it tends to remove way too much – Arioch 'The Jul 23 '13 at 06:05
  • We already done this by specially designed tool - and IDE speed was increased in several times. Compilation process - in about 5 times. (But I can't use this tool for another project.) – Dmitry Jul 23 '13 at 07:22
13

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.

Martin Schneider
  • 14,263
  • 7
  • 55
  • 58
norgepaul
  • 6,013
  • 4
  • 43
  • 76
4

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

House of Dexter
  • 386
  • 1
  • 7
  • Sounds interesting, although as [`the description`](http://www.peganza.com/products_icarus.htm) says, *ICARUS parses Delphi or Borland Pascal source code and generates a Uses Report.* so it's not much automated as was asked. – TLama Jul 22 '13 at 19:41
  • @TLama, there are cases where automatically removing "unused" units may break the program. Imagine a unit that registers a derived class that is nowhere else used directly. As a long time and happy user of Peganzas Pascal Analyzer (Icarus is a part of it) I am glad that there is no automatic removing. – Uwe Raabe Jul 22 '13 at 20:01
  • @Uwe, I know the risks, but CnWizard's `Uses Cleaner` offers you a [`dialog like this`](http://i.imgur.com/8WoWEb7.png) where you can select files you want to remove from the uses list and proceed that. It's not fully automated. It's upon you whose files you select and remove. With that report from Icarus you'd have to do all the work manually and that's annoying (and against *How to automatically remove unused units* as was asked in this question). – TLama Jul 22 '13 at 20:15
  • 1
    @TLAMA agreed it is manual...and I hadn't used Cleaner...but was familiar with ICARUS...so that was my answer/comment ;) – House of Dexter Jul 23 '13 at 13:59
  • Unfortunately, ICARUS doesn't actually identify unused units in using clauses. I've run it against projects with hundreds of known-unused units referenced by usings, and zero of them have been detected. – EKW Mar 31 '15 at 23:12
4

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.

Nicholas Ring
  • 1,016
  • 7
  • 14
  • i used CnWizards - but it tends to remove way too much for complex projects. – Arioch 'The Jul 23 '13 at 06:05
  • 1
    Just a heads-up. The process that does the work leaks memory, so unless you are on Berlin or better, it might run in to memory issues after a while (especially if you use ProjectGroups, which seems to leaks memory - my interpretation). – Nicholas Ring Apr 12 '17 at 08:23
0

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.

  • Hi Miguel, did you realize that the question you answered has been asked in 2013? And that there are already a number of good answers? And finally, look reading this: http://docwiki.embarcadero.com/RADStudio/Sydney/en/ReFind.exe,_the_Search_and_Replace_Utility_Using_Perl_RegEx_Expressions I don't think your answer is valid. Maybe I'm wrong? – fpiette May 15 '21 at 12:52
  • It is not an answer but still a useful tool for my purposes which I never heard of – Molochnik Nov 19 '22 at 21:03