How to detect in runtime if some Compiler Option (like Assertions) was set to ON?

我是研究僧i 提交于 2019-12-05 05:43:32

You can do this using the $IFOPT directive:

{$IFOPT C+}
  // this block conditionally compiled if and only if assertions are active
{$ENDIF}

So you could re-write your code like this:

procedure Whatever;
{$IFOPT C+}
var
   v : Integer;
{$ENDIF}
begin
   {$IFOPT C+}v := {$ENDIF}DoSomething;
   {$IFOPT C+}Assert(v >= 0);{$ENDIF}
end;

This will suppress the compiler hint, but it also makes your eyes bleed.

I would probably suppress it like this:

procedure SuppressH2077ValueAssignedToVariableNeverUsed(const X); inline;
begin
end;

procedure Whatever;
var
   v : Integer;
begin
   v := DoSomething;
   Assert(v >= 0);
   SuppressH2077ValueAssignedToVariableNeverUsed(v);
end;

The untyped parameter that the suppress function receives is sufficient to suppress H2077. And the use of inline means that the compiler emits no code since there is no function call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!