1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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_Square_Bracket --
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.
731 -- Same applies for the left square bracket starting from Ada version 2022.
733 procedure Check_Left_Paren_Square_Bracket
is
735 if Style_Check_Tokens
then
736 if Token_Ptr
> Source_First
(Current_Source_File
)
737 and then Identifier_Char
(Source
(Token_Ptr
- 1))
739 Error_Space_Required
(Token_Ptr
);
742 Check_No_Space_After
;
744 end Check_Left_Paren_Square_Bracket
;
746 ---------------------------
747 -- Check_Line_Max_Length --
748 ---------------------------
750 -- In check max line length mode (-gnatym), the line length must
751 -- not exceed the permitted maximum value.
753 procedure Check_Line_Max_Length
(Len
: Nat
) is
755 if Style_Check_Max_Line_Length
then
756 if Len
> Style_Max_Line_Length
then
758 ("(style) this line is too long",
759 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
762 end Check_Line_Max_Length
;
764 ---------------------------
765 -- Check_Line_Terminator --
766 ---------------------------
768 -- In check blanks at end mode (-gnatyb), lines may not end with a
771 -- In check form feeds mode (-gnatyf), the line terminator may not
772 -- be either of the characters FF or VT.
774 -- In check DOS line terminators node (-gnatyd), the line terminator
775 -- must be a single LF, without a following CR.
777 procedure Check_Line_Terminator
(Len
: Nat
) is
781 -- Length of line (adjusted down for blanks at end of line)
784 -- Reset count of blank lines if first line
786 if Get_Logical_Line_Number
(Scan_Ptr
) = 1 then
790 -- Check FF/VT terminators
792 if Style_Check_Form_Feeds
then
793 if Source
(Scan_Ptr
) = ASCII
.FF
then
794 Error_Msg_S
-- CODEFIX
795 ("(style) form feed not allowed");
796 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
797 Error_Msg_S
-- CODEFIX
798 ("(style) vertical tab not allowed");
802 -- Check DOS line terminator
804 if Style_Check_DOS_Line_Terminator
then
806 -- Ignore EOF, since we only get called with an EOF if it is the last
807 -- character in the buffer (and was therefore not in the source
808 -- file), since the terminating EOF is added to stop the scan.
810 if Source
(Scan_Ptr
) = EOF
then
813 -- Bad terminator if we don't have an LF
815 elsif Source
(Scan_Ptr
) /= LF
then
816 Error_Msg_S
("(style) incorrect line terminator");
820 -- Remove trailing spaces
823 while L
> 0 and then Is_White_Space
(Source
(S
- 1)) loop
828 -- Issue message for blanks at end of line if option enabled
830 if Style_Check_Blanks_At_End
and then L
< Len
then
832 ("(style) trailing spaces not permitted", S
);
835 -- Deal with empty (blank) line
839 -- Increment blank line count
841 Blank_Lines
:= Blank_Lines
+ 1;
843 -- If first blank line, record location for later error message
845 if Blank_Lines
= 1 then
846 Blank_Line_Location
:= Scan_Ptr
;
849 -- Non-blank line, check for previous multiple blank lines
852 if Style_Check_Blank_Lines
and then Blank_Lines
> 1 then
854 ("(style) multiple blank lines", Blank_Line_Location
);
857 -- And reset blank line count
861 end Check_Line_Terminator
;
867 -- In check tokens mode, only one space between NOT and IN
869 procedure Check_Not_In
is
871 if Style_Check_Tokens
then
872 if Source
(Token_Ptr
- 1) /= ' '
873 or else Token_Ptr
- Prev_Token_Ptr
/= 4
876 ("(style) single space must separate NOT and IN", Token_Ptr
- 1);
881 --------------------------
882 -- Check_No_Space_After --
883 --------------------------
885 procedure Check_No_Space_After
is
889 if Is_White_Space
(Source
(Scan_Ptr
)) then
891 -- Allow one or more spaces if followed by comment
895 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
898 elsif Is_White_Space
(Source
(S
)) then
906 Error_Space_Not_Allowed
(Scan_Ptr
);
908 end Check_No_Space_After
;
910 ---------------------------
911 -- Check_No_Space_Before --
912 ---------------------------
914 procedure Check_No_Space_Before
is
916 if Token_Ptr
> First_Non_Blank_Location
917 and then Source
(Token_Ptr
- 1) <= ' '
919 Error_Space_Not_Allowed
(Token_Ptr
- 1);
921 end Check_No_Space_Before
;
923 -----------------------
924 -- Check_Pragma_Name --
925 -----------------------
927 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
928 -- case, i.e. start with an upper case letter, and otherwise lower case,
929 -- except after an underline character.
931 procedure Check_Pragma_Name
is
933 if Style_Check_Pragma_Casing
then
934 if Determine_Token_Casing
/= Mixed_Case
then
935 Error_Msg_SC
-- CODEFIX
936 ("(style) bad capitalization, mixed case required");
939 end Check_Pragma_Name
;
941 -----------------------
942 -- Check_Right_Paren --
943 -----------------------
945 -- In check token mode (-gnatyt), right paren must not be immediately
946 -- followed by an identifier character, and must never be preceded by
947 -- a space unless it is the initial non-blank character on the line.
949 procedure Check_Right_Paren
is
951 if Style_Check_Tokens
then
952 if Identifier_Char
(Source
(Token_Ptr
+ 1)) then
953 Error_Space_Required
(Token_Ptr
+ 1);
956 Check_No_Space_Before
;
958 end Check_Right_Paren
;
960 ---------------------
961 -- Check_Semicolon --
962 ---------------------
964 -- In check token mode (-gnatyt), semicolon does not permit a preceding
965 -- space and a following space is required.
967 procedure Check_Semicolon
is
969 if Style_Check_Tokens
then
970 Check_No_Space_Before
;
972 if Source
(Scan_Ptr
) > ' ' then
973 Error_Space_Required
(Scan_Ptr
);
978 -------------------------------
979 -- Check_Separate_Stmt_Lines --
980 -------------------------------
982 procedure Check_Separate_Stmt_Lines
is
984 if Style_Check_Separate_Stmt_Lines
then
985 Check_Separate_Stmt_Lines_Cont
;
987 end Check_Separate_Stmt_Lines
;
989 ------------------------------------
990 -- Check_Separate_Stmt_Lines_Cont --
991 ------------------------------------
993 procedure Check_Separate_Stmt_Lines_Cont
is
997 -- Skip past white space
1000 while Is_White_Space
(Source
(S
)) loop
1004 -- Line terminator is OK
1006 if Source
(S
) in Line_Terminator
then
1011 elsif Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
1014 -- ABORT keyword is OK after THEN (THEN ABORT case)
1016 elsif Token
= Tok_Then
1017 and then (Source
(S
+ 0) = 'a' or else Source
(S
+ 0) = 'A')
1018 and then (Source
(S
+ 1) = 'b' or else Source
(S
+ 1) = 'B')
1019 and then (Source
(S
+ 2) = 'o' or else Source
(S
+ 2) = 'O')
1020 and then (Source
(S
+ 3) = 'r' or else Source
(S
+ 3) = 'R')
1021 and then (Source
(S
+ 4) = 't' or else Source
(S
+ 4) = 'T')
1022 and then (Source
(S
+ 5) in Line_Terminator
1023 or else Is_White_Space
(Source
(S
+ 5)))
1027 -- PRAGMA keyword is OK after ELSE
1029 elsif Token
= Tok_Else
1030 and then (Source
(S
+ 0) = 'p' or else Source
(S
+ 0) = 'P')
1031 and then (Source
(S
+ 1) = 'r' or else Source
(S
+ 1) = 'R')
1032 and then (Source
(S
+ 2) = 'a' or else Source
(S
+ 2) = 'A')
1033 and then (Source
(S
+ 3) = 'g' or else Source
(S
+ 3) = 'G')
1034 and then (Source
(S
+ 4) = 'm' or else Source
(S
+ 4) = 'M')
1035 and then (Source
(S
+ 5) = 'a' or else Source
(S
+ 5) = 'A')
1036 and then (Source
(S
+ 6) in Line_Terminator
1037 or else Is_White_Space
(Source
(S
+ 6)))
1041 -- Otherwise we have the style violation we are looking for
1044 if Token
= Tok_Then
then
1045 Error_Msg
-- CODEFIX
1046 ("(style) no statements may follow THEN on same line", S
);
1049 ("(style) no statements may follow ELSE on same line", S
);
1052 end Check_Separate_Stmt_Lines_Cont
;
1058 -- In check if then layout mode (-gnatyi), we expect a THEN keyword to
1059 -- appear either on the same line as the IF, or on a separate line if
1060 -- the IF statement extends for more than one line.
1062 procedure Check_Then
(If_Loc
: Source_Ptr
) is
1064 if Style_Check_If_Then_Layout
then
1066 If_Line
: constant Physical_Line_Number
:=
1067 Get_Physical_Line_Number
(If_Loc
);
1068 Then_Line
: constant Physical_Line_Number
:=
1069 Get_Physical_Line_Number
(Token_Ptr
);
1071 if If_Line
= Then_Line
then
1073 elsif Token_Ptr
/= First_Non_Blank_Location
then
1074 Error_Msg_SC
("(style) misplaced THEN");
1080 -------------------------------
1081 -- Check_Unary_Plus_Or_Minus --
1082 -------------------------------
1084 -- In check token mode (-gnatyt), unary plus or minus must not be
1085 -- followed by a space.
1087 -- Annoying exception: if we have the sequence =>+ within a Depends or
1088 -- Refined_Depends pragma or aspect, then we insist on a space rather
1089 -- than forbidding it.
1091 procedure Check_Unary_Plus_Or_Minus
(Inside_Depends
: Boolean := False) is
1093 if Style_Check_Tokens
then
1094 if Inside_Depends
then
1095 Require_Following_Space
;
1097 Check_No_Space_After
;
1100 end Check_Unary_Plus_Or_Minus
;
1102 ------------------------
1103 -- Check_Vertical_Bar --
1104 ------------------------
1106 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
1108 procedure Check_Vertical_Bar
is
1110 if Style_Check_Tokens
then
1111 Require_Preceding_Space
;
1112 Require_Following_Space
;
1114 end Check_Vertical_Bar
;
1116 -----------------------
1117 -- Check_Xtra_Parens --
1118 -----------------------
1120 procedure Check_Xtra_Parens
(Loc
: Source_Ptr
) is
1122 if Style_Check_Xtra_Parens
then
1123 Error_Msg
-- CODEFIX
1124 ("(style) redundant parentheses", Loc
);
1126 end Check_Xtra_Parens
;
1128 ----------------------------
1129 -- Determine_Token_Casing --
1130 ----------------------------
1132 function Determine_Token_Casing
return Casing_Type
is
1134 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
1135 end Determine_Token_Casing
;
1137 -----------------------------
1138 -- Error_Space_Not_Allowed --
1139 -----------------------------
1141 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
1143 Error_Msg
-- CODEFIX
1144 ("(style) space not allowed", S
);
1145 end Error_Space_Not_Allowed
;
1147 --------------------------
1148 -- Error_Space_Required --
1149 --------------------------
1151 procedure Error_Space_Required
(S
: Source_Ptr
) is
1153 Error_Msg
-- CODEFIX
1154 ("(style) space required", S
);
1155 end Error_Space_Required
;
1157 --------------------
1158 -- Is_White_Space --
1159 --------------------
1161 function Is_White_Space
(C
: Character) return Boolean is
1163 return C
= ' ' or else C
= HT
;
1170 function Mode_In_Check
return Boolean is
1172 return Style_Check
and Style_Check_Mode_In
;
1179 -- In check end/exit labels mode (-gnatye), always require the name of
1180 -- a subprogram or package to be present on the END, so this is an error.
1182 procedure No_End_Name
(Name
: Node_Id
) is
1184 if Style_Check_End_Labels
then
1185 Error_Msg_Node_1
:= Name
;
1186 Error_Msg_SP
-- CODEFIX
1187 ("(style) `END &` required");
1195 -- In check end/exit labels mode (-gnatye), always require the name of
1196 -- the loop to be present on the EXIT when exiting a named loop.
1198 procedure No_Exit_Name
(Name
: Node_Id
) is
1200 if Style_Check_End_Labels
then
1201 Error_Msg_Node_1
:= Name
;
1202 Error_Msg_SP
-- CODEFIX
1203 ("(style) `EXIT &` required");
1207 ----------------------------
1208 -- Non_Lower_Case_Keyword --
1209 ----------------------------
1211 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1212 -- in all lower case (excluding keywords range, access, delta and digits
1213 -- used as attribute designators).
1215 procedure Non_Lower_Case_Keyword
is
1217 if Style_Check_Keyword_Casing
then
1218 Error_Msg_SC
-- CODEFIX
1219 ("(style) reserved words must be all lower case");
1221 end Non_Lower_Case_Keyword
;
1223 -----------------------------
1224 -- Require_Following_Space --
1225 -----------------------------
1227 procedure Require_Following_Space
is
1229 if Source
(Scan_Ptr
) > ' ' then
1230 Error_Space_Required
(Scan_Ptr
);
1232 end Require_Following_Space
;
1234 -----------------------------
1235 -- Require_Preceding_Space --
1236 -----------------------------
1238 procedure Require_Preceding_Space
is
1240 if Token_Ptr
> Source_First
(Current_Source_File
)
1241 and then Source
(Token_Ptr
- 1) > ' '
1243 Error_Space_Required
(Token_Ptr
);
1245 end Require_Preceding_Space
;