hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / ada / styleg.adb
bloba7524ec05260f762796326dda330d5262bf01bdf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S T Y L E G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
36 with Errout;
37 with Opt; use Opt;
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
46 use ASCII;
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
56 -- is encountered.
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
104 -- a line feed.
106 procedure Check_Abs_Not is
107 begin
108 if Style_Check_Tokens then
109 if Source (Scan_Ptr) not in ' ' | ASCII.CR | ASCII.LF then
110 Error_Space_Required (Scan_Ptr);
111 end if;
112 end if;
113 end Check_Abs_Not;
115 ----------------------
116 -- Check_Apostrophe --
117 ----------------------
119 -- Do not allow space after apostrophe
121 procedure Check_Apostrophe is
122 begin
123 if Style_Check_Tokens then
124 Check_No_Space_After;
125 end if;
126 end Check_Apostrophe;
128 -----------------
129 -- Check_Arrow --
130 -----------------
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
137 begin
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) = '+'
146 then
147 Error_Space_Not_Allowed (Scan_Ptr);
149 elsif Source (Scan_Ptr) /= ' '
150 and then Source (Scan_Ptr) /= '+'
151 then
152 Require_Following_Space;
153 end if;
155 -- Normal case
157 else
158 Require_Following_Space;
159 end if;
160 end if;
161 end Check_Arrow;
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);
173 begin
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?");
178 end if;
179 end if;
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
190 begin
191 if Style_Check_Tokens then
192 Require_Preceding_Space;
193 Require_Following_Space;
194 end if;
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
212 begin
213 if Nkind (N) in N_Identifier | N_Expanded_Name then
214 return True;
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)))
221 and then
222 OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
224 else
225 return False;
226 end if;
227 end OK_Boolean_Operand;
229 -- Start of processing for Check_Boolean_Operator
231 begin
232 if Style_Check_Boolean_And_Or
233 and then Comes_From_Source (Node)
234 then
235 declare
236 Orig : constant Node_Id := Original_Node (Node);
238 begin
239 if Nkind (Orig) in N_Op_And | N_Op_Or then
240 declare
241 L : constant Node_Id := Original_Node (Left_Opnd (Orig));
242 R : constant Node_Id := Original_Node (Right_Opnd (Orig));
244 begin
245 -- First OK case, simple boolean constants/identifiers
247 if OK_Boolean_Operand (L)
248 and then
249 OK_Boolean_Operand (R)
250 then
251 return;
253 -- Second OK case, modular types
255 elsif Is_Modular_Integer_Type (Etype (Node)) then
256 return;
258 -- Third OK case, array types
260 elsif Is_Array_Type (Etype (Node)) then
261 return;
263 -- Otherwise we have an error
265 elsif Nkind (Orig) = N_Op_And then
266 Error_Msg -- CODEFIX
267 ("(style) `AND THEN` required?B?", Sloc (Orig));
268 else
269 Error_Msg -- CODEFIX
270 ("(style) `OR ELSE` required?B?", Sloc (Orig));
271 end if;
272 end;
273 end if;
274 end;
275 end if;
276 end Check_Boolean_Operator;
278 ---------------
279 -- Check_Box --
280 ---------------
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
287 begin
288 if Style_Check_Tokens then
289 if Prev_Token /= Tok_Left_Paren then
290 Require_Preceding_Space;
291 end if;
292 end if;
293 end Check_Box;
295 -----------------
296 -- Check_Colon --
297 -----------------
299 -- In check token mode (-gnatyt), colon must be surrounded by spaces
301 procedure Check_Colon is
302 begin
303 if Style_Check_Tokens then
304 Require_Preceding_Space;
305 Require_Following_Space;
306 end if;
307 end Check_Colon;
309 -----------------------
310 -- Check_Colon_Equal --
311 -----------------------
313 -- In check token mode (-gnatyt), := must be surrounded by spaces
315 procedure Check_Colon_Equal is
316 begin
317 if Style_Check_Tokens then
318 Require_Preceding_Space;
319 Require_Following_Space;
320 end if;
321 end Check_Colon_Equal;
323 -----------------
324 -- Check_Comma --
325 -----------------
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
332 begin
333 if Style_Check_Tokens then
334 Check_No_Space_Before;
336 if Source (Scan_Ptr) > ' ' then
337 Error_Space_Required (Scan_Ptr);
338 end if;
339 end if;
340 end Check_Comma;
342 -------------------
343 -- Check_Comment --
344 -------------------
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
383 S : Source_Ptr;
384 C : Character;
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,
402 -- otherwise False.
404 --------------------
405 -- Is_Box_Comment --
406 --------------------
408 function Is_Box_Comment return Boolean is
409 S : Source_Ptr;
411 begin
412 -- Do we need to worry about UTF_32 line terminators here ???
414 S := Scan_Ptr + 3;
415 while Source (S) not in Line_Terminator loop
416 S := S + 1;
417 end loop;
419 return Source (S - 1) = '-' and then Source (S - 2) = '-';
420 end Is_Box_Comment;
422 --------------------------
423 -- Is_Special_Character --
424 --------------------------
426 function Is_Special_Character (C : Character) return Boolean is
427 begin
428 if GNAT_Mode then
429 return C = '!';
430 else
431 return
432 Character'Pos (C) in 16#21# .. 16#2F#
433 or else
434 Character'Pos (C) in 16#3A# .. 16#3F#;
435 end if;
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
443 P : Source_Ptr;
445 begin
446 -- Step to end of line
448 P := Scan_Ptr + 2;
449 while Source (P) not in Line_Terminator loop
450 P := P + 1;
451 end loop;
453 -- Step past blanks, and line terminators (UTF_32 case???)
455 while Source (P) <= ' ' and then Source (P) /= EOF loop
456 P := P + 1;
457 end loop;
459 -- Compare columns
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
469 S, P : Source_Ptr;
471 begin
472 -- Point S to start of this line, and P to start of previous line
474 S := Line_Start (Scan_Ptr);
475 P := S;
476 Backup_Line (P);
478 -- Step P to first non-blank character on line
480 loop
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.
484 if P = S then
485 return False;
486 end if;
488 exit when Source (P) /= ' ' and then Source (P) /= ASCII.HT;
489 P := P + 1;
490 end loop;
492 -- Compare columns
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
499 begin
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) > ' '
508 then
509 Error_Msg_S -- CODEFIX
510 ("(style) space required?c?");
511 end if;
512 end if;
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))
522 then
523 Error_Msg -- CODEFIX
524 ("(style) space required?c?", Scan_Ptr + 2);
525 end if;
526 end if;
528 return;
530 -- Case of a comment that is at the start of a line
532 else
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
539 then
540 Error_Msg_S -- CODEFIX
541 ("(style) bad column?0?");
542 end if;
544 return;
545 end if;
546 end if;
548 -- If we are not checking comments, nothing more to do
550 if not Style_Check_Comments then
551 return;
552 end if;
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
562 if C < ' ' then
563 return;
564 end if;
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
571 return;
572 end if;
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).
578 S := Scan_Ptr + 2;
579 while Source (S) >= ' ' loop
580 if Source (S) /= '-' then
581 if Is_Box_Comment
582 or else Style_Check_Comments_Spacing = 1
583 then
584 Error_Space_Required (Scan_Ptr + 2);
585 else
586 Error_Msg -- CODEFIX
587 ("(style) two spaces required?c?", Scan_Ptr + 2);
588 end if;
590 return;
591 end if;
593 S := S + 1;
594 end loop;
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
602 then
603 return;
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
608 -- box comment.
610 elsif not Is_Box_Comment then
611 Error_Space_Required (Scan_Ptr + 3);
612 end if;
613 end if;
614 end Check_Comment;
616 --------------------------------------
617 -- Check_Defining_Identifier_Casing --
618 --------------------------------------
620 procedure Check_Defining_Identifier_Casing is
621 begin
622 if Style_Check_Mixed_Case_Decls then
623 case Determine_Token_Casing is
624 when All_Lower_Case
625 | All_Upper_Case
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.
633 when Mixed_Case
634 | Unknown
636 null; -- OK
637 end case;
638 end if;
639 end Check_Defining_Identifier_Casing;
641 -------------------
642 -- Check_Dot_Dot --
643 -------------------
645 -- In check token mode (-gnatyt), ".." must be surrounded by spaces
647 procedure Check_Dot_Dot is
648 begin
649 if Style_Check_Tokens then
650 Require_Preceding_Space;
651 Require_Following_Space;
652 end if;
653 end Check_Dot_Dot;
655 ---------------
656 -- Check_EOF --
657 ---------------
659 -- In check blanks at end mode, check no blank lines precede the EOF
661 procedure Check_EOF is
662 begin
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
668 Error_Msg -- CODEFIX
669 ("(style) blank line not allowed at end of file?u?",
670 Blank_Line_Location);
672 elsif Blank_Lines >= 3 then
673 Error_Msg -- CODEFIX
674 ("(style) blank lines not allowed at end of file?u?",
675 Blank_Line_Location);
676 end if;
677 end if;
678 end Check_EOF;
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
687 begin
688 null;
689 end Check_Exponentiation_Operator;
691 --------------
692 -- Check_HT --
693 --------------
695 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
697 procedure Check_HT is
698 begin
699 if Style_Check_Horizontal_Tabs then
700 Error_Msg_S -- CODEFIX
701 ("(style) horizontal tab not allowed?h?");
702 end if;
703 end Check_HT;
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
714 begin
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
718 then
719 Error_Msg_SC -- CODEFIX
720 ("(style) bad indentation?0?");
721 end if;
722 end if;
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
735 begin
736 if Style_Check_Tokens then
737 if Token_Ptr > Source_First (Current_Source_File)
738 and then Identifier_Char (Source (Token_Ptr - 1))
739 then
740 Error_Space_Required (Token_Ptr);
741 end if;
743 Check_No_Space_After;
744 end if;
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
755 begin
756 if Style_Check_Max_Line_Length then
757 if Len > Style_Max_Line_Length then
758 Error_Msg
759 ("(style) this line is too long?M?",
760 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
761 end if;
762 end if;
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
770 -- trailing space.
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
779 S : Source_Ptr;
781 L : Nat := Len;
782 -- Length of line (adjusted down for blanks at end of line)
784 begin
785 -- Reset count of blank lines if first line
787 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
788 Blank_Lines := 0;
789 end if;
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?");
800 end if;
801 end if;
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
812 null;
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?");
818 end if;
819 end if;
821 -- Remove trailing spaces
823 S := Scan_Ptr;
824 while L > 0 and then Is_White_Space (Source (S - 1)) loop
825 S := S - 1;
826 L := L - 1;
827 end 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
832 Error_Msg -- CODEFIX
833 ("(style) trailing spaces not permitted?b?", S);
834 end if;
836 -- Deal with empty (blank) line
838 if L = 0 then
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;
848 end if;
850 -- Non-blank line, check for previous multiple blank lines
852 else
853 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
854 Error_Msg -- CODEFIX
855 ("(style) multiple blank lines?u?", Blank_Line_Location);
856 end if;
858 -- And reset blank line count
860 Blank_Lines := 0;
861 end if;
862 end Check_Line_Terminator;
864 ------------------
865 -- Check_Not_In --
866 ------------------
868 -- In check tokens mode, only one space between NOT and IN
870 procedure Check_Not_In is
871 begin
872 if Style_Check_Tokens then
873 if Source (Token_Ptr - 1) /= ' '
874 or else Token_Ptr - Prev_Token_Ptr /= 4
875 then -- CODEFIX?
876 Error_Msg
877 ("(style) single space must separate NOT and IN?t?",
878 Token_Ptr - 1);
879 end if;
880 end if;
881 end Check_Not_In;
883 --------------------------
884 -- Check_No_Space_After --
885 --------------------------
887 procedure Check_No_Space_After is
888 S : Source_Ptr;
890 begin
891 if Is_White_Space (Source (Scan_Ptr)) then
893 -- Allow one or more spaces if followed by comment
895 S := Scan_Ptr + 1;
896 loop
897 if Source (S) = '-' and then Source (S + 1) = '-' then
898 return;
900 elsif Is_White_Space (Source (S)) then
901 S := S + 1;
903 else
904 exit;
905 end if;
906 end loop;
908 Error_Space_Not_Allowed (Scan_Ptr);
909 end if;
910 end Check_No_Space_After;
912 ---------------------------
913 -- Check_No_Space_Before --
914 ---------------------------
916 procedure Check_No_Space_Before is
917 begin
918 if Token_Ptr > First_Non_Blank_Location
919 and then Source (Token_Ptr - 1) <= ' '
920 then
921 Error_Space_Not_Allowed (Token_Ptr - 1);
922 end if;
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
934 begin
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?");
939 end if;
940 end if;
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
952 begin
953 if Style_Check_Tokens then
954 if Identifier_Char (Source (Token_Ptr + 1)) then
955 Error_Space_Required (Token_Ptr + 1);
956 end if;
958 Check_No_Space_Before;
959 end if;
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
970 begin
971 if Style_Check_Tokens then
972 Check_No_Space_Before;
974 if Source (Scan_Ptr) > ' ' then
975 Error_Space_Required (Scan_Ptr);
976 end if;
977 end if;
978 end Check_Semicolon;
980 -------------------------------
981 -- Check_Separate_Stmt_Lines --
982 -------------------------------
984 procedure Check_Separate_Stmt_Lines is
985 begin
986 if Style_Check_Separate_Stmt_Lines then
987 Check_Separate_Stmt_Lines_Cont;
988 end if;
989 end Check_Separate_Stmt_Lines;
991 ------------------------------------
992 -- Check_Separate_Stmt_Lines_Cont --
993 ------------------------------------
995 procedure Check_Separate_Stmt_Lines_Cont is
996 S : Source_Ptr;
998 begin
999 -- Skip past white space
1001 S := Scan_Ptr;
1002 while Is_White_Space (Source (S)) loop
1003 S := S + 1;
1004 end loop;
1006 -- Line terminator is OK
1008 if Source (S) in Line_Terminator then
1009 return;
1011 -- Comment is OK
1013 elsif Source (S) = '-' and then Source (S + 1) = '-' then
1014 return;
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)))
1026 then
1027 return;
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)))
1040 then
1041 return;
1043 -- Otherwise we have the style violation we are looking for
1045 else
1046 if Token = Tok_Then then
1047 Error_Msg -- CODEFIX
1048 ("(style) no statements may follow THEN on same line?S?", S);
1049 else
1050 Error_Msg
1051 ("(style) no statements may follow ELSE on same line?S?", S);
1052 end if;
1053 end if;
1054 end Check_Separate_Stmt_Lines_Cont;
1056 ----------------
1057 -- Check_Then --
1058 ----------------
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
1065 begin
1066 if Style_Check_If_Then_Layout then
1067 declare
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);
1072 begin
1073 if If_Line = Then_Line then
1074 null;
1075 elsif Token_Ptr /= First_Non_Blank_Location then
1076 Error_Msg_SC ("(style) misplaced THEN?i?");
1077 end if;
1078 end;
1079 end if;
1080 end Check_Then;
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
1094 begin
1095 if Style_Check_Tokens then
1096 if Inside_Depends then
1097 Require_Following_Space;
1098 else
1099 Check_No_Space_After;
1100 end if;
1101 end if;
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
1111 begin
1112 if Style_Check_Tokens then
1113 Require_Preceding_Space;
1114 Require_Following_Space;
1115 end if;
1116 end Check_Vertical_Bar;
1118 -----------------------
1119 -- Check_Xtra_Parens --
1120 -----------------------
1122 procedure Check_Xtra_Parens (N : Node_Id) is
1123 begin
1124 if Style_Check_Xtra_Parens
1125 and then
1126 Paren_Count (N) >
1127 (if Nkind (N) in N_Case_Expression
1128 | N_Expression_With_Actions
1129 | N_If_Expression
1130 | N_Quantified_Expression
1131 | N_Raise_Expression
1132 then 1
1133 else 0)
1134 then
1135 Error_Msg -- CODEFIX
1136 ("(style) redundant parentheses?x?", Errout.First_Sloc (N));
1137 end if;
1138 end Check_Xtra_Parens;
1140 ----------------------------------
1141 -- Check_Xtra_Parens_Precedence --
1142 ----------------------------------
1144 procedure Check_Xtra_Parens_Precedence (N : Node_Id) is
1145 begin
1146 if Style_Check_Xtra_Parens_Precedence
1147 and then
1148 Paren_Count (N) >
1149 (if Nkind (N) in N_Case_Expression
1150 | N_Expression_With_Actions
1151 | N_If_Expression
1152 | N_Quantified_Expression
1153 | N_Raise_Expression
1154 then 1
1155 else 0)
1156 then
1157 Error_Msg -- CODEFIX
1158 ("(style) redundant parentheses?z?", Errout.First_Sloc (N));
1159 end if;
1160 end Check_Xtra_Parens_Precedence;
1162 ----------------------------
1163 -- Determine_Token_Casing --
1164 ----------------------------
1166 function Determine_Token_Casing return Casing_Type is
1167 begin
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
1176 begin
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
1186 begin
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
1196 begin
1197 return C = ' ' or else C = HT;
1198 end Is_White_Space;
1200 -------------------
1201 -- Mode_In_Check --
1202 -------------------
1204 function Mode_In_Check return Boolean is
1205 begin
1206 return Style_Check and Style_Check_Mode_In;
1207 end Mode_In_Check;
1209 -----------------
1210 -- No_End_Name --
1211 -----------------
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
1217 begin
1218 if Style_Check_End_Labels then
1219 Error_Msg_Node_1 := Name;
1220 Error_Msg_SP -- CODEFIX
1221 ("(style) `END &` required?e?");
1222 end if;
1223 end No_End_Name;
1225 ------------------
1226 -- No_Exit_Name --
1227 ------------------
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
1233 begin
1234 if Style_Check_End_Labels then
1235 Error_Msg_Node_1 := Name;
1236 Error_Msg_SP -- CODEFIX
1237 ("(style) `EXIT &` required?e?");
1238 end if;
1239 end No_Exit_Name;
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
1250 begin
1251 if Style_Check_Keyword_Casing then
1252 Error_Msg_SC -- CODEFIX
1253 ("(style) reserved words must be all lower case?k?");
1254 end if;
1255 end Non_Lower_Case_Keyword;
1257 -----------------------------
1258 -- Require_Following_Space --
1259 -----------------------------
1261 procedure Require_Following_Space is
1262 begin
1263 if Source (Scan_Ptr) > ' ' then
1264 Error_Space_Required (Scan_Ptr);
1265 end if;
1266 end Require_Following_Space;
1268 -----------------------------
1269 -- Require_Preceding_Space --
1270 -----------------------------
1272 procedure Require_Preceding_Space is
1273 begin
1274 if Token_Ptr > Source_First (Current_Source_File)
1275 and then Source (Token_Ptr - 1) > ' '
1276 then
1277 Error_Space_Required (Token_Ptr);
1278 end if;
1279 end Require_Preceding_Space;
1281 end Styleg;