How to detect at runtime if the Compiler (e.g. Claims) is set to ON?

What is a condition to check if statements are active in Delphi?

I would like to do something to suppress hints about unused variables when statements are not active in the code, like

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

In the above code, when statements are inactive, there is a hint that the variable v is assigned a value that is never used.

The code is in a library that will be used in various environments, so I would be able to check for statements specifically, and not as a conditional conditional like DEBUG.

+7
source share
1 answer

You can do this using the $IFOPT directive:

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

So, you can rewrite your code as follows:

 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 put it down like this:

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

The unexplored parameter that the suppression function receives is sufficient to suppress the H2077. And using inline means that the compiler does not issue any code, since there is no function call.

+17
source

All Articles