1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 procedure Check_No_Space_After
;
48 -- Checks that there is a non-white space character after the current
49 -- token, or white space followed by a comment, or the end of line.
50 -- Issue error message if not.
52 procedure Check_No_Space_Before
;
53 -- Check that token is first token on line, or else is not preceded
54 -- by white space. Signal error of space not allowed if not.
56 function Determine_Token_Casing
return Casing_Type
;
58 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
);
59 -- Posts an error message indicating that a space is not allowed
60 -- at the given source location.
62 procedure Error_Space_Required
(S
: Source_Ptr
);
63 -- Posts an error message indicating that a space is required at
64 -- the given source location.
66 function Is_White_Space
(C
: Character) return Boolean;
67 pragma Inline
(Is_White_Space
);
68 -- Returns True for space, HT, VT or FF, False otherwise
70 procedure Require_Following_Space
;
71 pragma Inline
(Require_Following_Space
);
72 -- Require token to be followed by white space. Used only if in GNAT
73 -- style checking mode.
75 procedure Require_Preceding_Space
;
76 pragma Inline
(Require_Preceding_Space
);
77 -- Require token to be preceded by white space. Used only if in GNAT
78 -- style checking mode.
80 ----------------------
81 -- Check_Abs_Or_Not --
82 ----------------------
84 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
86 procedure Check_Abs_Not
is
88 if Style_Check_Tokens
then
89 if Source
(Scan_Ptr
) > ' ' then
90 Error_Space_Required
(Scan_Ptr
);
95 ----------------------
96 -- Check_Apostrophe --
97 ----------------------
99 -- Do not allow space before or after apostrophe
101 procedure Check_Apostrophe
is
103 if Style_Check_Tokens
then
104 Check_No_Space_After
;
106 end Check_Apostrophe
;
112 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
114 procedure Check_Arrow
is
116 if Style_Check_Tokens
then
117 Require_Preceding_Space
;
118 Require_Following_Space
;
122 --------------------------
123 -- Check_Attribute_Name --
124 --------------------------
126 -- In check attribute casing mode (-gnatya), attribute names must be
127 -- mixed case, i.e. start with an upper case letter, and otherwise
128 -- lower case, except after an underline character.
130 procedure Check_Attribute_Name
(Reserved
: Boolean) is
131 pragma Warnings
(Off
, Reserved
);
134 if Style_Check_Attribute_Casing
then
135 if Determine_Token_Casing
/= Mixed_Case
then
136 Error_Msg_SC
("(style) bad capitalization, mixed case required");
139 end Check_Attribute_Name
;
141 ---------------------------
142 -- Check_Binary_Operator --
143 ---------------------------
145 -- In check token mode (-gnatyt), binary operators other than the special
146 -- case of exponentiation require surrounding space characters.
148 procedure Check_Binary_Operator
is
150 if Style_Check_Tokens
then
151 Require_Preceding_Space
;
152 Require_Following_Space
;
154 end Check_Binary_Operator
;
160 -- In check token mode (-gnatyt), box must be preceded by a space or by
161 -- a left parenthesis. Spacing checking on the surrounding tokens takes
162 -- care of the remaining checks.
164 procedure Check_Box
is
166 if Style_Check_Tokens
then
167 if Prev_Token
/= Tok_Left_Paren
then
168 Require_Preceding_Space
;
177 -- In check token mode (-gnatyt), colon must be surrounded by spaces
179 procedure Check_Colon
is
181 if Style_Check_Tokens
then
182 Require_Preceding_Space
;
183 Require_Following_Space
;
187 -----------------------
188 -- Check_Colon_Equal --
189 -----------------------
191 -- In check token mode (-gnatyt), := must be surrounded by spaces
193 procedure Check_Colon_Equal
is
195 if Style_Check_Tokens
then
196 Require_Preceding_Space
;
197 Require_Following_Space
;
199 end Check_Colon_Equal
;
205 -- In check token mode (-gnatyt), comma must be either the first
206 -- token on a line, or be preceded by a non-blank character.
207 -- It must also always be followed by a blank.
209 procedure Check_Comma
is
211 if Style_Check_Tokens
then
212 Check_No_Space_Before
;
214 if Source
(Scan_Ptr
) > ' ' then
215 Error_Space_Required
(Scan_Ptr
);
224 -- In check comment mode (-gnatyc) there are several requirements on the
225 -- format of comments. The following are permissible comment formats:
227 -- 1. Any comment that is not at the start of a line, i.e. where the
228 -- initial minuses are not the first non-blank characters on the
229 -- line must have at least one blank after the second minus.
231 -- 2. A row of all minuses of any length is permitted (see procedure
232 -- box above in the source of this routine).
234 -- 3. A comment line starting with two minuses and a space, and ending
235 -- with a space and two minuses. Again see the procedure title box
236 -- immediately above in the source.
238 -- 4. A full line comment where two spaces follow the two minus signs.
239 -- This is the normal comment format in GNAT style, as typified by
240 -- the comments you are reading now.
242 -- 5. A full line comment where the first character after the second
243 -- minus is a special character, i.e. a character in the ASCII
244 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
245 -- comments, such as those generated by gnatprep, or those that
246 -- appear in the SPARK annotation language to be accepted.
248 -- Note: for GNAT internal files (-gnatg switch set on for the
249 -- compilation), the only special sequence recognized and allowed
250 -- is --! as generated by gnatprep.
252 procedure Check_Comment
is
256 function Is_Box_Comment
return Boolean;
257 -- Returns True if the last two characters on the line are -- which
258 -- characterizes a box comment (as for example follows this spec).
264 function Is_Box_Comment
return Boolean is
268 -- Do we need to worry about UTF_32 line terminators here ???
271 while Source
(S
) not in Line_Terminator
loop
275 return Source
(S
- 1) = '-' and then Source
(S
- 2) = '-';
278 -- Start of processing for Check_Comment
281 -- Can never have a non-blank character preceding the first minus
283 if Style_Check_Comments
then
284 if Scan_Ptr
> Source_First
(Current_Source_File
)
285 and then Source
(Scan_Ptr
- 1) > ' '
287 Error_Msg_S
("(style) space required");
291 -- For a comment that is not at the start of the line, the only
292 -- requirement is that we cannot have a non-blank character after
293 -- the second minus sign.
295 if Scan_Ptr
/= First_Non_Blank_Location
then
296 if Style_Check_Comments
then
297 if Source
(Scan_Ptr
+ 2) > ' ' then
298 Error_Msg
("(style) space required", Scan_Ptr
+ 2);
304 -- Case of a comment that is at the start of a line
307 -- First check, must be in appropriately indented column
309 if Style_Check_Indentation
/= 0 then
310 if Start_Column
rem Style_Check_Indentation
/= 0 then
311 Error_Msg_S
("(style) bad column");
316 -- If we are not checking comments, nothing to do
318 if not Style_Check_Comments
then
322 -- Case of not followed by a blank. Usually wrong, but there are
323 -- some exceptions that we permit.
325 if Source
(Scan_Ptr
+ 2) /= ' ' then
326 C
:= Source
(Scan_Ptr
+ 2);
328 -- Case of -- all on its own on a line is OK
334 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
335 -- This is not permitted in internal GNAT implementation units
336 -- except for the case of --! as used by gnatprep output.
344 if Character'Pos (C
) in 16#
21#
.. 16#
2F#
346 Character'Pos (C
) in 16#
3A#
.. 16#
3F#
352 -- The only other case in which we allow a character after
353 -- the -- other than a space is when we have a row of minus
354 -- signs (case of header lines for a box comment for example).
357 while Source
(S
) >= ' ' loop
358 if Source
(S
) /= '-' then
359 if Is_Box_Comment
then
360 Error_Space_Required
(Scan_Ptr
+ 2);
362 Error_Msg
("(style) two spaces required", Scan_Ptr
+ 2);
371 -- If we are followed by a blank, then the comment is OK if the
372 -- character following this blank is another blank or a format
375 elsif Source
(Scan_Ptr
+ 3) <= ' ' then
378 -- Here is the case where we only have one blank after the two
379 -- minus signs, which is an error unless the line ends with two
380 -- minus signs, the case of a box comment.
382 elsif not Is_Box_Comment
then
383 Error_Space_Required
(Scan_Ptr
+ 3);
392 -- In check token mode (-gnatyt), colon must be surrounded by spaces
394 procedure Check_Dot_Dot
is
396 if Style_Check_Tokens
then
397 Require_Preceding_Space
;
398 Require_Following_Space
;
402 -----------------------------------
403 -- Check_Exponentiation_Operator --
404 -----------------------------------
406 -- No spaces are required for the ** operator in GNAT style check mode
408 procedure Check_Exponentiation_Operator
is
411 end Check_Exponentiation_Operator
;
417 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
419 procedure Check_HT
is
421 if Style_Check_Horizontal_Tabs
then
422 Error_Msg_S
("(style) horizontal tab not allowed");
426 -----------------------
427 -- Check_Indentation --
428 -----------------------
430 -- In check indentation mode (-gnatyn for n a digit), a new statement or
431 -- declaration is required to start in a column that is a multiple of the
432 -- indentiation amount.
434 procedure Check_Indentation
is
436 if Style_Check_Indentation
/= 0 then
437 if Token_Ptr
= First_Non_Blank_Location
438 and then Start_Column
rem Style_Check_Indentation
/= 0
440 Error_Msg_SC
("(style) bad indentation");
443 end Check_Indentation
;
445 ----------------------
446 -- Check_Left_Paren --
447 ----------------------
449 -- In tone check mode (-gnatyt), left paren must not be preceded by an
450 -- identifier character or digit (a separating space is required) and
451 -- may never be followed by a space.
453 procedure Check_Left_Paren
is
455 if Style_Check_Tokens
then
456 if Token_Ptr
> Source_First
(Current_Source_File
)
457 and then Identifier_Char
(Source
(Token_Ptr
- 1))
459 Error_Space_Required
(Token_Ptr
);
462 Check_No_Space_After
;
464 end Check_Left_Paren
;
466 ---------------------------
467 -- Check_Line_Max_Length --
468 ---------------------------
470 -- In check max line length mode (-gnatym), the line length must
471 -- not exceed the permitted maximum value.
473 procedure Check_Line_Max_Length
(Len
: Int
) is
475 if Style_Check_Max_Line_Length
then
476 if Len
> Style_Max_Line_Length
then
478 ("(style) this line is too long",
479 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
482 end Check_Line_Max_Length
;
484 ---------------------------
485 -- Check_Line_Terminator --
486 ---------------------------
488 -- In check blanks at end mode (-gnatyb), lines may not end with a
491 -- In check form feeds mode (-gnatyf), the line terminator may not
492 -- be either of the characters FF or VT.
494 -- In check DOS line terminators node (-gnatyd), the line terminator
495 -- must be a single LF, without a following CR.
497 procedure Check_Line_Terminator
(Len
: Int
) is
501 -- Check FF/VT terminators
503 if Style_Check_Form_Feeds
then
504 if Source
(Scan_Ptr
) = ASCII
.FF
then
505 Error_Msg_S
("(style) form feed not allowed");
506 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
507 Error_Msg_S
("(style) vertical tab not allowed");
511 -- Check DOS line terminator (ignore EOF, since we only get called
512 -- with an EOF if it is the last character in the buffer, and was
513 -- therefore not present in the sources
515 if Style_Check_DOS_Line_Terminator
then
516 if Source
(Scan_Ptr
) = EOF
then
518 elsif Source
(Scan_Ptr
) /= LF
519 or else Source
(Scan_Ptr
+ 1) = CR
521 Error_Msg_S
("(style) incorrect line terminator");
525 -- We are now possibly going to check for trailing spaces. There is no
526 -- point in doing this if the current line is empty. It is actually
527 -- wrong to do so, because we scan backwards for this purpose, so we
528 -- would end up looking at different line, or even at invalid buffer
529 -- locations if we have the first source line at hand.
535 -- Check trailing space
537 if Style_Check_Blanks_At_End
then
538 if Scan_Ptr
>= First_Non_Blank_Location
then
539 if Is_White_Space
(Source
(Scan_Ptr
- 1)) then
542 while Is_White_Space
(Source
(S
- 1)) loop
546 Error_Msg
("(style) trailing spaces not permitted", S
);
550 end Check_Line_Terminator
;
552 --------------------------
553 -- Check_No_Space_After --
554 --------------------------
556 procedure Check_No_Space_After
is
560 if Is_White_Space
(Source
(Scan_Ptr
)) then
562 -- Allow one or more spaces if followed by comment
566 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
569 elsif Is_White_Space
(Source
(S
)) then
577 Error_Space_Not_Allowed
(Scan_Ptr
);
579 end Check_No_Space_After
;
581 ---------------------------
582 -- Check_No_Space_Before --
583 ---------------------------
585 procedure Check_No_Space_Before
is
587 if Token_Ptr
> First_Non_Blank_Location
588 and then Source
(Token_Ptr
- 1) <= ' '
590 Error_Space_Not_Allowed
(Token_Ptr
- 1);
592 end Check_No_Space_Before
;
594 -----------------------
595 -- Check_Pragma_Name --
596 -----------------------
598 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
599 -- case, i.e. start with an upper case letter, and otherwise lower case,
600 -- except after an underline character.
602 procedure Check_Pragma_Name
is
604 if Style_Check_Pragma_Casing
then
605 if Determine_Token_Casing
/= Mixed_Case
then
606 Error_Msg_SC
("(style) bad capitalization, mixed case required");
609 end Check_Pragma_Name
;
611 -----------------------
612 -- Check_Right_Paren --
613 -----------------------
615 -- In check tokens mode (-gnatyt), right paren must never be preceded by
616 -- a space unless it is the initial non-blank character on the line.
618 procedure Check_Right_Paren
is
620 if Style_Check_Tokens
then
621 Check_No_Space_Before
;
623 end Check_Right_Paren
;
625 ---------------------
626 -- Check_Semicolon --
627 ---------------------
629 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
630 -- space and a following space is required.
632 procedure Check_Semicolon
is
634 if Style_Check_Tokens
then
635 Check_No_Space_Before
;
637 if Source
(Scan_Ptr
) > ' ' then
638 Error_Space_Required
(Scan_Ptr
);
647 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
648 -- to appear either on the same line as the IF, or on a separate line
649 -- after multiple conditions. In any case, it may not appear on the
650 -- line immediately following the line with the IF.
652 procedure Check_Then
(If_Loc
: Source_Ptr
) is
654 if Style_Check_If_Then_Layout
then
655 if Get_Physical_Line_Number
(Token_Ptr
) =
656 Get_Physical_Line_Number
(If_Loc
) + 1
658 Error_Msg_SC
("(style) misplaced THEN");
663 -------------------------------
664 -- Check_Unary_Plus_Or_Minus --
665 -------------------------------
667 -- In check tokem mode (-gnatyt), unary plus or minus must not be
668 -- followed by a space.
670 procedure Check_Unary_Plus_Or_Minus
is
672 if Style_Check_Tokens
then
673 Check_No_Space_After
;
675 end Check_Unary_Plus_Or_Minus
;
677 ------------------------
678 -- Check_Vertical_Bar --
679 ------------------------
681 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
683 procedure Check_Vertical_Bar
is
685 if Style_Check_Tokens
then
686 Require_Preceding_Space
;
687 Require_Following_Space
;
689 end Check_Vertical_Bar
;
691 -----------------------
692 -- Check_Xtra_Parens --
693 -----------------------
695 procedure Check_Xtra_Parens
(Loc
: Source_Ptr
) is
697 if Style_Check_Xtra_Parens
then
698 Error_Msg
("redundant parentheses?", Loc
);
700 end Check_Xtra_Parens
;
702 ----------------------------
703 -- Determine_Token_Casing --
704 ----------------------------
706 function Determine_Token_Casing
return Casing_Type
is
708 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
709 end Determine_Token_Casing
;
711 -----------------------------
712 -- Error_Space_Not_Allowed --
713 -----------------------------
715 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
717 Error_Msg
("(style) space not allowed", S
);
718 end Error_Space_Not_Allowed
;
720 --------------------------
721 -- Error_Space_Required --
722 --------------------------
724 procedure Error_Space_Required
(S
: Source_Ptr
) is
726 Error_Msg
("(style) space required", S
);
727 end Error_Space_Required
;
733 function Is_White_Space
(C
: Character) return Boolean is
735 return C
= ' ' or else C
= HT
;
742 -- In check end/exit labels mode (-gnatye), always require the name of
743 -- a subprogram or package to be present on the END, so this is an error.
745 procedure No_End_Name
(Name
: Node_Id
) is
747 if Style_Check_End_Labels
then
748 Error_Msg_Node_1
:= Name
;
749 Error_Msg_SP
("(style) `END &` required");
757 -- In check end/exit labels mode (-gnatye), always require the name of
758 -- the loop to be present on the EXIT when exiting a named loop.
760 procedure No_Exit_Name
(Name
: Node_Id
) is
762 if Style_Check_End_Labels
then
763 Error_Msg_Node_1
:= Name
;
764 Error_Msg_SP
("(style) `EXIT &` required");
768 ----------------------------
769 -- Non_Lower_Case_Keyword --
770 ----------------------------
772 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
773 -- in all lower case (excluding keywords range, access, delta and digits
774 -- used as attribute designators).
776 procedure Non_Lower_Case_Keyword
is
778 if Style_Check_Keyword_Casing
then
779 Error_Msg_SC
("(style) reserved words must be all lower case");
781 end Non_Lower_Case_Keyword
;
783 -----------------------------
784 -- Require_Following_Space --
785 -----------------------------
787 procedure Require_Following_Space
is
789 if Source
(Scan_Ptr
) > ' ' then
790 Error_Space_Required
(Scan_Ptr
);
792 end Require_Following_Space
;
794 -----------------------------
795 -- Require_Preceding_Space --
796 -----------------------------
798 procedure Require_Preceding_Space
is
800 if Token_Ptr
> Source_First
(Current_Source_File
)
801 and then Source
(Token_Ptr
- 1) > ' '
803 Error_Space_Required
(Token_Ptr
);
805 end Require_Preceding_Space
;
807 ---------------------
808 -- RM_Column_Check --
809 ---------------------
811 function RM_Column_Check
return Boolean is
813 return Style_Check
and Style_Check_Layout
;