Pragma Style_Checks (GNAT Reference Manual)

### 2.175 Pragma Style_Checks ¶

Syntax:

    pragma Style_Checks (string_LITERAL | ALL_CHECKS |
                         On | Off [, LOCAL_NAME]);

This pragma is used in conjunction with compiler switches to control the built in style checking provided by GNAT. The compiler switches, if set, provide an initial setting for the switches, and this pragma may be used to modify these settings, or the settings may be provided entirely by the use of the pragma. This pragma can be used anywhere that a pragma is legal, including use as a configuration pragma (including use in the `gnat.adc` file).

The form with a string literal specifies which style options are to be activated. These are additive, so they apply in addition to any previously set style check options. The codes for the options are the same as those used in the ‘-gnaty’ switch to ‘gcc’ or ‘gnatmake’. For example the following two methods can be used to enable layout checking and to change the maximum nesting level value:

-     --  switch on layout checks
      pragma Style_Checks ("l");
      --  set the number of maximum allowed nesting levels to 15
      pragma Style_Checks ("L15");

-     gcc -c -gnatyl -gnatyL15 ...

The string literal values can be cumulatively switched on and off by prefixing the value with `+` or `-`, where:

- `+` is equivalent to no prefix. It applies the check referenced by the literal value;
- `-` switches the referenced check off.

    --  allow misaligned block by disabling layout check
    pragma Style_Checks ("-l");
    declare
        msg : constant String := "Hello";
    begin
        Put_Line (msg);
        end;

    --  enable the layout check again
    pragma Style_Checks ("l");
    declare
        msg : constant String := "Hello";
    begin
        Put_Line (msg);
        end;

The code above contains two layout errors, however, only the last line is picked up by the compiler.

Similarly, the switches containing a numeric value can be applied in sequence. In the example below, the permitted nesting level is reduced in in the middle block and the compiler raises a warning on the highlighted line.

    -- Permit 3 levels of nesting
    pragma Style_Checks ("L3");

    procedure Main is
    begin
        if True then
          if True then
              null;
          end if;
        end if;
        --  Reduce permitted nesting levels to 2.
        --  Note that "+L2" and "L2" are equivalent.
        pragma Style_Checks ("+L2");
        if True then
          if True then
              null;
          end if;
        end if;
        --  Disable checking permitted nesting levels.
        --  Note that the number after "-L" is insignificant,
        --  "-L", "-L3" and "-Lx" are all equivalent.
        pragma Style_Checks ("-L3");
        if True then
          if True then
              null;
          end if;
        end if;
    end Main;

The form `ALL_CHECKS` activates all standard checks (its use is equivalent to the use of the `gnaty` switch with no options. See the GNAT User’s Guide for details.)

Note: the behavior is slightly different in GNAT mode (`-gnatg` used). In this case, `ALL_CHECKS` implies the standard set of GNAT mode style check options (i.e. equivalent to `-gnatyg`).

The forms with `Off` and `On` can be used to temporarily disable style checks as shown in the following example:

    pragma Style_Checks ("k"); -- requires keywords in lower case
    pragma Style_Checks (Off); -- turn off style checks
    NULL;                      -- this will not generate an error message
    pragma Style_Checks (On);  -- turn style checks back on
    NULL;                      -- this will generate an error message

Finally the two argument form is allowed only if the first argument is `On` or `Off`. The effect is to turn of semantic style checks for the specified entity, as shown in the following example:

    pragma Style_Checks ("r"); -- require consistency of identifier casing
    Arg : Integer;
    Rf1 : Integer := ARG;      -- incorrect, wrong case
    pragma Style_Checks (Off, Arg);
    Rf2 : Integer := ARG;      -- OK, no error