Untitled Document
3.2.4 Style Checking
The -gnaty^x^(option,option,..)^ switch causes the compiler to
enforce specified style rules. A limited set of style rules has been used
in writing the GNAT sources themselves. This switch allows user programs
to activate all or some of these checks. If the source program fails a
specified style check, an appropriate warning message is given, preceded by
the character sequence "(style)".
The string x is a sequence of letters or digits
indicating the particular style
checks to be performed. The following checks are defined:
1-9 (specify indentation level)
- If a digit from 1-9 appears in the string after `-gnaty' then proper
indentation is checked, with the digit indicating the indentation level
required. The general style of required indentation is as specified by
the examples in the Ada Reference Manual. Full line comments must be
aligned with the
-- starting on a column that is a multiple of
the alignment level.
^a^ATTRIBUTE^ (check attribute casing)
- If the ^letter a^word ATTRIBUTE^ appears in the string after `-gnaty' then
attribute names, including the case of keywords such as
digits
used as attributes names, must be written in mixed case, that is, the
initial letter and any letter following an underscore must be uppercase.
All other letters must be lowercase.
^b^BLANKS^ (blanks not allowed at statement end)
- If the ^letter b^word BLANKS^ appears in the string after `-gnaty' then
trailing blanks are not allowed at the end of statements. The purpose of this
rule, together with h (no horizontal tabs), is to enforce a canonical format
for the use of blanks to separate source tokens.
^c^COMMENTS^ (check comments)
- If the ^letter c^word COMMENTS^ appears in the string after `-gnaty' then
comments must meet the following set of rules:
-
The "--" that starts the column must either start in column one, or else
at least one blank must precede this sequence.
-
Comments that follow other tokens on a line must have at least one blank
following the "--" at the start of the comment.
-
Full line comments must have two blanks following the "--" that starts
the comment, with the following exceptions.
-
A line consisting only of the "--" characters, possibly preceded by blanks
is permitted.
-
A comment starting with "--x" where x is a special character is permitted.
This alows proper processing of the output generated by specialized tools
including
gnatprep (where --! is used) and the SPARK annnotation
language (where --# is used). For the purposes of this rule, a special
character is defined as being in one of the ASCII ranges
16#21#..16#2F# or 16#3A#..16#3F#.
-
A line consisting entirely of minus signs, possibly preceded by blanks, is
permitted. This allows the construction of box comments where lines of minus
signs are used to form the top and bottom of the box.
-
If a comment starts and ends with "--" is permitted as long as at least
one blank follows the initial "--". Together with the preceding rule,
this allows the construction of box comments, as shown in the following
example:
| | ---------------------------
-- This is a box comment --
-- with two text lines. --
---------------------------
|
^e^END^ (check end/exit labels)
- If the ^letter e^word END^ appears in the string after `-gnaty' then
optional labels on
end statements ending subprograms and on
exit statements exiting named loops, are required to be present.
^f^VTABS^ (no form feeds or vertical tabs)
- If the ^letter f^word VTABS^ appears in the string after `-gnaty' then
neither form feeds nor vertical tab characters are not permitted
in the source text.
^h^HTABS^ (no horizontal tabs)
- If the ^letter h^word HTABS^ appears in the string after `-gnaty' then
horizontal tab characters are not permitted in the source text.
Together with the b (no blanks at end of line) check, this
enforces a canonical form for the use of blanks to separate
source tokens.
^i^IF_THEN^ (check if-then layout)
- If the ^letter i^word IF_THEN^ appears in the string after `-gnaty',
then the keyword
then must appear either on the same
line as corresponding if, or on a line on its own, lined
up under the if with at least one non-blank line in between
containing all or part of the condition to be tested.
^k^KEYWORD^ (check keyword casing)
- If the ^letter k^word KEYWORD^ appears in the string after `-gnaty' then
all keywords must be in lower case (with the exception of keywords
such as
digits used as attribute names to which this check
does not apply).
^l^LAYOUT^ (check layout)
- If the ^letter l^word LAYOUT^ appears in the string after `-gnaty' then
layout of statement and declaration constructs must follow the
recommendations in the Ada Reference Manual, as indicated by the
form of the syntax rules. For example an
else keyword must
be lined up with the corresponding if keyword.
There are two respects in which the style rule enforced by this check
option are more liberal than those in the Ada Reference Manual. First
in the case of record declarations, it is permissible to put the
record keyword on the same line as the type keyword, and
then the end in end record must line up under type.
For example, either of the following two layouts is acceptable:
| | type q is record
a : integer;
b : integer;
end record;
type q is
record
a : integer;
b : integer;
end record;
|
Second, in the case of a block statement, a permitted alternative
is to put the block label on the same line as the declare or
begin keyword, and then line the end keyword up under
the block label. For example both the following are permitted:
| | Block : declare
A : Integer := 3;
begin
Proc (A, A);
end Block;
Block :
declare
A : Integer := 3;
begin
Proc (A, A);
end Block;
|
The same alternative format is allowed for loops. For example, both of
the following are permitted:
| | Clear : while J < 10 loop
A (J) := 0;
end loop Clear;
Clear :
while J < 10 loop
A (J) := 0;
end loop Clear;
|
^m^LINE_LENGTH^ (check maximum line length)
- If the ^letter m^word LINE_LENGTH^ appears in the string after `-gnaty'
then the length of source lines must not exceed 79 characters, including
any trailing blanks. The value of 79 allows convenient display on an
80 character wide device or window, allowing for possible special
treatment of 80 character lines.
^Mnnn^MAX_LENGTH=nnn^ (set maximum line length)
- If the sequence ^M^MAX_LENGTH=^nnn, where nnn is a decimal number, appears in
the string after `-gnaty' then the length of lines must not exceed the
given value.
^n^STANDARD_CASING^ (check casing of entities in Standard)
- If the ^letter n^word STANDARD_CASING^ appears in the string
after `-gnaty' then any identifier from Standard must be cased
to match the presentation in the Ada Reference Manual (for example,
Integer and ASCII.NUL).
^o^ORDERED_SUBPROGRAMS^ (check order of subprogram bodies)
- If the ^letter o^word ORDERED_SUBPROGRAMS^ appears in the string
after `-gnaty' then all subprogram bodies in a given scope
(e.g. a package body) must be in alphabetical order. The ordering
rule uses normal Ada rules for comparing strings, ignoring casing
of letters, except that if there is a trailing numeric suffix, then
the value of this suffix is used in the ordering (e.g. Junk2 comes
before Junk10).
^p^PRAGMA^ (check pragma casing)
- If the ^letter p^word PRAGMA^ appears in the string after `-gnaty' then
pragma names must be written in mixed case, that is, the
initial letter and any letter following an underscore must be uppercase.
All other letters must be lowercase.
^r^REFERENCES^ (check references)
- If the ^letter r^word REFERENCES^ appears in the string after `-gnaty'
then all identifier references must be cased in the same way as the
corresponding declaration. No specific casing style is imposed on
identifiers. The only requirement is for consistency of references
with declarations.
^s^SPECS^ (check separate specs)
- If the ^letter s^word SPECS^ appears in the string after `-gnaty' then
separate declarations ("specs") are required for subprograms (a
body is not allowed to serve as its own declaration). The only
exception is that parameterless library level procedures are
not required to have a separate declaration. This exception covers
the most frequent form of main program procedures.
^t^TOKEN^ (check token spacing)
- If the ^letter t^word TOKEN^ appears in the string after `-gnaty' then
the following token spacing rules are enforced:
-
The keywords
abs and not must be followed by a space.
-
The token
=> must be surrounded by spaces.
-
The token
<> must be preceded by a space or a left parenthesis.
-
Binary operators other than
** must be surrounded by spaces.
There is no restriction on the layout of the ** binary operator.
-
Colon must be surrounded by spaces.
-
Colon-equal (assignment) must be surrounded by spaces.
-
Comma must be the first non-blank character on the line, or be
immediately preceded by a non-blank character, and must be followed
by a space.
-
If the token preceding a left paren ends with a letter or digit, then
a space must separate the two tokens.
-
A right parenthesis must either be the first non-blank character on
a line, or it must be preceded by a non-blank character.
-
A semicolon must not be preceded by a space, and must not be followed by
a non-blank character.
-
A unary plus or minus may not be followed by a space.
-
A vertical bar must be surrounded by spaces.
In the above rules, appearing in column one is always permitted, that is,
counts as meeting either a requirement for a required preceding space,
or as meeting a requirement for no preceding space.
Appearing at the end of a line is also always permitted, that is, counts
as meeting either a requirement for a following space, or as meeting
a requirement for no following space.
If any of these style rules is violated, a message is generated giving
details on the violation. The initial characters of such messages are
always "(style)". Note that these messages are treated as warning
messages, so they normally do not prevent the generation of an object
file. The `-gnatwe' switch can be used to treat warning messages,
including style messages, as fatal errors.
The switch
^`-gnaty' on its own (that is not followed by any letters or digits),^/STYLE_CHECKS=ALL_BUILTIN^
is equivalent to ^gnaty3abcefhiklmprst, that is^^ all checking
options ^are^^ enabled with
the exception of ^-gnatyo^ORDERED_SUBPROGRAMS^,
with an indentation level of 3. This is the standard
checking option that is used for the GNAT sources.