1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- This version of the Style package implements the standard GNAT style
30 -- checking rules. For documentation of these rules, see comments on the
31 -- individual procedures.
33 with Atree; use Atree;
34 with Casing; use Casing;
35 with Csets; use Csets;
36 with Einfo; use Einfo;
37 with Errout; use Errout;
38 with Namet; use Namet;
41 with Scans; use Scans;
42 with Sinfo; use Sinfo;
43 with Sinput; use Sinput;
44 with Stand; use Stand;
45 with Stylesw; use Stylesw;
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Error_Space_Not_Allowed (S : Source_Ptr);
54 -- Posts an error message indicating that a space is not allowed
55 -- at the given source location.
57 procedure Error_Space_Required (S : Source_Ptr);
58 -- Posts an error message indicating that a space is required at
59 -- the given source location.
61 procedure Require_Following_Space;
62 pragma Inline (Require_Following_Space);
63 -- Require token to be followed by white space. Used only if in GNAT
64 -- style checking mode.
66 procedure Require_Preceding_Space;
67 pragma Inline (Require_Preceding_Space);
68 -- Require token to be preceded by white space. Used only if in GNAT
69 -- style checking mode.
71 -----------------------
72 -- Body_With_No_Spec --
73 -----------------------
75 -- If the check specs mode (-gnatys) is set, then all subprograms must
76 -- have specs unless they are parameterless procedures that are not child
77 -- units at the library level (i.e. they are possible main programs).
79 procedure Body_With_No_Spec (N : Node_Id) is
81 if Style_Check_Specs then
82 if Nkind (Parent (N)) = N_Compilation_Unit then
84 Spec : constant Node_Id := Specification (N);
85 Defnm : constant Node_Id := Defining_Unit_Name (Spec);
88 if Nkind (Spec) = N_Procedure_Specification
89 and then Nkind (Defnm) = N_Defining_Identifier
90 and then No (First_Formal (Defnm))
97 Error_Msg_N ("(style): subprogram body has no previous spec", N);
99 end Body_With_No_Spec;
101 ----------------------
102 -- Check_Abs_Or_Not --
103 ----------------------
105 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
107 procedure Check_Abs_Not is
109 if Style_Check_Tokens then
110 if Source (Scan_Ptr) > ' ' then
111 Error_Space_Required (Scan_Ptr);
120 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
122 procedure Check_Arrow is
124 if Style_Check_Tokens then
125 Require_Preceding_Space;
126 Require_Following_Space;
130 --------------------------
131 -- Check_Attribute_Name --
132 --------------------------
134 -- In check attribute casing mode (-gnatya), attribute names must be
135 -- mixed case, i.e. start with an upper case letter, and otherwise
136 -- lower case, except after an underline character.
138 procedure Check_Attribute_Name (Reserved : Boolean) is
140 if Style_Check_Attribute_Casing then
141 if Determine_Token_Casing /= Mixed_Case then
142 Error_Msg_SC ("(style) bad capitalization, mixed case required");
145 end Check_Attribute_Name;
147 ---------------------------
148 -- Check_Binary_Operator --
149 ---------------------------
151 -- In check token mode (-gnatyt), binary operators other than the special
152 -- case of exponentiation require surrounding space characters.
154 procedure Check_Binary_Operator is
156 if Style_Check_Tokens then
157 Require_Preceding_Space;
158 Require_Following_Space;
160 end Check_Binary_Operator;
166 -- In check token mode (-gnatyt), box must be preceded by a space or by
167 -- a left parenthesis. Spacing checking on the surrounding tokens takes
168 -- care of the remaining checks.
170 procedure Check_Box is
172 if Style_Check_Tokens then
173 if Prev_Token /= Tok_Left_Paren then
174 Require_Preceding_Space;
183 -- In check token mode (-gnatyt), colon must be surrounded by spaces
185 procedure Check_Colon is
187 if Style_Check_Tokens then
188 Require_Preceding_Space;
189 Require_Following_Space;
193 -----------------------
194 -- Check_Colon_Equal --
195 -----------------------
197 -- In check token mode (-gnatyt), := must be surrounded by spaces
199 procedure Check_Colon_Equal is
201 if Style_Check_Tokens then
202 Require_Preceding_Space;
203 Require_Following_Space;
205 end Check_Colon_Equal;
211 -- In check token mode (-gnatyt), comma must be either the first
212 -- token on a line, or be preceded by a non-blank character.
213 -- It must also always be followed by a blank.
215 procedure Check_Comma is
217 if Style_Check_Tokens then
218 if Token_Ptr > First_Non_Blank_Location
219 and then Source (Token_Ptr - 1) = ' '
221 Error_Space_Not_Allowed (Token_Ptr - 1);
224 if Source (Scan_Ptr) > ' ' then
225 Error_Space_Required (Scan_Ptr);
234 -- In check comment mode (-gnatyc) there are several requirements on the
235 -- format of comments. The following are permissible comment formats:
237 -- 1. Any comment that is not at the start of a line, i.e. where the
238 -- initial minuses are not the first non-blank characters on the
239 -- line must have at least one blank after the second minus.
241 -- 2. A row of all minuses of any length is permitted (see procedure
242 -- box above in the source of this routine).
244 -- 3. A comment line starting with two minuses and a space, and ending
245 -- with a space and two minuses. Again see the procedure title box
246 -- immediately above in the source.
248 -- 4. A full line comment where two spaces follow the two minus signs.
249 -- This is the normal comment format in GNAT style, as typified by
250 -- the comments you are reading now.
252 -- 5. A full line comment where the first character after the second
253 -- minus is a special character, i.e. a character in the ASCII
254 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
255 -- comments, such as those generated by gnatprep, or those that
256 -- appear in the SPARK annotation language to be accepted.
258 procedure Check_Comment is
263 -- Can never have a non-blank character preceding the first minus
265 if Style_Check_Comments then
266 if Scan_Ptr > Source_First (Current_Source_File)
267 and then Source (Scan_Ptr - 1) > ' '
269 Error_Msg_S ("(style) space required");
273 -- For a comment that is not at the start of the line, the only
274 -- requirement is that we cannot have a non-blank character after
275 -- the second minus sign.
277 if Scan_Ptr /= First_Non_Blank_Location then
278 if Style_Check_Comments then
279 if Source (Scan_Ptr + 2) > ' ' then
280 Error_Msg ("(style) space required", Scan_Ptr + 2);
286 -- Case of a comment that is at the start of a line
289 -- First check, must be in appropriately indented column
291 if Style_Check_Indentation /= 0 then
292 if Start_Column rem Style_Check_Indentation /= 0 then
293 Error_Msg_S ("(style) bad column");
298 -- Now check form of the comment
300 if not Style_Check_Comments then
303 -- Case of not followed by a blank. Usually wrong, but there are
304 -- some exceptions that we permit.
306 elsif Source (Scan_Ptr + 2) /= ' ' then
307 C := Source (Scan_Ptr + 2);
309 -- Case of -- all on its own on a line is OK
314 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
316 elsif Character'Pos (C) in 16#21# .. 16#2F#
318 Character'Pos (C) in 16#3A# .. 16#3F#
322 -- Otherwise only cases allowed are when the entire line is
323 -- made up of minus signs (case of a box comment).
328 while Source (S) >= ' ' loop
329 if Source (S) /= '-' then
330 Error_Space_Required (Scan_Ptr + 2);
338 -- If we are followed by a blank, then the comment is OK if the
339 -- character following this blank is another blank or a format
342 elsif Source (Scan_Ptr + 3) <= ' ' then
345 -- Here is the case where we only have one blank after the two minus
346 -- signs, which is an error unless the line ends with two blanks, the
347 -- case of a box comment.
352 while Source (S) not in Line_Terminator loop
356 if Source (S - 1) /= '-' or else Source (S - 2) /= '-' then
357 Error_Space_Required (Scan_Ptr + 3);
367 -- In check token mode (-gnatyt), colon must be surrounded by spaces
369 procedure Check_Dot_Dot is
371 if Style_Check_Tokens then
372 Require_Preceding_Space;
373 Require_Following_Space;
377 -----------------------------------
378 -- Check_Exponentiation_Operator --
379 -----------------------------------
381 -- No spaces are required for the ** operator in GNAT style check mode
383 procedure Check_Exponentiation_Operator is
386 end Check_Exponentiation_Operator;
392 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
394 procedure Check_HT is
396 if Style_Check_Horizontal_Tabs then
397 Error_Msg_S ("(style) horizontal tab not allowed");
401 ----------------------
402 -- Check_Identifier --
403 ----------------------
405 -- In check references mode (-gnatyr), identifier uses must be cased
406 -- the same way as the corresponding identifier declaration.
408 procedure Check_Identifier
409 (Ref : Node_Or_Entity_Id;
410 Def : Node_Or_Entity_Id)
412 Sref : Source_Ptr := Sloc (Ref);
413 Sdef : Source_Ptr := Sloc (Def);
414 Tref : Source_Buffer_Ptr;
415 Tdef : Source_Buffer_Ptr;
420 -- If reference does not come from source, nothing to check
422 if not Comes_From_Source (Ref) then
425 -- Case of definition comes from source
427 elsif Comes_From_Source (Def) then
429 -- Check same casing if we are checking references
431 if Style_Check_References then
432 Tref := Source_Text (Get_Source_File_Index (Sref));
433 Tdef := Source_Text (Get_Source_File_Index (Sdef));
435 -- Ignore operator name case completely. This also catches the
436 -- case of where one is an operator and the other is not. This
437 -- is a phenomenon from rewriting of operators as functions,
438 -- and is to be ignored.
440 if Tref (Sref) = '"' or else Tdef (Sdef) = '"' then
444 while Tref (Sref) = Tdef (Sdef) loop
446 -- If end of identifier, all done
448 if not Identifier_Char (Tref (Sref)) then
451 -- Otherwise loop continues
459 -- Fall through loop when mismatch between identifiers
460 -- If either identifier is not terminated, error.
462 if Identifier_Char (Tref (Sref))
464 Identifier_Char (Tdef (Sdef))
466 Error_Msg_Node_1 := Def;
467 Error_Msg_Sloc := Sloc (Def);
469 ("(style) bad casing of & declared#", Sref);
472 -- Else end of identifiers, and they match
480 -- Case of definition in package Standard
482 elsif Sdef = Standard_Location then
484 -- Check case of identifiers in Standard
486 if Style_Check_Standard then
487 Tref := Source_Text (Get_Source_File_Index (Sref));
491 if Tref (Sref) = '"' then
494 -- Special case of ASCII
497 if Entity (Ref) = Standard_ASCII then
498 Cas := All_Upper_Case;
500 elsif Entity (Ref) in SE (S_LC_A) .. SE (S_LC_Z)
502 Entity (Ref) in SE (S_NUL) .. SE (S_US)
504 Entity (Ref) = SE (S_DEL)
506 Cas := All_Upper_Case;
512 Nlen := Length_Of_Name (Chars (Ref));
515 (Tref (Sref .. Sref + Source_Ptr (Nlen) - 1)) = Cas
520 ("(style) bad casing for entity in Standard", Ref);
525 end Check_Identifier;
527 -----------------------
528 -- Check_Indentation --
529 -----------------------
531 -- In check indentation mode (-gnatyn for n a digit), a new statement or
532 -- declaration is required to start in a column that is a multiple of the
533 -- indentiation amount.
535 procedure Check_Indentation is
537 if Style_Check_Indentation /= 0 then
538 if Token_Ptr = First_Non_Blank_Location
539 and then Start_Column rem Style_Check_Indentation /= 0
541 Error_Msg_SC ("(style) bad indentation");
544 end Check_Indentation;
546 ----------------------
547 -- Check_Left_Paren --
548 ----------------------
550 -- In tone check mode (-gnatyt), left paren must not be preceded by an
551 -- identifier character or digit (a separating space is required) and
552 -- may never be followed by a space.
554 procedure Check_Left_Paren is
558 if Style_Check_Tokens then
559 if Token_Ptr > Source_First (Current_Source_File)
560 and then Identifier_Char (Source (Token_Ptr - 1))
562 Error_Space_Required (Token_Ptr);
565 if Source (Scan_Ptr) = ' ' then
567 -- Allow one or more spaces if followed by comment
571 if Source (S) = '-' and then Source (S + 1) = '-' then
573 elsif Source (S) /= ' ' then
580 Error_Space_Not_Allowed (Scan_Ptr);
583 end Check_Left_Paren;
585 ---------------------------
586 -- Check_Line_Terminator --
587 ---------------------------
589 -- In check blanks at end mode (-gnatyb), lines may not end with a
592 -- In check max line length mode (-gnatym), the line length must
593 -- not exceed the permitted maximum value.
595 -- In check form feeds mode (-gnatyf), the line terminator may not
596 -- be either of the characters FF or VT.
598 procedure Check_Line_Terminator (Len : Int) is
602 -- Check FF/VT terminators
604 if Style_Check_Form_Feeds then
605 if Source (Scan_Ptr) = ASCII.FF then
606 Error_Msg_S ("(style) form feed not allowed");
608 elsif Source (Scan_Ptr) = ASCII.VT then
609 Error_Msg_S ("(style) vertical tab not allowed");
613 -- Check trailing space
615 if Style_Check_Blanks_At_End then
616 if Scan_Ptr >= First_Non_Blank_Location then
617 if Source (Scan_Ptr - 1) = ' ' then
620 while Source (S - 1) = ' ' loop
624 Error_Msg ("(style) trailing spaces not permitted", S);
629 -- Check max line length
631 if Style_Check_Max_Line_Length then
632 if Len > Style_Max_Line_Length then
634 ("(style) this line is too long",
635 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
639 end Check_Line_Terminator;
641 -----------------------
642 -- Check_Pragma_Name --
643 -----------------------
645 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
646 -- case, i.e. start with an upper case letter, and otherwise lower case,
647 -- except after an underline character.
649 procedure Check_Pragma_Name is
651 if Style_Check_Pragma_Casing then
652 if Determine_Token_Casing /= Mixed_Case then
653 Error_Msg_SC ("(style) bad capitalization, mixed case required");
656 end Check_Pragma_Name;
658 -----------------------
659 -- Check_Right_Paren --
660 -----------------------
662 -- In check tokens mode (-gnatyt), right paren must never be preceded by
663 -- a space unless it is the initial non-blank character on the line.
665 procedure Check_Right_Paren is
667 if Style_Check_Tokens then
668 if Token_Ptr > First_Non_Blank_Location
669 and then Source (Token_Ptr - 1) = ' '
671 Error_Space_Not_Allowed (Token_Ptr - 1);
674 end Check_Right_Paren;
676 ---------------------
677 -- Check_Semicolon --
678 ---------------------
680 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
681 -- space and a following space is required.
683 procedure Check_Semicolon is
685 if Style_Check_Tokens then
686 if Scan_Ptr > Source_First (Current_Source_File)
687 and then Source (Token_Ptr - 1) = ' '
689 Error_Space_Not_Allowed (Token_Ptr - 1);
691 elsif Source (Scan_Ptr) > ' ' then
692 Error_Space_Required (Scan_Ptr);
701 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
702 -- to appear either on the same line as the IF, or on a separate line
703 -- after multiple conditions. In any case, it may not appear on the
704 -- line immediately following the line with the IF.
706 procedure Check_Then (If_Loc : Source_Ptr) is
708 if Style_Check_If_Then_Layout then
709 if Get_Physical_Line_Number (Token_Ptr) =
710 Get_Physical_Line_Number (If_Loc) + 1
712 Error_Msg_SC ("(style) misplaced THEN");
717 -------------------------------
718 -- Check_Unary_Plus_Or_Minus --
719 -------------------------------
721 -- In check tokem mode (-gnatyt), unary plus or minus must not be
722 -- followed by a space.
724 procedure Check_Unary_Plus_Or_Minus is
726 if Style_Check_Tokens then
727 if Source (Scan_Ptr) = ' ' then
728 Error_Space_Not_Allowed (Scan_Ptr);
731 end Check_Unary_Plus_Or_Minus;
733 ------------------------
734 -- Check_Vertical_Bar --
735 ------------------------
737 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
739 procedure Check_Vertical_Bar is
741 if Style_Check_Tokens then
742 Require_Preceding_Space;
743 Require_Following_Space;
745 end Check_Vertical_Bar;
747 -----------------------------
748 -- Error_Space_Not_Allowed --
749 -----------------------------
751 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
753 Error_Msg ("(style) space not allowed", S);
754 end Error_Space_Not_Allowed;
756 --------------------------
757 -- Error_Space_Required --
758 --------------------------
760 procedure Error_Space_Required (S : Source_Ptr) is
762 Error_Msg ("(style) space required", S);
763 end Error_Space_Required;
769 -- In check end/exit labels mode (-gnatye), always require the name of
770 -- a subprogram or package to be present on the END, so this is an error.
772 procedure No_End_Name (Name : Node_Id) is
774 if Style_Check_End_Labels then
775 Error_Msg_Node_1 := Name;
776 Error_Msg_SP ("(style) `END &` required");
784 -- In check end/exit labels mode (-gnatye), always require the name of
785 -- the loop to be present on the EXIT when exiting a named loop.
787 procedure No_Exit_Name (Name : Node_Id) is
789 if Style_Check_End_Labels then
790 Error_Msg_Node_1 := Name;
791 Error_Msg_SP ("(style) `EXIT &` required");
795 ----------------------------
796 -- Non_Lower_Case_Keyword --
797 ----------------------------
799 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
800 -- in all lower case (excluding keywords range, access, delta and digits
801 -- used as attribute designators).
803 procedure Non_Lower_Case_Keyword is
805 if Style_Check_Keyword_Casing then
806 Error_Msg_SC ("(style) reserved words must be all lower case");
808 end Non_Lower_Case_Keyword;
810 -----------------------------
811 -- Require_Following_Space --
812 -----------------------------
814 procedure Require_Following_Space is
816 if Source (Scan_Ptr) > ' ' then
817 Error_Space_Required (Scan_Ptr);
819 end Require_Following_Space;
821 -----------------------------
822 -- Require_Preceding_Space --
823 -----------------------------
825 procedure Require_Preceding_Space is
827 if Token_Ptr > Source_First (Current_Source_File)
828 and then Source (Token_Ptr - 1) > ' '
830 Error_Space_Required (Token_Ptr);
832 end Require_Preceding_Space;
834 ---------------------
835 -- RM_Column_Check --
836 ---------------------
838 function RM_Column_Check return Boolean is
840 return Style_Check and Style_Check_Layout;
843 -----------------------------------
844 -- Subprogram_Not_In_Alpha_Order --
845 -----------------------------------
847 procedure Subprogram_Not_In_Alpha_Order (Name : Node_Id) is
849 if Style_Check_Subprogram_Order then
851 ("(style) subprogram body& not in alphabetical order", Name);
853 end Subprogram_Not_In_Alpha_Order;