1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This version of the Style package implements the standard GNAT style
27 -- checking rules. For documentation of these rules, see comments on the
28 -- individual procedures.
30 with Atree
; use Atree
;
31 with Casing
; use Casing
;
32 with Csets
; use Csets
;
33 with Einfo
; use Einfo
;
34 with Einfo
.Utils
; use Einfo
.Utils
;
35 with Err_Vars
; use Err_Vars
;
37 with Scans
; use Scans
;
38 with Sinfo
; use Sinfo
;
39 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
40 with Sinput
; use Sinput
;
41 with Stylesw
; use Stylesw
;
43 package body Styleg
is
47 Blank_Lines
: Nat
:= 0;
48 -- Counts number of empty lines seen. Reset to zero if a non-empty line
49 -- is encountered. Used to check for trailing blank lines in Check_EOF,
50 -- and for multiple blank lines.
52 Blank_Line_Location
: Source_Ptr
;
53 -- Remembers location of first blank line in a series. Used to issue an
54 -- appropriate diagnostic if subsequent blank lines or the end of file
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Check_No_Space_After
;
62 -- Checks that there is a non-white space character after the current
63 -- token, or white space followed by a comment, or the end of line.
64 -- Issue error message if not.
66 procedure Check_No_Space_Before
;
67 -- Check that token is first token on line, or else is not preceded
68 -- by white space. Signal error of space not allowed if not.
70 procedure Check_Separate_Stmt_Lines_Cont
;
71 -- Non-inlined continuation of Check_Separate_Stmt_Lines
73 function Determine_Token_Casing
return Casing_Type
;
74 -- Determine casing of current token
76 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
);
77 -- Posts an error message indicating that a space is not allowed
78 -- at the given source location.
80 procedure Error_Space_Required
(S
: Source_Ptr
);
81 -- Posts an error message indicating that a space is required at
82 -- the given source location.
84 function Is_White_Space
(C
: Character) return Boolean;
85 pragma Inline
(Is_White_Space
);
86 -- Returns True for space or HT, False otherwise
88 procedure Require_Following_Space
;
89 pragma Inline
(Require_Following_Space
);
90 -- Require token to be followed by white space. Used only if in GNAT
91 -- style checking mode.
93 procedure Require_Preceding_Space
;
94 pragma Inline
(Require_Preceding_Space
);
95 -- Require token to be preceded by white space. Used only if in GNAT
96 -- style checking mode.
98 ----------------------
99 -- Check_Abs_Or_Not --
100 ----------------------
102 -- In check token mode (-gnatyt), ABS/NOT must be followed by a space or
105 procedure Check_Abs_Not
is
107 if Style_Check_Tokens
then
108 if Source
(Scan_Ptr
) not in ' ' | ASCII
.CR | ASCII
.LF
then
109 Error_Space_Required
(Scan_Ptr
);
114 ----------------------
115 -- Check_Apostrophe --
116 ----------------------
118 -- Do not allow space after apostrophe
120 procedure Check_Apostrophe
is
122 if Style_Check_Tokens
then
123 Check_No_Space_After
;
125 end Check_Apostrophe
;
131 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces,
132 -- except that within the argument of a Depends or Refined_Depends aspect
133 -- or pragma the required format is "=>+ " rather than "=> +").
135 procedure Check_Arrow
(Inside_Depends
: Boolean := False) is
137 if Style_Check_Tokens
then
138 Require_Preceding_Space
;
140 -- Special handling for Depends and Refined_Depends
142 if Inside_Depends
then
143 if Source
(Scan_Ptr
) = ' '
144 and then Source
(Scan_Ptr
+ 1) = '+'
146 Error_Space_Not_Allowed
(Scan_Ptr
);
148 elsif Source
(Scan_Ptr
) /= ' '
149 and then Source
(Scan_Ptr
) /= '+'
151 Require_Following_Space
;
157 Require_Following_Space
;
162 --------------------------
163 -- Check_Attribute_Name --
164 --------------------------
166 -- In check attribute casing mode (-gnatya), attribute names must be
167 -- mixed case, i.e. start with an upper case letter, and otherwise
168 -- lower case, except after an underline character.
170 procedure Check_Attribute_Name
(Reserved
: Boolean) is
171 pragma Warnings
(Off
, Reserved
);
173 if Style_Check_Attribute_Casing
then
174 if Determine_Token_Casing
/= Mixed_Case
then
175 Error_Msg_SC
-- CODEFIX
176 ("(style) bad capitalization, mixed case required");
179 end Check_Attribute_Name
;
181 ---------------------------
182 -- Check_Binary_Operator --
183 ---------------------------
185 -- In check token mode (-gnatyt), binary operators other than the special
186 -- case of exponentiation require surrounding space characters.
188 procedure Check_Binary_Operator
is
190 if Style_Check_Tokens
then
191 Require_Preceding_Space
;
192 Require_Following_Space
;
194 end Check_Binary_Operator
;
196 ----------------------------
197 -- Check_Boolean_Operator --
198 ----------------------------
200 procedure Check_Boolean_Operator
(Node
: Node_Id
) is
202 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean;
203 -- Returns True for simple variable, or "not X1" or "X1 and X2" or
204 -- "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
206 ------------------------
207 -- OK_Boolean_Operand --
208 ------------------------
210 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean is
212 if Nkind
(N
) in N_Identifier | N_Expanded_Name
then
215 elsif Nkind
(N
) = N_Op_Not
then
216 return OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
218 elsif Nkind
(N
) in N_Op_And | N_Op_Or
then
219 return OK_Boolean_Operand
(Original_Node
(Left_Opnd
(N
)))
221 OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
226 end OK_Boolean_Operand
;
228 -- Start of processing for Check_Boolean_Operator
231 if Style_Check_Boolean_And_Or
232 and then Comes_From_Source
(Node
)
235 Orig
: constant Node_Id
:= Original_Node
(Node
);
238 if Nkind
(Orig
) in N_Op_And | N_Op_Or
then
240 L
: constant Node_Id
:= Original_Node
(Left_Opnd
(Orig
));
241 R
: constant Node_Id
:= Original_Node
(Right_Opnd
(Orig
));
244 -- First OK case, simple boolean constants/identifiers
246 if OK_Boolean_Operand
(L
)
248 OK_Boolean_Operand
(R
)
252 -- Second OK case, modular types
254 elsif Is_Modular_Integer_Type
(Etype
(Node
)) then
257 -- Third OK case, array types
259 elsif Is_Array_Type
(Etype
(Node
)) then
262 -- Otherwise we have an error
264 elsif Nkind
(Orig
) = N_Op_And
then
266 ("(style) `AND THEN` required", Sloc
(Orig
));
269 ("(style) `OR ELSE` required", Sloc
(Orig
));
275 end Check_Boolean_Operator
;
281 -- In check token mode (-gnatyt), box must be preceded by a space or by
282 -- a left parenthesis. Spacing checking on the surrounding tokens takes
283 -- care of the remaining checks.
285 procedure Check_Box
is
287 if Style_Check_Tokens
then
288 if Prev_Token
/= Tok_Left_Paren
then
289 Require_Preceding_Space
;
298 -- In check token mode (-gnatyt), colon must be surrounded by spaces
300 procedure Check_Colon
is
302 if Style_Check_Tokens
then
303 Require_Preceding_Space
;
304 Require_Following_Space
;
308 -----------------------
309 -- Check_Colon_Equal --
310 -----------------------
312 -- In check token mode (-gnatyt), := must be surrounded by spaces
314 procedure Check_Colon_Equal
is
316 if Style_Check_Tokens
then
317 Require_Preceding_Space
;
318 Require_Following_Space
;
320 end Check_Colon_Equal
;
326 -- In check token mode (-gnatyt), comma must be either the first
327 -- token on a line, or be preceded by a non-blank character.
328 -- It must also always be followed by a blank.
330 procedure Check_Comma
is
332 if Style_Check_Tokens
then
333 Check_No_Space_Before
;
335 if Source
(Scan_Ptr
) > ' ' then
336 Error_Space_Required
(Scan_Ptr
);
345 -- In check comment mode (-gnatyc) there are several requirements on the
346 -- format of comments. The following are permissible comment formats:
348 -- 1. Any comment that is not at the start of a line, i.e. where the
349 -- initial minuses are not the first non-blank characters on the
350 -- line must have at least one blank after the second minus or a
351 -- special character as defined in rule 5.
353 -- 2. A row of all minuses of any length is permitted (see procedure
354 -- box above in the source of this routine).
356 -- 3. A comment line starting with two minuses and a space, and ending
357 -- with a space and two minuses. Again see the procedure title box
358 -- immediately above in the source.
360 -- 4. A full line comment where two spaces follow the two minus signs.
361 -- This is the normal comment format in GNAT style, as typified by
362 -- the comments you are reading now.
364 -- 5. A full line comment where the first character after the second
365 -- minus is a special character, i.e. a character in the ASCII
366 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
367 -- comments, such as those generated by gnatprep, or those that
368 -- appear in the SPARK annotation language to be accepted.
370 -- Note: for GNAT internal files (-gnatg switch set on for the
371 -- compilation), the only special sequence recognized and allowed
372 -- is --! as generated by gnatprep.
374 -- 6. In addition, the comment must be properly indented if comment
375 -- indentation checking is active (Style_Check_Indentation non-zero).
376 -- Either the start column must be a multiple of this indentation,
377 -- or the indentation must match that of the next non-blank line,
378 -- or must match the indentation of the immediately preciding line
379 -- if it is non-blank.
381 procedure Check_Comment
is
385 function Is_Box_Comment
return Boolean;
386 -- Returns True if the last two characters on the line are -- which
387 -- characterizes a box comment (as for example follows this spec).
389 function Is_Special_Character
(C
: Character) return Boolean;
390 -- Determines if C is a special character (see rule 5 above)
392 function Same_Column_As_Next_Non_Blank_Line
return Boolean;
393 -- Called for a full line comment. If the indentation of this comment
394 -- matches that of the next non-blank line in the source, then True is
395 -- returned, otherwise False.
397 function Same_Column_As_Previous_Line
return Boolean;
398 -- Called for a full line comment. If the previous line is blank, then
399 -- returns False. Otherwise, if the indentation of this comment matches
400 -- that of the previous line in the source, then True is returned,
407 function Is_Box_Comment
return Boolean is
411 -- Do we need to worry about UTF_32 line terminators here ???
414 while Source
(S
) not in Line_Terminator
loop
418 return Source
(S
- 1) = '-' and then Source
(S
- 2) = '-';
421 --------------------------
422 -- Is_Special_Character --
423 --------------------------
425 function Is_Special_Character
(C
: Character) return Boolean is
431 Character'Pos (C
) in 16#
21#
.. 16#
2F#
433 Character'Pos (C
) in 16#
3A#
.. 16#
3F#
;
435 end Is_Special_Character
;
437 ----------------------------------------
438 -- Same_Column_As_Next_Non_Blank_Line --
439 ----------------------------------------
441 function Same_Column_As_Next_Non_Blank_Line
return Boolean is
445 -- Step to end of line
448 while Source
(P
) not in Line_Terminator
loop
452 -- Step past blanks, and line terminators (UTF_32 case???)
454 while Source
(P
) <= ' ' and then Source
(P
) /= EOF
loop
460 return Get_Column_Number
(Scan_Ptr
) = Get_Column_Number
(P
);
461 end Same_Column_As_Next_Non_Blank_Line
;
463 ----------------------------------
464 -- Same_Column_As_Previous_Line --
465 ----------------------------------
467 function Same_Column_As_Previous_Line
return Boolean is
471 -- Point S to start of this line, and P to start of previous line
473 S
:= Line_Start
(Scan_Ptr
);
477 -- Step P to first non-blank character on line
480 -- If we get back to start of current line, then the previous line
481 -- was blank, and we always return False in that situation.
487 exit when Source
(P
) /= ' ' and then Source
(P
) /= ASCII
.HT
;
493 return Get_Column_Number
(Scan_Ptr
) = Get_Column_Number
(P
);
494 end Same_Column_As_Previous_Line
;
496 -- Start of processing for Check_Comment
499 -- Can never have a non-blank character preceding the first minus.
500 -- The "+ 3" is to leave room for a possible byte order mark (BOM);
501 -- we want to avoid a warning for a comment at the start of the
502 -- file just after the BOM.
504 if Style_Check_Comments
then
505 if Scan_Ptr
> Source_First
(Current_Source_File
) + 3
506 and then Source
(Scan_Ptr
- 1) > ' '
508 Error_Msg_S
-- CODEFIX
509 ("(style) space required");
513 -- For a comment that is not at the start of the line, the only
514 -- requirement is that we cannot have a non-blank character after
515 -- the second minus sign or a special character.
517 if Scan_Ptr
/= First_Non_Blank_Location
then
518 if Style_Check_Comments
then
519 if Source
(Scan_Ptr
+ 2) > ' '
520 and then not Is_Special_Character
(Source
(Scan_Ptr
+ 2))
523 ("(style) space required", Scan_Ptr
+ 2);
529 -- Case of a comment that is at the start of a line
532 -- First check, must be in appropriately indented column
534 if Style_Check_Indentation
/= 0 then
535 if Start_Column
rem Style_Check_Indentation
/= 0 then
536 if not Same_Column_As_Next_Non_Blank_Line
537 and then not Same_Column_As_Previous_Line
539 Error_Msg_S
-- CODEFIX
540 ("(style) bad column");
547 -- If we are not checking comments, nothing more to do
549 if not Style_Check_Comments
then
553 -- Case of not followed by a blank. Usually wrong, but there are
554 -- some exceptions that we permit.
556 if Source
(Scan_Ptr
+ 2) /= ' ' then
557 C
:= Source
(Scan_Ptr
+ 2);
559 -- Case of -- all on its own on a line is OK
565 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
566 -- This is not permitted in internal GNAT implementation units
567 -- except for the case of --! as used by gnatprep output.
569 if Is_Special_Character
(C
) then
573 -- The only other case in which we allow a character after
574 -- the -- other than a space is when we have a row of minus
575 -- signs (case of header lines for a box comment for example).
578 while Source
(S
) >= ' ' loop
579 if Source
(S
) /= '-' then
581 or else Style_Check_Comments_Spacing
= 1
583 Error_Space_Required
(Scan_Ptr
+ 2);
586 ("(style) two spaces required", Scan_Ptr
+ 2);
595 -- If we are followed by a blank, then the comment is OK if the
596 -- character following this blank is another blank or a format
597 -- effector, or if the required comment spacing is 1.
599 elsif Source
(Scan_Ptr
+ 3) <= ' '
600 or else Style_Check_Comments_Spacing
= 1
604 -- Here is the case where we only have one blank after the two minus
605 -- signs, with Style_Check_Comments_Spacing set to 2, which is an
606 -- error unless the line ends with two minus signs, the case of a
609 elsif not Is_Box_Comment
then
610 Error_Space_Required
(Scan_Ptr
+ 3);
615 --------------------------------------
616 -- Check_Defining_Identifier_Casing --
617 --------------------------------------
619 procedure Check_Defining_Identifier_Casing
is
621 if Style_Check_Mixed_Case_Decls
then
622 case Determine_Token_Casing
is
626 Error_Msg_SC
-- CODEFIX
627 ("(style) bad capitalization, mixed case required");
629 -- The Unknown case is something like A_B_C, which is both all
630 -- caps and mixed case.
638 end Check_Defining_Identifier_Casing
;
644 -- In check token mode (-gnatyt), ".." must be surrounded by spaces
646 procedure Check_Dot_Dot
is
648 if Style_Check_Tokens
then
649 Require_Preceding_Space
;
650 Require_Following_Space
;
658 -- In check blanks at end mode, check no blank lines precede the EOF
660 procedure Check_EOF
is
662 if Style_Check_Blank_Lines
then
664 -- We expect one blank line, from the EOF, but no more than one
666 if Blank_Lines
= 2 then
668 ("(style) blank line not allowed at end of file",
669 Blank_Line_Location
);
671 elsif Blank_Lines
>= 3 then
673 ("(style) blank lines not allowed at end of file",
674 Blank_Line_Location
);
679 -----------------------------------
680 -- Check_Exponentiation_Operator --
681 -----------------------------------
683 -- No spaces are required for the ** operator in GNAT style check mode
685 procedure Check_Exponentiation_Operator
is
688 end Check_Exponentiation_Operator
;
694 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
696 procedure Check_HT
is
698 if Style_Check_Horizontal_Tabs
then
699 Error_Msg_S
-- CODEFIX
700 ("(style) horizontal tab not allowed");
704 -----------------------
705 -- Check_Indentation --
706 -----------------------
708 -- In check indentation mode (-gnaty? for ? a digit), a new statement or
709 -- declaration is required to start in a column that is a multiple of the
710 -- indentation amount.
712 procedure Check_Indentation
is
714 if Style_Check_Indentation
/= 0 then
715 if Token_Ptr
= First_Non_Blank_Location
716 and then Start_Column
rem Style_Check_Indentation
/= 0
718 Error_Msg_SC
-- CODEFIX
719 ("(style) bad indentation");
722 end Check_Indentation
;
724 ----------------------
725 -- Check_Left_Paren --
726 ----------------------
728 -- In check token mode (-gnatyt), left paren must not be preceded by an
729 -- identifier character or digit (a separating space is required) and may
730 -- never be followed by a space.
732 procedure Check_Left_Paren
is
734 if Style_Check_Tokens
then
735 if Token_Ptr
> Source_First
(Current_Source_File
)
736 and then Identifier_Char
(Source
(Token_Ptr
- 1))
738 Error_Space_Required
(Token_Ptr
);
741 Check_No_Space_After
;
743 end Check_Left_Paren
;
745 ---------------------------
746 -- Check_Line_Max_Length --
747 ---------------------------
749 -- In check max line length mode (-gnatym), the line length must
750 -- not exceed the permitted maximum value.
752 procedure Check_Line_Max_Length
(Len
: Nat
) is
754 if Style_Check_Max_Line_Length
then
755 if Len
> Style_Max_Line_Length
then
757 ("(style) this line is too long",
758 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
761 end Check_Line_Max_Length
;
763 ---------------------------
764 -- Check_Line_Terminator --
765 ---------------------------
767 -- In check blanks at end mode (-gnatyb), lines may not end with a
770 -- In check form feeds mode (-gnatyf), the line terminator may not
771 -- be either of the characters FF or VT.
773 -- In check DOS line terminators node (-gnatyd), the line terminator
774 -- must be a single LF, without a following CR.
776 procedure Check_Line_Terminator
(Len
: Nat
) is
780 -- Length of line (adjusted down for blanks at end of line)
783 -- Reset count of blank lines if first line
785 if Get_Logical_Line_Number
(Scan_Ptr
) = 1 then
789 -- Check FF/VT terminators
791 if Style_Check_Form_Feeds
then
792 if Source
(Scan_Ptr
) = ASCII
.FF
then
793 Error_Msg_S
-- CODEFIX
794 ("(style) form feed not allowed");
795 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
796 Error_Msg_S
-- CODEFIX
797 ("(style) vertical tab not allowed");
801 -- Check DOS line terminator
803 if Style_Check_DOS_Line_Terminator
then
805 -- Ignore EOF, since we only get called with an EOF if it is the last
806 -- character in the buffer (and was therefore not in the source
807 -- file), since the terminating EOF is added to stop the scan.
809 if Source
(Scan_Ptr
) = EOF
then
812 -- Bad terminator if we don't have an LF
814 elsif Source
(Scan_Ptr
) /= LF
then
815 Error_Msg_S
("(style) incorrect line terminator");
819 -- Remove trailing spaces
822 while L
> 0 and then Is_White_Space
(Source
(S
- 1)) loop
827 -- Issue message for blanks at end of line if option enabled
829 if Style_Check_Blanks_At_End
and then L
< Len
then
831 ("(style) trailing spaces not permitted", S
);
834 -- Deal with empty (blank) line
838 -- Increment blank line count
840 Blank_Lines
:= Blank_Lines
+ 1;
842 -- If first blank line, record location for later error message
844 if Blank_Lines
= 1 then
845 Blank_Line_Location
:= Scan_Ptr
;
848 -- Non-blank line, check for previous multiple blank lines
851 if Style_Check_Blank_Lines
and then Blank_Lines
> 1 then
853 ("(style) multiple blank lines", Blank_Line_Location
);
856 -- And reset blank line count
860 end Check_Line_Terminator
;
866 -- In check tokens mode, only one space between NOT and IN
868 procedure Check_Not_In
is
870 if Style_Check_Tokens
then
871 if Source
(Token_Ptr
- 1) /= ' '
872 or else Token_Ptr
- Prev_Token_Ptr
/= 4
875 ("(style) single space must separate NOT and IN", Token_Ptr
- 1);
880 --------------------------
881 -- Check_No_Space_After --
882 --------------------------
884 procedure Check_No_Space_After
is
888 if Is_White_Space
(Source
(Scan_Ptr
)) then
890 -- Allow one or more spaces if followed by comment
894 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
897 elsif Is_White_Space
(Source
(S
)) then
905 Error_Space_Not_Allowed
(Scan_Ptr
);
907 end Check_No_Space_After
;
909 ---------------------------
910 -- Check_No_Space_Before --
911 ---------------------------
913 procedure Check_No_Space_Before
is
915 if Token_Ptr
> First_Non_Blank_Location
916 and then Source
(Token_Ptr
- 1) <= ' '
918 Error_Space_Not_Allowed
(Token_Ptr
- 1);
920 end Check_No_Space_Before
;
922 -----------------------
923 -- Check_Pragma_Name --
924 -----------------------
926 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
927 -- case, i.e. start with an upper case letter, and otherwise lower case,
928 -- except after an underline character.
930 procedure Check_Pragma_Name
is
932 if Style_Check_Pragma_Casing
then
933 if Determine_Token_Casing
/= Mixed_Case
then
934 Error_Msg_SC
-- CODEFIX
935 ("(style) bad capitalization, mixed case required");
938 end Check_Pragma_Name
;
940 -----------------------
941 -- Check_Right_Paren --
942 -----------------------
944 -- In check token mode (-gnatyt), right paren must not be immediately
945 -- followed by an identifier character, and must never be preceded by
946 -- a space unless it is the initial non-blank character on the line.
948 procedure Check_Right_Paren
is
950 if Style_Check_Tokens
then
951 if Identifier_Char
(Source
(Token_Ptr
+ 1)) then
952 Error_Space_Required
(Token_Ptr
+ 1);
955 Check_No_Space_Before
;
957 end Check_Right_Paren
;
959 ---------------------
960 -- Check_Semicolon --
961 ---------------------
963 -- In check token mode (-gnatyt), semicolon does not permit a preceding
964 -- space and a following space is required.
966 procedure Check_Semicolon
is
968 if Style_Check_Tokens
then
969 Check_No_Space_Before
;
971 if Source
(Scan_Ptr
) > ' ' then
972 Error_Space_Required
(Scan_Ptr
);
977 -------------------------------
978 -- Check_Separate_Stmt_Lines --
979 -------------------------------
981 procedure Check_Separate_Stmt_Lines
is
983 if Style_Check_Separate_Stmt_Lines
then
984 Check_Separate_Stmt_Lines_Cont
;
986 end Check_Separate_Stmt_Lines
;
988 ------------------------------------
989 -- Check_Separate_Stmt_Lines_Cont --
990 ------------------------------------
992 procedure Check_Separate_Stmt_Lines_Cont
is
996 -- Skip past white space
999 while Is_White_Space
(Source
(S
)) loop
1003 -- Line terminator is OK
1005 if Source
(S
) in Line_Terminator
then
1010 elsif Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
1013 -- ABORT keyword is OK after THEN (THEN ABORT case)
1015 elsif Token
= Tok_Then
1016 and then (Source
(S
+ 0) = 'a' or else Source
(S
+ 0) = 'A')
1017 and then (Source
(S
+ 1) = 'b' or else Source
(S
+ 1) = 'B')
1018 and then (Source
(S
+ 2) = 'o' or else Source
(S
+ 2) = 'O')
1019 and then (Source
(S
+ 3) = 'r' or else Source
(S
+ 3) = 'R')
1020 and then (Source
(S
+ 4) = 't' or else Source
(S
+ 4) = 'T')
1021 and then (Source
(S
+ 5) in Line_Terminator
1022 or else Is_White_Space
(Source
(S
+ 5)))
1026 -- PRAGMA keyword is OK after ELSE
1028 elsif Token
= Tok_Else
1029 and then (Source
(S
+ 0) = 'p' or else Source
(S
+ 0) = 'P')
1030 and then (Source
(S
+ 1) = 'r' or else Source
(S
+ 1) = 'R')
1031 and then (Source
(S
+ 2) = 'a' or else Source
(S
+ 2) = 'A')
1032 and then (Source
(S
+ 3) = 'g' or else Source
(S
+ 3) = 'G')
1033 and then (Source
(S
+ 4) = 'm' or else Source
(S
+ 4) = 'M')
1034 and then (Source
(S
+ 5) = 'a' or else Source
(S
+ 5) = 'A')
1035 and then (Source
(S
+ 6) in Line_Terminator
1036 or else Is_White_Space
(Source
(S
+ 6)))
1040 -- Otherwise we have the style violation we are looking for
1043 if Token
= Tok_Then
then
1044 Error_Msg
-- CODEFIX
1045 ("(style) no statements may follow THEN on same line", S
);
1048 ("(style) no statements may follow ELSE on same line", S
);
1051 end Check_Separate_Stmt_Lines_Cont
;
1057 -- In check if then layout mode (-gnatyi), we expect a THEN keyword to
1058 -- appear either on the same line as the IF, or on a separate line if
1059 -- the IF statement extends for more than one line.
1061 procedure Check_Then
(If_Loc
: Source_Ptr
) is
1063 if Style_Check_If_Then_Layout
then
1065 If_Line
: constant Physical_Line_Number
:=
1066 Get_Physical_Line_Number
(If_Loc
);
1067 Then_Line
: constant Physical_Line_Number
:=
1068 Get_Physical_Line_Number
(Token_Ptr
);
1070 if If_Line
= Then_Line
then
1072 elsif Token_Ptr
/= First_Non_Blank_Location
then
1073 Error_Msg_SC
("(style) misplaced THEN");
1079 -------------------------------
1080 -- Check_Unary_Plus_Or_Minus --
1081 -------------------------------
1083 -- In check token mode (-gnatyt), unary plus or minus must not be
1084 -- followed by a space.
1086 -- Annoying exception: if we have the sequence =>+ within a Depends or
1087 -- Refined_Depends pragma or aspect, then we insist on a space rather
1088 -- than forbidding it.
1090 procedure Check_Unary_Plus_Or_Minus
(Inside_Depends
: Boolean := False) is
1092 if Style_Check_Tokens
then
1093 if Inside_Depends
then
1094 Require_Following_Space
;
1096 Check_No_Space_After
;
1099 end Check_Unary_Plus_Or_Minus
;
1101 ------------------------
1102 -- Check_Vertical_Bar --
1103 ------------------------
1105 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
1107 procedure Check_Vertical_Bar
is
1109 if Style_Check_Tokens
then
1110 Require_Preceding_Space
;
1111 Require_Following_Space
;
1113 end Check_Vertical_Bar
;
1115 -----------------------
1116 -- Check_Xtra_Parens --
1117 -----------------------
1119 procedure Check_Xtra_Parens
(Loc
: Source_Ptr
) is
1121 if Style_Check_Xtra_Parens
then
1122 Error_Msg
-- CODEFIX
1123 ("(style) redundant parentheses", Loc
);
1125 end Check_Xtra_Parens
;
1127 ----------------------------
1128 -- Determine_Token_Casing --
1129 ----------------------------
1131 function Determine_Token_Casing
return Casing_Type
is
1133 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
1134 end Determine_Token_Casing
;
1136 -----------------------------
1137 -- Error_Space_Not_Allowed --
1138 -----------------------------
1140 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
1142 Error_Msg
-- CODEFIX
1143 ("(style) space not allowed", S
);
1144 end Error_Space_Not_Allowed
;
1146 --------------------------
1147 -- Error_Space_Required --
1148 --------------------------
1150 procedure Error_Space_Required
(S
: Source_Ptr
) is
1152 Error_Msg
-- CODEFIX
1153 ("(style) space required", S
);
1154 end Error_Space_Required
;
1156 --------------------
1157 -- Is_White_Space --
1158 --------------------
1160 function Is_White_Space
(C
: Character) return Boolean is
1162 return C
= ' ' or else C
= HT
;
1169 function Mode_In_Check
return Boolean is
1171 return Style_Check
and Style_Check_Mode_In
;
1178 -- In check end/exit labels mode (-gnatye), always require the name of
1179 -- a subprogram or package to be present on the END, so this is an error.
1181 procedure No_End_Name
(Name
: Node_Id
) is
1183 if Style_Check_End_Labels
then
1184 Error_Msg_Node_1
:= Name
;
1185 Error_Msg_SP
-- CODEFIX
1186 ("(style) `END &` required");
1194 -- In check end/exit labels mode (-gnatye), always require the name of
1195 -- the loop to be present on the EXIT when exiting a named loop.
1197 procedure No_Exit_Name
(Name
: Node_Id
) is
1199 if Style_Check_End_Labels
then
1200 Error_Msg_Node_1
:= Name
;
1201 Error_Msg_SP
-- CODEFIX
1202 ("(style) `EXIT &` required");
1206 ----------------------------
1207 -- Non_Lower_Case_Keyword --
1208 ----------------------------
1210 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1211 -- in all lower case (excluding keywords range, access, delta and digits
1212 -- used as attribute designators).
1214 procedure Non_Lower_Case_Keyword
is
1216 if Style_Check_Keyword_Casing
then
1217 Error_Msg_SC
-- CODEFIX
1218 ("(style) reserved words must be all lower case");
1220 end Non_Lower_Case_Keyword
;
1222 -----------------------------
1223 -- Require_Following_Space --
1224 -----------------------------
1226 procedure Require_Following_Space
is
1228 if Source
(Scan_Ptr
) > ' ' then
1229 Error_Space_Required
(Scan_Ptr
);
1231 end Require_Following_Space
;
1233 -----------------------------
1234 -- Require_Preceding_Space --
1235 -----------------------------
1237 procedure Require_Preceding_Space
is
1239 if Token_Ptr
> Source_First
(Current_Source_File
)
1240 and then Source
(Token_Ptr
- 1) > ' '
1242 Error_Space_Required
(Token_Ptr
);
1244 end Require_Preceding_Space
;