1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 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
269 while Source
(S
) not in Line_Terminator
loop
273 return Source
(S
- 1) = '-' and then Source
(S
- 2) = '-';
276 -- Start of processing for Check_Comment
279 -- Can never have a non-blank character preceding the first minus
281 if Style_Check_Comments
then
282 if Scan_Ptr
> Source_First
(Current_Source_File
)
283 and then Source
(Scan_Ptr
- 1) > ' '
285 Error_Msg_S
("(style) space required");
289 -- For a comment that is not at the start of the line, the only
290 -- requirement is that we cannot have a non-blank character after
291 -- the second minus sign.
293 if Scan_Ptr
/= First_Non_Blank_Location
then
294 if Style_Check_Comments
then
295 if Source
(Scan_Ptr
+ 2) > ' ' then
296 Error_Msg
("(style) space required", Scan_Ptr
+ 2);
302 -- Case of a comment that is at the start of a line
305 -- First check, must be in appropriately indented column
307 if Style_Check_Indentation
/= 0 then
308 if Start_Column
rem Style_Check_Indentation
/= 0 then
309 Error_Msg_S
("(style) bad column");
314 -- If we are not checking comments, nothing to do
316 if not Style_Check_Comments
then
320 -- Case of not followed by a blank. Usually wrong, but there are
321 -- some exceptions that we permit.
323 if Source
(Scan_Ptr
+ 2) /= ' ' then
324 C
:= Source
(Scan_Ptr
+ 2);
326 -- Case of -- all on its own on a line is OK
332 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
333 -- This is not permitted in internal GNAT implementation units
334 -- except for the case of --! as used by gnatprep output.
342 if Character'Pos (C
) in 16#
21#
.. 16#
2F#
344 Character'Pos (C
) in 16#
3A#
.. 16#
3F#
350 -- The only other case in which we allow a character after
351 -- the -- other than a space is when we have a row of minus
352 -- signs (case of header lines for a box comment for example).
355 while Source
(S
) >= ' ' loop
356 if Source
(S
) /= '-' then
357 if Is_Box_Comment
then
358 Error_Space_Required
(Scan_Ptr
+ 2);
360 Error_Msg
("(style) two spaces required", Scan_Ptr
+ 2);
369 -- If we are followed by a blank, then the comment is OK if the
370 -- character following this blank is another blank or a format
373 elsif Source
(Scan_Ptr
+ 3) <= ' ' then
376 -- Here is the case where we only have one blank after the two
377 -- minus signs, which is an error unless the line ends with two
378 -- minus signs, the case of a box comment.
380 elsif not Is_Box_Comment
then
381 Error_Space_Required
(Scan_Ptr
+ 3);
390 -- In check token mode (-gnatyt), colon must be surrounded by spaces
392 procedure Check_Dot_Dot
is
394 if Style_Check_Tokens
then
395 Require_Preceding_Space
;
396 Require_Following_Space
;
400 -----------------------------------
401 -- Check_Exponentiation_Operator --
402 -----------------------------------
404 -- No spaces are required for the ** operator in GNAT style check mode
406 procedure Check_Exponentiation_Operator
is
409 end Check_Exponentiation_Operator
;
415 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
417 procedure Check_HT
is
419 if Style_Check_Horizontal_Tabs
then
420 Error_Msg_S
("(style) horizontal tab not allowed");
424 -----------------------
425 -- Check_Indentation --
426 -----------------------
428 -- In check indentation mode (-gnatyn for n a digit), a new statement or
429 -- declaration is required to start in a column that is a multiple of the
430 -- indentiation amount.
432 procedure Check_Indentation
is
434 if Style_Check_Indentation
/= 0 then
435 if Token_Ptr
= First_Non_Blank_Location
436 and then Start_Column
rem Style_Check_Indentation
/= 0
438 Error_Msg_SC
("(style) bad indentation");
441 end Check_Indentation
;
443 ----------------------
444 -- Check_Left_Paren --
445 ----------------------
447 -- In tone check mode (-gnatyt), left paren must not be preceded by an
448 -- identifier character or digit (a separating space is required) and
449 -- may never be followed by a space.
451 procedure Check_Left_Paren
is
453 if Style_Check_Tokens
then
454 if Token_Ptr
> Source_First
(Current_Source_File
)
455 and then Identifier_Char
(Source
(Token_Ptr
- 1))
457 Error_Space_Required
(Token_Ptr
);
460 Check_No_Space_After
;
462 end Check_Left_Paren
;
464 ---------------------------
465 -- Check_Line_Terminator --
466 ---------------------------
468 -- In check blanks at end mode (-gnatyb), lines may not end with a
471 -- In check max line length mode (-gnatym), the line length must
472 -- not exceed the permitted maximum value.
474 -- In check form feeds mode (-gnatyf), the line terminator may not
475 -- be either of the characters FF or VT.
477 procedure Check_Line_Terminator
(Len
: Int
) is
481 -- Check FF/VT terminators
483 if Style_Check_Form_Feeds
then
484 if Source
(Scan_Ptr
) = ASCII
.FF
then
485 Error_Msg_S
("(style) form feed not allowed");
487 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
488 Error_Msg_S
("(style) vertical tab not allowed");
492 -- We are now possibly going to check for trailing spaces and maximum
493 -- line length. There is no point in doing this if the current line is
494 -- empty. It is actually wrong in the case of trailing spaces, because
495 -- we scan backwards for this purpose, so we would end up looking at a
496 -- different line, or even at invalid buffer locations if we have the
497 -- first source line at hand.
503 -- Check trailing space
505 if Style_Check_Blanks_At_End
then
506 if Scan_Ptr
>= First_Non_Blank_Location
then
507 if Is_White_Space
(Source
(Scan_Ptr
- 1)) then
510 while Is_White_Space
(Source
(S
- 1)) loop
514 Error_Msg
("(style) trailing spaces not permitted", S
);
519 -- Check max line length
521 if Style_Check_Max_Line_Length
then
522 if Len
> Style_Max_Line_Length
then
524 ("(style) this line is too long",
525 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
529 end Check_Line_Terminator
;
531 --------------------------
532 -- Check_No_Space_After --
533 --------------------------
535 procedure Check_No_Space_After
is
539 if Is_White_Space
(Source
(Scan_Ptr
)) then
541 -- Allow one or more spaces if followed by comment
545 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
548 elsif Is_White_Space
(Source
(S
)) then
556 Error_Space_Not_Allowed
(Scan_Ptr
);
558 end Check_No_Space_After
;
560 ---------------------------
561 -- Check_No_Space_Before --
562 ---------------------------
564 procedure Check_No_Space_Before
is
566 if Token_Ptr
> First_Non_Blank_Location
567 and then Source
(Token_Ptr
- 1) <= ' '
569 Error_Space_Not_Allowed
(Token_Ptr
- 1);
571 end Check_No_Space_Before
;
573 -----------------------
574 -- Check_Pragma_Name --
575 -----------------------
577 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
578 -- case, i.e. start with an upper case letter, and otherwise lower case,
579 -- except after an underline character.
581 procedure Check_Pragma_Name
is
583 if Style_Check_Pragma_Casing
then
584 if Determine_Token_Casing
/= Mixed_Case
then
585 Error_Msg_SC
("(style) bad capitalization, mixed case required");
588 end Check_Pragma_Name
;
590 -----------------------
591 -- Check_Right_Paren --
592 -----------------------
594 -- In check tokens mode (-gnatyt), right paren must never be preceded by
595 -- a space unless it is the initial non-blank character on the line.
597 procedure Check_Right_Paren
is
599 if Style_Check_Tokens
then
600 Check_No_Space_Before
;
602 end Check_Right_Paren
;
604 ---------------------
605 -- Check_Semicolon --
606 ---------------------
608 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
609 -- space and a following space is required.
611 procedure Check_Semicolon
is
613 if Style_Check_Tokens
then
614 Check_No_Space_Before
;
616 if Source
(Scan_Ptr
) > ' ' then
617 Error_Space_Required
(Scan_Ptr
);
626 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
627 -- to appear either on the same line as the IF, or on a separate line
628 -- after multiple conditions. In any case, it may not appear on the
629 -- line immediately following the line with the IF.
631 procedure Check_Then
(If_Loc
: Source_Ptr
) is
633 if Style_Check_If_Then_Layout
then
634 if Get_Physical_Line_Number
(Token_Ptr
) =
635 Get_Physical_Line_Number
(If_Loc
) + 1
637 Error_Msg_SC
("(style) misplaced THEN");
642 -------------------------------
643 -- Check_Unary_Plus_Or_Minus --
644 -------------------------------
646 -- In check tokem mode (-gnatyt), unary plus or minus must not be
647 -- followed by a space.
649 procedure Check_Unary_Plus_Or_Minus
is
651 if Style_Check_Tokens
then
652 Check_No_Space_After
;
654 end Check_Unary_Plus_Or_Minus
;
656 ------------------------
657 -- Check_Vertical_Bar --
658 ------------------------
660 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
662 procedure Check_Vertical_Bar
is
664 if Style_Check_Tokens
then
665 Require_Preceding_Space
;
666 Require_Following_Space
;
668 end Check_Vertical_Bar
;
670 ----------------------------
671 -- Determine_Token_Casing --
672 ----------------------------
674 function Determine_Token_Casing
return Casing_Type
is
676 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
677 end Determine_Token_Casing
;
679 -----------------------------
680 -- Error_Space_Not_Allowed --
681 -----------------------------
683 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
685 Error_Msg
("(style) space not allowed", S
);
686 end Error_Space_Not_Allowed
;
688 --------------------------
689 -- Error_Space_Required --
690 --------------------------
692 procedure Error_Space_Required
(S
: Source_Ptr
) is
694 Error_Msg
("(style) space required", S
);
695 end Error_Space_Required
;
701 function Is_White_Space
(C
: Character) return Boolean is
703 return C
= ' ' or else C
= HT
;
710 -- In check end/exit labels mode (-gnatye), always require the name of
711 -- a subprogram or package to be present on the END, so this is an error.
713 procedure No_End_Name
(Name
: Node_Id
) is
715 if Style_Check_End_Labels
then
716 Error_Msg_Node_1
:= Name
;
717 Error_Msg_SP
("(style) `END &` required");
725 -- In check end/exit labels mode (-gnatye), always require the name of
726 -- the loop to be present on the EXIT when exiting a named loop.
728 procedure No_Exit_Name
(Name
: Node_Id
) is
730 if Style_Check_End_Labels
then
731 Error_Msg_Node_1
:= Name
;
732 Error_Msg_SP
("(style) `EXIT &` required");
736 ----------------------------
737 -- Non_Lower_Case_Keyword --
738 ----------------------------
740 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
741 -- in all lower case (excluding keywords range, access, delta and digits
742 -- used as attribute designators).
744 procedure Non_Lower_Case_Keyword
is
746 if Style_Check_Keyword_Casing
then
747 Error_Msg_SC
("(style) reserved words must be all lower case");
749 end Non_Lower_Case_Keyword
;
751 -----------------------------
752 -- Require_Following_Space --
753 -----------------------------
755 procedure Require_Following_Space
is
757 if Source
(Scan_Ptr
) > ' ' then
758 Error_Space_Required
(Scan_Ptr
);
760 end Require_Following_Space
;
762 -----------------------------
763 -- Require_Preceding_Space --
764 -----------------------------
766 procedure Require_Preceding_Space
is
768 if Token_Ptr
> Source_First
(Current_Source_File
)
769 and then Source
(Token_Ptr
- 1) > ' '
771 Error_Space_Required
(Token_Ptr
);
773 end Require_Preceding_Space
;
775 ---------------------
776 -- RM_Column_Check --
777 ---------------------
779 function RM_Column_Check
return Boolean is
781 return Style_Check
and Style_Check_Layout
;