Recent

Author Topic: Detect/Disable the -CR option in code?  (Read 1620 times)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Detect/Disable the -CR option in code?
« on: July 18, 2023, 10:55:19 pm »
Is there a way to detect (ie, via {$IFOPT} or other?), and preferably to disable, the -CR compiler option for a block of code?

I've got a situation where a user has -CR enabled, and some code (specifically, in Indy) is using object hard-casts (to utilize Delphi-style accessor classes to access protected members in other units) which is being silently converted into the as operator, which is breaking the code with EInvalidCast errors at runtime.

For context:

https://forum.lazarus.freepascal.org/index.php/topic,63996.msg485641.html#msg485641

Indy uses several accessor classes internally, and having -CR silently create calls to the as operator is going to wreak havoc with it.  I never knew about this feature before.  I'll have to start warning users not to compile Indy with -CR enabled, but it would be nice to either fail the compile if -CR is enabled, or be able to disable/restore -CR dynamically with compiler directives on a case-by-case basis.
« Last Edit: July 18, 2023, 11:02:11 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Kays

  • Hero Member
  • *****
  • Posts: 618
  • Whasup!?
    • KaiBurghardt.de
Re: Detect/Disable the ‑CR option in code?
« Reply #1 on: July 18, 2023, 11:16:33 pm »
Is there a way to detect (ie, via {$IFOPT} or other?), […] the -CR compiler option […]?
No, {$ifOpt} can only be used for those switches that have “short options”, i. e. those consisting of a single letter (e. g. {$R+})

Is there a way to […] disable, the -CR compiler option for a block of code? […]
The pattern usually looks like this:
Code: Pascal  [Select][+][-]
  1. statements_possibly_subject_to_object_checks;
  2. {$push}
  3. {$objectChecks off}
  4. statements_not_subject_to_object_checks;
  5. {$pop}
  6. further_statements_possibly_subject_to_object_checks;
Yours Sincerely
Kai Burghardt

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Re: Detect/Disable the ‑CR option in code?
« Reply #2 on: July 19, 2023, 10:10:52 pm »
The pattern usually looks like this:
Code: Pascal  [Select][+][-]
  1. statements_possibly_subject_to_object_checks;
  2. {$push}
  3. {$objectChecks off}
  4. statements_not_subject_to_object_checks;
  5. {$pop}
  6. further_statements_possibly_subject_to_object_checks;

Will that prevent the compiler from changing hard-casts into the as operator?  The documentation only mentions that {$OBJECTCHECKS ON} will check Self for nil when a method is called, and that it is ALSO enabled by the -CR compiler option.  Which implies that converting hard-casts into the as operator is handled separately?  Or, is the documentation just out of date?
« Last Edit: July 22, 2023, 11:19:09 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Re: Detect/Disable the ‑CR option in code?
« Reply #3 on: July 20, 2023, 05:48:11 pm »
Will that prevent the compiler from changing hard-casts into the as operator?

Apparently it does, so I will use this workaround.  Thanks.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6039
  • Compiler Developer
Re: Detect/Disable the -CR option in code?
« Reply #4 on: July 20, 2023, 09:33:37 pm »
Will that prevent the compiler from changing hard-casts into the as operator?

Apparently it does, so I will use this workaround.  Thanks.

It does indeed. Maybe file a documentation bug to have that improved...

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Re: Detect/Disable the -CR option in code?
« Reply #5 on: July 22, 2023, 11:35:35 pm »
It does indeed. Maybe file a documentation bug to have that improved...

Turns out there is already an open ticket that was submitted a year ago:

#39644: update of documentation for $OBJECTCHECKS and -CR compiler option

Which links to this discussion:

https://forum.lazarus.freepascal.org/index.php/topic,58944.msg439261.html

Which is the exact same issue I encountered.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018