Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / styleg.adb
blob91c807b1a0707728a66df2db76430a4fd346d922
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-2004 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- This version of the Style package implements the standard GNAT style
28 -- checking rules. For documentation of these rules, see comments on the
29 -- individual procedures.
31 with Casing; use Casing;
32 with Csets; use Csets;
33 with Err_Vars; use Err_Vars;
34 with Opt; use Opt;
35 with Scans; use Scans;
36 with Sinput; use Sinput;
37 with Stylesw; use Stylesw;
39 package body Styleg is
41 use ASCII;
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 procedure Check_No_Space_After;
48 -- Checks that there is a non-white space character after the current
49 -- token, or white space followed by a comment, or the end of line.
50 -- Issue error message if not.
52 procedure Check_No_Space_Before;
53 -- Check that token is first token on line, or else is not preceded
54 -- by white space. Signal error of space not allowed if not.
56 function Determine_Token_Casing return Casing_Type;
58 procedure Error_Space_Not_Allowed (S : Source_Ptr);
59 -- Posts an error message indicating that a space is not allowed
60 -- at the given source location.
62 procedure Error_Space_Required (S : Source_Ptr);
63 -- Posts an error message indicating that a space is required at
64 -- the given source location.
66 function Is_White_Space (C : Character) return Boolean;
67 pragma Inline (Is_White_Space);
68 -- Returns True for space, HT, VT or FF, False otherwise
70 procedure Require_Following_Space;
71 pragma Inline (Require_Following_Space);
72 -- Require token to be followed by white space. Used only if in GNAT
73 -- style checking mode.
75 procedure Require_Preceding_Space;
76 pragma Inline (Require_Preceding_Space);
77 -- Require token to be preceded by white space. Used only if in GNAT
78 -- style checking mode.
80 ----------------------
81 -- Check_Abs_Or_Not --
82 ----------------------
84 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
86 procedure Check_Abs_Not is
87 begin
88 if Style_Check_Tokens then
89 if Source (Scan_Ptr) > ' ' then
90 Error_Space_Required (Scan_Ptr);
91 end if;
92 end if;
93 end Check_Abs_Not;
95 ----------------------
96 -- Check_Apostrophe --
97 ----------------------
99 -- Do not allow space before or after apostrophe
101 procedure Check_Apostrophe is
102 begin
103 if Style_Check_Tokens then
104 Check_No_Space_After;
105 end if;
106 end Check_Apostrophe;
108 -----------------
109 -- Check_Arrow --
110 -----------------
112 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
114 procedure Check_Arrow is
115 begin
116 if Style_Check_Tokens then
117 Require_Preceding_Space;
118 Require_Following_Space;
119 end if;
120 end Check_Arrow;
122 --------------------------
123 -- Check_Attribute_Name --
124 --------------------------
126 -- In check attribute casing mode (-gnatya), attribute names must be
127 -- mixed case, i.e. start with an upper case letter, and otherwise
128 -- lower case, except after an underline character.
130 procedure Check_Attribute_Name (Reserved : Boolean) is
131 pragma Warnings (Off, Reserved);
133 begin
134 if Style_Check_Attribute_Casing then
135 if Determine_Token_Casing /= Mixed_Case then
136 Error_Msg_SC ("(style) bad capitalization, mixed case required");
137 end if;
138 end if;
139 end Check_Attribute_Name;
141 ---------------------------
142 -- Check_Binary_Operator --
143 ---------------------------
145 -- In check token mode (-gnatyt), binary operators other than the special
146 -- case of exponentiation require surrounding space characters.
148 procedure Check_Binary_Operator is
149 begin
150 if Style_Check_Tokens then
151 Require_Preceding_Space;
152 Require_Following_Space;
153 end if;
154 end Check_Binary_Operator;
156 ---------------
157 -- Check_Box --
158 ---------------
160 -- In check token mode (-gnatyt), box must be preceded by a space or by
161 -- a left parenthesis. Spacing checking on the surrounding tokens takes
162 -- care of the remaining checks.
164 procedure Check_Box is
165 begin
166 if Style_Check_Tokens then
167 if Prev_Token /= Tok_Left_Paren then
168 Require_Preceding_Space;
169 end if;
170 end if;
171 end Check_Box;
173 -----------------
174 -- Check_Colon --
175 -----------------
177 -- In check token mode (-gnatyt), colon must be surrounded by spaces
179 procedure Check_Colon is
180 begin
181 if Style_Check_Tokens then
182 Require_Preceding_Space;
183 Require_Following_Space;
184 end if;
185 end Check_Colon;
187 -----------------------
188 -- Check_Colon_Equal --
189 -----------------------
191 -- In check token mode (-gnatyt), := must be surrounded by spaces
193 procedure Check_Colon_Equal is
194 begin
195 if Style_Check_Tokens then
196 Require_Preceding_Space;
197 Require_Following_Space;
198 end if;
199 end Check_Colon_Equal;
201 -----------------
202 -- Check_Comma --
203 -----------------
205 -- In check token mode (-gnatyt), comma must be either the first
206 -- token on a line, or be preceded by a non-blank character.
207 -- It must also always be followed by a blank.
209 procedure Check_Comma is
210 begin
211 if Style_Check_Tokens then
212 Check_No_Space_Before;
214 if Source (Scan_Ptr) > ' ' then
215 Error_Space_Required (Scan_Ptr);
216 end if;
217 end if;
218 end Check_Comma;
220 -------------------
221 -- Check_Comment --
222 -------------------
224 -- In check comment mode (-gnatyc) there are several requirements on the
225 -- format of comments. The following are permissible comment formats:
227 -- 1. Any comment that is not at the start of a line, i.e. where the
228 -- initial minuses are not the first non-blank characters on the
229 -- line must have at least one blank after the second minus.
231 -- 2. A row of all minuses of any length is permitted (see procedure
232 -- box above in the source of this routine).
234 -- 3. A comment line starting with two minuses and a space, and ending
235 -- with a space and two minuses. Again see the procedure title box
236 -- immediately above in the source.
238 -- 4. A full line comment where two spaces follow the two minus signs.
239 -- This is the normal comment format in GNAT style, as typified by
240 -- the comments you are reading now.
242 -- 5. A full line comment where the first character after the second
243 -- minus is a special character, i.e. a character in the ASCII
244 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
245 -- comments, such as those generated by gnatprep, or those that
246 -- appear in the SPARK annotation language to be accepted.
248 -- Note: for GNAT internal files (-gnatg switch set on for the
249 -- compilation), the only special sequence recognized and allowed
250 -- is --! as generated by gnatprep.
252 procedure Check_Comment is
253 S : Source_Ptr;
254 C : Character;
256 function Is_Box_Comment return Boolean;
257 -- Returns True if the last two characters on the line are -- which
258 -- characterizes a box comment (as for example follows this spec).
260 --------------------
261 -- Is_Box_Comment --
262 --------------------
264 function Is_Box_Comment return Boolean is
265 S : Source_Ptr;
267 begin
268 S := Scan_Ptr + 3;
269 while Source (S) not in Line_Terminator loop
270 S := S + 1;
271 end loop;
273 return Source (S - 1) = '-' and then Source (S - 2) = '-';
274 end Is_Box_Comment;
276 -- Start of processing for Check_Comment
278 begin
279 -- Can never have a non-blank character preceding the first minus
281 if Style_Check_Comments then
282 if Scan_Ptr > Source_First (Current_Source_File)
283 and then Source (Scan_Ptr - 1) > ' '
284 then
285 Error_Msg_S ("(style) space required");
286 end if;
287 end if;
289 -- For a comment that is not at the start of the line, the only
290 -- requirement is that we cannot have a non-blank character after
291 -- the second minus sign.
293 if Scan_Ptr /= First_Non_Blank_Location then
294 if Style_Check_Comments then
295 if Source (Scan_Ptr + 2) > ' ' then
296 Error_Msg ("(style) space required", Scan_Ptr + 2);
297 end if;
298 end if;
300 return;
302 -- Case of a comment that is at the start of a line
304 else
305 -- First check, must be in appropriately indented column
307 if Style_Check_Indentation /= 0 then
308 if Start_Column rem Style_Check_Indentation /= 0 then
309 Error_Msg_S ("(style) bad column");
310 return;
311 end if;
312 end if;
314 -- If we are not checking comments, nothing to do
316 if not Style_Check_Comments then
317 return;
318 end if;
320 -- Case of not followed by a blank. Usually wrong, but there are
321 -- some exceptions that we permit.
323 if Source (Scan_Ptr + 2) /= ' ' then
324 C := Source (Scan_Ptr + 2);
326 -- Case of -- all on its own on a line is OK
328 if C < ' ' then
329 return;
330 end if;
332 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
333 -- This is not permitted in internal GNAT implementation units
334 -- except for the case of --! as used by gnatprep output.
336 if GNAT_Mode then
337 if C = '!' then
338 return;
339 end if;
341 else
342 if Character'Pos (C) in 16#21# .. 16#2F#
343 or else
344 Character'Pos (C) in 16#3A# .. 16#3F#
345 then
346 return;
347 end if;
348 end if;
350 -- The only other case in which we allow a character after
351 -- the -- other than a space is when we have a row of minus
352 -- signs (case of header lines for a box comment for example).
354 S := Scan_Ptr + 2;
355 while Source (S) >= ' ' loop
356 if Source (S) /= '-' then
357 if Is_Box_Comment then
358 Error_Space_Required (Scan_Ptr + 2);
359 else
360 Error_Msg ("(style) two spaces required", Scan_Ptr + 2);
361 end if;
363 return;
364 end if;
366 S := S + 1;
367 end loop;
369 -- If we are followed by a blank, then the comment is OK if the
370 -- character following this blank is another blank or a format
371 -- effector.
373 elsif Source (Scan_Ptr + 3) <= ' ' then
374 return;
376 -- Here is the case where we only have one blank after the two
377 -- minus signs, which is an error unless the line ends with two
378 -- minus signs, the case of a box comment.
380 elsif not Is_Box_Comment then
381 Error_Space_Required (Scan_Ptr + 3);
382 end if;
383 end if;
384 end Check_Comment;
386 -------------------
387 -- Check_Dot_Dot --
388 -------------------
390 -- In check token mode (-gnatyt), colon must be surrounded by spaces
392 procedure Check_Dot_Dot is
393 begin
394 if Style_Check_Tokens then
395 Require_Preceding_Space;
396 Require_Following_Space;
397 end if;
398 end Check_Dot_Dot;
400 -----------------------------------
401 -- Check_Exponentiation_Operator --
402 -----------------------------------
404 -- No spaces are required for the ** operator in GNAT style check mode
406 procedure Check_Exponentiation_Operator is
407 begin
408 null;
409 end Check_Exponentiation_Operator;
411 --------------
412 -- Check_HT --
413 --------------
415 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
417 procedure Check_HT is
418 begin
419 if Style_Check_Horizontal_Tabs then
420 Error_Msg_S ("(style) horizontal tab not allowed");
421 end if;
422 end Check_HT;
424 -----------------------
425 -- Check_Indentation --
426 -----------------------
428 -- In check indentation mode (-gnatyn for n a digit), a new statement or
429 -- declaration is required to start in a column that is a multiple of the
430 -- indentiation amount.
432 procedure Check_Indentation is
433 begin
434 if Style_Check_Indentation /= 0 then
435 if Token_Ptr = First_Non_Blank_Location
436 and then Start_Column rem Style_Check_Indentation /= 0
437 then
438 Error_Msg_SC ("(style) bad indentation");
439 end if;
440 end if;
441 end Check_Indentation;
443 ----------------------
444 -- Check_Left_Paren --
445 ----------------------
447 -- In tone check mode (-gnatyt), left paren must not be preceded by an
448 -- identifier character or digit (a separating space is required) and
449 -- may never be followed by a space.
451 procedure Check_Left_Paren is
452 begin
453 if Style_Check_Tokens then
454 if Token_Ptr > Source_First (Current_Source_File)
455 and then Identifier_Char (Source (Token_Ptr - 1))
456 then
457 Error_Space_Required (Token_Ptr);
458 end if;
460 Check_No_Space_After;
461 end if;
462 end Check_Left_Paren;
464 ---------------------------
465 -- Check_Line_Terminator --
466 ---------------------------
468 -- In check blanks at end mode (-gnatyb), lines may not end with a
469 -- trailing space.
471 -- In check max line length mode (-gnatym), the line length must
472 -- not exceed the permitted maximum value.
474 -- In check form feeds mode (-gnatyf), the line terminator may not
475 -- be either of the characters FF or VT.
477 procedure Check_Line_Terminator (Len : Int) is
478 S : Source_Ptr;
480 begin
481 -- Check FF/VT terminators
483 if Style_Check_Form_Feeds then
484 if Source (Scan_Ptr) = ASCII.FF then
485 Error_Msg_S ("(style) form feed not allowed");
487 elsif Source (Scan_Ptr) = ASCII.VT then
488 Error_Msg_S ("(style) vertical tab not allowed");
489 end if;
490 end if;
492 -- We are now possibly going to check for trailing spaces and maximum
493 -- line length. There is no point in doing this if the current line is
494 -- empty. It is actually wrong in the case of trailing spaces, because
495 -- we scan backwards for this purpose, so we would end up looking at a
496 -- different line, or even at invalid buffer locations if we have the
497 -- first source line at hand.
499 if Len = 0 then
500 return;
501 end if;
503 -- Check trailing space
505 if Style_Check_Blanks_At_End then
506 if Scan_Ptr >= First_Non_Blank_Location then
507 if Is_White_Space (Source (Scan_Ptr - 1)) then
508 S := Scan_Ptr - 1;
510 while Is_White_Space (Source (S - 1)) loop
511 S := S - 1;
512 end loop;
514 Error_Msg ("(style) trailing spaces not permitted", S);
515 end if;
516 end if;
517 end if;
519 -- Check max line length
521 if Style_Check_Max_Line_Length then
522 if Len > Style_Max_Line_Length then
523 Error_Msg
524 ("(style) this line is too long",
525 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
526 end if;
527 end if;
529 end Check_Line_Terminator;
531 --------------------------
532 -- Check_No_Space_After --
533 --------------------------
535 procedure Check_No_Space_After is
536 S : Source_Ptr;
538 begin
539 if Is_White_Space (Source (Scan_Ptr)) then
541 -- Allow one or more spaces if followed by comment
543 S := Scan_Ptr + 1;
544 loop
545 if Source (S) = '-' and then Source (S + 1) = '-' then
546 return;
548 elsif Is_White_Space (Source (S)) then
549 S := S + 1;
551 else
552 exit;
553 end if;
554 end loop;
556 Error_Space_Not_Allowed (Scan_Ptr);
557 end if;
558 end Check_No_Space_After;
560 ---------------------------
561 -- Check_No_Space_Before --
562 ---------------------------
564 procedure Check_No_Space_Before is
565 begin
566 if Token_Ptr > First_Non_Blank_Location
567 and then Source (Token_Ptr - 1) <= ' '
568 then
569 Error_Space_Not_Allowed (Token_Ptr - 1);
570 end if;
571 end Check_No_Space_Before;
573 -----------------------
574 -- Check_Pragma_Name --
575 -----------------------
577 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
578 -- case, i.e. start with an upper case letter, and otherwise lower case,
579 -- except after an underline character.
581 procedure Check_Pragma_Name is
582 begin
583 if Style_Check_Pragma_Casing then
584 if Determine_Token_Casing /= Mixed_Case then
585 Error_Msg_SC ("(style) bad capitalization, mixed case required");
586 end if;
587 end if;
588 end Check_Pragma_Name;
590 -----------------------
591 -- Check_Right_Paren --
592 -----------------------
594 -- In check tokens mode (-gnatyt), right paren must never be preceded by
595 -- a space unless it is the initial non-blank character on the line.
597 procedure Check_Right_Paren is
598 begin
599 if Style_Check_Tokens then
600 Check_No_Space_Before;
601 end if;
602 end Check_Right_Paren;
604 ---------------------
605 -- Check_Semicolon --
606 ---------------------
608 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
609 -- space and a following space is required.
611 procedure Check_Semicolon is
612 begin
613 if Style_Check_Tokens then
614 Check_No_Space_Before;
616 if Source (Scan_Ptr) > ' ' then
617 Error_Space_Required (Scan_Ptr);
618 end if;
619 end if;
620 end Check_Semicolon;
622 ----------------
623 -- Check_Then --
624 ----------------
626 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
627 -- to appear either on the same line as the IF, or on a separate line
628 -- after multiple conditions. In any case, it may not appear on the
629 -- line immediately following the line with the IF.
631 procedure Check_Then (If_Loc : Source_Ptr) is
632 begin
633 if Style_Check_If_Then_Layout then
634 if Get_Physical_Line_Number (Token_Ptr) =
635 Get_Physical_Line_Number (If_Loc) + 1
636 then
637 Error_Msg_SC ("(style) misplaced THEN");
638 end if;
639 end if;
640 end Check_Then;
642 -------------------------------
643 -- Check_Unary_Plus_Or_Minus --
644 -------------------------------
646 -- In check tokem mode (-gnatyt), unary plus or minus must not be
647 -- followed by a space.
649 procedure Check_Unary_Plus_Or_Minus is
650 begin
651 if Style_Check_Tokens then
652 Check_No_Space_After;
653 end if;
654 end Check_Unary_Plus_Or_Minus;
656 ------------------------
657 -- Check_Vertical_Bar --
658 ------------------------
660 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
662 procedure Check_Vertical_Bar is
663 begin
664 if Style_Check_Tokens then
665 Require_Preceding_Space;
666 Require_Following_Space;
667 end if;
668 end Check_Vertical_Bar;
670 -----------------------
671 -- Check_Xtra_Parens --
672 -----------------------
674 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
675 begin
676 if Style_Check_Xtra_Parens then
677 Error_Msg ("redundant parentheses?", Loc);
678 end if;
679 end Check_Xtra_Parens;
681 ----------------------------
682 -- Determine_Token_Casing --
683 ----------------------------
685 function Determine_Token_Casing return Casing_Type is
686 begin
687 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
688 end Determine_Token_Casing;
690 -----------------------------
691 -- Error_Space_Not_Allowed --
692 -----------------------------
694 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
695 begin
696 Error_Msg ("(style) space not allowed", S);
697 end Error_Space_Not_Allowed;
699 --------------------------
700 -- Error_Space_Required --
701 --------------------------
703 procedure Error_Space_Required (S : Source_Ptr) is
704 begin
705 Error_Msg ("(style) space required", S);
706 end Error_Space_Required;
708 --------------------
709 -- Is_White_Space --
710 --------------------
712 function Is_White_Space (C : Character) return Boolean is
713 begin
714 return C = ' ' or else C = HT;
715 end Is_White_Space;
717 -----------------
718 -- No_End_Name --
719 -----------------
721 -- In check end/exit labels mode (-gnatye), always require the name of
722 -- a subprogram or package to be present on the END, so this is an error.
724 procedure No_End_Name (Name : Node_Id) is
725 begin
726 if Style_Check_End_Labels then
727 Error_Msg_Node_1 := Name;
728 Error_Msg_SP ("(style) `END &` required");
729 end if;
730 end No_End_Name;
732 ------------------
733 -- No_Exit_Name --
734 ------------------
736 -- In check end/exit labels mode (-gnatye), always require the name of
737 -- the loop to be present on the EXIT when exiting a named loop.
739 procedure No_Exit_Name (Name : Node_Id) is
740 begin
741 if Style_Check_End_Labels then
742 Error_Msg_Node_1 := Name;
743 Error_Msg_SP ("(style) `EXIT &` required");
744 end if;
745 end No_Exit_Name;
747 ----------------------------
748 -- Non_Lower_Case_Keyword --
749 ----------------------------
751 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
752 -- in all lower case (excluding keywords range, access, delta and digits
753 -- used as attribute designators).
755 procedure Non_Lower_Case_Keyword is
756 begin
757 if Style_Check_Keyword_Casing then
758 Error_Msg_SC ("(style) reserved words must be all lower case");
759 end if;
760 end Non_Lower_Case_Keyword;
762 -----------------------------
763 -- Require_Following_Space --
764 -----------------------------
766 procedure Require_Following_Space is
767 begin
768 if Source (Scan_Ptr) > ' ' then
769 Error_Space_Required (Scan_Ptr);
770 end if;
771 end Require_Following_Space;
773 -----------------------------
774 -- Require_Preceding_Space --
775 -----------------------------
777 procedure Require_Preceding_Space is
778 begin
779 if Token_Ptr > Source_First (Current_Source_File)
780 and then Source (Token_Ptr - 1) > ' '
781 then
782 Error_Space_Required (Token_Ptr);
783 end if;
784 end Require_Preceding_Space;
786 ---------------------
787 -- RM_Column_Check --
788 ---------------------
790 function RM_Column_Check return Boolean is
791 begin
792 return Style_Check and Style_Check_Layout;
793 end RM_Column_Check;
795 end Styleg;