Sadly one can't check whether an optimization is enabled or not
Isn't that what {$IFOPT} is meant for? At least for optimizations with short-letter directives, like {$W} for {$STACKFRAMES}.
These are two different things. The option controls whether stackframes are enforced while the optimization controls whether they are stripped away.
To illustrate:
- if the option is set, but the optimization is not, then stackframes are generated for all routines
- if the option is set and the optimization is as well, then stackframes are generated for all routines
- if the option is not set, but the optimization is, then stackframes will be omitted when possible
- if the option is not set and the optimization is not either, then stackframes will be generated for all routines except assembly routines in mode Delphi with no local variables
Note: Both the option and the optimization are local, thus they (in this case) apply to the routine level, not a unit level.
The default is that
$W is not set and the stackframe optimization is set together with
-O2.
But you mentioned a good point: by protecting the routine with
{$push}{$W+} { function } {$pop} you can enforce the generation of a stackframe for that function.
To be on the safe side you could also add {$OPTIMIZATION NOAUTOINLINE} or {$OPTIMIZATION NOSTACKFRAME, NOAUTOINLINE}.
AUTOINLINE is not listed on 1.2.58 $OPTIMIZATION (see Undocumented optimization compiler directives), but it is mentioned in passing on Optimization.
Best would be to file a documentation bug. Please note that
AUTOINLINE is (for whatever reason) not part of the supported optimization list for any CPU target and thus it won't be listed in
fpc -io either.