Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Is there a tool that can remove conditional directives for particular conditional symbols?

Tags:

delphi

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.

like image 263
Astaroth Avatar asked Oct 16 '12 17:10

Astaroth


People also ask

What are the different types of directives in Delphi?

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.

How do I test if a symbol is defined in Delphi?

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');

How to write code which works with several versions of Delphi?

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.

What is a preprocessor conditional compilation directive?

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.


2 Answers

The freeware program DIPP from Delphi Inspiration can remove conditionals.

http://www.yunqa.de/delphi/doku.php/products/dipp/index

like image 127
ctomita Avatar answered Oct 25 '22 18:10

ctomita


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.

Test1.pas

//--------------------------------------
{$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"

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.

like image 38
Astaroth Avatar answered Oct 25 '22 19:10

Astaroth