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.
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;
1_000_000
16#8000_000#
3.14159_26535_89793_23846
return else
z : Integer;
-- Integer value for storing value of z
--
-- The previous line was a blank line.
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 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
if this_complex_condition
and then that_other_one
and then one_last_one
then
...
A := 5;
if A = 5 then
null;
end if;
A := 6;
case expression is
when condition =>
...
when condition =>
...
end case;
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;
Some_Block : declare
...
begin
...
end Some_Block;
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.
-- Start of processing for Enclosing_Subprogram
begin
package P is
...
end P;
with A; use A;
with B; use B;
package Entity is
type Entity_Kind is ...;
...
end Entity;