1 \input texinfo @c -*-texinfo-*-
3 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c GNAT DOCUMENTATION o
7 @c G N A T C O D I N G S T Y L E o
11 @c Copyright (C) 1992-2001 Ada Core Technologies, Inc. o
13 @c GNAT is free software; you can redistribute it and/or modify it under o
14 @c terms of the GNU General Public License as published by the Free Soft- o
15 @c ware Foundation; either version 2, or (at your option) any later ver- o
16 @c sion. GNAT is distributed in the hope that it will be useful, but WITH- o
17 @c OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY o
18 @c or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License o
19 @c for more details. You should have received a copy of the GNU General o
20 @c Public License distributed with GNAT; see file COPYING. If not, write o
21 @c to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, o
22 @c MA 02111-1307, USA. o
24 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
26 @setfilename gnat-style.info
27 @settitle GNAT Coding Style
31 @center GNAT Coding Style
33 @center A guide for GNAT developers
34 Copyright (C) 1992-2001 Ada Core Technologies, Inc.
39 @title GNAT Coding Stye
40 @subtitle A guide for GNAT developers
41 @subtitle Document revision level $Revision: 1.1 $
42 @subtitle Date: @today{}
43 @author Ada Core Technologies, Inc.
47 @node Top, General, , (dir)
48 @comment node-name, next, previous, up
53 * Declarations and Types::
54 * Expressions and Names::
61 @c -------------------------------------------------------------------------
62 @node General, Lexical Elements, Top, Top
64 @c -------------------------------------------------------------------------
67 Most of GNAT is written in Ada using a consistent style to ensure
68 readability of the code. This document has been written to help
69 maintain this consistent style, while having a large group of developers
73 For the coding style in the C parts of the compiler and run time,
74 see the GNU Coding Guidelines.
77 This document is structured after the Ada Reference manual.
78 Those familiar with that document should be able to quickly
79 lookup style rules for particular constructs.
81 @c -------------------------------------------------------------------------
82 @node Lexical Elements, Declarations and Types, General, Top
83 @section Lexical Elements
84 @c -------------------------------------------------------------------------
86 @subsection Character Set and Separators
87 @c -------------------------------------------------------------------------
91 The character set used should be plain 7-bit ASCII.
92 The only separators allowed are space and the end-of-line sequence.
93 No other control character or format effector (such as HT, VT, FF)
95 The normal end-of-line sequence is used, which may be LF, CR/LF or CR,
96 depending on the host system. An optional SUB (16#1A#) may be present as the
97 last character in the file on hosts using that character as file terminator.
100 Files that are checked in or distributed should be in host format.
103 A line should never be longer than 79 characters, not counting the line
107 Lines must not have trailing blanks.
110 Indentation is 3 characters per level for if-statements, loops, case
111 statements. For exact information on required spacing between lexical
112 elements, see file @file{style.adb}.
116 @subsection Identifiers
117 @c -------------------------------------------------------------------------
120 Identifiers will start with an upper case letter, and each letter following
121 an underscore will be upper case. Short acronyms may be all upper case.
122 All other letters are lower case.
123 An exception is for identifiers matching a foreign language. In particular,
124 we use all lower case where appropriate for C.
127 Use underscores to separate words in an identifier.
129 @item Try to limit your use of abbreviations in identifiers.
130 It is ok to make a few abbreviations, explain what they mean, and then use them frequently, but don't use lots of obscure abbreviations.
131 An example is the @code{ALI} word which stands for Ada Library Information
132 and is by convention always written in upper-case when used in entity names.
135 procedure Find_ALI_Files;
139 Don't use the variable I, use J instead, I is too easily mixed up with
140 1 in some fonts. Similarly don't use the variable O, which is too easily
144 @subsection Numeric Literals
145 @c -------------------------------------------------------------------------
148 Numeric literals should include underscores where helpful for
154 3.14159_26535_89793_23846
158 @subsection Reserved Words
159 @c -------------------------------------------------------------------------
162 Reserved words use all lower case.
169 The words "Access", "Delta" and "Digits" are capitalized when used
170 as attribute_designator.
174 @c -------------------------------------------------------------------------
178 Comment start with @code{-- } (ie @code{--} followed by two spaces).
179 The only exception to this rule (i.e. one space is tolerated) is when the
180 comment ends with @code{--}.
181 It also accepted to have only one space between @code{--} and the start
182 of the comment when the comment is at the end of a line,
183 after an Ada statement.
186 Every sentence in a comment should start with an upper-case letter (including
187 the first letter of the comment).
190 When declarations are commented with "hanging" comments, i.e. comments
191 after the declaration, there is no blank line before the comment, and
192 if it is absolutely necessary to have blank lines within the comments
193 these blank lines *do* have a -- (unlike the normal rule, which is to
194 use entirely blank lines for separating comment paragraphs).
195 The comment start at same level of indentation as code they are commenting.
199 -- Integer value for storing value of z
201 -- The previous line was a blank line
205 Comments that are dubious or incomplete or comment on possibly
206 wrong or incomplete code should be preceded or followed by ???
209 Comments in a subprogram body must generally be surrounded by blank lines,
210 except after a "begin":
214 -- Comment for the next statement
218 -- Comment for the B statement
224 In sequences of statements, comments at the end of the lines should be
228 My_Identifier := 5; -- First comment
229 Other_Id := 6; -- Second comment
233 Short comments that fit on a single line are NOT ended with a period.
234 Comments taking more than a line are punctuated in the normal manner.
237 Comments should focus on why instead of what.
238 Descriptions of what subprograms do go with the specification.
241 Comments describing a subprogram spec should specifically mention the
242 formal argument names. General rule: write a comment that does not
243 depend on the names of things. The names are supplementary, not
244 sufficient, as comments.
247 Do NOT put two spaces after periods in comments.
250 @c -------------------------------------------------------------------------
251 @node Declarations and Types, Expressions and Names, Lexical Elements,Top
252 @section Declarations and Types
253 @c -------------------------------------------------------------------------
257 In entity declarations, colons must be surrounded by spaces. Colons
266 Declarations should be grouped in a logical order.
267 Related groups of declarations may be preceded by a header comment.
270 All local subprograms in a subprogram or package body should be declared
271 before the first local subprogram body.
274 Do not declare discriminated record types where the discriminant is used
275 for constraining an unconstrained array type. (Discriminated
276 records for a variant part are allowed.)
279 Don't declare local entities that hide global entities.
282 Don't declare multiple variables in one declaration that spans lines.
283 Start a new declaration on each line, instead
286 The defining_identifiers of global declarations serve as comments of a sort.
287 So don't choose terse names, but look for names that give useful information
291 Local names can be shorter, because they are used only within
292 one context, where comments explain their purpose.
297 @c -------------------------------------------------------------------------
298 @node Expressions and Names, Statements, Declarations and Types, Top
299 @section Expressions and Names
300 @c -------------------------------------------------------------------------
305 Every operator must be surrounded by spaces, except for the
306 exponentiation operator.
309 E := A * B**2 + 3 * (C - D);
313 When folding a long line, fold before an operator, not after.
316 Use parentheses where they make the intended order of evaluation clearer:
322 @c -------------------------------------------------------------------------
323 @node Statements, Subprograms, Expressions and Names, Top
325 @c -------------------------------------------------------------------------
327 @subsection Simple and Compound Statements
328 @c -------------------------------------------------------------------------
331 Use only one statement or label per line.
333 A longer sequence_of_statements may be divided in logical groups
334 or separated from surrounding code using a blank line.
337 @subsection If Statements
338 @c -------------------------------------------------------------------------
341 When the "if", "elsif" or "else" keywords fit on the same line with the
342 condition and the "then" keyword, then the statement is formatted as follows:
347 elsif <condition> then
355 When the above layout is not possible, "then" should be aligned with "if",
356 and conditions should preferably be split before an "and" or "or" keyword
360 if <long_condition_that_has_to_be_split>
361 and then <continued_on_the_next_line>
368 The "elsif", "else" and "end if" always line up with the "if" keyword. The
369 preferred location for splitting the line is before "and" or "or". The
370 continuation of a condition is indented with two spaces or as many as needed
371 to make nesting clear.
372 As exception, if conditions are closely related either of the following is
376 if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf
378 x = asldkjhalkdsjfhhfd
383 if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf or else
384 x = asldkjhalkdsjfhhfd or else
390 Conditions should use short-circuit forms ("and then", "or else").
393 Complex conditions in if-statements are indented two characters:
396 if this_complex_condition
397 and then that_other_one
398 and then one_last_one
404 Every "if" block is preceded and followed by a blank line, except
405 where it begins or ends a sequence_of_statements.
418 @subsection Case statements
422 Layout is as below. For long case statements, the extra indentation
423 can be saved by aligning the when clauses with the opening case.
435 @subsection Loop statements
439 When possible, have "for" or "while" on one line with the condition
440 and the "loop" keyword.
443 for I in S'Range loop
449 If the condition is too long, split the condition (see if_statement)
450 and align "loop" with the "for" or "while" keyword.
453 while <long_condition_that_has_to_be_split>
454 and then <continued_on_the_next_line>
461 If the loop_statement has an identifier, it is layout as follows:
464 Outer : while not <condition> loop
470 @subsection Block Statements
474 The (optional) "declare", "begin" and "end" statements are aligned,
475 except when the block_statement is named:
487 @c -------------------------------------------------------------------------
488 @node Subprograms, Packages, Statements, Top
490 @c -------------------------------------------------------------------------
493 @subsection Subprogram Declarations
494 @c -------------------------------------------------------------------------
498 Do not write the "in" for parameters, especially in functions:
501 function Length (S : String) return Integer;
505 When the declaration line for a procedure or a function is too long, fold it.
511 Pad : Character := Space)
516 The parameter list for a subprogram is preceded by a space:
519 procedure Func (A : Integer);
524 @subsection Subprogram Bodies
525 @c -------------------------------------------------------------------------
529 The functions and procedures should always be sorted alphabetically in
533 All subprograms have a header giving the function name, with the following
541 procedure My_Function is
544 Note that the name in the header is preceded by a single space,
545 not two spaces as for other comments.
548 Every subprogram body must have a preceding subprogram_declaration.
551 If declarations of a subprogram contain at least one nested subprogram
552 body, then just before the begin is a line:
555 -- Start of processing for bla bla
562 @c -------------------------------------------------------------------------
563 @node Packages, Program Structure, Subprograms, Top
564 @section Packages and Visibility Rules
565 @c -------------------------------------------------------------------------
570 All program units and subprograms have their name at the end:
579 We will use the style of use-ing with-ed packages, with the context
580 clauses looking like:
588 Names declared in the visible part of packages should be
589 unique, to prevent name clashes when the packages are "use"d.
593 type Entity_Kind is ...;
599 After the file header comment, the context clause and unit specification
600 should be the first thing in a program_unit.
603 @c -------------------------------------------------------------------------
604 @node Program Structure,, Packages, Top
605 @section Program Structure and Compilation Issues
606 @c -------------------------------------------------------------------------
610 Every GNAT source file must be compiled with the "-gnatg" switch to check
611 the coding style (Note that you should look at @file{style.adb} to
612 see the lexical rules enforced by -gnatg).
615 Each source file should contain only one compilation unit.
618 Filenames should be 8 characters or less followed by the ".adb" extension
619 for a body or ".ads" for a spec.
622 Unit names should be distinct when krunched to 8 characters
623 (see @file{krunch.ads}) and the filenames should match the unit name,
624 except that they are all lower case.