Pragma Warning_As_Error (GNAT Reference Manual)

### 2.207 Pragma Warning_As_Error ¶

Syntax:

    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 that matches the pattern given by the pragma argument will be treated as an error. This gives more precise control than -gnatwe, which treats warnings as errors.

This pragma can apply to regular warnings (messages enabled by -gnatw) and to style warnings (messages that start with “(style)”, enabled by -gnaty).

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. All characters other than asterisk 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 a single ‘-gnatw’ tag string, enclosed in brackets, as shown in the example below, to treat one category of warnings as errors. Note that if you want to treat multiple categories of warnings as errors, you can use multiple pragma Warning_As_Error.

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.