Next: , Previous: Pragma Annotate, Up: Implementation Defined Pragmas


Pragma Assert

Syntax:

     pragma Assert (
       boolean_EXPRESSION
       [, static_string_EXPRESSION]);

The effect of this pragma depends on whether the corresponding command line switch is set to activate assertions. The pragma expands into code equivalent to the following:

     if assertions-enabled then
        if not boolean_EXPRESSION then
           System.Assertions.Raise_Assert_Failure
             (string_EXPRESSION);
        end if;
     end if;

The string argument, if given, is the message that will be associated with the exception occurrence if the exception is raised. If no second argument is given, the default message is `file:nnn', where file is the name of the source file containing the assert, and nnn is the line number of the assert. A pragma is not a statement, so if a statement sequence contains nothing but a pragma assert, then a null statement is required in addition, as in:

     ...
     if J > 3 then
        pragma Assert (K > 3, "Bad value for K");
        null;
     end if;

Note that, as with the if statement to which it is equivalent, the type of the expression is either Standard.Boolean, or any type derived from this standard type.

If assertions are disabled (switch -gnata not used), then there is no effect (and in particular, any side effects from the expression are suppressed). More precisely it is not quite true that the pragma has no effect, since the expression is analyzed, and may cause types to be frozen if they are mentioned here for the first time.

If assertions are enabled, then the given expression is tested, and if it is False then System.Assertions.Raise_Assert_Failure is called which results in the raising of Assert_Failure with the given message.

If the boolean expression has side effects, these side effects will turn on and off with the setting of the assertions mode, resulting in assertions that have an effect on the program. You should generally avoid side effects in the expression arguments of this pragma. However, the expressions are analyzed for semantic correctness whether or not assertions are enabled, so turning assertions on and off cannot affect the legality of a program.