1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This version of the Style package implements the standard GNAT style
28 -- checking rules. For documentation of these rules, see comments on the
29 -- individual procedures.
31 with Casing
; use Casing
;
32 with Csets
; use Csets
;
33 with Err_Vars
; use Err_Vars
;
35 with Scans
; use Scans
;
36 with Sinput
; use Sinput
;
37 with Stylesw
; use Stylesw
;
39 package body Styleg
is
43 Blank_Lines
: Nat
:= 0;
44 -- Counts number of empty lines seen. Reset to zero if a non-empty line
45 -- is encountered. Used to check for trailing blank lines in Check_EOF,
46 -- and for multiple blank lines.
48 Blank_Line_Location
: Source_Ptr
;
49 -- Remembers location of first blank line in a series. Used to issue an
50 -- appropriate diagnostic if subsequent blank lines or the end of file
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Check_No_Space_After
;
58 -- Checks that there is a non-white space character after the current
59 -- token, or white space followed by a comment, or the end of line.
60 -- Issue error message if not.
62 procedure Check_No_Space_Before
;
63 -- Check that token is first token on line, or else is not preceded
64 -- by white space. Signal error of space not allowed if not.
66 function Determine_Token_Casing
return Casing_Type
;
68 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
);
69 -- Posts an error message indicating that a space is not allowed
70 -- at the given source location.
72 procedure Error_Space_Required
(S
: Source_Ptr
);
73 -- Posts an error message indicating that a space is required at
74 -- the given source location.
76 function Is_White_Space
(C
: Character) return Boolean;
77 pragma Inline
(Is_White_Space
);
78 -- Returns True for space, HT, VT or FF, False otherwise
80 procedure Require_Following_Space
;
81 pragma Inline
(Require_Following_Space
);
82 -- Require token to be followed by white space. Used only if in GNAT
83 -- style checking mode.
85 procedure Require_Preceding_Space
;
86 pragma Inline
(Require_Preceding_Space
);
87 -- Require token to be preceded by white space. Used only if in GNAT
88 -- style checking mode.
90 ----------------------
91 -- Check_Abs_Or_Not --
92 ----------------------
94 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
96 procedure Check_Abs_Not
is
98 if Style_Check_Tokens
then
99 if Source
(Scan_Ptr
) > ' ' then
100 Error_Space_Required
(Scan_Ptr
);
105 ----------------------
106 -- Check_Apostrophe --
107 ----------------------
109 -- Do not allow space before or after apostrophe
111 procedure Check_Apostrophe
is
113 if Style_Check_Tokens
then
114 Check_No_Space_After
;
116 end Check_Apostrophe
;
122 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
124 procedure Check_Arrow
is
126 if Style_Check_Tokens
then
127 Require_Preceding_Space
;
128 Require_Following_Space
;
132 --------------------------
133 -- Check_Attribute_Name --
134 --------------------------
136 -- In check attribute casing mode (-gnatya), attribute names must be
137 -- mixed case, i.e. start with an upper case letter, and otherwise
138 -- lower case, except after an underline character.
140 procedure Check_Attribute_Name
(Reserved
: Boolean) is
141 pragma Warnings
(Off
, Reserved
);
143 if Style_Check_Attribute_Casing
then
144 if Determine_Token_Casing
/= Mixed_Case
then
145 Error_Msg_SC
("(style) bad capitalization, mixed case required");
148 end Check_Attribute_Name
;
150 ---------------------------
151 -- Check_Binary_Operator --
152 ---------------------------
154 -- In check token mode (-gnatyt), binary operators other than the special
155 -- case of exponentiation require surrounding space characters.
157 procedure Check_Binary_Operator
is
159 if Style_Check_Tokens
then
160 Require_Preceding_Space
;
161 Require_Following_Space
;
163 end Check_Binary_Operator
;
169 -- In check token mode (-gnatyt), box must be preceded by a space or by
170 -- a left parenthesis. Spacing checking on the surrounding tokens takes
171 -- care of the remaining checks.
173 procedure Check_Box
is
175 if Style_Check_Tokens
then
176 if Prev_Token
/= Tok_Left_Paren
then
177 Require_Preceding_Space
;
186 -- In check token mode (-gnatyt), colon must be surrounded by spaces
188 procedure Check_Colon
is
190 if Style_Check_Tokens
then
191 Require_Preceding_Space
;
192 Require_Following_Space
;
196 -----------------------
197 -- Check_Colon_Equal --
198 -----------------------
200 -- In check token mode (-gnatyt), := must be surrounded by spaces
202 procedure Check_Colon_Equal
is
204 if Style_Check_Tokens
then
205 Require_Preceding_Space
;
206 Require_Following_Space
;
208 end Check_Colon_Equal
;
214 -- In check token mode (-gnatyt), comma must be either the first
215 -- token on a line, or be preceded by a non-blank character.
216 -- It must also always be followed by a blank.
218 procedure Check_Comma
is
220 if Style_Check_Tokens
then
221 Check_No_Space_Before
;
223 if Source
(Scan_Ptr
) > ' ' then
224 Error_Space_Required
(Scan_Ptr
);
233 -- In check comment mode (-gnatyc) there are several requirements on the
234 -- format of comments. The following are permissible comment formats:
236 -- 1. Any comment that is not at the start of a line, i.e. where the
237 -- initial minuses are not the first non-blank characters on the
238 -- line must have at least one blank after the second minus.
240 -- 2. A row of all minuses of any length is permitted (see procedure
241 -- box above in the source of this routine).
243 -- 3. A comment line starting with two minuses and a space, and ending
244 -- with a space and two minuses. Again see the procedure title box
245 -- immediately above in the source.
247 -- 4. A full line comment where two spaces follow the two minus signs.
248 -- This is the normal comment format in GNAT style, as typified by
249 -- the comments you are reading now.
251 -- 5. A full line comment where the first character after the second
252 -- minus is a special character, i.e. a character in the ASCII
253 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
254 -- comments, such as those generated by gnatprep, or those that
255 -- appear in the SPARK annotation language to be accepted.
257 -- Note: for GNAT internal files (-gnatg switch set on for the
258 -- compilation), the only special sequence recognized and allowed
259 -- is --! as generated by gnatprep.
261 procedure Check_Comment
is
265 function Is_Box_Comment
return Boolean;
266 -- Returns True if the last two characters on the line are -- which
267 -- characterizes a box comment (as for example follows this spec).
273 function Is_Box_Comment
return Boolean is
277 -- Do we need to worry about UTF_32 line terminators here ???
280 while Source
(S
) not in Line_Terminator
loop
284 return Source
(S
- 1) = '-' and then Source
(S
- 2) = '-';
287 -- Start of processing for Check_Comment
290 -- Can never have a non-blank character preceding the first minus
292 if Style_Check_Comments
then
293 if Scan_Ptr
> Source_First
(Current_Source_File
)
294 and then Source
(Scan_Ptr
- 1) > ' '
296 Error_Msg_S
("(style) space required");
300 -- For a comment that is not at the start of the line, the only
301 -- requirement is that we cannot have a non-blank character after
302 -- the second minus sign.
304 if Scan_Ptr
/= First_Non_Blank_Location
then
305 if Style_Check_Comments
then
306 if Source
(Scan_Ptr
+ 2) > ' ' then
307 Error_Msg
("(style) space required", Scan_Ptr
+ 2);
313 -- Case of a comment that is at the start of a line
316 -- First check, must be in appropriately indented column
318 if Style_Check_Indentation
/= 0 then
319 if Start_Column
rem Style_Check_Indentation
/= 0 then
320 Error_Msg_S
("(style) bad column");
325 -- If we are not checking comments, nothing to do
327 if not Style_Check_Comments
then
331 -- Case of not followed by a blank. Usually wrong, but there are
332 -- some exceptions that we permit.
334 if Source
(Scan_Ptr
+ 2) /= ' ' then
335 C
:= Source
(Scan_Ptr
+ 2);
337 -- Case of -- all on its own on a line is OK
343 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
344 -- This is not permitted in internal GNAT implementation units
345 -- except for the case of --! as used by gnatprep output.
353 if Character'Pos (C
) in 16#
21#
.. 16#
2F#
355 Character'Pos (C
) in 16#
3A#
.. 16#
3F#
361 -- The only other case in which we allow a character after
362 -- the -- other than a space is when we have a row of minus
363 -- signs (case of header lines for a box comment for example).
366 while Source
(S
) >= ' ' loop
367 if Source
(S
) /= '-' then
368 if Is_Box_Comment
then
369 Error_Space_Required
(Scan_Ptr
+ 2);
371 Error_Msg
("(style) two spaces required", Scan_Ptr
+ 2);
380 -- If we are followed by a blank, then the comment is OK if the
381 -- character following this blank is another blank or a format
384 elsif Source
(Scan_Ptr
+ 3) <= ' ' then
387 -- Here is the case where we only have one blank after the two
388 -- minus signs, which is an error unless the line ends with two
389 -- minus signs, the case of a box comment.
391 elsif not Is_Box_Comment
then
392 Error_Space_Required
(Scan_Ptr
+ 3);
401 -- In check token mode (-gnatyt), colon must be surrounded by spaces
403 procedure Check_Dot_Dot
is
405 if Style_Check_Tokens
then
406 Require_Preceding_Space
;
407 Require_Following_Space
;
415 -- In check blanks at end mode, check no blank lines precede the EOF
417 procedure Check_EOF
is
419 if Style_Check_Blank_Lines
then
421 -- We expect one blank line, from the EOF, but no more than one
423 if Blank_Lines
= 2 then
425 ("(style) blank line not allowed at end of file",
426 Blank_Line_Location
);
428 elsif Blank_Lines
>= 3 then
430 ("(style) blank lines not allowed at end of file",
431 Blank_Line_Location
);
436 -----------------------------------
437 -- Check_Exponentiation_Operator --
438 -----------------------------------
440 -- No spaces are required for the ** operator in GNAT style check mode
442 procedure Check_Exponentiation_Operator
is
445 end Check_Exponentiation_Operator
;
451 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
453 procedure Check_HT
is
455 if Style_Check_Horizontal_Tabs
then
456 Error_Msg_S
("(style) horizontal tab not allowed");
460 -----------------------
461 -- Check_Indentation --
462 -----------------------
464 -- In check indentation mode (-gnatyn for n a digit), a new statement or
465 -- declaration is required to start in a column that is a multiple of the
466 -- indentiation amount.
468 procedure Check_Indentation
is
470 if Style_Check_Indentation
/= 0 then
471 if Token_Ptr
= First_Non_Blank_Location
472 and then Start_Column
rem Style_Check_Indentation
/= 0
474 Error_Msg_SC
("(style) bad indentation");
477 end Check_Indentation
;
479 ----------------------
480 -- Check_Left_Paren --
481 ----------------------
483 -- In tone check mode (-gnatyt), left paren must not be preceded by an
484 -- identifier character or digit (a separating space is required) and
485 -- may never be followed by a space.
487 procedure Check_Left_Paren
is
489 if Style_Check_Tokens
then
490 if Token_Ptr
> Source_First
(Current_Source_File
)
491 and then Identifier_Char
(Source
(Token_Ptr
- 1))
493 Error_Space_Required
(Token_Ptr
);
496 Check_No_Space_After
;
498 end Check_Left_Paren
;
500 ---------------------------
501 -- Check_Line_Max_Length --
502 ---------------------------
504 -- In check max line length mode (-gnatym), the line length must
505 -- not exceed the permitted maximum value.
507 procedure Check_Line_Max_Length
(Len
: Int
) is
509 if Style_Check_Max_Line_Length
then
510 if Len
> Style_Max_Line_Length
then
512 ("(style) this line is too long",
513 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
516 end Check_Line_Max_Length
;
518 ---------------------------
519 -- Check_Line_Terminator --
520 ---------------------------
522 -- In check blanks at end mode (-gnatyb), lines may not end with a
525 -- In check form feeds mode (-gnatyf), the line terminator may not
526 -- be either of the characters FF or VT.
528 -- In check DOS line terminators node (-gnatyd), the line terminator
529 -- must be a single LF, without a following CR.
531 procedure Check_Line_Terminator
(Len
: Int
) is
535 -- Length of line (adjusted down for blanks at end of line)
538 -- Reset count of blank lines if first line
540 if Get_Logical_Line_Number
(Scan_Ptr
) = 1 then
544 -- Check FF/VT terminators
546 if Style_Check_Form_Feeds
then
547 if Source
(Scan_Ptr
) = ASCII
.FF
then
548 Error_Msg_S
("(style) form feed not allowed");
549 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
550 Error_Msg_S
("(style) vertical tab not allowed");
554 -- Check DOS line terminator (ignore EOF, since we only get called
555 -- with an EOF if it is the last character in the buffer, and was
556 -- therefore not present in the sources
558 if Style_Check_DOS_Line_Terminator
then
559 if Source
(Scan_Ptr
) = EOF
then
561 elsif Source
(Scan_Ptr
) /= LF
562 or else Source
(Scan_Ptr
+ 1) = CR
564 Error_Msg_S
("(style) incorrect line terminator");
568 -- Remove trailing spaces
571 while L
> 0 and then Is_White_Space
(Source
(S
- 1)) loop
576 -- Issue message for blanks at end of line if option enabled
578 if Style_Check_Blanks_At_End
and then L
< Len
then
580 ("(style) trailing spaces not permitted", S
);
583 -- Deal with empty (blank) line
587 -- Increment blank line count
589 Blank_Lines
:= Blank_Lines
+ 1;
591 -- If first blank line, record location for later error message
593 if Blank_Lines
= 1 then
594 Blank_Line_Location
:= Scan_Ptr
;
597 -- Non-blank line, check for previous multiple blank lines
600 if Style_Check_Blank_Lines
and then Blank_Lines
> 1 then
602 ("(style) multiple blank lines", Blank_Line_Location
);
605 -- And reset blank line count
609 end Check_Line_Terminator
;
611 --------------------------
612 -- Check_No_Space_After --
613 --------------------------
615 procedure Check_No_Space_After
is
619 if Is_White_Space
(Source
(Scan_Ptr
)) then
621 -- Allow one or more spaces if followed by comment
625 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
628 elsif Is_White_Space
(Source
(S
)) then
636 Error_Space_Not_Allowed
(Scan_Ptr
);
638 end Check_No_Space_After
;
640 ---------------------------
641 -- Check_No_Space_Before --
642 ---------------------------
644 procedure Check_No_Space_Before
is
646 if Token_Ptr
> First_Non_Blank_Location
647 and then Source
(Token_Ptr
- 1) <= ' '
649 Error_Space_Not_Allowed
(Token_Ptr
- 1);
651 end Check_No_Space_Before
;
653 -----------------------
654 -- Check_Pragma_Name --
655 -----------------------
657 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
658 -- case, i.e. start with an upper case letter, and otherwise lower case,
659 -- except after an underline character.
661 procedure Check_Pragma_Name
is
663 if Style_Check_Pragma_Casing
then
664 if Determine_Token_Casing
/= Mixed_Case
then
665 Error_Msg_SC
("(style) bad capitalization, mixed case required");
668 end Check_Pragma_Name
;
670 -----------------------
671 -- Check_Right_Paren --
672 -----------------------
674 -- In check tokens mode (-gnatyt), right paren must never be preceded by
675 -- a space unless it is the initial non-blank character on the line.
677 procedure Check_Right_Paren
is
679 if Style_Check_Tokens
then
680 Check_No_Space_Before
;
682 end Check_Right_Paren
;
684 ---------------------
685 -- Check_Semicolon --
686 ---------------------
688 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
689 -- space and a following space is required.
691 procedure Check_Semicolon
is
693 if Style_Check_Tokens
then
694 Check_No_Space_Before
;
696 if Source
(Scan_Ptr
) > ' ' then
697 Error_Space_Required
(Scan_Ptr
);
706 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
707 -- to appear either on the same line as the IF, or on a separate line
708 -- after multiple conditions. In any case, it may not appear on the
709 -- line immediately following the line with the IF.
711 procedure Check_Then
(If_Loc
: Source_Ptr
) is
713 if Style_Check_If_Then_Layout
then
714 if Get_Physical_Line_Number
(Token_Ptr
) =
715 Get_Physical_Line_Number
(If_Loc
) + 1
717 Error_Msg_SC
("(style) misplaced THEN");
722 -------------------------------
723 -- Check_Unary_Plus_Or_Minus --
724 -------------------------------
726 -- In check tokem mode (-gnatyt), unary plus or minus must not be
727 -- followed by a space.
729 procedure Check_Unary_Plus_Or_Minus
is
731 if Style_Check_Tokens
then
732 Check_No_Space_After
;
734 end Check_Unary_Plus_Or_Minus
;
736 ------------------------
737 -- Check_Vertical_Bar --
738 ------------------------
740 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
742 procedure Check_Vertical_Bar
is
744 if Style_Check_Tokens
then
745 Require_Preceding_Space
;
746 Require_Following_Space
;
748 end Check_Vertical_Bar
;
750 -----------------------
751 -- Check_Xtra_Parens --
752 -----------------------
754 procedure Check_Xtra_Parens
(Loc
: Source_Ptr
) is
756 if Style_Check_Xtra_Parens
then
757 Error_Msg
("redundant parentheses?", Loc
);
759 end Check_Xtra_Parens
;
761 ----------------------------
762 -- Determine_Token_Casing --
763 ----------------------------
765 function Determine_Token_Casing
return Casing_Type
is
767 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
768 end Determine_Token_Casing
;
770 -----------------------------
771 -- Error_Space_Not_Allowed --
772 -----------------------------
774 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
776 Error_Msg
("(style) space not allowed", S
);
777 end Error_Space_Not_Allowed
;
779 --------------------------
780 -- Error_Space_Required --
781 --------------------------
783 procedure Error_Space_Required
(S
: Source_Ptr
) is
785 Error_Msg
("(style) space required", S
);
786 end Error_Space_Required
;
792 function Is_White_Space
(C
: Character) return Boolean is
794 return C
= ' ' or else C
= HT
;
801 function Mode_In_Check
return Boolean is
803 return Style_Check
and Style_Check_Mode_In
;
810 -- In check end/exit labels mode (-gnatye), always require the name of
811 -- a subprogram or package to be present on the END, so this is an error.
813 procedure No_End_Name
(Name
: Node_Id
) is
815 if Style_Check_End_Labels
then
816 Error_Msg_Node_1
:= Name
;
817 Error_Msg_SP
("(style) `END &` required");
825 -- In check end/exit labels mode (-gnatye), always require the name of
826 -- the loop to be present on the EXIT when exiting a named loop.
828 procedure No_Exit_Name
(Name
: Node_Id
) is
830 if Style_Check_End_Labels
then
831 Error_Msg_Node_1
:= Name
;
832 Error_Msg_SP
("(style) `EXIT &` required");
836 ----------------------------
837 -- Non_Lower_Case_Keyword --
838 ----------------------------
840 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
841 -- in all lower case (excluding keywords range, access, delta and digits
842 -- used as attribute designators).
844 procedure Non_Lower_Case_Keyword
is
846 if Style_Check_Keyword_Casing
then
847 Error_Msg_SC
("(style) reserved words must be all lower case");
849 end Non_Lower_Case_Keyword
;
851 -----------------------------
852 -- Require_Following_Space --
853 -----------------------------
855 procedure Require_Following_Space
is
857 if Source
(Scan_Ptr
) > ' ' then
858 Error_Space_Required
(Scan_Ptr
);
860 end Require_Following_Space
;
862 -----------------------------
863 -- Require_Preceding_Space --
864 -----------------------------
866 procedure Require_Preceding_Space
is
868 if Token_Ptr
> Source_First
(Current_Source_File
)
869 and then Source
(Token_Ptr
- 1) > ' '
871 Error_Space_Required
(Token_Ptr
);
873 end Require_Preceding_Space
;
875 ---------------------
876 -- RM_Column_Check --
877 ---------------------
879 function RM_Column_Check
return Boolean is
881 return Style_Check
and Style_Check_Layout
;