1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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
;
38 with Scans
; use Scans
;
39 with Sinfo
; use Sinfo
;
40 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
41 with Sinput
; use Sinput
;
42 with Stylesw
; use Stylesw
;
44 package body Styleg
is
48 Blank_Lines
: Nat
:= 0;
49 -- Counts number of empty lines seen. Reset to zero if a non-empty line
50 -- is encountered. Used to check for trailing blank lines in Check_EOF,
51 -- and for multiple blank lines.
53 Blank_Line_Location
: Source_Ptr
;
54 -- Remembers location of first blank line in a series. Used to issue an
55 -- appropriate diagnostic if subsequent blank lines or the end of file
58 -----------------------
59 -- Local Subprograms --
60 -----------------------
62 procedure Check_No_Space_After
;
63 -- Checks that there is a non-white space character after the current
64 -- token, or white space followed by a comment, or the end of line.
65 -- Issue error message if not.
67 procedure Check_No_Space_Before
;
68 -- Check that token is first token on line, or else is not preceded
69 -- by white space. Signal error of space not allowed if not.
71 procedure Check_Separate_Stmt_Lines_Cont
;
72 -- Non-inlined continuation of Check_Separate_Stmt_Lines
74 function Determine_Token_Casing
return Casing_Type
;
75 -- Determine casing of current token
77 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
);
78 -- Posts an error message indicating that a space is not allowed
79 -- at the given source location.
81 procedure Error_Space_Required
(S
: Source_Ptr
);
82 -- Posts an error message indicating that a space is required at
83 -- the given source location.
85 function Is_White_Space
(C
: Character) return Boolean;
86 pragma Inline
(Is_White_Space
);
87 -- Returns True for space or HT, False otherwise
89 procedure Require_Following_Space
;
90 pragma Inline
(Require_Following_Space
);
91 -- Require token to be followed by white space. Used only if in GNAT
92 -- style checking mode.
94 procedure Require_Preceding_Space
;
95 pragma Inline
(Require_Preceding_Space
);
96 -- Require token to be preceded by white space. Used only if in GNAT
97 -- style checking mode.
99 ----------------------
100 -- Check_Abs_Or_Not --
101 ----------------------
103 -- In check token mode (-gnatyt), ABS/NOT must be followed by a space or
106 procedure Check_Abs_Not
is
108 if Style_Check_Tokens
then
109 if Source
(Scan_Ptr
) not in ' ' | ASCII
.CR | ASCII
.LF
then
110 Error_Space_Required
(Scan_Ptr
);
115 ----------------------
116 -- Check_Apostrophe --
117 ----------------------
119 -- Do not allow space after apostrophe
121 procedure Check_Apostrophe
is
123 if Style_Check_Tokens
then
124 Check_No_Space_After
;
126 end Check_Apostrophe
;
132 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces,
133 -- except that within the argument of a Depends or Refined_Depends aspect
134 -- or pragma the required format is "=>+ " rather than "=> +").
136 procedure Check_Arrow
(Inside_Depends
: Boolean := False) is
138 if Style_Check_Tokens
then
139 Require_Preceding_Space
;
141 -- Special handling for Depends and Refined_Depends
143 if Inside_Depends
then
144 if Source
(Scan_Ptr
) = ' '
145 and then Source
(Scan_Ptr
+ 1) = '+'
147 Error_Space_Not_Allowed
(Scan_Ptr
);
149 elsif Source
(Scan_Ptr
) /= ' '
150 and then Source
(Scan_Ptr
) /= '+'
152 Require_Following_Space
;
158 Require_Following_Space
;
163 --------------------------
164 -- Check_Attribute_Name --
165 --------------------------
167 -- In check attribute casing mode (-gnatya), attribute names must be
168 -- mixed case, i.e. start with an upper case letter, and otherwise
169 -- lower case, except after an underline character.
171 procedure Check_Attribute_Name
(Reserved
: Boolean) is
172 pragma Warnings
(Off
, Reserved
);
174 if Style_Check_Attribute_Casing
then
175 if Determine_Token_Casing
/= Mixed_Case
then
176 Error_Msg_SC
-- CODEFIX
177 ("(style) bad capitalization, mixed case required?a?");
180 end Check_Attribute_Name
;
182 ---------------------------
183 -- Check_Binary_Operator --
184 ---------------------------
186 -- In check token mode (-gnatyt), binary operators other than the special
187 -- case of exponentiation require surrounding space characters.
189 procedure Check_Binary_Operator
is
191 if Style_Check_Tokens
then
192 Require_Preceding_Space
;
193 Require_Following_Space
;
195 end Check_Binary_Operator
;
197 ----------------------------
198 -- Check_Boolean_Operator --
199 ----------------------------
201 procedure Check_Boolean_Operator
(Node
: Node_Id
) is
203 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean;
204 -- Returns True for simple variable, or "not X1" or "X1 and X2" or
205 -- "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
207 ------------------------
208 -- OK_Boolean_Operand --
209 ------------------------
211 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean is
213 if Nkind
(N
) in N_Identifier | N_Expanded_Name
then
216 elsif Nkind
(N
) = N_Op_Not
then
217 return OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
219 elsif Nkind
(N
) in N_Op_And | N_Op_Or
then
220 return OK_Boolean_Operand
(Original_Node
(Left_Opnd
(N
)))
222 OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
227 end OK_Boolean_Operand
;
229 -- Start of processing for Check_Boolean_Operator
232 if Style_Check_Boolean_And_Or
233 and then Comes_From_Source
(Node
)
236 Orig
: constant Node_Id
:= Original_Node
(Node
);
239 if Nkind
(Orig
) in N_Op_And | N_Op_Or
then
241 L
: constant Node_Id
:= Original_Node
(Left_Opnd
(Orig
));
242 R
: constant Node_Id
:= Original_Node
(Right_Opnd
(Orig
));
245 -- First OK case, simple boolean constants/identifiers
247 if OK_Boolean_Operand
(L
)
249 OK_Boolean_Operand
(R
)
253 -- Second OK case, modular types
255 elsif Is_Modular_Integer_Type
(Etype
(Node
)) then
258 -- Third OK case, array types
260 elsif Is_Array_Type
(Etype
(Node
)) then
263 -- Otherwise we have an error
265 elsif Nkind
(Orig
) = N_Op_And
then
267 ("(style) `AND THEN` required?B?", Sloc
(Orig
));
270 ("(style) `OR ELSE` required?B?", Sloc
(Orig
));
276 end Check_Boolean_Operator
;
282 -- In check token mode (-gnatyt), box must be preceded by a space or by
283 -- a left parenthesis. Spacing checking on the surrounding tokens takes
284 -- care of the remaining checks.
286 procedure Check_Box
is
288 if Style_Check_Tokens
then
289 if Prev_Token
/= Tok_Left_Paren
then
290 Require_Preceding_Space
;
299 -- In check token mode (-gnatyt), colon must be surrounded by spaces
301 procedure Check_Colon
is
303 if Style_Check_Tokens
then
304 Require_Preceding_Space
;
305 Require_Following_Space
;
309 -----------------------
310 -- Check_Colon_Equal --
311 -----------------------
313 -- In check token mode (-gnatyt), := must be surrounded by spaces
315 procedure Check_Colon_Equal
is
317 if Style_Check_Tokens
then
318 Require_Preceding_Space
;
319 Require_Following_Space
;
321 end Check_Colon_Equal
;
327 -- In check token mode (-gnatyt), comma must be either the first
328 -- token on a line, or be preceded by a non-blank character.
329 -- It must also always be followed by a blank.
331 procedure Check_Comma
is
333 if Style_Check_Tokens
then
334 Check_No_Space_Before
;
336 if Source
(Scan_Ptr
) > ' ' then
337 Error_Space_Required
(Scan_Ptr
);
346 -- In check comment mode (-gnatyc) there are several requirements on the
347 -- format of comments. The following are permissible comment formats:
349 -- 1. Any comment that is not at the start of a line, i.e. where the
350 -- initial minuses are not the first non-blank characters on the
351 -- line must have at least one blank after the second minus or a
352 -- special character as defined in rule 5.
354 -- 2. A row of all minuses of any length is permitted (see procedure
355 -- box above in the source of this routine).
357 -- 3. A comment line starting with two minuses and a space, and ending
358 -- with a space and two minuses. Again see the procedure title box
359 -- immediately above in the source.
361 -- 4. A full line comment where two spaces follow the two minus signs.
362 -- This is the normal comment format in GNAT style, as typified by
363 -- the comments you are reading now.
365 -- 5. A full line comment where the first character after the second
366 -- minus is a special character, i.e. a character in the ASCII
367 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
368 -- comments, such as those generated by gnatprep, or those that
369 -- appear in the SPARK annotation language to be accepted.
371 -- Note: for GNAT internal files (-gnatg switch set on for the
372 -- compilation), the only special sequence recognized and allowed
373 -- is --! as generated by gnatprep.
375 -- 6. In addition, the comment must be properly indented if comment
376 -- indentation checking is active (Style_Check_Indentation non-zero).
377 -- Either the start column must be a multiple of this indentation,
378 -- or the indentation must match that of the next non-blank line,
379 -- or must match the indentation of the immediately preciding line
380 -- if it is non-blank.
382 procedure Check_Comment
is
386 function Is_Box_Comment
return Boolean;
387 -- Returns True if the last two characters on the line are -- which
388 -- characterizes a box comment (as for example follows this spec).
390 function Is_Special_Character
(C
: Character) return Boolean;
391 -- Determines if C is a special character (see rule 5 above)
393 function Same_Column_As_Next_Non_Blank_Line
return Boolean;
394 -- Called for a full line comment. If the indentation of this comment
395 -- matches that of the next non-blank line in the source, then True is
396 -- returned, otherwise False.
398 function Same_Column_As_Previous_Line
return Boolean;
399 -- Called for a full line comment. If the previous line is blank, then
400 -- returns False. Otherwise, if the indentation of this comment matches
401 -- that of the previous line in the source, then True is returned,
408 function Is_Box_Comment
return Boolean is
412 -- Do we need to worry about UTF_32 line terminators here ???
415 while Source
(S
) not in Line_Terminator
loop
419 return Source
(S
- 1) = '-' and then Source
(S
- 2) = '-';
422 --------------------------
423 -- Is_Special_Character --
424 --------------------------
426 function Is_Special_Character
(C
: Character) return Boolean is
432 Character'Pos (C
) in 16#
21#
.. 16#
2F#
434 Character'Pos (C
) in 16#
3A#
.. 16#
3F#
;
436 end Is_Special_Character
;
438 ----------------------------------------
439 -- Same_Column_As_Next_Non_Blank_Line --
440 ----------------------------------------
442 function Same_Column_As_Next_Non_Blank_Line
return Boolean is
446 -- Step to end of line
449 while Source
(P
) not in Line_Terminator
loop
453 -- Step past blanks, and line terminators (UTF_32 case???)
455 while Source
(P
) <= ' ' and then Source
(P
) /= EOF
loop
461 return Get_Column_Number
(Scan_Ptr
) = Get_Column_Number
(P
);
462 end Same_Column_As_Next_Non_Blank_Line
;
464 ----------------------------------
465 -- Same_Column_As_Previous_Line --
466 ----------------------------------
468 function Same_Column_As_Previous_Line
return Boolean is
472 -- Point S to start of this line, and P to start of previous line
474 S
:= Line_Start
(Scan_Ptr
);
478 -- Step P to first non-blank character on line
481 -- If we get back to start of current line, then the previous line
482 -- was blank, and we always return False in that situation.
488 exit when Source
(P
) /= ' ' and then Source
(P
) /= ASCII
.HT
;
494 return Get_Column_Number
(Scan_Ptr
) = Get_Column_Number
(P
);
495 end Same_Column_As_Previous_Line
;
497 -- Start of processing for Check_Comment
500 -- Can never have a non-blank character preceding the first minus.
501 -- The "+ 3" is to leave room for a possible byte order mark (BOM);
502 -- we want to avoid a warning for a comment at the start of the
503 -- file just after the BOM.
505 if Style_Check_Comments
then
506 if Scan_Ptr
> Source_First
(Current_Source_File
) + 3
507 and then Source
(Scan_Ptr
- 1) > ' '
509 Error_Msg_S
-- CODEFIX
510 ("(style) space required?c?");
514 -- For a comment that is not at the start of the line, the only
515 -- requirement is that we cannot have a non-blank character after
516 -- the second minus sign or a special character.
518 if Scan_Ptr
/= First_Non_Blank_Location
then
519 if Style_Check_Comments
then
520 if Source
(Scan_Ptr
+ 2) > ' '
521 and then not Is_Special_Character
(Source
(Scan_Ptr
+ 2))
524 ("(style) space required?c?", Scan_Ptr
+ 2);
530 -- Case of a comment that is at the start of a line
533 -- First check, must be in appropriately indented column
535 if Style_Check_Indentation
/= 0 then
536 if Start_Column
rem Style_Check_Indentation
/= 0 then
537 if not Same_Column_As_Next_Non_Blank_Line
538 and then not Same_Column_As_Previous_Line
540 Error_Msg_S
-- CODEFIX
541 ("(style) bad column?0?");
548 -- If we are not checking comments, nothing more to do
550 if not Style_Check_Comments
then
554 -- Case of not followed by a blank. Usually wrong, but there are
555 -- some exceptions that we permit.
557 if Source
(Scan_Ptr
+ 2) /= ' ' then
558 C
:= Source
(Scan_Ptr
+ 2);
560 -- Case of -- all on its own on a line is OK
566 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
567 -- This is not permitted in internal GNAT implementation units
568 -- except for the case of --! as used by gnatprep output.
570 if Is_Special_Character
(C
) then
574 -- The only other case in which we allow a character after
575 -- the -- other than a space is when we have a row of minus
576 -- signs (case of header lines for a box comment for example).
579 while Source
(S
) >= ' ' loop
580 if Source
(S
) /= '-' then
582 or else Style_Check_Comments_Spacing
= 1
584 Error_Space_Required
(Scan_Ptr
+ 2);
587 ("(style) two spaces required?c?", Scan_Ptr
+ 2);
596 -- If we are followed by a blank, then the comment is OK if the
597 -- character following this blank is another blank or a format
598 -- effector, or if the required comment spacing is 1.
600 elsif Source
(Scan_Ptr
+ 3) <= ' '
601 or else Style_Check_Comments_Spacing
= 1
605 -- Here is the case where we only have one blank after the two minus
606 -- signs, with Style_Check_Comments_Spacing set to 2, which is an
607 -- error unless the line ends with two minus signs, the case of a
610 elsif not Is_Box_Comment
then
611 Error_Space_Required
(Scan_Ptr
+ 3);
616 --------------------------------------
617 -- Check_Defining_Identifier_Casing --
618 --------------------------------------
620 procedure Check_Defining_Identifier_Casing
is
622 if Style_Check_Mixed_Case_Decls
then
623 case Determine_Token_Casing
is
627 Error_Msg_SC
-- CODEFIX
628 ("(style) bad capitalization, mixed case required?D?");
630 -- The Unknown case is something like A_B_C, which is both all
631 -- caps and mixed case.
639 end Check_Defining_Identifier_Casing
;
645 -- In check token mode (-gnatyt), ".." must be surrounded by spaces
647 procedure Check_Dot_Dot
is
649 if Style_Check_Tokens
then
650 Require_Preceding_Space
;
651 Require_Following_Space
;
659 -- In check blanks at end mode, check no blank lines precede the EOF
661 procedure Check_EOF
is
663 if Style_Check_Blank_Lines
then
665 -- We expect one blank line, from the EOF, but no more than one
667 if Blank_Lines
= 2 then
669 ("(style) blank line not allowed at end of file?u?",
670 Blank_Line_Location
);
672 elsif Blank_Lines
>= 3 then
674 ("(style) blank lines not allowed at end of file?u?",
675 Blank_Line_Location
);
680 -----------------------------------
681 -- Check_Exponentiation_Operator --
682 -----------------------------------
684 -- No spaces are required for the ** operator in GNAT style check mode
686 procedure Check_Exponentiation_Operator
is
689 end Check_Exponentiation_Operator
;
695 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
697 procedure Check_HT
is
699 if Style_Check_Horizontal_Tabs
then
700 Error_Msg_S
-- CODEFIX
701 ("(style) horizontal tab not allowed?h?");
705 -----------------------
706 -- Check_Indentation --
707 -----------------------
709 -- In check indentation mode (-gnaty? for ? a digit), a new statement or
710 -- declaration is required to start in a column that is a multiple of the
711 -- indentation amount.
713 procedure Check_Indentation
is
715 if Style_Check_Indentation
/= 0 then
716 if Token_Ptr
= First_Non_Blank_Location
717 and then Start_Column
rem Style_Check_Indentation
/= 0
719 Error_Msg_SC
-- CODEFIX
720 ("(style) bad indentation?0?");
723 end Check_Indentation
;
725 -------------------------------------
726 -- Check_Left_Paren_Square_Bracket --
727 -------------------------------------
729 -- In check token mode (-gnatyt), left paren must not be preceded by an
730 -- identifier character or digit (a separating space is required) and may
731 -- never be followed by a space.
732 -- Same applies for the left square bracket starting from Ada version 2022.
734 procedure Check_Left_Paren_Square_Bracket
is
736 if Style_Check_Tokens
then
737 if Token_Ptr
> Source_First
(Current_Source_File
)
738 and then Identifier_Char
(Source
(Token_Ptr
- 1))
740 Error_Space_Required
(Token_Ptr
);
743 Check_No_Space_After
;
745 end Check_Left_Paren_Square_Bracket
;
747 ---------------------------
748 -- Check_Line_Max_Length --
749 ---------------------------
751 -- In check max line length mode (-gnatym), the line length must
752 -- not exceed the permitted maximum value.
754 procedure Check_Line_Max_Length
(Len
: Nat
) is
756 if Style_Check_Max_Line_Length
then
757 if Len
> Style_Max_Line_Length
then
759 ("(style) this line is too long?M?",
760 Current_Line_Start
+ Source_Ptr
(Style_Max_Line_Length
));
763 end Check_Line_Max_Length
;
765 ---------------------------
766 -- Check_Line_Terminator --
767 ---------------------------
769 -- In check blanks at end mode (-gnatyb), lines may not end with a
772 -- In check form feeds mode (-gnatyf), the line terminator may not
773 -- be either of the characters FF or VT.
775 -- In check DOS line terminators node (-gnatyd), the line terminator
776 -- must be a single LF, without a following CR.
778 procedure Check_Line_Terminator
(Len
: Nat
) is
782 -- Length of line (adjusted down for blanks at end of line)
785 -- Reset count of blank lines if first line
787 if Get_Logical_Line_Number
(Scan_Ptr
) = 1 then
791 -- Check FF/VT terminators
793 if Style_Check_Form_Feeds
then
794 if Source
(Scan_Ptr
) = ASCII
.FF
then
795 Error_Msg_S
-- CODEFIX
796 ("(style) form feed not allowed?f?");
797 elsif Source
(Scan_Ptr
) = ASCII
.VT
then
798 Error_Msg_S
-- CODEFIX
799 ("(style) vertical tab not allowed?f?");
803 -- Check DOS line terminator
805 if Style_Check_DOS_Line_Terminator
then
807 -- Ignore EOF, since we only get called with an EOF if it is the last
808 -- character in the buffer (and was therefore not in the source
809 -- file), since the terminating EOF is added to stop the scan.
811 if Source
(Scan_Ptr
) = EOF
then
814 -- Bad terminator if we don't have an LF
816 elsif Source
(Scan_Ptr
) /= LF
then
817 Error_Msg_S
("(style) incorrect line terminator?d?");
821 -- Remove trailing spaces
824 while L
> 0 and then Is_White_Space
(Source
(S
- 1)) loop
829 -- Issue message for blanks at end of line if option enabled
831 if Style_Check_Blanks_At_End
and then L
< Len
then
833 ("(style) trailing spaces not permitted?b?", S
);
836 -- Deal with empty (blank) line
840 -- Increment blank line count
842 Blank_Lines
:= Blank_Lines
+ 1;
844 -- If first blank line, record location for later error message
846 if Blank_Lines
= 1 then
847 Blank_Line_Location
:= Scan_Ptr
;
850 -- Non-blank line, check for previous multiple blank lines
853 if Style_Check_Blank_Lines
and then Blank_Lines
> 1 then
855 ("(style) multiple blank lines?u?", Blank_Line_Location
);
858 -- And reset blank line count
862 end Check_Line_Terminator
;
868 -- In check tokens mode, only one space between NOT and IN
870 procedure Check_Not_In
is
872 if Style_Check_Tokens
then
873 if Source
(Token_Ptr
- 1) /= ' '
874 or else Token_Ptr
- Prev_Token_Ptr
/= 4
877 ("(style) single space must separate NOT and IN?t?",
883 --------------------------
884 -- Check_No_Space_After --
885 --------------------------
887 procedure Check_No_Space_After
is
891 if Is_White_Space
(Source
(Scan_Ptr
)) then
893 -- Allow one or more spaces if followed by comment
897 if Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
900 elsif Is_White_Space
(Source
(S
)) then
908 Error_Space_Not_Allowed
(Scan_Ptr
);
910 end Check_No_Space_After
;
912 ---------------------------
913 -- Check_No_Space_Before --
914 ---------------------------
916 procedure Check_No_Space_Before
is
918 if Token_Ptr
> First_Non_Blank_Location
919 and then Source
(Token_Ptr
- 1) <= ' '
921 Error_Space_Not_Allowed
(Token_Ptr
- 1);
923 end Check_No_Space_Before
;
925 -----------------------
926 -- Check_Pragma_Name --
927 -----------------------
929 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
930 -- case, i.e. start with an upper case letter, and otherwise lower case,
931 -- except after an underline character.
933 procedure Check_Pragma_Name
is
935 if Style_Check_Pragma_Casing
then
936 if Determine_Token_Casing
/= Mixed_Case
then
937 Error_Msg_SC
-- CODEFIX
938 ("(style) bad capitalization, mixed case required?p?");
941 end Check_Pragma_Name
;
943 -----------------------
944 -- Check_Right_Paren --
945 -----------------------
947 -- In check token mode (-gnatyt), right paren must not be immediately
948 -- followed by an identifier character, and must never be preceded by
949 -- a space unless it is the initial non-blank character on the line.
951 procedure Check_Right_Paren
is
953 if Style_Check_Tokens
then
954 if Identifier_Char
(Source
(Token_Ptr
+ 1)) then
955 Error_Space_Required
(Token_Ptr
+ 1);
958 Check_No_Space_Before
;
960 end Check_Right_Paren
;
962 ---------------------
963 -- Check_Semicolon --
964 ---------------------
966 -- In check token mode (-gnatyt), semicolon does not permit a preceding
967 -- space and a following space is required.
969 procedure Check_Semicolon
is
971 if Style_Check_Tokens
then
972 Check_No_Space_Before
;
974 if Source
(Scan_Ptr
) > ' ' then
975 Error_Space_Required
(Scan_Ptr
);
980 -------------------------------
981 -- Check_Separate_Stmt_Lines --
982 -------------------------------
984 procedure Check_Separate_Stmt_Lines
is
986 if Style_Check_Separate_Stmt_Lines
then
987 Check_Separate_Stmt_Lines_Cont
;
989 end Check_Separate_Stmt_Lines
;
991 ------------------------------------
992 -- Check_Separate_Stmt_Lines_Cont --
993 ------------------------------------
995 procedure Check_Separate_Stmt_Lines_Cont
is
999 -- Skip past white space
1002 while Is_White_Space
(Source
(S
)) loop
1006 -- Line terminator is OK
1008 if Source
(S
) in Line_Terminator
then
1013 elsif Source
(S
) = '-' and then Source
(S
+ 1) = '-' then
1016 -- ABORT keyword is OK after THEN (THEN ABORT case)
1018 elsif Token
= Tok_Then
1019 and then (Source
(S
+ 0) = 'a' or else Source
(S
+ 0) = 'A')
1020 and then (Source
(S
+ 1) = 'b' or else Source
(S
+ 1) = 'B')
1021 and then (Source
(S
+ 2) = 'o' or else Source
(S
+ 2) = 'O')
1022 and then (Source
(S
+ 3) = 'r' or else Source
(S
+ 3) = 'R')
1023 and then (Source
(S
+ 4) = 't' or else Source
(S
+ 4) = 'T')
1024 and then (Source
(S
+ 5) in Line_Terminator
1025 or else Is_White_Space
(Source
(S
+ 5)))
1029 -- PRAGMA keyword is OK after ELSE
1031 elsif Token
= Tok_Else
1032 and then (Source
(S
+ 0) = 'p' or else Source
(S
+ 0) = 'P')
1033 and then (Source
(S
+ 1) = 'r' or else Source
(S
+ 1) = 'R')
1034 and then (Source
(S
+ 2) = 'a' or else Source
(S
+ 2) = 'A')
1035 and then (Source
(S
+ 3) = 'g' or else Source
(S
+ 3) = 'G')
1036 and then (Source
(S
+ 4) = 'm' or else Source
(S
+ 4) = 'M')
1037 and then (Source
(S
+ 5) = 'a' or else Source
(S
+ 5) = 'A')
1038 and then (Source
(S
+ 6) in Line_Terminator
1039 or else Is_White_Space
(Source
(S
+ 6)))
1043 -- Otherwise we have the style violation we are looking for
1046 if Token
= Tok_Then
then
1047 Error_Msg
-- CODEFIX
1048 ("(style) no statements may follow THEN on same line?S?", S
);
1051 ("(style) no statements may follow ELSE on same line?S?", S
);
1054 end Check_Separate_Stmt_Lines_Cont
;
1060 -- In check if then layout mode (-gnatyi), we expect a THEN keyword to
1061 -- appear either on the same line as the IF, or on a separate line if
1062 -- the IF statement extends for more than one line.
1064 procedure Check_Then
(If_Loc
: Source_Ptr
) is
1066 if Style_Check_If_Then_Layout
then
1068 If_Line
: constant Physical_Line_Number
:=
1069 Get_Physical_Line_Number
(If_Loc
);
1070 Then_Line
: constant Physical_Line_Number
:=
1071 Get_Physical_Line_Number
(Token_Ptr
);
1073 if If_Line
= Then_Line
then
1075 elsif Token_Ptr
/= First_Non_Blank_Location
then
1076 Error_Msg_SC
("(style) misplaced THEN?i?");
1082 -------------------------------
1083 -- Check_Unary_Plus_Or_Minus --
1084 -------------------------------
1086 -- In check token mode (-gnatyt), unary plus or minus must not be
1087 -- followed by a space.
1089 -- Annoying exception: if we have the sequence =>+ within a Depends or
1090 -- Refined_Depends pragma or aspect, then we insist on a space rather
1091 -- than forbidding it.
1093 procedure Check_Unary_Plus_Or_Minus
(Inside_Depends
: Boolean := False) is
1095 if Style_Check_Tokens
then
1096 if Inside_Depends
then
1097 Require_Following_Space
;
1099 Check_No_Space_After
;
1102 end Check_Unary_Plus_Or_Minus
;
1104 ------------------------
1105 -- Check_Vertical_Bar --
1106 ------------------------
1108 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
1110 procedure Check_Vertical_Bar
is
1112 if Style_Check_Tokens
then
1113 Require_Preceding_Space
;
1114 Require_Following_Space
;
1116 end Check_Vertical_Bar
;
1118 -----------------------
1119 -- Check_Xtra_Parens --
1120 -----------------------
1122 procedure Check_Xtra_Parens
(N
: Node_Id
) is
1124 if Style_Check_Xtra_Parens
1127 (if Nkind
(N
) in N_Case_Expression
1128 | N_Expression_With_Actions
1130 | N_Quantified_Expression
1131 | N_Raise_Expression
1135 Error_Msg
-- CODEFIX
1136 ("(style) redundant parentheses?x?", Errout
.First_Sloc
(N
));
1138 end Check_Xtra_Parens
;
1140 ----------------------------------
1141 -- Check_Xtra_Parens_Precedence --
1142 ----------------------------------
1144 procedure Check_Xtra_Parens_Precedence
(N
: Node_Id
) is
1146 if Style_Check_Xtra_Parens_Precedence
1149 (if Nkind
(N
) in N_Case_Expression
1150 | N_Expression_With_Actions
1152 | N_Quantified_Expression
1153 | N_Raise_Expression
1157 Error_Msg
-- CODEFIX
1158 ("(style) redundant parentheses?z?", Errout
.First_Sloc
(N
));
1160 end Check_Xtra_Parens_Precedence
;
1162 ----------------------------
1163 -- Determine_Token_Casing --
1164 ----------------------------
1166 function Determine_Token_Casing
return Casing_Type
is
1168 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
1169 end Determine_Token_Casing
;
1171 -----------------------------
1172 -- Error_Space_Not_Allowed --
1173 -----------------------------
1175 procedure Error_Space_Not_Allowed
(S
: Source_Ptr
) is
1177 Error_Msg
-- CODEFIX
1178 ("(style) space not allowed?t?", S
);
1179 end Error_Space_Not_Allowed
;
1181 --------------------------
1182 -- Error_Space_Required --
1183 --------------------------
1185 procedure Error_Space_Required
(S
: Source_Ptr
) is
1187 Error_Msg
-- CODEFIX
1188 ("(style) space required?t?", S
);
1189 end Error_Space_Required
;
1191 --------------------
1192 -- Is_White_Space --
1193 --------------------
1195 function Is_White_Space
(C
: Character) return Boolean is
1197 return C
= ' ' or else C
= HT
;
1204 function Mode_In_Check
return Boolean is
1206 return Style_Check
and Style_Check_Mode_In
;
1213 -- In check end/exit labels mode (-gnatye), always require the name of
1214 -- a subprogram or package to be present on the END, so this is an error.
1216 procedure No_End_Name
(Name
: Node_Id
) is
1218 if Style_Check_End_Labels
then
1219 Error_Msg_Node_1
:= Name
;
1220 Error_Msg_SP
-- CODEFIX
1221 ("(style) `END &` required?e?");
1229 -- In check end/exit labels mode (-gnatye), always require the name of
1230 -- the loop to be present on the EXIT when exiting a named loop.
1232 procedure No_Exit_Name
(Name
: Node_Id
) is
1234 if Style_Check_End_Labels
then
1235 Error_Msg_Node_1
:= Name
;
1236 Error_Msg_SP
-- CODEFIX
1237 ("(style) `EXIT &` required?e?");
1241 ----------------------------
1242 -- Non_Lower_Case_Keyword --
1243 ----------------------------
1245 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1246 -- in all lower case (excluding keywords range, access, delta and digits
1247 -- used as attribute designators).
1249 procedure Non_Lower_Case_Keyword
is
1251 if Style_Check_Keyword_Casing
then
1252 Error_Msg_SC
-- CODEFIX
1253 ("(style) reserved words must be all lower case?k?");
1255 end Non_Lower_Case_Keyword
;
1257 -----------------------------
1258 -- Require_Following_Space --
1259 -----------------------------
1261 procedure Require_Following_Space
is
1263 if Source
(Scan_Ptr
) > ' ' then
1264 Error_Space_Required
(Scan_Ptr
);
1266 end Require_Following_Space
;
1268 -----------------------------
1269 -- Require_Preceding_Space --
1270 -----------------------------
1272 procedure Require_Preceding_Space
is
1274 if Token_Ptr
> Source_First
(Current_Source_File
)
1275 and then Source
(Token_Ptr
- 1) > ' '
1277 Error_Space_Required
(Token_Ptr
);
1279 end Require_Preceding_Space
;