Next: , Previous: , Up: Implementation Defined Pragmas   [Contents][Index]

2.200 Pragma Warning_As_Error


pragma Warning_As_Error (static_string_EXPRESSION);

This configuration pragma allows the programmer to specify a set of warnings that will be treated as errors. Any warning which matches the pattern given by the pragma argument will be treated as an error. This gives much more precise control that -gnatwe which treats all warnings as errors.

The pattern may contain asterisks, which match zero or more characters in the message. For example, you can use pragma Warning_As_Error ("bits of*unused") to treat the warning message warning: 960 bits of "a" unused as an error. No other regular expression notations are permitted. All characters other than asterisk in these three specific cases are treated as literal characters in the match. The match is case insensitive, for example XYZ matches xyz.

Note that the pattern matches if it occurs anywhere within the warning message string (it is not necessary to put an asterisk at the start and the end of the message, since this is implied).

Another possibility for the static_string_EXPRESSION which works whether or not error tags are enabled (`-gnatw.d') is to use the `-gnatw' tag string, enclosed in brackets, as shown in the example below, to treat a class of warnings as errors.

The above use of patterns to match the message applies only to warning messages generated by the front end. This pragma can also be applied to warnings provided by the back end and mentioned in Pragma Warnings. By using a single full `-Wxxx' switch in the pragma, such warnings can also be treated as errors.

The pragma can appear either in a global configuration pragma file (e.g. gnat.adc), or at the start of a file. Given a global configuration pragma file containing:

pragma Warning_As_Error ("[-gnatwj]");

which will treat all obsolescent feature warnings as errors, the following program compiles as shown (compile options here are `-gnatwa.d -gnatl -gnatj55').

    1. pragma Warning_As_Error ("*never assigned*");
    2. function Warnerr return String is
    3.    X : Integer;
       >>> error: variable "X" is never read and
           never assigned [-gnatwv] [warning-as-error]

    4.    Y : Integer;
       >>> warning: variable "Y" is assigned but
           never read [-gnatwu]

    5. begin
    6.    Y := 0;
    7.    return %ABC%;
       >>> error: use of "%" is an obsolescent
           feature (RM J.2(4)), use """ instead
           [-gnatwj] [warning-as-error]

    8. end;

8 lines: No errors, 3 warnings (2 treated as errors)

Note that this pragma does not affect the set of warnings issued in any way, it merely changes the effect of a matching warning if one is produced as a result of other warnings options. As shown in this example, if the pragma results in a warning being treated as an error, the tag is changed from "warning:" to "error:" and the string "[warning-as-error]" is appended to the end of the message.

Next: , Previous: , Up: Implementation Defined Pragmas   [Contents][Index]