views:

449

answers:

5

I am currently working with 'inherited' code that has (scattered randomly throughout) a whole bunch of conditional compiler directives based on the version of Delphi, going back to Delphi 2 . From now on, all development will be with Delphi 2009 or future. Is there a tool in Delphi 2009 , or a plugin, that will automatically remove compiler conditional code segments based on a specified 'minimum' version?

+1  A: 

I don't know of such a tool but it should be relatively straight-forward to write it.

  1. Loop through all files in the directory using FindFirst and FindNext
  2. Use TStringList.LoadFromFile to read all pas files.
  3. Loop through the strings and look for {$IfDef} directives. If the version specified in the conditional section is older than D2009 remove all text until the {$EndIf}.
  4. Use TStringList.SaveToFile to write the modified file to disk.
Smasher
Don't forget to be on the lookout for {$ELSE} and {$ELSEIF} as well, if you do it like this.
Michael Madsen
ditto {$if defined(x)} with which complete expressions can be made
Marco van de Voort
A: 

Use GExperts, you can use both GrepSearch or GrepRegularExpressions to search in your code and then use the Replace tool in the GrepResults to remove whatever you need.

You can do a search and replace operation on all of the matches in the list or only the selected file/match. When you choose one of those options, a dialog appears prompting for the string to use in place of the matched text. Note that forms that are currently open can not have their text replaced, due to limitations in the IDE. Please close all forms before trying to replace text in them.

Jorge Córdoba
Grep cannot really is nothing more like an editor trick. It doesn't understand ifndef conditionals (what if you have $if defined(y) and defined(x) } (and even then I would use something that's not manual, that I can retry several times, like SED).Only use stuff like this when there are totally no other options
Marco van de Voort
+4  A: 

You should give JEDI PreProcessor (Pascal PreProcessor) in the JCL a try.

In the trunk in our SVN the source can be found in the dir jcl/devtools/jpp and in our latest release (2.1) zip-file the jpp.exe can be found in the dir devtools.

Uwe Schuster
+5  A: 

I highly recommend the Delphi Inspiration Pascal Preprocessor (DIPP)

This can do a number of things with a source file in addition to removing conditional defines, including the "inlining" of include files and removing comments (all of course highly configurable and controllable by options supplied to the processor).

The conditional defines functionality is especially useful as you can either have all such conditionals simply removed or provide a set of DEFINE's that you wish to apply. DIPP will then emit a source file that reflects how it would appear to the compiler with those symbols DEFINED, but without the conditional directives themselves.

So in your case you would simply defined the symbols appropriate to your "baseline" Delphi version.

Deltics
+1 for DIPP. Didn't know about it, looks very interesting!
François
Looks very useful. Cheers Jolyon
HMcG
+1  A: 

My advice would be to ONLY change code you completely manage. If however you are also going to modify existing 3rd party code, then I suggest you go through each IFDEF defined for validation. Some vendors do not use the standard IFDEF VERxxx calls, but create their own which might be called something like VER70UP or such. The most common place for this would be in include files, so look for a {$I ???.INC} file near the beginning of each file, then analyze this for what is being used and how.

The other reason to analyze each $DEFINE/$UNDEF is the fact that a version specific one might turn on a new define that your previously not checking...one that ultimately leads into dead code.

skamradt