Most of GNAT is written in Ada using a consistent style to ensure readability of the code. This document has been written to help maintain this consistent style, while having a large group of developers work on the compiler.
For the coding style in the C parts of the compiler and run time, see the GNU Coding Guidelines.
This document is structured after the Ada Reference manual. Those familiar with that document should be able to quickly lookup style rules for particular constructs.
style.adb
.
ALI
word which stands for Ada Library
Information and is by convention always written in upper-case when
used in entity names.
procedure Find_ALI_Files;
I
, use J
instead, I
is too
easily mixed up with 1
in some fonts. Similarly don't use the
variable O
, which is too easily mixed up with 0
.
1_000_000 16#8000_000# 3.14159_26535_89793_23846
return else
Access
, Delta
and Digits
are
capitalized when used as attribute_designator.
--
(i.e. --
followed by two spaces).
The only exception to this rule (i.e. one space is tolerated) is when the
comment ends with --
.
It also accepted to have only one space between --
and the start
of the comment when the comment is at the end of a line,
after some Ada code.
--
(unlike the
normal rule, which is to use entirely blank lines for separating
comment paragraphs). The comment start at same level of indentation
as code they are commenting.
z : Integer; -- Integer value for storing value of z -- -- The previous line was a blank line.
???
.
begin
:
begin -- Comment for the next statement A := 5; -- Comment for the B statement B := 6;
My_Identifier := 5; -- First comment Other_Id := 6; -- Second comment
Entity1 : Integer; My_Entity : Integer;
E := A * B**2 + 3 * (C - D);
(A / B) * C
if
, elsif
or else
keywords fit on the
same line with the condition and the then
keyword, then the
statement is formatted as follows:
if condition then ... elsif condition then ... else ... end if;
When the above layout is not possible, then
should be aligned
with if
, and conditions should preferably be split before an
and
or or
keyword a follows:
if long_condition_that_has_to_be_split and then continued_on_the_next_line then ... end if;
The elsif
, else
and end if
always line up with
the if
keyword. The preferred location for splitting the line
is before and
or or
. The continuation of a condition is
indented with two spaces or as many as needed to make nesting clear.
As exception, if conditions are closely related either of the
following is allowed:
if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf or else x = asldkjhalkdsjfhhfd or else x = asdfadsfadsf then if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf or else x = asldkjhalkdsjfhhfd or else x = asdfadsfadsf then
and then
,
or else
).
if this_complex_condition and then that_other_one and then one_last_one then ...
if
block is preceded and followed by a blank line, except
where it begins or ends a sequence_of_statements.
A := 5; if A = 5 then null; end if; A := 6;
case expression is when condition => ... when condition => ... end case;
When possible, have for
or while
on one line with the
condition and the loop
keyword.
for J in S'Range loop ... end loop;
If the condition is too long, split the condition (see "If
statements" above) and align loop
with the for
or
while
keyword.
while long_condition_that_has_to_be_split and then continued_on_the_next_line loop ... end loop;
If the loop_statement has an identifier, it is laid out as follows:
Outer : while not condition loop ... end Outer;
declare
(optional), begin
and end
statements
are aligned, except when the block_statement is named. There
is a blank line before the begin
keyword:
Some_Block : declare ... begin ... end Some_Block;
in
for parameters, especially in functions:
function Length (S : String) return Integer;
function Head (Source : String; Count : Natural; Pad : Character := Space) return String;
procedure Func (A : Integer);
----------------- -- My_Function -- ----------------- procedure My_Function is begin
Note that the name in the header is preceded by a single space, not two spaces as for other comments.
begin
keyword is
preceded by a blank line.
begin
, there is a line:
-- Start of processing for Enclosing_Subprogram begin
package P is ... end P;
use
-ing with
-ed packages, with
the context clauses looking like:
with A; use A; with B; use B;
use
d.
package Entity is type Entity_Kind is ...; ... end Entity;
-gnatg
switch to check the coding style (Note that you should look at
style.adb
to see the lexical rules enforced by
-gnatg
).
.adb
extension for a body or .ads
for a spec.
krunch.ads
) and the filenames should match the unit name,
except that they are all lower case.