c++: contracts fixes
[official-gcc.git] / gcc / ada / styleg.adb
blob6a785b5835aadbd566ac985d5560ff9be25c35f2
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-2022, 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 Opt; use Opt;
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
45 use ASCII;
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
55 -- is encountered.
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
103 -- a line feed.
105 procedure Check_Abs_Not is
106 begin
107 if Style_Check_Tokens then
108 if Source (Scan_Ptr) not in ' ' | ASCII.CR | ASCII.LF then
109 Error_Space_Required (Scan_Ptr);
110 end if;
111 end if;
112 end Check_Abs_Not;
114 ----------------------
115 -- Check_Apostrophe --
116 ----------------------
118 -- Do not allow space after apostrophe
120 procedure Check_Apostrophe is
121 begin
122 if Style_Check_Tokens then
123 Check_No_Space_After;
124 end if;
125 end Check_Apostrophe;
127 -----------------
128 -- Check_Arrow --
129 -----------------
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
136 begin
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) = '+'
145 then
146 Error_Space_Not_Allowed (Scan_Ptr);
148 elsif Source (Scan_Ptr) /= ' '
149 and then Source (Scan_Ptr) /= '+'
150 then
151 Require_Following_Space;
152 end if;
154 -- Normal case
156 else
157 Require_Following_Space;
158 end if;
159 end if;
160 end Check_Arrow;
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);
172 begin
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");
177 end if;
178 end if;
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
189 begin
190 if Style_Check_Tokens then
191 Require_Preceding_Space;
192 Require_Following_Space;
193 end if;
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
211 begin
212 if Nkind (N) in N_Identifier | N_Expanded_Name then
213 return True;
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)))
220 and then
221 OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
223 else
224 return False;
225 end if;
226 end OK_Boolean_Operand;
228 -- Start of processing for Check_Boolean_Operator
230 begin
231 if Style_Check_Boolean_And_Or
232 and then Comes_From_Source (Node)
233 then
234 declare
235 Orig : constant Node_Id := Original_Node (Node);
237 begin
238 if Nkind (Orig) in N_Op_And | N_Op_Or then
239 declare
240 L : constant Node_Id := Original_Node (Left_Opnd (Orig));
241 R : constant Node_Id := Original_Node (Right_Opnd (Orig));
243 begin
244 -- First OK case, simple boolean constants/identifiers
246 if OK_Boolean_Operand (L)
247 and then
248 OK_Boolean_Operand (R)
249 then
250 return;
252 -- Second OK case, modular types
254 elsif Is_Modular_Integer_Type (Etype (Node)) then
255 return;
257 -- Third OK case, array types
259 elsif Is_Array_Type (Etype (Node)) then
260 return;
262 -- Otherwise we have an error
264 elsif Nkind (Orig) = N_Op_And then
265 Error_Msg -- CODEFIX
266 ("(style) `AND THEN` required", Sloc (Orig));
267 else
268 Error_Msg -- CODEFIX
269 ("(style) `OR ELSE` required", Sloc (Orig));
270 end if;
271 end;
272 end if;
273 end;
274 end if;
275 end Check_Boolean_Operator;
277 ---------------
278 -- Check_Box --
279 ---------------
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
286 begin
287 if Style_Check_Tokens then
288 if Prev_Token /= Tok_Left_Paren then
289 Require_Preceding_Space;
290 end if;
291 end if;
292 end Check_Box;
294 -----------------
295 -- Check_Colon --
296 -----------------
298 -- In check token mode (-gnatyt), colon must be surrounded by spaces
300 procedure Check_Colon is
301 begin
302 if Style_Check_Tokens then
303 Require_Preceding_Space;
304 Require_Following_Space;
305 end if;
306 end Check_Colon;
308 -----------------------
309 -- Check_Colon_Equal --
310 -----------------------
312 -- In check token mode (-gnatyt), := must be surrounded by spaces
314 procedure Check_Colon_Equal is
315 begin
316 if Style_Check_Tokens then
317 Require_Preceding_Space;
318 Require_Following_Space;
319 end if;
320 end Check_Colon_Equal;
322 -----------------
323 -- Check_Comma --
324 -----------------
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
331 begin
332 if Style_Check_Tokens then
333 Check_No_Space_Before;
335 if Source (Scan_Ptr) > ' ' then
336 Error_Space_Required (Scan_Ptr);
337 end if;
338 end if;
339 end Check_Comma;
341 -------------------
342 -- Check_Comment --
343 -------------------
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
382 S : Source_Ptr;
383 C : Character;
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,
401 -- otherwise False.
403 --------------------
404 -- Is_Box_Comment --
405 --------------------
407 function Is_Box_Comment return Boolean is
408 S : Source_Ptr;
410 begin
411 -- Do we need to worry about UTF_32 line terminators here ???
413 S := Scan_Ptr + 3;
414 while Source (S) not in Line_Terminator loop
415 S := S + 1;
416 end loop;
418 return Source (S - 1) = '-' and then Source (S - 2) = '-';
419 end Is_Box_Comment;
421 --------------------------
422 -- Is_Special_Character --
423 --------------------------
425 function Is_Special_Character (C : Character) return Boolean is
426 begin
427 if GNAT_Mode then
428 return C = '!';
429 else
430 return
431 Character'Pos (C) in 16#21# .. 16#2F#
432 or else
433 Character'Pos (C) in 16#3A# .. 16#3F#;
434 end if;
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
442 P : Source_Ptr;
444 begin
445 -- Step to end of line
447 P := Scan_Ptr + 2;
448 while Source (P) not in Line_Terminator loop
449 P := P + 1;
450 end loop;
452 -- Step past blanks, and line terminators (UTF_32 case???)
454 while Source (P) <= ' ' and then Source (P) /= EOF loop
455 P := P + 1;
456 end loop;
458 -- Compare columns
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
468 S, P : Source_Ptr;
470 begin
471 -- Point S to start of this line, and P to start of previous line
473 S := Line_Start (Scan_Ptr);
474 P := S;
475 Backup_Line (P);
477 -- Step P to first non-blank character on line
479 loop
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.
483 if P = S then
484 return False;
485 end if;
487 exit when Source (P) /= ' ' and then Source (P) /= ASCII.HT;
488 P := P + 1;
489 end loop;
491 -- Compare columns
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
498 begin
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) > ' '
507 then
508 Error_Msg_S -- CODEFIX
509 ("(style) space required");
510 end if;
511 end if;
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))
521 then
522 Error_Msg -- CODEFIX
523 ("(style) space required", Scan_Ptr + 2);
524 end if;
525 end if;
527 return;
529 -- Case of a comment that is at the start of a line
531 else
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
538 then
539 Error_Msg_S -- CODEFIX
540 ("(style) bad column");
541 end if;
543 return;
544 end if;
545 end if;
547 -- If we are not checking comments, nothing more to do
549 if not Style_Check_Comments then
550 return;
551 end if;
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
561 if C < ' ' then
562 return;
563 end if;
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
570 return;
571 end if;
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).
577 S := Scan_Ptr + 2;
578 while Source (S) >= ' ' loop
579 if Source (S) /= '-' then
580 if Is_Box_Comment
581 or else Style_Check_Comments_Spacing = 1
582 then
583 Error_Space_Required (Scan_Ptr + 2);
584 else
585 Error_Msg -- CODEFIX
586 ("(style) two spaces required", Scan_Ptr + 2);
587 end if;
589 return;
590 end if;
592 S := S + 1;
593 end loop;
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
601 then
602 return;
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
607 -- box comment.
609 elsif not Is_Box_Comment then
610 Error_Space_Required (Scan_Ptr + 3);
611 end if;
612 end if;
613 end Check_Comment;
615 --------------------------------------
616 -- Check_Defining_Identifier_Casing --
617 --------------------------------------
619 procedure Check_Defining_Identifier_Casing is
620 begin
621 if Style_Check_Mixed_Case_Decls then
622 case Determine_Token_Casing is
623 when All_Lower_Case
624 | All_Upper_Case
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.
632 when Mixed_Case
633 | Unknown
635 null; -- OK
636 end case;
637 end if;
638 end Check_Defining_Identifier_Casing;
640 -------------------
641 -- Check_Dot_Dot --
642 -------------------
644 -- In check token mode (-gnatyt), ".." must be surrounded by spaces
646 procedure Check_Dot_Dot is
647 begin
648 if Style_Check_Tokens then
649 Require_Preceding_Space;
650 Require_Following_Space;
651 end if;
652 end Check_Dot_Dot;
654 ---------------
655 -- Check_EOF --
656 ---------------
658 -- In check blanks at end mode, check no blank lines precede the EOF
660 procedure Check_EOF is
661 begin
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
667 Error_Msg -- CODEFIX
668 ("(style) blank line not allowed at end of file",
669 Blank_Line_Location);
671 elsif Blank_Lines >= 3 then
672 Error_Msg -- CODEFIX
673 ("(style) blank lines not allowed at end of file",
674 Blank_Line_Location);
675 end if;
676 end if;
677 end Check_EOF;
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
686 begin
687 null;
688 end Check_Exponentiation_Operator;
690 --------------
691 -- Check_HT --
692 --------------
694 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
696 procedure Check_HT is
697 begin
698 if Style_Check_Horizontal_Tabs then
699 Error_Msg_S -- CODEFIX
700 ("(style) horizontal tab not allowed");
701 end if;
702 end Check_HT;
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
713 begin
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
717 then
718 Error_Msg_SC -- CODEFIX
719 ("(style) bad indentation");
720 end if;
721 end if;
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
734 begin
735 if Style_Check_Tokens then
736 if Token_Ptr > Source_First (Current_Source_File)
737 and then Identifier_Char (Source (Token_Ptr - 1))
738 then
739 Error_Space_Required (Token_Ptr);
740 end if;
742 Check_No_Space_After;
743 end if;
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
754 begin
755 if Style_Check_Max_Line_Length then
756 if Len > Style_Max_Line_Length then
757 Error_Msg
758 ("(style) this line is too long",
759 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
760 end if;
761 end if;
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
769 -- trailing space.
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
778 S : Source_Ptr;
780 L : Nat := Len;
781 -- Length of line (adjusted down for blanks at end of line)
783 begin
784 -- Reset count of blank lines if first line
786 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
787 Blank_Lines := 0;
788 end if;
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");
799 end if;
800 end if;
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
811 null;
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");
817 end if;
818 end if;
820 -- Remove trailing spaces
822 S := Scan_Ptr;
823 while L > 0 and then Is_White_Space (Source (S - 1)) loop
824 S := S - 1;
825 L := L - 1;
826 end 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
831 Error_Msg -- CODEFIX
832 ("(style) trailing spaces not permitted", S);
833 end if;
835 -- Deal with empty (blank) line
837 if L = 0 then
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;
847 end if;
849 -- Non-blank line, check for previous multiple blank lines
851 else
852 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
853 Error_Msg -- CODEFIX
854 ("(style) multiple blank lines", Blank_Line_Location);
855 end if;
857 -- And reset blank line count
859 Blank_Lines := 0;
860 end if;
861 end Check_Line_Terminator;
863 ------------------
864 -- Check_Not_In --
865 ------------------
867 -- In check tokens mode, only one space between NOT and IN
869 procedure Check_Not_In is
870 begin
871 if Style_Check_Tokens then
872 if Source (Token_Ptr - 1) /= ' '
873 or else Token_Ptr - Prev_Token_Ptr /= 4
874 then -- CODEFIX?
875 Error_Msg
876 ("(style) single space must separate NOT and IN", Token_Ptr - 1);
877 end if;
878 end if;
879 end Check_Not_In;
881 --------------------------
882 -- Check_No_Space_After --
883 --------------------------
885 procedure Check_No_Space_After is
886 S : Source_Ptr;
888 begin
889 if Is_White_Space (Source (Scan_Ptr)) then
891 -- Allow one or more spaces if followed by comment
893 S := Scan_Ptr + 1;
894 loop
895 if Source (S) = '-' and then Source (S + 1) = '-' then
896 return;
898 elsif Is_White_Space (Source (S)) then
899 S := S + 1;
901 else
902 exit;
903 end if;
904 end loop;
906 Error_Space_Not_Allowed (Scan_Ptr);
907 end if;
908 end Check_No_Space_After;
910 ---------------------------
911 -- Check_No_Space_Before --
912 ---------------------------
914 procedure Check_No_Space_Before is
915 begin
916 if Token_Ptr > First_Non_Blank_Location
917 and then Source (Token_Ptr - 1) <= ' '
918 then
919 Error_Space_Not_Allowed (Token_Ptr - 1);
920 end if;
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
932 begin
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");
937 end if;
938 end if;
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
950 begin
951 if Style_Check_Tokens then
952 if Identifier_Char (Source (Token_Ptr + 1)) then
953 Error_Space_Required (Token_Ptr + 1);
954 end if;
956 Check_No_Space_Before;
957 end if;
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
968 begin
969 if Style_Check_Tokens then
970 Check_No_Space_Before;
972 if Source (Scan_Ptr) > ' ' then
973 Error_Space_Required (Scan_Ptr);
974 end if;
975 end if;
976 end Check_Semicolon;
978 -------------------------------
979 -- Check_Separate_Stmt_Lines --
980 -------------------------------
982 procedure Check_Separate_Stmt_Lines is
983 begin
984 if Style_Check_Separate_Stmt_Lines then
985 Check_Separate_Stmt_Lines_Cont;
986 end if;
987 end Check_Separate_Stmt_Lines;
989 ------------------------------------
990 -- Check_Separate_Stmt_Lines_Cont --
991 ------------------------------------
993 procedure Check_Separate_Stmt_Lines_Cont is
994 S : Source_Ptr;
996 begin
997 -- Skip past white space
999 S := Scan_Ptr;
1000 while Is_White_Space (Source (S)) loop
1001 S := S + 1;
1002 end loop;
1004 -- Line terminator is OK
1006 if Source (S) in Line_Terminator then
1007 return;
1009 -- Comment is OK
1011 elsif Source (S) = '-' and then Source (S + 1) = '-' then
1012 return;
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)))
1024 then
1025 return;
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)))
1038 then
1039 return;
1041 -- Otherwise we have the style violation we are looking for
1043 else
1044 if Token = Tok_Then then
1045 Error_Msg -- CODEFIX
1046 ("(style) no statements may follow THEN on same line", S);
1047 else
1048 Error_Msg
1049 ("(style) no statements may follow ELSE on same line", S);
1050 end if;
1051 end if;
1052 end Check_Separate_Stmt_Lines_Cont;
1054 ----------------
1055 -- Check_Then --
1056 ----------------
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
1063 begin
1064 if Style_Check_If_Then_Layout then
1065 declare
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);
1070 begin
1071 if If_Line = Then_Line then
1072 null;
1073 elsif Token_Ptr /= First_Non_Blank_Location then
1074 Error_Msg_SC ("(style) misplaced THEN");
1075 end if;
1076 end;
1077 end if;
1078 end Check_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
1092 begin
1093 if Style_Check_Tokens then
1094 if Inside_Depends then
1095 Require_Following_Space;
1096 else
1097 Check_No_Space_After;
1098 end if;
1099 end if;
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
1109 begin
1110 if Style_Check_Tokens then
1111 Require_Preceding_Space;
1112 Require_Following_Space;
1113 end if;
1114 end Check_Vertical_Bar;
1116 -----------------------
1117 -- Check_Xtra_Parens --
1118 -----------------------
1120 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
1121 begin
1122 if Style_Check_Xtra_Parens then
1123 Error_Msg -- CODEFIX
1124 ("(style) redundant parentheses", Loc);
1125 end if;
1126 end Check_Xtra_Parens;
1128 ----------------------------
1129 -- Determine_Token_Casing --
1130 ----------------------------
1132 function Determine_Token_Casing return Casing_Type is
1133 begin
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
1142 begin
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
1152 begin
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
1162 begin
1163 return C = ' ' or else C = HT;
1164 end Is_White_Space;
1166 -------------------
1167 -- Mode_In_Check --
1168 -------------------
1170 function Mode_In_Check return Boolean is
1171 begin
1172 return Style_Check and Style_Check_Mode_In;
1173 end Mode_In_Check;
1175 -----------------
1176 -- No_End_Name --
1177 -----------------
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
1183 begin
1184 if Style_Check_End_Labels then
1185 Error_Msg_Node_1 := Name;
1186 Error_Msg_SP -- CODEFIX
1187 ("(style) `END &` required");
1188 end if;
1189 end No_End_Name;
1191 ------------------
1192 -- No_Exit_Name --
1193 ------------------
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
1199 begin
1200 if Style_Check_End_Labels then
1201 Error_Msg_Node_1 := Name;
1202 Error_Msg_SP -- CODEFIX
1203 ("(style) `EXIT &` required");
1204 end if;
1205 end No_Exit_Name;
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
1216 begin
1217 if Style_Check_Keyword_Casing then
1218 Error_Msg_SC -- CODEFIX
1219 ("(style) reserved words must be all lower case");
1220 end if;
1221 end Non_Lower_Case_Keyword;
1223 -----------------------------
1224 -- Require_Following_Space --
1225 -----------------------------
1227 procedure Require_Following_Space is
1228 begin
1229 if Source (Scan_Ptr) > ' ' then
1230 Error_Space_Required (Scan_Ptr);
1231 end if;
1232 end Require_Following_Space;
1234 -----------------------------
1235 -- Require_Preceding_Space --
1236 -----------------------------
1238 procedure Require_Preceding_Space is
1239 begin
1240 if Token_Ptr > Source_First (Current_Source_File)
1241 and then Source (Token_Ptr - 1) > ' '
1242 then
1243 Error_Space_Required (Token_Ptr);
1244 end if;
1245 end Require_Preceding_Space;
1247 end Styleg;