Debian Codegraph @

gcc/ada/styleg.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
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.
29
30 with Atree; use Atree;
31 with Casing; use Casing;
32 with Csets; use Csets;
33 with Err_Vars; use Err_Vars;
34 with Errout;
35 with Opt; use Opt;
36 with Scans; use Scans;
37 with Sinfo; use Sinfo;
38 with Sinfo.Nodes; use Sinfo.Nodes;
39 with Sinput; use Sinput;
40 with Stylesw; use Stylesw;
41 with Uintp; use Uintp;
42
43 package body Styleg is
44
45 use ASCII;
46
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.
51
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.
56
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
60
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.
65
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.
69
70 procedure Check_Separate_Stmt_Lines_Cont;
71 -- Non-inlined continuation of Check_Separate_Stmt_Lines
72
73 function Determine_Token_Casing return Casing_Type;
74 -- Determine casing of current token
75
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.
79
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.
83
84 function Is_White_Space (C : Character) return Boolean;
85 pragma Inline (Is_White_Space);
86 -- Returns True for space or HT, False otherwise
87
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.
92
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.
97
98 ----------------------
99 -- Check_Abs_Or_Not --
100 ----------------------
101
102 -- In check token mode (-gnatyt), ABS/NOT must be followed by a space or
103 -- a line feed.
104
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;
113
114 ----------------------
115 -- Check_Apostrophe --
116 ----------------------
117
118 -- Do not allow space after apostrophe
119
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;
126
127 -----------------
128 -- Check_Arrow --
129 -----------------
130
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 "=> +").
134
135 procedure Check_Arrow (Inside_Depends : Boolean := False) is
136 begin
137 if Style_Check_Tokens then
138 Require_Preceding_Space;
139
140 -- Special handling for Depends and Refined_Depends
141
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);
147
148 elsif Source (Scan_Ptr) /= ' '
149 and then Source (Scan_Ptr) /= '+'
150 then
151 Require_Following_Space;
152 end if;
153
154 -- Normal case
155
156 else
157 Require_Following_Space;
158 end if;
159 end if;
160 end Check_Arrow;
161
162 --------------------------
163 -- Check_Attribute_Name --
164 --------------------------
165
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.
169
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?a?");
177 end if;
178 end if;
179 end Check_Attribute_Name;
180
181 ---------------------------
182 -- Check_Binary_Operator --
183 ---------------------------
184
185 -- In check token mode (-gnatyt), binary operators other than the special
186 -- case of exponentiation require surrounding space characters.
187
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;
195
196 ---------------
197 -- Check_Box --
198 ---------------
199
200 -- In check token mode (-gnatyt), box must be preceded by a space or by
201 -- a left parenthesis. Spacing checking on the surrounding tokens takes
202 -- care of the remaining checks.
203
204 procedure Check_Box is
205 begin
206 if Style_Check_Tokens then
207 if Prev_Token /= Tok_Left_Paren then
208 Require_Preceding_Space;
209 end if;
210 end if;
211 end Check_Box;
212
213 -----------------
214 -- Check_Colon --
215 -----------------
216
217 -- In check token mode (-gnatyt), colon must be surrounded by spaces
218
219 procedure Check_Colon is
220 begin
221 if Style_Check_Tokens then
222 Require_Preceding_Space;
223 Require_Following_Space;
224 end if;
225 end Check_Colon;
226
227 -----------------------
228 -- Check_Colon_Equal --
229 -----------------------
230
231 -- In check token mode (-gnatyt), := must be surrounded by spaces
232
233 procedure Check_Colon_Equal is
234 begin
235 if Style_Check_Tokens then
236 Require_Preceding_Space;
237 Require_Following_Space;
238 end if;
239 end Check_Colon_Equal;
240
241 -----------------
242 -- Check_Comma --
243 -----------------
244
245 -- In check token mode (-gnatyt), comma must be either the first
246 -- token on a line, or be preceded by a non-blank character.
247 -- It must also always be followed by a blank.
248
249 procedure Check_Comma is
250 begin
251 if Style_Check_Tokens then
252 Check_No_Space_Before;
253
254 if Source (Scan_Ptr) > ' ' then
255 Error_Space_Required (Scan_Ptr);
256 end if;
257 end if;
258 end Check_Comma;
259
260 -------------------
261 -- Check_Comment --
262 -------------------
263
264 -- In check comment mode (-gnatyc) there are several requirements on the
265 -- format of comments. The following are permissible comment formats:
266
267 -- 1. Any comment that is not at the start of a line, i.e. where the
268 -- initial minuses are not the first non-blank characters on the
269 -- line must have at least one blank after the second minus or a
270 -- special character as defined in rule 5.
271
272 -- 2. A row of all minuses of any length is permitted (see procedure
273 -- box above in the source of this routine).
274
275 -- 3. A comment line starting with two minuses and a space, and ending
276 -- with a space and two minuses. Again see the procedure title box
277 -- immediately above in the source.
278
279 -- 4. A full line comment where two spaces follow the two minus signs.
280 -- This is the normal comment format in GNAT style, as typified by
281 -- the comments you are reading now.
282
283 -- 5. A full line comment where the first character after the second
284 -- minus is a special character, i.e. a character in the ASCII
285 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
286 -- comments, such as those generated by gnatprep, or those that
287 -- appear in the SPARK annotation language to be accepted.
288
289 -- Note: for GNAT internal files (-gnatg switch set on for the
290 -- compilation), the only special sequence recognized and allowed
291 -- is --! as generated by gnatprep.
292
293 -- 6. In addition, the comment must be properly indented if comment
294 -- indentation checking is active (Style_Check_Indentation non-zero).
295 -- Either the start column must be a multiple of this indentation,
296 -- or the indentation must match that of the next non-blank line,
297 -- or must match the indentation of the immediately preciding line
298 -- if it is non-blank.
299
300 procedure Check_Comment is
301 S : Source_Ptr;
302 C : Character;
303
304 function Is_Box_Comment return Boolean;
305 -- Returns True if the last two characters on the line are -- which
306 -- characterizes a box comment (as for example follows this spec).
307
308 function Is_Special_Character (C : Character) return Boolean;
309 -- Determines if C is a special character (see rule 5 above)
310
311 function Same_Column_As_Next_Non_Blank_Line return Boolean;
312 -- Called for a full line comment. If the indentation of this comment
313 -- matches that of the next non-blank line in the source, then True is
314 -- returned, otherwise False.
315
316 function Same_Column_As_Previous_Line return Boolean;
317 -- Called for a full line comment. If the previous line is blank, then
318 -- returns False. Otherwise, if the indentation of this comment matches
319 -- that of the previous line in the source, then True is returned,
320 -- otherwise False.
321
322 --------------------
323 -- Is_Box_Comment --
324 --------------------
325
326 function Is_Box_Comment return Boolean is
327 S : Source_Ptr;
328
329 begin
330 -- Do we need to worry about UTF_32 line terminators here ???
331
332 S := Scan_Ptr + 3;
333 while Source (S) not in EOF | Line_Terminator loop
334 S := S + 1;
335 end loop;
336
337 return Source (S - 1) = '-' and then Source (S - 2) = '-';
338 end Is_Box_Comment;
339
340 --------------------------
341 -- Is_Special_Character --
342 --------------------------
343
344 function Is_Special_Character (C : Character) return Boolean is
345 begin
346 if GNAT_Mode then
347 return C = '!';
348 else
349 return
350 Character'Pos (C) in 16#21# .. 16#2F#
351 or else
352 Character'Pos (C) in 16#3A# .. 16#3F#;
353 end if;
354 end Is_Special_Character;
355
356 ----------------------------------------
357 -- Same_Column_As_Next_Non_Blank_Line --
358 ----------------------------------------
359
360 function Same_Column_As_Next_Non_Blank_Line return Boolean is
361 P : Source_Ptr;
362
363 begin
364 -- Step to end of line
365
366 P := Scan_Ptr + 2;
367 while Source (P) not in Line_Terminator loop
368 P := P + 1;
369 end loop;
370
371 -- Step past blanks, and line terminators (UTF_32 case???)
372
373 while Source (P) <= ' ' and then Source (P) /= EOF loop
374 P := P + 1;
375 end loop;
376
377 -- Compare columns
378
379 return Get_Column_Number (Scan_Ptr) = Get_Column_Number (P);
380 end Same_Column_As_Next_Non_Blank_Line;
381
382 ----------------------------------
383 -- Same_Column_As_Previous_Line --
384 ----------------------------------
385
386 function Same_Column_As_Previous_Line return Boolean is
387 S, P : Source_Ptr;
388
389 begin
390 -- Point S to start of this line, and P to start of previous line
391
392 S := Line_Start (Scan_Ptr);
393 P := S;
394 Backup_Line (P);
395
396 -- Step P to first non-blank character on line
397
398 loop
399 -- If we get back to start of current line, then the previous line
400 -- was blank, and we always return False in that situation.
401
402 if P = S then
403 return False;
404 end if;
405
406 exit when Source (P) /= ' ' and then Source (P) /= ASCII.HT;
407 P := P + 1;
408 end loop;
409
410 -- Compare columns
411
412 return Get_Column_Number (Scan_Ptr) = Get_Column_Number (P);
413 end Same_Column_As_Previous_Line;
414
415 -- Start of processing for Check_Comment
416
417 begin
418 -- Can never have a non-blank character preceding the first minus.
419 -- The "+ 3" is to leave room for a possible byte order mark (BOM);
420 -- we want to avoid a warning for a comment at the start of the
421 -- file just after the BOM.
422
423 if Style_Check_Comments then
424 if Scan_Ptr > Source_First (Current_Source_File) + 3
425 and then Source (Scan_Ptr - 1) > ' '
426 then
427 Error_Msg_S -- CODEFIX
428 ("(style) space required?c?");
429 end if;
430 end if;
431
432 -- For a comment that is not at the start of the line, the only
433 -- requirement is that we cannot have a non-blank character after
434 -- the second minus sign or a special character.
435
436 if Scan_Ptr /= First_Non_Blank_Location then
437 if Style_Check_Comments then
438 if Source (Scan_Ptr + 2) > ' '
439 and then not Is_Special_Character (Source (Scan_Ptr + 2))
440 then
441 Error_Msg -- CODEFIX
442 ("(style) space required?c?", Scan_Ptr + 2);
443 end if;
444 end if;
445
446 return;
447
448 -- Case of a comment that is at the start of a line
449
450 else
451 -- First check, must be in appropriately indented column
452
453 if Style_Check_Indentation /= 0 then
454 if Start_Column rem Style_Check_Indentation /= 0 then
455 if not Same_Column_As_Next_Non_Blank_Line
456 and then not Same_Column_As_Previous_Line
457 then
458 Error_Msg_S -- CODEFIX
459 ("(style) bad column?0?");
460 end if;
461
462 return;
463 end if;
464 end if;
465
466 -- If we are not checking comments, nothing more to do
467
468 if not Style_Check_Comments then
469 return;
470 end if;
471
472 -- Case of not followed by a blank. Usually wrong, but there are
473 -- some exceptions that we permit.
474
475 if Source (Scan_Ptr + 2) /= ' ' then
476 C := Source (Scan_Ptr + 2);
477
478 -- Case of -- all on its own on a line is OK
479
480 if C < ' ' then
481 return;
482 end if;
483
484 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
485 -- This is not permitted in internal GNAT implementation units
486 -- except for the case of --! as used by gnatprep output.
487
488 if Is_Special_Character (C) then
489 return;
490 end if;
491
492 -- The only other case in which we allow a character after
493 -- the -- other than a space is when we have a row of minus
494 -- signs (case of header lines for a box comment for example).
495
496 S := Scan_Ptr + 2;
497 while Source (S) >= ' ' loop
498 if Source (S) /= '-' then
499 if Is_Box_Comment
500 or else Style_Check_Comments_Spacing = 1
501 then
502 Error_Msg -- CODEFIX
503 ("(style) space required?c?", Scan_Ptr + 2);
504 else
505 Error_Msg -- CODEFIX
506 ("(style) two spaces required?c?", Scan_Ptr + 2);
507 end if;
508
509 return;
510 end if;
511
512 S := S + 1;
513 end loop;
514
515 -- If we are followed by a blank, then the comment is OK if the
516 -- character following this blank is another blank or a format
517 -- effector, or if the required comment spacing is 1.
518
519 elsif Source (Scan_Ptr + 3) <= ' '
520 or else Style_Check_Comments_Spacing = 1
521 then
522 return;
523
524 -- Here is the case where we only have one blank after the two minus
525 -- signs, with Style_Check_Comments_Spacing set to 2, which is an
526 -- error unless the line ends with two minus signs, the case of a
527 -- box comment.
528
529 elsif not Is_Box_Comment then
530 Error_Msg -- CODEFIX
531 ("(style) space required?c?", Scan_Ptr + 3);
532 end if;
533 end if;
534 end Check_Comment;
535
536 --------------------------------------
537 -- Check_Defining_Identifier_Casing --
538 --------------------------------------
539
540 procedure Check_Defining_Identifier_Casing is
541 begin
542 if Style_Check_Mixed_Case_Decls then
543 case Determine_Token_Casing is
544 when All_Lower_Case
545 | All_Upper_Case
546 =>
547 Error_Msg_SC -- CODEFIX
548 ("(style) bad capitalization, mixed case required?D?");
549
550 -- The Unknown case is something like A_B_C, which is both all
551 -- caps and mixed case.
552
553 when Mixed_Case
554 | Unknown
555 =>
556 null; -- OK
557 end case;
558 end if;
559 end Check_Defining_Identifier_Casing;
560
561 -------------------
562 -- Check_Dot_Dot --
563 -------------------
564
565 -- In check token mode (-gnatyt), ".." must be surrounded by spaces
566
567 procedure Check_Dot_Dot is
568 begin
569 if Style_Check_Tokens then
570 Require_Preceding_Space;
571 Require_Following_Space;
572 end if;
573 end Check_Dot_Dot;
574
575 ---------------
576 -- Check_EOF --
577 ---------------
578
579 -- In check blanks at end mode, check no blank lines precede the EOF
580
581 procedure Check_EOF is
582 begin
583 if Style_Check_Blank_Lines then
584
585 -- We expect one blank line, from the EOF, but no more than one
586
587 if Blank_Lines = 2 then
588 Error_Msg -- CODEFIX
589 ("(style) blank line not allowed at end of file?u?",
590 Blank_Line_Location);
591
592 elsif Blank_Lines >= 3 then
593 Error_Msg -- CODEFIX
594 ("(style) blank lines not allowed at end of file?u?",
595 Blank_Line_Location);
596 end if;
597 end if;
598 end Check_EOF;
599
600 -----------------------------------
601 -- Check_Exponentiation_Operator --
602 -----------------------------------
603
604 -- No spaces are required for the ** operator in GNAT style check mode
605
606 procedure Check_Exponentiation_Operator is
607 begin
608 null;
609 end Check_Exponentiation_Operator;
610
611 --------------
612 -- Check_HT --
613 --------------
614
615 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
616
617 procedure Check_HT is
618 begin
619 if Style_Check_Horizontal_Tabs then
620 Error_Msg_S -- CODEFIX
621 ("(style) horizontal tab not allowed?h?");
622 end if;
623 end Check_HT;
624
625 -----------------------
626 -- Check_Indentation --
627 -----------------------
628
629 -- In check indentation mode (-gnaty? for ? a digit), a new statement or
630 -- declaration is required to start in a column that is a multiple of the
631 -- indentation amount.
632
633 procedure Check_Indentation is
634 begin
635 if Style_Check_Indentation /= 0 then
636 if Token_Ptr = First_Non_Blank_Location
637 and then Start_Column rem Style_Check_Indentation /= 0
638 then
639 Error_Msg_SC -- CODEFIX
640 ("(style) bad indentation?0?");
641 end if;
642 end if;
643 end Check_Indentation;
644
645 -------------------------------------
646 -- Check_Left_Paren_Square_Bracket --
647 -------------------------------------
648
649 -- In check token mode (-gnatyt), left paren must not be preceded by an
650 -- identifier character or digit (a separating space is required) and may
651 -- never be followed by a space.
652 -- Same applies for the left square bracket starting from Ada version 2022.
653
654 procedure Check_Left_Paren_Square_Bracket is
655 begin
656 if Style_Check_Tokens then
657 if Token_Ptr > Source_First (Current_Source_File)
658 and then Identifier_Char (Source (Token_Ptr - 1))
659 then
660 Error_Space_Required (Token_Ptr);
661 end if;
662
663 Check_No_Space_After;
664 end if;
665 end Check_Left_Paren_Square_Bracket;
666
667 ---------------------------
668 -- Check_Line_Max_Length --
669 ---------------------------
670
671 -- In check max line length mode (-gnatym), the line length must
672 -- not exceed the permitted maximum value.
673
674 procedure Check_Line_Max_Length (Len : Nat) is
675 begin
676 if Style_Check_Max_Line_Length then
677 if Len > Style_Max_Line_Length then
678 Error_Msg_Uint_1 := UI_From_Int (Len);
679 Error_Msg
680 ("(style) this line is too long: ^?M?",
681 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
682 end if;
683 end if;
684 end Check_Line_Max_Length;
685
686 ---------------------------
687 -- Check_Line_Terminator --
688 ---------------------------
689
690 -- In check blanks at end mode (-gnatyb), lines may not end with a
691 -- trailing space.
692
693 -- In check form feeds mode (-gnatyf), the line terminator may not
694 -- be either of the characters FF or VT.
695
696 -- In check DOS line terminators node (-gnatyd), the line terminator
697 -- must be a single LF, without a following CR.
698
699 procedure Check_Line_Terminator (Len : Nat) is
700 S : Source_Ptr;
701
702 L : Nat := Len;
703 -- Length of line (adjusted down for blanks at end of line)
704
705 begin
706 -- Reset count of blank lines if first line
707
708 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
709 Blank_Lines := 0;
710 end if;
711
712 -- Check FF/VT terminators
713
714 if Style_Check_Form_Feeds then
715 if Source (Scan_Ptr) = ASCII.FF then
716 Error_Msg_S -- CODEFIX
717 ("(style) form feed not allowed?f?");
718 elsif Source (Scan_Ptr) = ASCII.VT then
719 Error_Msg_S -- CODEFIX
720 ("(style) vertical tab not allowed?f?");
721 end if;
722 end if;
723
724 -- Check DOS line terminator
725
726 if Style_Check_DOS_Line_Terminator then
727
728 -- Ignore EOF, since we only get called with an EOF if it is the last
729 -- character in the buffer (and was therefore not in the source
730 -- file), since the terminating EOF is added to stop the scan.
731
732 if Source (Scan_Ptr) = EOF then
733 null;
734
735 -- Bad terminator if we don't have an LF
736
737 elsif Source (Scan_Ptr) /= LF then
738 Error_Msg_S ("(style) incorrect line terminator?d?");
739 end if;
740 end if;
741
742 -- Remove trailing spaces
743
744 S := Scan_Ptr;
745 while L > 0 and then Is_White_Space (Source (S - 1)) loop
746 S := S - 1;
747 L := L - 1;
748 end loop;
749
750 -- Issue message for blanks at end of line if option enabled
751
752 if Style_Check_Blanks_At_End and then L < Len then
753 Error_Msg -- CODEFIX
754 ("(style) trailing spaces not permitted?b?", S);
755 end if;
756
757 -- Deal with empty (blank) line
758
759 if L = 0 then
760
761 -- Increment blank line count
762
763 Blank_Lines := Blank_Lines + 1;
764
765 -- If first blank line, record location for later error message
766
767 if Blank_Lines = 1 then
768 Blank_Line_Location := Scan_Ptr;
769 end if;
770
771 -- Non-blank line, check for previous multiple blank lines
772
773 else
774 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
775 Error_Msg -- CODEFIX
776 ("(style) multiple blank lines?u?", Blank_Line_Location);
777 end if;
778
779 -- And reset blank line count
780
781 Blank_Lines := 0;
782 end if;
783 end Check_Line_Terminator;
784
785 ------------------
786 -- Check_Not_In --
787 ------------------
788
789 -- In check tokens mode, only one space between NOT and IN
790
791 procedure Check_Not_In is
792 begin
793 if Style_Check_Tokens then
794 if Source (Token_Ptr - 1) /= ' '
795 or else Token_Ptr - Prev_Token_Ptr /= 4
796 then -- CODEFIX?
797 Error_Msg
798 ("(style) single space must separate NOT and IN?t?",
799 Token_Ptr - 1);
800 end if;
801 end if;
802 end Check_Not_In;
803
804 --------------------------
805 -- Check_No_Space_After --
806 --------------------------
807
808 procedure Check_No_Space_After is
809 S : Source_Ptr;
810
811 begin
812 if Is_White_Space (Source (Scan_Ptr)) then
813
814 -- Allow one or more spaces if followed by comment
815
816 S := Scan_Ptr + 1;
817 loop
818 if Source (S) = '-' and then Source (S + 1) = '-' then
819 return;
820
821 elsif Is_White_Space (Source (S)) then
822 S := S + 1;
823
824 else
825 exit;
826 end if;
827 end loop;
828
829 Error_Space_Not_Allowed (Scan_Ptr);
830 end if;
831 end Check_No_Space_After;
832
833 ---------------------------
834 -- Check_No_Space_Before --
835 ---------------------------
836
837 procedure Check_No_Space_Before is
838 begin
839 if Token_Ptr > First_Non_Blank_Location
840 and then Source (Token_Ptr - 1) <= ' '
841 then
842 Error_Space_Not_Allowed (Token_Ptr - 1);
843 end if;
844 end Check_No_Space_Before;
845
846 -----------------------
847 -- Check_Pragma_Name --
848 -----------------------
849
850 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
851 -- case, i.e. start with an upper case letter, and otherwise lower case,
852 -- except after an underline character.
853
854 procedure Check_Pragma_Name is
855 begin
856 if Style_Check_Pragma_Casing then
857 if Determine_Token_Casing /= Mixed_Case then
858 Error_Msg_SC -- CODEFIX
859 ("(style) bad capitalization, mixed case required?p?");
860 end if;
861 end if;
862 end Check_Pragma_Name;
863
864 -----------------------
865 -- Check_Right_Paren --
866 -----------------------
867
868 -- In check token mode (-gnatyt), right paren must not be immediately
869 -- followed by an identifier character, and must never be preceded by
870 -- a space unless it is the initial non-blank character on the line.
871
872 procedure Check_Right_Paren is
873 begin
874 if Style_Check_Tokens then
875 if Identifier_Char (Source (Token_Ptr + 1)) then
876 Error_Space_Required (Token_Ptr + 1);
877 end if;
878
879 Check_No_Space_Before;
880 end if;
881 end Check_Right_Paren;
882
883 ---------------------
884 -- Check_Semicolon --
885 ---------------------
886
887 -- In check token mode (-gnatyt), semicolon does not permit a preceding
888 -- space and a following space is required.
889
890 procedure Check_Semicolon is
891 begin
892 if Style_Check_Tokens then
893 Check_No_Space_Before;
894
895 if Source (Scan_Ptr) > ' ' then
896 Error_Space_Required (Scan_Ptr);
897 end if;
898 end if;
899 end Check_Semicolon;
900
901 -------------------------------
902 -- Check_Separate_Stmt_Lines --
903 -------------------------------
904
905 procedure Check_Separate_Stmt_Lines is
906 begin
907 if Style_Check_Separate_Stmt_Lines then
908 Check_Separate_Stmt_Lines_Cont;
909 end if;
910 end Check_Separate_Stmt_Lines;
911
912 ------------------------------------
913 -- Check_Separate_Stmt_Lines_Cont --
914 ------------------------------------
915
916 procedure Check_Separate_Stmt_Lines_Cont is
917 S : Source_Ptr;
918
919 begin
920 -- Skip past white space
921
922 S := Scan_Ptr;
923 while Is_White_Space (Source (S)) loop
924 S := S + 1;
925 end loop;
926
927 -- Line terminator is OK
928
929 if Source (S) in Line_Terminator then
930 return;
931
932 -- Comment is OK
933
934 elsif Source (S) = '-' and then Source (S + 1) = '-' then
935 return;
936
937 -- ABORT keyword is OK after THEN (THEN ABORT case)
938
939 elsif Token = Tok_Then
940 and then (Source (S + 0) = 'a' or else Source (S + 0) = 'A')
941 and then (Source (S + 1) = 'b' or else Source (S + 1) = 'B')
942 and then (Source (S + 2) = 'o' or else Source (S + 2) = 'O')
943 and then (Source (S + 3) = 'r' or else Source (S + 3) = 'R')
944 and then (Source (S + 4) = 't' or else Source (S + 4) = 'T')
945 and then (Source (S + 5) in Line_Terminator
946 or else Is_White_Space (Source (S + 5)))
947 then
948 return;
949
950 -- PRAGMA keyword is OK after ELSE
951
952 elsif Token = Tok_Else
953 and then (Source (S + 0) = 'p' or else Source (S + 0) = 'P')
954 and then (Source (S + 1) = 'r' or else Source (S + 1) = 'R')
955 and then (Source (S + 2) = 'a' or else Source (S + 2) = 'A')
956 and then (Source (S + 3) = 'g' or else Source (S + 3) = 'G')
957 and then (Source (S + 4) = 'm' or else Source (S + 4) = 'M')
958 and then (Source (S + 5) = 'a' or else Source (S + 5) = 'A')
959 and then (Source (S + 6) in Line_Terminator
960 or else Is_White_Space (Source (S + 6)))
961 then
962 return;
963
964 -- Otherwise we have the style violation we are looking for
965
966 else
967 if Token = Tok_Then then
968 Error_Msg -- CODEFIX
969 ("(style) no statements may follow THEN on same line?S?", S);
970 else
971 Error_Msg
972 ("(style) no statements may follow ELSE on same line?S?", S);
973 end if;
974 end if;
975 end Check_Separate_Stmt_Lines_Cont;
976
977 ----------------
978 -- Check_Then --
979 ----------------
980
981 -- In check if then layout mode (-gnatyi), we expect a THEN keyword to
982 -- appear either on the same line as the IF, or on a separate line if
983 -- the IF statement extends for more than one line.
984
985 procedure Check_Then (If_Loc : Source_Ptr) is
986 begin
987 if Style_Check_If_Then_Layout then
988 declare
989 If_Line : constant Physical_Line_Number :=
990 Get_Physical_Line_Number (If_Loc);
991 Then_Line : constant Physical_Line_Number :=
992 Get_Physical_Line_Number (Token_Ptr);
993 begin
994 if If_Line = Then_Line then
995 null;
996 elsif Token_Ptr /= First_Non_Blank_Location then
997 Error_Msg_SC ("(style) misplaced THEN?i?");
998 end if;
999 end;
1000 end if;
1001 end Check_Then;
1002
1003 -------------------------------
1004 -- Check_Unary_Plus_Or_Minus --
1005 -------------------------------
1006
1007 -- In check token mode (-gnatyt), unary plus or minus must not be
1008 -- followed by a space.
1009
1010 -- Annoying exception: if we have the sequence =>+ within a Depends or
1011 -- Refined_Depends pragma or aspect, then we insist on a space rather
1012 -- than forbidding it.
1013
1014 procedure Check_Unary_Plus_Or_Minus (Inside_Depends : Boolean := False) is
1015 begin
1016 if Style_Check_Tokens then
1017 if Inside_Depends then
1018 Require_Following_Space;
1019 else
1020 Check_No_Space_After;
1021 end if;
1022 end if;
1023 end Check_Unary_Plus_Or_Minus;
1024
1025 ------------------------
1026 -- Check_Vertical_Bar --
1027 ------------------------
1028
1029 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
1030
1031 procedure Check_Vertical_Bar is
1032 begin
1033 if Style_Check_Tokens then
1034 Require_Preceding_Space;
1035 Require_Following_Space;
1036 end if;
1037 end Check_Vertical_Bar;
1038
1039 -----------------------
1040 -- Check_Xtra_Parens --
1041 -----------------------
1042
1043 procedure Check_Xtra_Parens (N : Node_Id) is
1044 begin
1045 if Style_Check_Xtra_Parens
1046 and then
1047 Paren_Count (N) >
1048 (if Nkind (N) in N_Case_Expression
1049 | N_Expression_With_Actions
1050 | N_If_Expression
1051 | N_Quantified_Expression
1052 | N_Raise_Expression
1053 then 1
1054 else 0)
1055 then
1056 Error_Msg -- CODEFIX
1057 ("(style) redundant parentheses?x?", Errout.First_Sloc (N));
1058 end if;
1059 end Check_Xtra_Parens;
1060
1061 ----------------------------
1062 -- Determine_Token_Casing --
1063 ----------------------------
1064
1065 function Determine_Token_Casing return Casing_Type is
1066 begin
1067 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
1068 end Determine_Token_Casing;
1069
1070 -----------------------------
1071 -- Error_Space_Not_Allowed --
1072 -----------------------------
1073
1074 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
1075 begin
1076 Error_Msg -- CODEFIX
1077 ("(style) space not allowed?t?", S);
1078 end Error_Space_Not_Allowed;
1079
1080 --------------------------
1081 -- Error_Space_Required --
1082 --------------------------
1083
1084 procedure Error_Space_Required (S : Source_Ptr) is
1085 begin
1086 Error_Msg -- CODEFIX
1087 ("(style) space required?t?", S);
1088 end Error_Space_Required;
1089
1090 --------------------
1091 -- Is_White_Space --
1092 --------------------
1093
1094 function Is_White_Space (C : Character) return Boolean is
1095 begin
1096 return C = ' ' or else C = HT;
1097 end Is_White_Space;
1098
1099 -------------------
1100 -- Mode_In_Check --
1101 -------------------
1102
1103 function Mode_In_Check return Boolean is
1104 begin
1105 return Style_Check and Style_Check_Mode_In;
1106 end Mode_In_Check;
1107
1108 -----------------
1109 -- No_End_Name --
1110 -----------------
1111
1112 -- In check end/exit labels mode (-gnatye), always require the name of
1113 -- a subprogram or package to be present on the END, so this is an error.
1114
1115 procedure No_End_Name (Name : Node_Id) is
1116 begin
1117 if Style_Check_End_Labels then
1118 Error_Msg_Node_1 := Name;
1119 Error_Msg_SP -- CODEFIX
1120 ("(style) `END &` required?e?");
1121 end if;
1122 end No_End_Name;
1123
1124 ------------------
1125 -- No_Exit_Name --
1126 ------------------
1127
1128 -- In check end/exit labels mode (-gnatye), always require the name of
1129 -- the loop to be present on the EXIT when exiting a named loop.
1130
1131 procedure No_Exit_Name (Name : Node_Id) is
1132 begin
1133 if Style_Check_End_Labels then
1134 Error_Msg_Node_1 := Name;
1135 Error_Msg_SP -- CODEFIX
1136 ("(style) `EXIT &` required?e?");
1137 end if;
1138 end No_Exit_Name;
1139
1140 ----------------------------
1141 -- Non_Lower_Case_Keyword --
1142 ----------------------------
1143
1144 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1145 -- in all lower case (excluding keywords range, access, delta and digits
1146 -- used as attribute designators).
1147
1148 procedure Non_Lower_Case_Keyword is
1149 begin
1150 if Style_Check_Keyword_Casing then
1151 Error_Msg_SC -- CODEFIX
1152 ("(style) reserved words must be all lower case?k?");
1153 end if;
1154 end Non_Lower_Case_Keyword;
1155
1156 -----------------------------
1157 -- Require_Following_Space --
1158 -----------------------------
1159
1160 procedure Require_Following_Space is
1161 begin
1162 if Source (Scan_Ptr) > ' ' then
1163 Error_Space_Required (Scan_Ptr);
1164 end if;
1165 end Require_Following_Space;
1166
1167 -----------------------------
1168 -- Require_Preceding_Space --
1169 -----------------------------
1170
1171 procedure Require_Preceding_Space is
1172 begin
1173 if Token_Ptr > Source_First (Current_Source_File)
1174 and then Source (Token_Ptr - 1) > ' '
1175 then
1176 Error_Space_Required (Token_Ptr);
1177 end if;
1178 end Require_Preceding_Space;
1179
1180 end Styleg;