I have many units that contain many conditional directive blocks such as:
{$IFDEF DELPHI6ANDLOWER}
*Do something 1*
{$ELSE}
*Do something 2*
{$ENDIF}
Now, I decide to drop the support for Delphi 6 (VER140) and the lower versions.
Is there a tool that can do the magic? Hopefully, the above code becomes:
*Do something 2*
I have tested ModelMaker, CnPack, GExperts, but none of them could do the magic.
The Delphi compiler has three types of directives: s witch directives, parameter directives, and conditional directives. Conditional compilation lets us selectively compile parts of a source code depending on which conditions are set.
Delphi has always had a $IFDEF directive you could use to test whether a specific symbol was defined. (Delphi also has a $IFNDEF directive, with the opposite test.) This is used to obtain conditional compilation, as in {$IFDEF DEBUG] // executes only if the DEBUG directive is set ShowMessage ('Executing critical code');
By knowing the above symbols it is possible to write code which works with several versions of Delphi by using compiler directives to compile appropriate source code for each version. Note: symbol VER185, for example, is used to indicate Delphi 2007 compiler or an earlier version.
A preprocessor conditional compilation directive causes the preprocessor to conditionally suppress the compilation of portions of source code. These directives test a constant expression or an identifier to determine which tokens the preprocessor should pass on to the compiler and which tokens should be bypassed during preprocessing.
The freeware program DIPP from Delphi Inspiration can remove conditionals.
http://www.yunqa.de/delphi/doku.php/products/dipp/index
First, I confirm that the DIPP tool mentioned by @ctomita works perfectly for my case.
Second, I think that it might be useful to share with people how I use the tool to solve my case example.
//--------------------------------------
{$IFDEF ONE}
ShowMessage('If ONE was defined');
WriteLn('If ONE was defined');
{$IFDEF ONE_ONE}
ShowMessage('If ONE_ONE was defined');
WriteLn('If ONE_ONE was defined');
{$ENDIF}
{$ELSE}
ShowMessage('If ONE was not defined');
WriteLn('If ONE was not defined');
{$ENDIF}
//--------------------------------------
{$IFNDEF ONE}
ShowMessage('If ONE was not defined');
WriteLn('If ONE was not defined');
{$ELSE}
ShowMessage('If ONE was defined');
WriteLn('If ONE was defined');
{$ENDIF}
//--------------------------------------
To remove the all ONE
conditional block from Test1.pas and with an assumption that as if there is a conditional symbol named ZERO
currently defined, use this command from the Command Prompt:
dipp -o -c -dZERO -h-ONE "Test1.pas" "Test1_output.pas"
//--------------------------------------
ShowMessage('If ONE was not defined');
WriteLn('If ONE was not defined');
//--------------------------------------
ShowMessage('If ONE was not defined');
WriteLn('If ONE was not defined');
//--------------------------------------
Note that when the -c
option is specified, DIPP skips over code enclosed by undefined conditionals, inserts include files depending on defined conditionals. In other words, DIPP treats source codes like the compiler would.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With