The new PUSH/POP directive in Delphi 13

The {$PUSHOPT} and {$POPOPT} directives introduced in Delphi 13 are used to save and restore the current set of compiler options and warning configurations. Useful when we want to include .inc files or code sections that might alter compiler settings, and we want to ensure that these changes do not affect the rest of your code.

 

{$PUSHOPT} saves the current state of all or a selected subset of compiler options.

{$POPOPT} restores the compiler options and warnings to the state they were in at the last {$PUSHOPT}. It effectively removes the most recent changes to the compiler options.

 

The example below shows how we can temporarily disable the Range Checking. After calling {$POPOPT}, the change is forgotten.

procedure TForm1.FormCreate(Sender: TObject);
begin
  {$R+}          // Initial state: range checking on

  {$IFOPT R+}
  Memo1.Lines.Add('RangeChecking on');
  {$ELSE}
  Memo1.Lines.Add('RangeChecking off');
  {$ENDIF}

  {$PUSHOPT}     // Save the current state of compiler options
      {$R-}      // Disable the range checking option (temporary)

      {$IFOPT R+}
      Memo1.Lines.Add('RangeChecking on');
      {$ELSE}
      Memo1.Lines.Add('RangeChecking off');
      {$ENDIF}

  {$POPOPT}      // Restore the previous state of compiler options

  // Check to see if the state was restored correctly
  {$IFOPT R+}
  Memo1.Lines.Add('RangeChecking on');
  {$ELSE}
  Memo1.Lines.Add('RangeChecking off');
  {$ENDIF}
end;

The program will print:

RangeChecking on
RangeChecking off
RangeChecking on

 

More on similar subject in the Delphi in all its glory book.

Leave a Comment

Scroll to Top