Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / ada / styleg.adb
blobdc6b6a64892f5a52f3dc67f8856e2444b2da597a
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-2010, 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 Err_Vars; use Err_Vars;
35 with Opt; use Opt;
36 with Scans; use Scans;
37 with Sinfo; use Sinfo;
38 with Sinput; use Sinput;
39 with Stylesw; use Stylesw;
41 package body Styleg is
43 use ASCII;
45 Blank_Lines : Nat := 0;
46 -- Counts number of empty lines seen. Reset to zero if a non-empty line
47 -- is encountered. Used to check for trailing blank lines in Check_EOF,
48 -- and for multiple blank lines.
50 Blank_Line_Location : Source_Ptr;
51 -- Remembers location of first blank line in a series. Used to issue an
52 -- appropriate diagnostic if subsequent blank lines or the end of file
53 -- is encountered.
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 procedure Check_No_Space_After;
60 -- Checks that there is a non-white space character after the current
61 -- token, or white space followed by a comment, or the end of line.
62 -- Issue error message if not.
64 procedure Check_No_Space_Before;
65 -- Check that token is first token on line, or else is not preceded
66 -- by white space. Signal error of space not allowed if not.
68 procedure Check_Separate_Stmt_Lines_Cont;
69 -- Non-inlined continuation of Check_Separate_Stmt_Lines
71 function Determine_Token_Casing return Casing_Type;
72 -- Determine casing of current token
74 procedure Error_Space_Not_Allowed (S : Source_Ptr);
75 -- Posts an error message indicating that a space is not allowed
76 -- at the given source location.
78 procedure Error_Space_Required (S : Source_Ptr);
79 -- Posts an error message indicating that a space is required at
80 -- the given source location.
82 function Is_White_Space (C : Character) return Boolean;
83 pragma Inline (Is_White_Space);
84 -- Returns True for space, HT, VT or FF, False otherwise
86 procedure Require_Following_Space;
87 pragma Inline (Require_Following_Space);
88 -- Require token to be followed by white space. Used only if in GNAT
89 -- style checking mode.
91 procedure Require_Preceding_Space;
92 pragma Inline (Require_Preceding_Space);
93 -- Require token to be preceded by white space. Used only if in GNAT
94 -- style checking mode.
96 ----------------------
97 -- Check_Abs_Or_Not --
98 ----------------------
100 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
102 procedure Check_Abs_Not is
103 begin
104 if Style_Check_Tokens then
105 if Source (Scan_Ptr) > ' ' then
106 Error_Space_Required (Scan_Ptr);
107 end if;
108 end if;
109 end Check_Abs_Not;
111 ----------------------
112 -- Check_Apostrophe --
113 ----------------------
115 -- Do not allow space before or after apostrophe
117 procedure Check_Apostrophe is
118 begin
119 if Style_Check_Tokens then
120 Check_No_Space_After;
121 end if;
122 end Check_Apostrophe;
124 -----------------
125 -- Check_Arrow --
126 -----------------
128 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
130 procedure Check_Arrow is
131 begin
132 if Style_Check_Tokens then
133 Require_Preceding_Space;
134 Require_Following_Space;
135 end if;
136 end Check_Arrow;
138 --------------------------
139 -- Check_Attribute_Name --
140 --------------------------
142 -- In check attribute casing mode (-gnatya), attribute names must be
143 -- mixed case, i.e. start with an upper case letter, and otherwise
144 -- lower case, except after an underline character.
146 procedure Check_Attribute_Name (Reserved : Boolean) is
147 pragma Warnings (Off, Reserved);
148 begin
149 if Style_Check_Attribute_Casing then
150 if Determine_Token_Casing /= Mixed_Case then
151 Error_Msg_SC -- CODEFIX
152 ("(style) bad capitalization, mixed case required");
153 end if;
154 end if;
155 end Check_Attribute_Name;
157 ---------------------------
158 -- Check_Binary_Operator --
159 ---------------------------
161 -- In check token mode (-gnatyt), binary operators other than the special
162 -- case of exponentiation require surrounding space characters.
164 procedure Check_Binary_Operator is
165 begin
166 if Style_Check_Tokens then
167 Require_Preceding_Space;
168 Require_Following_Space;
169 end if;
170 end Check_Binary_Operator;
172 ----------------------------
173 -- Check_Boolean_Operator --
174 ----------------------------
176 procedure Check_Boolean_Operator (Node : Node_Id) is
178 function OK_Boolean_Operand (N : Node_Id) return Boolean;
179 -- Returns True for simple variable, or "not X1" or "X1 and X2" or
180 -- "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
182 ------------------------
183 -- OK_Boolean_Operand --
184 ------------------------
186 function OK_Boolean_Operand (N : Node_Id) return Boolean is
187 begin
188 if Nkind_In (N, N_Identifier, N_Expanded_Name) then
189 return True;
191 elsif Nkind (N) = N_Op_Not then
192 return OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
194 elsif Nkind_In (N, N_Op_And, N_Op_Or) then
195 return OK_Boolean_Operand (Original_Node (Left_Opnd (N)))
196 and then
197 OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
199 else
200 return False;
201 end if;
202 end OK_Boolean_Operand;
204 -- Start of processig for Check_Boolean_Operator
206 begin
207 if Style_Check_Boolean_And_Or
208 and then Comes_From_Source (Node)
209 then
210 declare
211 Orig : constant Node_Id := Original_Node (Node);
213 begin
214 if Nkind_In (Orig, N_Op_And, N_Op_Or) then
215 declare
216 L : constant Node_Id := Original_Node (Left_Opnd (Orig));
217 R : constant Node_Id := Original_Node (Right_Opnd (Orig));
219 begin
220 -- First OK case, simple boolean constants/identifiers
222 if OK_Boolean_Operand (L)
223 and then
224 OK_Boolean_Operand (R)
225 then
226 return;
228 -- Second OK case, modular types
230 elsif Is_Modular_Integer_Type (Etype (Node)) then
231 return;
233 -- Third OK case, array types
235 elsif Is_Array_Type (Etype (Node)) then
236 return;
238 -- Otherwise we have an error
240 elsif Nkind (Orig) = N_Op_And then
241 Error_Msg -- CODEFIX
242 ("(style) `AND THEN` required", Sloc (Orig));
243 else
244 Error_Msg -- CODEFIX
245 ("(style) `OR ELSE` required", Sloc (Orig));
246 end if;
247 end;
248 end if;
249 end;
250 end if;
251 end Check_Boolean_Operator;
253 ---------------
254 -- Check_Box --
255 ---------------
257 -- In check token mode (-gnatyt), box must be preceded by a space or by
258 -- a left parenthesis. Spacing checking on the surrounding tokens takes
259 -- care of the remaining checks.
261 procedure Check_Box is
262 begin
263 if Style_Check_Tokens then
264 if Prev_Token /= Tok_Left_Paren then
265 Require_Preceding_Space;
266 end if;
267 end if;
268 end Check_Box;
270 -----------------
271 -- Check_Colon --
272 -----------------
274 -- In check token mode (-gnatyt), colon must be surrounded by spaces
276 procedure Check_Colon is
277 begin
278 if Style_Check_Tokens then
279 Require_Preceding_Space;
280 Require_Following_Space;
281 end if;
282 end Check_Colon;
284 -----------------------
285 -- Check_Colon_Equal --
286 -----------------------
288 -- In check token mode (-gnatyt), := must be surrounded by spaces
290 procedure Check_Colon_Equal is
291 begin
292 if Style_Check_Tokens then
293 Require_Preceding_Space;
294 Require_Following_Space;
295 end if;
296 end Check_Colon_Equal;
298 -----------------
299 -- Check_Comma --
300 -----------------
302 -- In check token mode (-gnatyt), comma must be either the first
303 -- token on a line, or be preceded by a non-blank character.
304 -- It must also always be followed by a blank.
306 procedure Check_Comma is
307 begin
308 if Style_Check_Tokens then
309 Check_No_Space_Before;
311 if Source (Scan_Ptr) > ' ' then
312 Error_Space_Required (Scan_Ptr);
313 end if;
314 end if;
315 end Check_Comma;
317 -------------------
318 -- Check_Comment --
319 -------------------
321 -- In check comment mode (-gnatyc) there are several requirements on the
322 -- format of comments. The following are permissible comment formats:
324 -- 1. Any comment that is not at the start of a line, i.e. where the
325 -- initial minuses are not the first non-blank characters on the
326 -- line must have at least one blank after the second minus or a
327 -- special character as defined in rule 5.
329 -- 2. A row of all minuses of any length is permitted (see procedure
330 -- box above in the source of this routine).
332 -- 3. A comment line starting with two minuses and a space, and ending
333 -- with a space and two minuses. Again see the procedure title box
334 -- immediately above in the source.
336 -- 4. A full line comment where two spaces follow the two minus signs.
337 -- This is the normal comment format in GNAT style, as typified by
338 -- the comments you are reading now.
340 -- 5. A full line comment where the first character after the second
341 -- minus is a special character, i.e. a character in the ASCII
342 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
343 -- comments, such as those generated by gnatprep, or those that
344 -- appear in the SPARK annotation language to be accepted.
346 -- Note: for GNAT internal files (-gnatg switch set on for the
347 -- compilation), the only special sequence recognized and allowed
348 -- is --! as generated by gnatprep.
350 -- 6. In addition, the comment must be properly indented if comment
351 -- indentation checking is active (Style_Check_Indentation non-zero).
352 -- Either the start column must be a multiple of this indentation,
353 -- or the indentation must match that of the next non-blank line.
355 procedure Check_Comment is
356 S : Source_Ptr;
357 C : Character;
359 function Is_Box_Comment return Boolean;
360 -- Returns True if the last two characters on the line are -- which
361 -- characterizes a box comment (as for example follows this spec).
363 function Is_Special_Character (C : Character) return Boolean;
364 -- Determines if C is a special character (see rule 5 above)
366 function Same_Column_As_Next_Non_Blank_Line return Boolean;
367 -- Called for a full line comment. If the indentation of this comment
368 -- matches that of the next non-blank line in the source, then True is
369 -- returned, otherwise False.
371 --------------------
372 -- Is_Box_Comment --
373 --------------------
375 function Is_Box_Comment return Boolean is
376 S : Source_Ptr;
378 begin
379 -- Do we need to worry about UTF_32 line terminators here ???
381 S := Scan_Ptr + 3;
382 while Source (S) not in Line_Terminator loop
383 S := S + 1;
384 end loop;
386 return Source (S - 1) = '-' and then Source (S - 2) = '-';
387 end Is_Box_Comment;
389 --------------------------
390 -- Is_Special_Character --
391 --------------------------
393 function Is_Special_Character (C : Character) return Boolean is
394 begin
395 if GNAT_Mode then
396 return C = '!';
397 else
398 return
399 Character'Pos (C) in 16#21# .. 16#2F#
400 or else
401 Character'Pos (C) in 16#3A# .. 16#3F#;
402 end if;
403 end Is_Special_Character;
405 ----------------------------------------
406 -- Same_Column_As_Next_Non_Blank_Line --
407 ----------------------------------------
409 function Same_Column_As_Next_Non_Blank_Line return Boolean is
410 P : Source_Ptr;
412 begin
413 -- Step to end of line
415 P := Scan_Ptr + 2;
416 while Source (P) not in Line_Terminator loop
417 P := P + 1;
418 end loop;
420 -- Step past blanks, and line terminators (UTF_32 case???)
422 while Source (P) <= ' ' and then Source (P) /= EOF loop
423 P := P + 1;
424 end loop;
426 -- Compare columns
428 return Get_Column_Number (Scan_Ptr) = Get_Column_Number (P);
429 end Same_Column_As_Next_Non_Blank_Line;
431 -- Start of processing for Check_Comment
433 begin
434 -- Can never have a non-blank character preceding the first minus
436 if Style_Check_Comments then
437 if Scan_Ptr > Source_First (Current_Source_File)
438 and then Source (Scan_Ptr - 1) > ' '
439 then
440 Error_Msg_S -- CODEFIX
441 ("(style) space required");
442 end if;
443 end if;
445 -- For a comment that is not at the start of the line, the only
446 -- requirement is that we cannot have a non-blank character after
447 -- the second minus sign or a special character.
449 if Scan_Ptr /= First_Non_Blank_Location then
450 if Style_Check_Comments then
451 if Source (Scan_Ptr + 2) > ' '
452 and then not Is_Special_Character (Source (Scan_Ptr + 2))
453 then
454 Error_Msg -- CODEFIX
455 ("(style) space required", Scan_Ptr + 2);
456 end if;
457 end if;
459 return;
461 -- Case of a comment that is at the start of a line
463 else
464 -- First check, must be in appropriately indented column
466 if Style_Check_Indentation /= 0 then
467 if Start_Column rem Style_Check_Indentation /= 0 then
468 if not Same_Column_As_Next_Non_Blank_Line then
469 Error_Msg_S -- CODEFIX
470 ("(style) bad column");
471 end if;
473 return;
474 end if;
475 end if;
477 -- If we are not checking comments, nothing more to do
479 if not Style_Check_Comments then
480 return;
481 end if;
483 -- Case of not followed by a blank. Usually wrong, but there are
484 -- some exceptions that we permit.
486 if Source (Scan_Ptr + 2) /= ' ' then
487 C := Source (Scan_Ptr + 2);
489 -- Case of -- all on its own on a line is OK
491 if C < ' ' then
492 return;
493 end if;
495 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
496 -- This is not permitted in internal GNAT implementation units
497 -- except for the case of --! as used by gnatprep output.
499 if Is_Special_Character (C) then
500 return;
501 end if;
503 -- The only other case in which we allow a character after
504 -- the -- other than a space is when we have a row of minus
505 -- signs (case of header lines for a box comment for example).
507 S := Scan_Ptr + 2;
508 while Source (S) >= ' ' loop
509 if Source (S) /= '-' then
510 if Is_Box_Comment then
511 Error_Space_Required (Scan_Ptr + 2);
512 else
513 Error_Msg -- CODEFIX
514 ("(style) two spaces required", Scan_Ptr + 2);
515 end if;
517 return;
518 end if;
520 S := S + 1;
521 end loop;
523 -- If we are followed by a blank, then the comment is OK if the
524 -- character following this blank is another blank or a format
525 -- effector.
527 elsif Source (Scan_Ptr + 3) <= ' ' then
528 return;
530 -- Here is the case where we only have one blank after the two
531 -- minus signs, which is an error unless the line ends with two
532 -- minus signs, the case of a box comment.
534 elsif not Is_Box_Comment then
535 Error_Space_Required (Scan_Ptr + 3);
536 end if;
537 end if;
538 end Check_Comment;
540 -------------------
541 -- Check_Dot_Dot --
542 -------------------
544 -- In check token mode (-gnatyt), colon must be surrounded by spaces
546 procedure Check_Dot_Dot is
547 begin
548 if Style_Check_Tokens then
549 Require_Preceding_Space;
550 Require_Following_Space;
551 end if;
552 end Check_Dot_Dot;
554 ---------------
555 -- Check_EOF --
556 ---------------
558 -- In check blanks at end mode, check no blank lines precede the EOF
560 procedure Check_EOF is
561 begin
562 if Style_Check_Blank_Lines then
564 -- We expect one blank line, from the EOF, but no more than one
566 if Blank_Lines = 2 then
567 Error_Msg -- CODEFIX
568 ("(style) blank line not allowed at end of file",
569 Blank_Line_Location);
571 elsif Blank_Lines >= 3 then
572 Error_Msg -- CODEFIX
573 ("(style) blank lines not allowed at end of file",
574 Blank_Line_Location);
575 end if;
576 end if;
577 end Check_EOF;
579 -----------------------------------
580 -- Check_Exponentiation_Operator --
581 -----------------------------------
583 -- No spaces are required for the ** operator in GNAT style check mode
585 procedure Check_Exponentiation_Operator is
586 begin
587 null;
588 end Check_Exponentiation_Operator;
590 --------------
591 -- Check_HT --
592 --------------
594 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
596 procedure Check_HT is
597 begin
598 if Style_Check_Horizontal_Tabs then
599 Error_Msg_S -- CODEFIX
600 ("(style) horizontal tab not allowed");
601 end if;
602 end Check_HT;
604 -----------------------
605 -- Check_Indentation --
606 -----------------------
608 -- In check indentation mode (-gnatyn for n a digit), a new statement or
609 -- declaration is required to start in a column that is a multiple of the
610 -- indentation amount.
612 procedure Check_Indentation is
613 begin
614 if Style_Check_Indentation /= 0 then
615 if Token_Ptr = First_Non_Blank_Location
616 and then Start_Column rem Style_Check_Indentation /= 0
617 then
618 Error_Msg_SC -- CODEFIX
619 ("(style) bad indentation");
620 end if;
621 end if;
622 end Check_Indentation;
624 ----------------------
625 -- Check_Left_Paren --
626 ----------------------
628 -- In tone check mode (-gnatyt), left paren must not be preceded by an
629 -- identifier character or digit (a separating space is required) and
630 -- may never be followed by a space.
632 procedure Check_Left_Paren is
633 begin
634 if Style_Check_Tokens then
635 if Token_Ptr > Source_First (Current_Source_File)
636 and then Identifier_Char (Source (Token_Ptr - 1))
637 then
638 Error_Space_Required (Token_Ptr);
639 end if;
641 Check_No_Space_After;
642 end if;
643 end Check_Left_Paren;
645 ---------------------------
646 -- Check_Line_Max_Length --
647 ---------------------------
649 -- In check max line length mode (-gnatym), the line length must
650 -- not exceed the permitted maximum value.
652 procedure Check_Line_Max_Length (Len : Int) is
653 begin
654 if Style_Check_Max_Line_Length then
655 if Len > Style_Max_Line_Length then
656 Error_Msg
657 ("(style) this line is too long",
658 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
659 end if;
660 end if;
661 end Check_Line_Max_Length;
663 ---------------------------
664 -- Check_Line_Terminator --
665 ---------------------------
667 -- In check blanks at end mode (-gnatyb), lines may not end with a
668 -- trailing space.
670 -- In check form feeds mode (-gnatyf), the line terminator may not
671 -- be either of the characters FF or VT.
673 -- In check DOS line terminators node (-gnatyd), the line terminator
674 -- must be a single LF, without a following CR.
676 procedure Check_Line_Terminator (Len : Int) is
677 S : Source_Ptr;
679 L : Int := Len;
680 -- Length of line (adjusted down for blanks at end of line)
682 begin
683 -- Reset count of blank lines if first line
685 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
686 Blank_Lines := 0;
687 end if;
689 -- Check FF/VT terminators
691 if Style_Check_Form_Feeds then
692 if Source (Scan_Ptr) = ASCII.FF then
693 Error_Msg_S -- CODEFIX
694 ("(style) form feed not allowed");
695 elsif Source (Scan_Ptr) = ASCII.VT then
696 Error_Msg_S -- CODEFIX
697 ("(style) vertical tab not allowed");
698 end if;
699 end if;
701 -- Check DOS line terminator
703 if Style_Check_DOS_Line_Terminator then
705 -- Ignore EOF, since we only get called with an EOF if it is the last
706 -- character in the buffer (and was therefore not in the source file),
707 -- since the terminating EOF is added to stop the scan.
709 if Source (Scan_Ptr) = EOF then
710 null;
712 -- Bad terminator if we don't have an LF
714 elsif Source (Scan_Ptr) /= LF then
715 Error_Msg_S ("(style) incorrect line terminator");
716 end if;
717 end if;
719 -- Remove trailing spaces
721 S := Scan_Ptr;
722 while L > 0 and then Is_White_Space (Source (S - 1)) loop
723 S := S - 1;
724 L := L - 1;
725 end loop;
727 -- Issue message for blanks at end of line if option enabled
729 if Style_Check_Blanks_At_End and then L < Len then
730 Error_Msg -- CODEFIX
731 ("(style) trailing spaces not permitted", S);
732 end if;
734 -- Deal with empty (blank) line
736 if L = 0 then
738 -- Increment blank line count
740 Blank_Lines := Blank_Lines + 1;
742 -- If first blank line, record location for later error message
744 if Blank_Lines = 1 then
745 Blank_Line_Location := Scan_Ptr;
746 end if;
748 -- Non-blank line, check for previous multiple blank lines
750 else
751 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
752 Error_Msg -- CODEFIX
753 ("(style) multiple blank lines", Blank_Line_Location);
754 end if;
756 -- And reset blank line count
758 Blank_Lines := 0;
759 end if;
760 end Check_Line_Terminator;
762 --------------------------
763 -- Check_No_Space_After --
764 --------------------------
766 procedure Check_No_Space_After is
767 S : Source_Ptr;
769 begin
770 if Is_White_Space (Source (Scan_Ptr)) then
772 -- Allow one or more spaces if followed by comment
774 S := Scan_Ptr + 1;
775 loop
776 if Source (S) = '-' and then Source (S + 1) = '-' then
777 return;
779 elsif Is_White_Space (Source (S)) then
780 S := S + 1;
782 else
783 exit;
784 end if;
785 end loop;
787 Error_Space_Not_Allowed (Scan_Ptr);
788 end if;
789 end Check_No_Space_After;
791 ---------------------------
792 -- Check_No_Space_Before --
793 ---------------------------
795 procedure Check_No_Space_Before is
796 begin
797 if Token_Ptr > First_Non_Blank_Location
798 and then Source (Token_Ptr - 1) <= ' '
799 then
800 Error_Space_Not_Allowed (Token_Ptr - 1);
801 end if;
802 end Check_No_Space_Before;
804 -----------------------
805 -- Check_Pragma_Name --
806 -----------------------
808 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
809 -- case, i.e. start with an upper case letter, and otherwise lower case,
810 -- except after an underline character.
812 procedure Check_Pragma_Name is
813 begin
814 if Style_Check_Pragma_Casing then
815 if Determine_Token_Casing /= Mixed_Case then
816 Error_Msg_SC -- CODEFIX
817 ("(style) bad capitalization, mixed case required");
818 end if;
819 end if;
820 end Check_Pragma_Name;
822 -----------------------
823 -- Check_Right_Paren --
824 -----------------------
826 -- In check tokens mode (-gnatyt), right paren must not be immediately
827 -- followed by an identifier character, and must never be preceded by
828 -- a space unless it is the initial non-blank character on the line.
830 procedure Check_Right_Paren is
831 begin
832 if Style_Check_Tokens then
833 if Identifier_Char (Source (Token_Ptr + 1)) then
834 Error_Space_Required (Token_Ptr + 1);
835 end if;
837 Check_No_Space_Before;
838 end if;
839 end Check_Right_Paren;
841 ---------------------
842 -- Check_Semicolon --
843 ---------------------
845 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
846 -- space and a following space is required.
848 procedure Check_Semicolon is
849 begin
850 if Style_Check_Tokens then
851 Check_No_Space_Before;
853 if Source (Scan_Ptr) > ' ' then
854 Error_Space_Required (Scan_Ptr);
855 end if;
856 end if;
857 end Check_Semicolon;
859 -------------------------------
860 -- Check_Separate_Stmt_Lines --
861 -------------------------------
863 procedure Check_Separate_Stmt_Lines is
864 begin
865 if Style_Check_Separate_Stmt_Lines then
866 Check_Separate_Stmt_Lines_Cont;
867 end if;
868 end Check_Separate_Stmt_Lines;
870 ------------------------------------
871 -- Check_Separate_Stmt_Lines_Cont --
872 ------------------------------------
874 procedure Check_Separate_Stmt_Lines_Cont is
875 S : Source_Ptr;
877 begin
878 -- Skip past white space
880 S := Scan_Ptr;
881 while Is_White_Space (Source (S)) loop
882 S := S + 1;
883 end loop;
885 -- Line terminator is OK
887 if Source (S) in Line_Terminator then
888 return;
890 -- Comment is OK
892 elsif Source (S) = '-' and then Source (S + 1) = '-' then
893 return;
895 -- ABORT keyword is OK after THEN (THEN ABORT case)
897 elsif Token = Tok_Then
898 and then (Source (S + 0) = 'a' or else Source (S + 0) = 'A')
899 and then (Source (S + 1) = 'b' or else Source (S + 1) = 'B')
900 and then (Source (S + 2) = 'o' or else Source (S + 2) = 'O')
901 and then (Source (S + 3) = 'r' or else Source (S + 3) = 'R')
902 and then (Source (S + 4) = 't' or else Source (S + 4) = 'T')
903 and then (Source (S + 5) in Line_Terminator
904 or else Is_White_Space (Source (S + 5)))
905 then
906 return;
908 -- PRAGMA keyword is OK after ELSE
910 elsif Token = Tok_Else
911 and then (Source (S + 0) = 'p' or else Source (S + 0) = 'P')
912 and then (Source (S + 1) = 'r' or else Source (S + 1) = 'R')
913 and then (Source (S + 2) = 'a' or else Source (S + 2) = 'A')
914 and then (Source (S + 3) = 'g' or else Source (S + 3) = 'G')
915 and then (Source (S + 4) = 'm' or else Source (S + 4) = 'M')
916 and then (Source (S + 5) = 'a' or else Source (S + 5) = 'A')
917 and then (Source (S + 6) in Line_Terminator
918 or else Is_White_Space (Source (S + 6)))
919 then
920 return;
922 -- Otherwise we have the style violation we are looking for
924 else
925 if Token = Tok_Then then
926 Error_Msg -- CODEFIX
927 ("(style) no statements may follow THEN on same line", S);
928 else
929 Error_Msg
930 ("(style) no statements may follow ELSE on same line", S);
931 end if;
932 end if;
933 end Check_Separate_Stmt_Lines_Cont;
935 ----------------
936 -- Check_Then --
937 ----------------
939 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
940 -- to appear either on the same line as the IF, or on a separate line
941 -- after multiple conditions. In any case, it may not appear on the
942 -- line immediately following the line with the IF.
944 procedure Check_Then (If_Loc : Source_Ptr) is
945 begin
946 if Style_Check_If_Then_Layout then
947 if Get_Physical_Line_Number (Token_Ptr) =
948 Get_Physical_Line_Number (If_Loc) + 1
949 then
950 Error_Msg_SC ("(style) misplaced THEN");
951 end if;
952 end if;
953 end Check_Then;
955 -------------------------------
956 -- Check_Unary_Plus_Or_Minus --
957 -------------------------------
959 -- In check token mode (-gnatyt), unary plus or minus must not be
960 -- followed by a space.
962 procedure Check_Unary_Plus_Or_Minus is
963 begin
964 if Style_Check_Tokens then
965 Check_No_Space_After;
966 end if;
967 end Check_Unary_Plus_Or_Minus;
969 ------------------------
970 -- Check_Vertical_Bar --
971 ------------------------
973 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
975 procedure Check_Vertical_Bar is
976 begin
977 if Style_Check_Tokens then
978 Require_Preceding_Space;
979 Require_Following_Space;
980 end if;
981 end Check_Vertical_Bar;
983 -----------------------
984 -- Check_Xtra_Parens --
985 -----------------------
987 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
988 begin
989 if Style_Check_Xtra_Parens then
990 Error_Msg -- CODEFIX
991 ("redundant parentheses?", Loc);
992 end if;
993 end Check_Xtra_Parens;
995 ----------------------------
996 -- Determine_Token_Casing --
997 ----------------------------
999 function Determine_Token_Casing return Casing_Type is
1000 begin
1001 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
1002 end Determine_Token_Casing;
1004 -----------------------------
1005 -- Error_Space_Not_Allowed --
1006 -----------------------------
1008 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
1009 begin
1010 Error_Msg -- CODEFIX
1011 ("(style) space not allowed", S);
1012 end Error_Space_Not_Allowed;
1014 --------------------------
1015 -- Error_Space_Required --
1016 --------------------------
1018 procedure Error_Space_Required (S : Source_Ptr) is
1019 begin
1020 Error_Msg -- CODEFIX
1021 ("(style) space required", S);
1022 end Error_Space_Required;
1024 --------------------
1025 -- Is_White_Space --
1026 --------------------
1028 function Is_White_Space (C : Character) return Boolean is
1029 begin
1030 return C = ' ' or else C = HT;
1031 end Is_White_Space;
1033 -------------------
1034 -- Mode_In_Check --
1035 -------------------
1037 function Mode_In_Check return Boolean is
1038 begin
1039 return Style_Check and Style_Check_Mode_In;
1040 end Mode_In_Check;
1042 -----------------
1043 -- No_End_Name --
1044 -----------------
1046 -- In check end/exit labels mode (-gnatye), always require the name of
1047 -- a subprogram or package to be present on the END, so this is an error.
1049 procedure No_End_Name (Name : Node_Id) is
1050 begin
1051 if Style_Check_End_Labels then
1052 Error_Msg_Node_1 := Name;
1053 Error_Msg_SP -- CODEFIX
1054 ("(style) `END &` required");
1055 end if;
1056 end No_End_Name;
1058 ------------------
1059 -- No_Exit_Name --
1060 ------------------
1062 -- In check end/exit labels mode (-gnatye), always require the name of
1063 -- the loop to be present on the EXIT when exiting a named loop.
1065 procedure No_Exit_Name (Name : Node_Id) is
1066 begin
1067 if Style_Check_End_Labels then
1068 Error_Msg_Node_1 := Name;
1069 Error_Msg_SP -- CODEFIX
1070 ("(style) `EXIT &` required");
1071 end if;
1072 end No_Exit_Name;
1074 ----------------------------
1075 -- Non_Lower_Case_Keyword --
1076 ----------------------------
1078 -- In check casing mode (-gnatyk), reserved keywords must be spelled
1079 -- in all lower case (excluding keywords range, access, delta and digits
1080 -- used as attribute designators).
1082 procedure Non_Lower_Case_Keyword is
1083 begin
1084 if Style_Check_Keyword_Casing then
1085 Error_Msg_SC -- CODEFIX
1086 ("(style) reserved words must be all lower case");
1087 end if;
1088 end Non_Lower_Case_Keyword;
1090 -----------------------------
1091 -- Require_Following_Space --
1092 -----------------------------
1094 procedure Require_Following_Space is
1095 begin
1096 if Source (Scan_Ptr) > ' ' then
1097 Error_Space_Required (Scan_Ptr);
1098 end if;
1099 end Require_Following_Space;
1101 -----------------------------
1102 -- Require_Preceding_Space --
1103 -----------------------------
1105 procedure Require_Preceding_Space is
1106 begin
1107 if Token_Ptr > Source_First (Current_Source_File)
1108 and then Source (Token_Ptr - 1) > ' '
1109 then
1110 Error_Space_Required (Token_Ptr);
1111 end if;
1112 end Require_Preceding_Space;
1114 end Styleg;