4

here's my problem, I'm tidying up some old code which I've modified over the years and removing redundent functions,

I can do it the slow way & comment it out and see if the compiler throws an error. But I'm just wondering if there are any tools which can which scan code and give a list of functions which call a given function.

I Had a look in ge-experts & icarus, but they only do this at the level of units & classes not down to functions.

any suggestions welcome, many thanks, Brian

Hamish_Fernsby
  • 558
  • 1
  • 7
  • 28
  • Similar problems, different question. [Finding unused (aka “dead”) code in Delphi](http://stackoverflow.com/q/4226970/576719) and [Tools to detect Dead code in delphi2007 or above](http://stackoverflow.com/q/605977/576719). – LU RD Jul 14 '12 at 17:07
  • SolarBrian: You only have only accepted 25% of the questions you've asked up until today. Such a low percentage may discourage people from taking the time to answer any questions you post in the future. If there is a posting below that you feel answers your question, you can click on the large check mark next to the answer to flag it as such. That awards the author points, and encourages others to answer any questions you ask in the future. – RobertFrank Jul 14 '12 at 19:50
  • Give a try to [Sonar Delphi Plugin](http://docs.codehaus.org/display/SONAR/Delphi+Plugin) by Sabre Airline Solutions. You can refer to this recent [Blog post](http://mikejustin.wordpress.com/2012/06/19/sonar-delphi-plugin/) by Michael Justin aka [mjn](http://stackoverflow.com/users/80901/mjn) here on SO. – menjaraz Jul 15 '12 at 08:53

5 Answers5

6

Recent versions of Delphi have "Search for References" available via context menu or Ctrl-Shift-Enter. This has the advantage over a simple "search in files" that it will find only references to the current function under the cursor and not also any function or other identifier with the same name.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
6

Compile your project. Then, in the IDE, those lines that are accessed (rather than being dead code) will have a blue dot in the left margin:

enter image description here

TLama
  • 75,147
  • 17
  • 214
  • 392
RobertFrank
  • 7,332
  • 11
  • 53
  • 99
  • In modern Delphi this approach is unreliable. With the default RTTI settings, functions can be called dynamically and so static compile time analysis cannot remove them. But in principle, and accounting for the above proviso, this is a very reasonable approach. – David Heffernan Jul 14 '12 at 16:38
  • Even though this does not answer the direct question, but since the question is a: [what-is-the-xy-problem](http://meta.stackexchange.com/q/66377), this is a reasonable answer. – LU RD Jul 14 '12 at 17:13
  • Extended RTTI can be turned off. And by the same definition it is not safe for old Delphi either, since that also RTTI for protected methods. – Marco van de Voort Jul 15 '12 at 13:39
4

No method is perfect, limitations of the one below are these:

  • the .MAP file will include functions that the linker cannot eliminate (for instance overrides of methods in classes touched by your code)
  • it will only give you method names, but if methods are overloaded multiple versions of these methods could be used

The big pro over using .MAP files is that they are easier to scan than blue dots in the code editor.

So it does answer your question to provide a list of functions. But that might not actually what you are after (:

Method using .MAP file:

  • change your project to include a detailed .MAP file
  • rebuild your project
  • the directory of your .EXE file now will include a .MAP file
  • scan the .MAP file for function names that are included in the .EXE

That .MAP file will exclude functions eliminated by the compiler and linker.
Those are a good indication of what 'dead' code you have.

In a similar way, you could use the JDBG information. That contains more context, but also requires you to write some tooling yourself.

Community
  • 1
  • 1
Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • This is only tractable if you have a list of all functions in the code – David Heffernan Jul 14 '12 at 17:47
  • I know, but it is more of a list than the blue dots in the IDE. What helps getting those lists is the source indexer from ModelMaker Code Explorer (http://www.modelmakertools.com/code-explorer/index.html). Not perfect either, I know. – Jeroen Wiert Pluimers Jul 14 '12 at 18:47
0

For Pascal this is trivial. First make a list of all the functions, then for each function search for it in the text not following the word "function" and followed by a "(". Awk would be a good tool for doing this.

stark
  • 12,615
  • 3
  • 33
  • 50
0

If your Delphi version is 2007 (or may be prior to it?), I strongly suggest you to consider using DGrok: Give it a try and you will see how capable it is (The demo app will tell).

As it was pointed out by the author, you still need to implement symbol table support, so that the tools can do refactorings or Find References: Roll your own...

Don't worry if you are stuck to Delphi, please head to PasParse (The Delphi port of the DGrok parser that was originally written in C# !) by Turbo87.

Notice that Turbo87 has forked the original Joe White's Dgrok (latest release in 2008) (Update to VS2010 and add some documentation for LexScanner).

menjaraz
  • 7,551
  • 4
  • 41
  • 81