Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to locate unused event handlers in Delphi?

Finding dead code in Delphi is usually real simple: just compile and then scan for routines missing their blue dots. The smart linker's very good about tracking them down, most of the time.

Problem is, this doesn't work for event handlers because they're published methods, which (theoretically) could be invoked via RTTI somehow, even though this almost never happens in actual practice.

I'm trying to clean up a large VCL form unit that's been bent, folded, spindled and mutilated various times throughout its history. It would sure be nice if I had some way to find event handlers that aren't actually referenced by the form's DFM and delete them. Is there any easy way to do this? A plug-in IDE Expert, for example?

like image 1000
Mason Wheeler Avatar asked Mar 26 '09 21:03

Mason Wheeler


4 Answers

This is a bit ugly (OK, it's a lot ugly), but for one unit it's close to foolproof, and requires no additional tools:

  1. Make sure that the current version of the form is checked into source control!
  2. Go to the top of the interface of the class where the event handlers are. Delete all of the event handler method interfaces.
  3. Look at Code Explorer/Error Insight. The methods which have implementations but no interfaces will be highlighted. Delete the implementations.
  4. Now save the unit. Delphi will, one at a time, complained about the missing event handler for each event that is actually handled. Write these down as the errors come up.
  5. Check out the original version of the form, and remove the event handlers for anything not on your list.
like image 170
Craig Stuntz Avatar answered Nov 14 '22 03:11

Craig Stuntz


Use the "Rename Method" refactoring to rename each event handler. Check the "View references before refactoring" checkbox.

Check the Refactoring window. If the event handler is linked to a control, there will be a "VCL Designer Updates" section show which control(s) are linked to the method.

This will also show if the method is called from any other units, or is assigned programmatically.

Note: this is for D2006, may be slightly different in later versions.

like image 27
Gerry Coll Avatar answered Nov 14 '22 01:11

Gerry Coll


ModelMaker Code Explorer contains an so-called Event handler view. It also shows event handlers not connected to any component.

like image 4
Erwin Avatar answered Nov 14 '22 01:11

Erwin


I dont think this is possible from an automatic point of view. event handlers are activated when a particular event occurs inside an object. That the even is not triggered in a given run doesnt mean that there isnt an execution pathway to lead to it.

also you can assign handlers dynamically at runtime so whats used in one situation is not garuanteed.

e.g.

button.onclick := DefaultClickHandler;

button.onClick := SpecialClickHandler;

Assuming that the click handlers match the onclick event signature, but you wouldnt get a compile if the signature was incorrect.


however, you can probably find all the abandoned handlers by looking for all the methods that find have a (Sender: TObject) method signature and comparing that his of methods to those in the .dfm (make sure you save it as text if you are working with an older version of delphi), antyhing not wired up automatically would be suspect in my book.

--

if you dont want to go down the cygwin path, you can load the src and dfm into two TStirngLists and rip out the name/idents from each and generate a list with a couple of loops and some string manipulations. my guess is about 20 minutes of work to get something you can live with .

like image 2
MikeJ Avatar answered Nov 14 '22 03:11

MikeJ