Fix typo in t-dimode
[official-gcc.git] / gcc / ada / styleg.adb
blob188af1aa9faf6fd077c3b5096c58b535d7a75ab3
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-2021, 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 --
726 ----------------------
728 -- In check token mode (-gnatyt), left paren must not be preceded by an
729 -- identifier character or digit (a separating space is required) and may
730 -- never be followed by a space.
732 procedure Check_Left_Paren is
733 begin
734 if Style_Check_Tokens then
735 if Token_Ptr > Source_First (Current_Source_File)
736 and then Identifier_Char (Source (Token_Ptr - 1))
737 then
738 Error_Space_Required (Token_Ptr);
739 end if;
741 Check_No_Space_After;
742 end if;
743 end Check_Left_Paren;
745 ---------------------------
746 -- Check_Line_Max_Length --
747 ---------------------------
749 -- In check max line length mode (-gnatym), the line length must
750 -- not exceed the permitted maximum value.
752 procedure Check_Line_Max_Length (Len : Nat) is
753 begin
754 if Style_Check_Max_Line_Length then
755 if Len > Style_Max_Line_Length then
756 Error_Msg
757 ("(style) this line is too long",
758 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
759 end if;
760 end if;
761 end Check_Line_Max_Length;
763 ---------------------------
764 -- Check_Line_Terminator --
765 ---------------------------
767 -- In check blanks at end mode (-gnatyb), lines may not end with a
768 -- trailing space.
770 -- In check form feeds mode (-gnatyf), the line terminator may not
771 -- be either of the characters FF or VT.
773 -- In check DOS line terminators node (-gnatyd), the line terminator
774 -- must be a single LF, without a following CR.
776 procedure Check_Line_Terminator (Len : Nat) is
777 S : Source_Ptr;
779 L : Nat := Len;
780 -- Length of line (adjusted down for blanks at end of line)
782 begin
783 -- Reset count of blank lines if first line
785 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
786 Blank_Lines := 0;
787 end if;
789 -- Check FF/VT terminators
791 if Style_Check_Form_Feeds then
792 if Source (Scan_Ptr) = ASCII.FF then
793 Error_Msg_S -- CODEFIX
794 ("(style) form feed not allowed");
795 elsif Source (Scan_Ptr) = ASCII.VT then
796 Error_Msg_S -- CODEFIX
797 ("(style) vertical tab not allowed");
798 end if;
799 end if;
801 -- Check DOS line terminator
803 if Style_Check_DOS_Line_Terminator then
805 -- Ignore EOF, since we only get called with an EOF if it is the last
806 -- character in the buffer (and was therefore not in the source
807 -- file), since the terminating EOF is added to stop the scan.
809 if Source (Scan_Ptr) = EOF then
810 null;
812 -- Bad terminator if we don't have an LF
814 elsif Source (Scan_Ptr) /= LF then
815 Error_Msg_S ("(style) incorrect line terminator");
816 end if;
817 end if;
819 -- Remove trailing spaces
821 S := Scan_Ptr;
822 while L > 0 and then Is_White_Space (Source (S - 1)) loop
823 S := S - 1;
824 L := L - 1;
825 end loop;
827 -- Issue message for blanks at end of line if option enabled
829 if Style_Check_Blanks_At_End and then L < Len then
830 Error_Msg -- CODEFIX
831 ("(style) trailing spaces not permitted", S);
832 end if;
834 -- Deal with empty (blank) line
836 if L = 0 then
838 -- Increment blank line count
840 Blank_Lines := Blank_Lines + 1;
842 -- If first blank line, record location for later error message
844 if Blank_Lines = 1 then
845 Blank_Line_Location := Scan_Ptr;
846 end if;
848 -- Non-blank line, check for previous multiple blank lines
850 else
851 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
852 Error_Msg -- CODEFIX
853 ("(style) multiple blank lines", Blank_Line_Location);
854 end if;
856 -- And reset blank line count
858 Blank_Lines := 0;
859 end if;
860 end Check_Line_Terminator;
862 ------------------
863 -- Check_Not_In --
864 ------------------
866 -- In check tokens mode, only one space between NOT and IN
868 procedure Check_Not_In is
869 begin
870 if Style_Check_Tokens then
871 if Source (Token_Ptr - 1) /= ' '
872 or else Token_Ptr - Prev_Token_Ptr /= 4
873 then -- CODEFIX?
874 Error_Msg
875 ("(style) single space must separate NOT and IN", Token_Ptr - 1);
876 end if;
877 end if;
878 end Check_Not_In;
880 --------------------------
881 -- Check_No_Space_After --
882 --------------------------
884 procedure Check_No_Space_After is
885 S : Source_Ptr;
887 begin
888 if Is_White_Space (Source (Scan_Ptr)) then
890 -- Allow one or more spaces if followed by comment
892 S := Scan_Ptr + 1;
893 loop
894 if Source (S) = '-' and then Source (S + 1) = '-' then
895 return;
897 elsif Is_White_Space (Source (S)) then
898 S := S + 1;
900 else
901 exit;
902 end if;
903 end loop;
905 Error_Space_Not_Allowed (Scan_Ptr);
906 end if;
907 end Check_No_Space_After;
909 ---------------------------
910 -- Check_No_Space_Before --
911 ---------------------------
913 procedure Check_No_Space_Before is
914 begin
915 if Token_Ptr > First_Non_Blank_Location
916 and then Source (Token_Ptr - 1) <= ' '
917 then
918 Error_Space_Not_Allowed (Token_Ptr - 1);
919 end if;
920 end Check_No_Space_Before;
922 -----------------------
923 -- Check_Pragma_Name --
924 -----------------------
926 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
927 -- case, i.e. start with an upper case letter, and otherwise lower case,
928 -- except after an underline character.
930 procedure Check_Pragma_Name is
931 begin
932 if Style_Check_Pragma_Casing then
933 if Determine_Token_Casing /= Mixed_Case then
934 Error_Msg_SC -- CODEFIX
935 ("(style) bad capitalization, mixed case required");
936 end if;
937 end if;
938 end Check_Pragma_Name;
940 -----------------------
941 -- Check_Right_Paren --
942 -----------------------
944 -- In check token mode (-gnatyt), right paren must not be immediately
945 -- followed by an identifier character, and must never be preceded by
946 -- a space unless it is the initial non-blank character on the line.
948 procedure Check_Right_Paren is
949 begin
950 if Style_Check_Tokens then
951 if Identifier_Char (Source (Token_Ptr + 1)) then
952 Error_Space_Required (Token_Ptr + 1);
953 end if;
955 Check_No_Space_Before;
956 end if;
957 end Check_Right_Paren;
959 ---------------------
960 -- Check_Semicolon --
961 ---------------------
963 -- In check token mode (-gnatyt), semicolon does not permit a preceding
964 -- space and a following space is required.
966 procedure Check_Semicolon is
967 begin
968 if Style_Check_Tokens then
969 Check_No_Space_Before;
971 if Source (Scan_Ptr) > ' ' then
972 Error_Space_Required (Scan_Ptr);
973 end if;
974 end if;
975 end Check_Semicolon;
977 -------------------------------
978 -- Check_Separate_Stmt_Lines --
979 -------------------------------
981 procedure Check_Separate_Stmt_Lines is
982 begin
983 if Style_Check_Separate_Stmt_Lines then
984 Check_Separate_Stmt_Lines_Cont;
985 end if;
986 end Check_Separate_Stmt_Lines;
988 ------------------------------------
989 -- Check_Separate_Stmt_Lines_Cont --
990 ------------------------------------
992 procedure Check_Separate_Stmt_Lines_Cont is
993 S : Source_Ptr;
995 begin
996 -- Skip past white space
998 S := Scan_Ptr;
999 while Is_White_Space (Source (S)) loop
1000 S := S + 1;
1001 end loop;
1003 -- Line terminator is OK
1005 if Source (S) in Line_Terminator then
1006 return;
1008 -- Comment is OK
1010 elsif Source (S) = '-' and then Source (S + 1) = '-' then
1011 return;
1013 -- ABORT keyword is OK after THEN (THEN ABORT case)
1015 elsif Token = Tok_Then
1016 and then (Source (S + 0) = 'a' or else Source (S + 0) = 'A')
1017 and then (Source (S + 1) = 'b' or else Source (S + 1) = 'B')
1018 and then (Source (S + 2) = 'o' or else Source (S + 2) = 'O')
1019 and then (Source (S + 3) = 'r' or else Source (S + 3) = 'R')
1020 and then (Source (S + 4) = 't' or else Source (S + 4) = 'T')
1021 and then (Source (S + 5) in Line_Terminator
1022 or else Is_White_Space (Source (S + 5)))
1023 then
1024 return;
1026 -- PRAGMA keyword is OK after ELSE
1028 elsif Token = Tok_Else
1029 and then (Source (S + 0) = 'p' or else Source (S + 0) = 'P')
1030 and then (Source (S + 1) = 'r' or else Source (S + 1) = 'R')
1031 and then (Source (S + 2) = 'a' or else Source (S + 2) = 'A')
1032 and then (Source (S + 3) = 'g' or else Source (S + 3) = 'G')
1033 and then (Source (S + 4) = 'm' or else Source (S + 4) = 'M')
1034 and then (Source (S + 5) = 'a' or else Source (S + 5) = 'A')
1035 and then (Source (S + 6) in Line_Terminator
1036 or else Is_White_Space (Source (S + 6)))
1037 then
1038 return;
1040 -- Otherwise we have the style violation we are looking for
1042 else
1043 if Token = Tok_Then then
1044 Error_Msg -- CODEFIX
1045 ("(style) no statements may follow THEN on same line", S);
1046 else
1047 Error_Msg
1048 ("(style) no statements may follow ELSE on same line", S);
1049 end if;
1050 end if;
1051 end Check_Separate_Stmt_Lines_Cont;
1053 ----------------
1054 -- Check_Then --
1055 ----------------
1057 -- In check if then layout mode (-gnatyi), we expect a THEN keyword to
1058 -- appear either on the same line as the IF, or on a separate line if
1059 -- the IF statement extends for more than one line.
1061 procedure Check_Then (If_Loc : Source_Ptr) is
1062 begin
1063 if Style_Check_If_Then_Layout then
1064 declare
1065 If_Line : constant Physical_Line_Number :=
1066 Get_Physical_Line_Number (If_Loc);
1067 Then_Line : constant Physical_Line_Number :=
1068 Get_Physical_Line_Number (Token_Ptr);
1069 begin
1070 if If_Line = Then_Line then
1071 null;
1072 elsif Token_Ptr /= First_Non_Blank_Location then
1073 Error_Msg_SC ("(style) misplaced THEN");
1074 end if;
1075 end;
1076 end if;
1077 end Check_Then;
1079 -------------------------------
1080 -- Check_Unary_Plus_Or_Minus --
1081 -------------------------------
1083 -- In check token mode (-gnatyt), unary plus or minus must not be
1084 -- followed by a space.
1086 -- Annoying exception: if we have the sequence =>+ within a Depends or
1087 -- Refined_Depends pragma or aspect, then we insist on a space rather
1088 -- than forbidding it.
1090 procedure Check_Unary_Plus_Or_Minus (Inside_Depends : Boolean := False) is
1091 begin
1092 if Style_Check_Tokens then
1093 if Inside_Depends then
1094 Require_Following_Space;
1095 else
1096 Check_No_Space_After;
1097 end if;
1098 end if;
1099 end Check_Unary_Plus_Or_Minus;
1101 ------------------------
1102 -- Check_Vertical_Bar --
1103 ------------------------
1105 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
1107 procedure Check_Vertical_Bar is
1108 begin
1109 if Style_Check_Tokens then
1110 Require_Preceding_Space;
1111 Require_Following_Space;
1112 end if;
1113 end Check_Vertical_Bar;
1115 -----------------------
1116 -- Check_Xtra_Parens --
1117 -----------------------
1119 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
1120 begin
1121 if Style_Check_Xtra_Parens then
1122 Error_Msg -- CODEFIX
1123 ("(style) redundant parentheses", Loc);
1124 end if;
1125 end Check_Xtra_Parens;
1127 ----------------------------
1128 -- Determine_Token_Casing --
1129 ----------------------------
1131 function Determine_Token_Casing return Casing_Type is
1132 begin
1133 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
1134 end Determine_Token_Casing;
1136 -----------------------------
1137 -- Error_Space_Not_Allowed --
1138 -----------------------------
1140 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
1141 begin
1142 Error_Msg -- CODEFIX
1143 ("(style) space not allowed", S);
1144 end Error_Space_Not_Allowed;
1146 --------------------------
1147 -- Error_Space_Required --
1148 --------------------------
1150 procedure Error_Space_Required (S : Source_Ptr) is
1151 begin
1152 Error_Msg -- CODEFIX
1153 ("(style) space required", S);
1154 end Error_Space_Required;
1156 --------------------
1157 -- Is_White_Space --
1158 --------------------
1160 function Is_White_Space (C : Character) return Boolean is
1161 begin
1162 return C = ' ' or else C = HT;
1163 end Is_White_Space;
1165 -------------------
1166 -- Mode_In_Check --
1167 -------------------
1169 function Mode_In_Check return Boolean is
1170 begin
1171 return Style_Check and Style_Check_Mode_In;
1172 end Mode_In_Check;
1174 -----------------
1175 -- No_End_Name --
1176 -----------------
1178 -- In check end/exit labels mode (-gnatye), always require the name of
1179 -- a subprogram or package to be present on the END, so this is an error.
1181 procedure No_End_Name (Name : Node_Id) is
1182 begin
1183 if Style_Check_End_Labels then
1184 Error_Msg_Node_1 := Name;
1185 Error_Msg_SP -- CODEFIX
1186 ("(style) `END &` required");
1187 end if;
1188 end No_End_Name;
1190 ------------------
1191 -- No_Exit_Name --
1192 ------------------
1194 -- In check end/exit labels mode (-gnatye), always require the name of
1195 -- the loop to be present on the EXIT when exiting a named loop.
1197 procedure No_Exit_Name (Name : Node_Id) is
1198 begin
1199 if Style_Check_End_Labels then
1200 Error_Msg_Node_1 := Name;
1201 Error_Msg_SP -- CODEFIX
1202 ("(style) `EXIT &` required");
1203 end if;
1204 end No_Exit_Name;
1206 ----------------------------
1207 -- Non_Lower_Case_Keyword --
1208 ----------------------------
1210 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1211 -- in all lower case (excluding keywords range, access, delta and digits
1212 -- used as attribute designators).
1214 procedure Non_Lower_Case_Keyword is
1215 begin
1216 if Style_Check_Keyword_Casing then
1217 Error_Msg_SC -- CODEFIX
1218 ("(style) reserved words must be all lower case");
1219 end if;
1220 end Non_Lower_Case_Keyword;
1222 -----------------------------
1223 -- Require_Following_Space --
1224 -----------------------------
1226 procedure Require_Following_Space is
1227 begin
1228 if Source (Scan_Ptr) > ' ' then
1229 Error_Space_Required (Scan_Ptr);
1230 end if;
1231 end Require_Following_Space;
1233 -----------------------------
1234 -- Require_Preceding_Space --
1235 -----------------------------
1237 procedure Require_Preceding_Space is
1238 begin
1239 if Token_Ptr > Source_First (Current_Source_File)
1240 and then Source (Token_Ptr - 1) > ' '
1241 then
1242 Error_Space_Required (Token_Ptr);
1243 end if;
1244 end Require_Preceding_Space;
1246 end Styleg;