* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / ada / styleg.adb
blobaec09dd028eb10d136792f0859d858839273c5f0
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-2005 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 -- Do we need to worry about UTF_32 line terminators here ???
270 S := Scan_Ptr + 3;
271 while Source (S) not in Line_Terminator loop
272 S := S + 1;
273 end loop;
275 return Source (S - 1) = '-' and then Source (S - 2) = '-';
276 end Is_Box_Comment;
278 -- Start of processing for Check_Comment
280 begin
281 -- Can never have a non-blank character preceding the first minus
283 if Style_Check_Comments then
284 if Scan_Ptr > Source_First (Current_Source_File)
285 and then Source (Scan_Ptr - 1) > ' '
286 then
287 Error_Msg_S ("(style) space required");
288 end if;
289 end if;
291 -- For a comment that is not at the start of the line, the only
292 -- requirement is that we cannot have a non-blank character after
293 -- the second minus sign.
295 if Scan_Ptr /= First_Non_Blank_Location then
296 if Style_Check_Comments then
297 if Source (Scan_Ptr + 2) > ' ' then
298 Error_Msg ("(style) space required", Scan_Ptr + 2);
299 end if;
300 end if;
302 return;
304 -- Case of a comment that is at the start of a line
306 else
307 -- First check, must be in appropriately indented column
309 if Style_Check_Indentation /= 0 then
310 if Start_Column rem Style_Check_Indentation /= 0 then
311 Error_Msg_S ("(style) bad column");
312 return;
313 end if;
314 end if;
316 -- If we are not checking comments, nothing to do
318 if not Style_Check_Comments then
319 return;
320 end if;
322 -- Case of not followed by a blank. Usually wrong, but there are
323 -- some exceptions that we permit.
325 if Source (Scan_Ptr + 2) /= ' ' then
326 C := Source (Scan_Ptr + 2);
328 -- Case of -- all on its own on a line is OK
330 if C < ' ' then
331 return;
332 end if;
334 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
335 -- This is not permitted in internal GNAT implementation units
336 -- except for the case of --! as used by gnatprep output.
338 if GNAT_Mode then
339 if C = '!' then
340 return;
341 end if;
343 else
344 if Character'Pos (C) in 16#21# .. 16#2F#
345 or else
346 Character'Pos (C) in 16#3A# .. 16#3F#
347 then
348 return;
349 end if;
350 end if;
352 -- The only other case in which we allow a character after
353 -- the -- other than a space is when we have a row of minus
354 -- signs (case of header lines for a box comment for example).
356 S := Scan_Ptr + 2;
357 while Source (S) >= ' ' loop
358 if Source (S) /= '-' then
359 if Is_Box_Comment then
360 Error_Space_Required (Scan_Ptr + 2);
361 else
362 Error_Msg ("(style) two spaces required", Scan_Ptr + 2);
363 end if;
365 return;
366 end if;
368 S := S + 1;
369 end loop;
371 -- If we are followed by a blank, then the comment is OK if the
372 -- character following this blank is another blank or a format
373 -- effector.
375 elsif Source (Scan_Ptr + 3) <= ' ' then
376 return;
378 -- Here is the case where we only have one blank after the two
379 -- minus signs, which is an error unless the line ends with two
380 -- minus signs, the case of a box comment.
382 elsif not Is_Box_Comment then
383 Error_Space_Required (Scan_Ptr + 3);
384 end if;
385 end if;
386 end Check_Comment;
388 -------------------
389 -- Check_Dot_Dot --
390 -------------------
392 -- In check token mode (-gnatyt), colon must be surrounded by spaces
394 procedure Check_Dot_Dot is
395 begin
396 if Style_Check_Tokens then
397 Require_Preceding_Space;
398 Require_Following_Space;
399 end if;
400 end Check_Dot_Dot;
402 -----------------------------------
403 -- Check_Exponentiation_Operator --
404 -----------------------------------
406 -- No spaces are required for the ** operator in GNAT style check mode
408 procedure Check_Exponentiation_Operator is
409 begin
410 null;
411 end Check_Exponentiation_Operator;
413 --------------
414 -- Check_HT --
415 --------------
417 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
419 procedure Check_HT is
420 begin
421 if Style_Check_Horizontal_Tabs then
422 Error_Msg_S ("(style) horizontal tab not allowed");
423 end if;
424 end Check_HT;
426 -----------------------
427 -- Check_Indentation --
428 -----------------------
430 -- In check indentation mode (-gnatyn for n a digit), a new statement or
431 -- declaration is required to start in a column that is a multiple of the
432 -- indentiation amount.
434 procedure Check_Indentation is
435 begin
436 if Style_Check_Indentation /= 0 then
437 if Token_Ptr = First_Non_Blank_Location
438 and then Start_Column rem Style_Check_Indentation /= 0
439 then
440 Error_Msg_SC ("(style) bad indentation");
441 end if;
442 end if;
443 end Check_Indentation;
445 ----------------------
446 -- Check_Left_Paren --
447 ----------------------
449 -- In tone check mode (-gnatyt), left paren must not be preceded by an
450 -- identifier character or digit (a separating space is required) and
451 -- may never be followed by a space.
453 procedure Check_Left_Paren is
454 begin
455 if Style_Check_Tokens then
456 if Token_Ptr > Source_First (Current_Source_File)
457 and then Identifier_Char (Source (Token_Ptr - 1))
458 then
459 Error_Space_Required (Token_Ptr);
460 end if;
462 Check_No_Space_After;
463 end if;
464 end Check_Left_Paren;
466 ---------------------------
467 -- Check_Line_Max_Length --
468 ---------------------------
470 -- In check max line length mode (-gnatym), the line length must
471 -- not exceed the permitted maximum value.
473 procedure Check_Line_Max_Length (Len : Int) is
474 begin
475 if Style_Check_Max_Line_Length then
476 if Len > Style_Max_Line_Length then
477 Error_Msg
478 ("(style) this line is too long",
479 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
480 end if;
481 end if;
482 end Check_Line_Max_Length;
484 ---------------------------
485 -- Check_Line_Terminator --
486 ---------------------------
488 -- In check blanks at end mode (-gnatyb), lines may not end with a
489 -- trailing space.
491 -- In check form feeds mode (-gnatyf), the line terminator may not
492 -- be either of the characters FF or VT.
494 -- In check DOS line terminators node (-gnatyd), the line terminator
495 -- must be a single LF, without a following CR.
497 procedure Check_Line_Terminator (Len : Int) is
498 S : Source_Ptr;
500 begin
501 -- Check FF/VT terminators
503 if Style_Check_Form_Feeds then
504 if Source (Scan_Ptr) = ASCII.FF then
505 Error_Msg_S ("(style) form feed not allowed");
506 elsif Source (Scan_Ptr) = ASCII.VT then
507 Error_Msg_S ("(style) vertical tab not allowed");
508 end if;
509 end if;
511 -- Check DOS line terminator (ignore EOF, since we only get called
512 -- with an EOF if it is the last character in the buffer, and was
513 -- therefore not present in the sources
515 if Style_Check_DOS_Line_Terminator then
516 if Source (Scan_Ptr) = EOF then
517 null;
518 elsif Source (Scan_Ptr) /= LF
519 or else Source (Scan_Ptr + 1) = CR
520 then
521 Error_Msg_S ("(style) incorrect line terminator");
522 end if;
523 end if;
525 -- We are now possibly going to check for trailing spaces. There is no
526 -- point in doing this if the current line is empty. It is actually
527 -- wrong to do so, because we scan backwards for this purpose, so we
528 -- would end up looking at different line, or even at invalid buffer
529 -- locations if we have the first source line at hand.
531 if Len = 0 then
532 return;
533 end if;
535 -- Check trailing space
537 if Style_Check_Blanks_At_End then
538 if Scan_Ptr >= First_Non_Blank_Location then
539 if Is_White_Space (Source (Scan_Ptr - 1)) then
540 S := Scan_Ptr - 1;
542 while Is_White_Space (Source (S - 1)) loop
543 S := S - 1;
544 end loop;
546 Error_Msg ("(style) trailing spaces not permitted", S);
547 end if;
548 end if;
549 end if;
550 end Check_Line_Terminator;
552 --------------------------
553 -- Check_No_Space_After --
554 --------------------------
556 procedure Check_No_Space_After is
557 S : Source_Ptr;
559 begin
560 if Is_White_Space (Source (Scan_Ptr)) then
562 -- Allow one or more spaces if followed by comment
564 S := Scan_Ptr + 1;
565 loop
566 if Source (S) = '-' and then Source (S + 1) = '-' then
567 return;
569 elsif Is_White_Space (Source (S)) then
570 S := S + 1;
572 else
573 exit;
574 end if;
575 end loop;
577 Error_Space_Not_Allowed (Scan_Ptr);
578 end if;
579 end Check_No_Space_After;
581 ---------------------------
582 -- Check_No_Space_Before --
583 ---------------------------
585 procedure Check_No_Space_Before is
586 begin
587 if Token_Ptr > First_Non_Blank_Location
588 and then Source (Token_Ptr - 1) <= ' '
589 then
590 Error_Space_Not_Allowed (Token_Ptr - 1);
591 end if;
592 end Check_No_Space_Before;
594 -----------------------
595 -- Check_Pragma_Name --
596 -----------------------
598 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
599 -- case, i.e. start with an upper case letter, and otherwise lower case,
600 -- except after an underline character.
602 procedure Check_Pragma_Name is
603 begin
604 if Style_Check_Pragma_Casing then
605 if Determine_Token_Casing /= Mixed_Case then
606 Error_Msg_SC ("(style) bad capitalization, mixed case required");
607 end if;
608 end if;
609 end Check_Pragma_Name;
611 -----------------------
612 -- Check_Right_Paren --
613 -----------------------
615 -- In check tokens mode (-gnatyt), right paren must never be preceded by
616 -- a space unless it is the initial non-blank character on the line.
618 procedure Check_Right_Paren is
619 begin
620 if Style_Check_Tokens then
621 Check_No_Space_Before;
622 end if;
623 end Check_Right_Paren;
625 ---------------------
626 -- Check_Semicolon --
627 ---------------------
629 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
630 -- space and a following space is required.
632 procedure Check_Semicolon is
633 begin
634 if Style_Check_Tokens then
635 Check_No_Space_Before;
637 if Source (Scan_Ptr) > ' ' then
638 Error_Space_Required (Scan_Ptr);
639 end if;
640 end if;
641 end Check_Semicolon;
643 ----------------
644 -- Check_Then --
645 ----------------
647 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
648 -- to appear either on the same line as the IF, or on a separate line
649 -- after multiple conditions. In any case, it may not appear on the
650 -- line immediately following the line with the IF.
652 procedure Check_Then (If_Loc : Source_Ptr) is
653 begin
654 if Style_Check_If_Then_Layout then
655 if Get_Physical_Line_Number (Token_Ptr) =
656 Get_Physical_Line_Number (If_Loc) + 1
657 then
658 Error_Msg_SC ("(style) misplaced THEN");
659 end if;
660 end if;
661 end Check_Then;
663 -------------------------------
664 -- Check_Unary_Plus_Or_Minus --
665 -------------------------------
667 -- In check tokem mode (-gnatyt), unary plus or minus must not be
668 -- followed by a space.
670 procedure Check_Unary_Plus_Or_Minus is
671 begin
672 if Style_Check_Tokens then
673 Check_No_Space_After;
674 end if;
675 end Check_Unary_Plus_Or_Minus;
677 ------------------------
678 -- Check_Vertical_Bar --
679 ------------------------
681 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
683 procedure Check_Vertical_Bar is
684 begin
685 if Style_Check_Tokens then
686 Require_Preceding_Space;
687 Require_Following_Space;
688 end if;
689 end Check_Vertical_Bar;
691 -----------------------
692 -- Check_Xtra_Parens --
693 -----------------------
695 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
696 begin
697 if Style_Check_Xtra_Parens then
698 Error_Msg ("redundant parentheses?", Loc);
699 end if;
700 end Check_Xtra_Parens;
702 ----------------------------
703 -- Determine_Token_Casing --
704 ----------------------------
706 function Determine_Token_Casing return Casing_Type is
707 begin
708 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
709 end Determine_Token_Casing;
711 -----------------------------
712 -- Error_Space_Not_Allowed --
713 -----------------------------
715 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
716 begin
717 Error_Msg ("(style) space not allowed", S);
718 end Error_Space_Not_Allowed;
720 --------------------------
721 -- Error_Space_Required --
722 --------------------------
724 procedure Error_Space_Required (S : Source_Ptr) is
725 begin
726 Error_Msg ("(style) space required", S);
727 end Error_Space_Required;
729 --------------------
730 -- Is_White_Space --
731 --------------------
733 function Is_White_Space (C : Character) return Boolean is
734 begin
735 return C = ' ' or else C = HT;
736 end Is_White_Space;
738 -----------------
739 -- No_End_Name --
740 -----------------
742 -- In check end/exit labels mode (-gnatye), always require the name of
743 -- a subprogram or package to be present on the END, so this is an error.
745 procedure No_End_Name (Name : Node_Id) is
746 begin
747 if Style_Check_End_Labels then
748 Error_Msg_Node_1 := Name;
749 Error_Msg_SP ("(style) `END &` required");
750 end if;
751 end No_End_Name;
753 ------------------
754 -- No_Exit_Name --
755 ------------------
757 -- In check end/exit labels mode (-gnatye), always require the name of
758 -- the loop to be present on the EXIT when exiting a named loop.
760 procedure No_Exit_Name (Name : Node_Id) is
761 begin
762 if Style_Check_End_Labels then
763 Error_Msg_Node_1 := Name;
764 Error_Msg_SP ("(style) `EXIT &` required");
765 end if;
766 end No_Exit_Name;
768 ----------------------------
769 -- Non_Lower_Case_Keyword --
770 ----------------------------
772 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
773 -- in all lower case (excluding keywords range, access, delta and digits
774 -- used as attribute designators).
776 procedure Non_Lower_Case_Keyword is
777 begin
778 if Style_Check_Keyword_Casing then
779 Error_Msg_SC ("(style) reserved words must be all lower case");
780 end if;
781 end Non_Lower_Case_Keyword;
783 -----------------------------
784 -- Require_Following_Space --
785 -----------------------------
787 procedure Require_Following_Space is
788 begin
789 if Source (Scan_Ptr) > ' ' then
790 Error_Space_Required (Scan_Ptr);
791 end if;
792 end Require_Following_Space;
794 -----------------------------
795 -- Require_Preceding_Space --
796 -----------------------------
798 procedure Require_Preceding_Space is
799 begin
800 if Token_Ptr > Source_First (Current_Source_File)
801 and then Source (Token_Ptr - 1) > ' '
802 then
803 Error_Space_Required (Token_Ptr);
804 end if;
805 end Require_Preceding_Space;
807 ---------------------
808 -- RM_Column_Check --
809 ---------------------
811 function RM_Column_Check return Boolean is
812 begin
813 return Style_Check and Style_Check_Layout;
814 end RM_Column_Check;
816 end Styleg;