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
139 pragma Warnings (Off, Reserved);
142 if Style_Check_Attribute_Casing then
143 if Determine_Token_Casing /= Mixed_Case then
144 Error_Msg_SC ("(style) bad capitalization, mixed case required");
147 end Check_Attribute_Name;
149 ---------------------------
150 -- Check_Binary_Operator --
151 ---------------------------
153 -- In check token mode (-gnatyt), binary operators other than the special
154 -- case of exponentiation require surrounding space characters.
156 procedure Check_Binary_Operator is
158 if Style_Check_Tokens then
159 Require_Preceding_Space;
160 Require_Following_Space;
162 end Check_Binary_Operator;
168 -- In check token mode (-gnatyt), box must be preceded by a space or by
169 -- a left parenthesis. Spacing checking on the surrounding tokens takes
170 -- care of the remaining checks.
172 procedure Check_Box is
174 if Style_Check_Tokens then
175 if Prev_Token /= Tok_Left_Paren then
176 Require_Preceding_Space;
185 -- In check token mode (-gnatyt), colon must be surrounded by spaces
187 procedure Check_Colon is
189 if Style_Check_Tokens then
190 Require_Preceding_Space;
191 Require_Following_Space;
195 -----------------------
196 -- Check_Colon_Equal --
197 -----------------------
199 -- In check token mode (-gnatyt), := must be surrounded by spaces
201 procedure Check_Colon_Equal is
203 if Style_Check_Tokens then
204 Require_Preceding_Space;
205 Require_Following_Space;
207 end Check_Colon_Equal;
213 -- In check token mode (-gnatyt), comma must be either the first
214 -- token on a line, or be preceded by a non-blank character.
215 -- It must also always be followed by a blank.
217 procedure Check_Comma is
219 if Style_Check_Tokens then
220 if Token_Ptr > First_Non_Blank_Location
221 and then Source (Token_Ptr - 1) = ' '
223 Error_Space_Not_Allowed (Token_Ptr - 1);
226 if Source (Scan_Ptr) > ' ' then
227 Error_Space_Required (Scan_Ptr);
236 -- In check comment mode (-gnatyc) there are several requirements on the
237 -- format of comments. The following are permissible comment formats:
239 -- 1. Any comment that is not at the start of a line, i.e. where the
240 -- initial minuses are not the first non-blank characters on the
241 -- line must have at least one blank after the second minus.
243 -- 2. A row of all minuses of any length is permitted (see procedure
244 -- box above in the source of this routine).
246 -- 3. A comment line starting with two minuses and a space, and ending
247 -- with a space and two minuses. Again see the procedure title box
248 -- immediately above in the source.
250 -- 4. A full line comment where two spaces follow the two minus signs.
251 -- This is the normal comment format in GNAT style, as typified by
252 -- the comments you are reading now.
254 -- 5. A full line comment where the first character after the second
255 -- minus is a special character, i.e. a character in the ASCII
256 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
257 -- comments, such as those generated by gnatprep, or those that
258 -- appear in the SPARK annotation language to be accepted.
260 procedure Check_Comment is
265 -- Can never have a non-blank character preceding the first minus
267 if Style_Check_Comments then
268 if Scan_Ptr > Source_First (Current_Source_File)
269 and then Source (Scan_Ptr - 1) > ' '
271 Error_Msg_S ("(style) space required");
275 -- For a comment that is not at the start of the line, the only
276 -- requirement is that we cannot have a non-blank character after
277 -- the second minus sign.
279 if Scan_Ptr /= First_Non_Blank_Location then
280 if Style_Check_Comments then
281 if Source (Scan_Ptr + 2) > ' ' then
282 Error_Msg ("(style) space required", Scan_Ptr + 2);
288 -- Case of a comment that is at the start of a line
291 -- First check, must be in appropriately indented column
293 if Style_Check_Indentation /= 0 then
294 if Start_Column rem Style_Check_Indentation /= 0 then
295 Error_Msg_S ("(style) bad column");
300 -- Now check form of the comment
302 if not Style_Check_Comments then
305 -- Case of not followed by a blank. Usually wrong, but there are
306 -- some exceptions that we permit.
308 elsif Source (Scan_Ptr + 2) /= ' ' then
309 C := Source (Scan_Ptr + 2);
311 -- Case of -- all on its own on a line is OK
316 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
318 elsif Character'Pos (C) in 16#21# .. 16#2F#
320 Character'Pos (C) in 16#3A# .. 16#3F#
324 -- Otherwise only cases allowed are when the entire line is
325 -- made up of minus signs (case of a box comment).
330 while Source (S) >= ' ' loop
331 if Source (S) /= '-' then
332 Error_Space_Required (Scan_Ptr + 2);
340 -- If we are followed by a blank, then the comment is OK if the
341 -- character following this blank is another blank or a format
344 elsif Source (Scan_Ptr + 3) <= ' ' then
347 -- Here is the case where we only have one blank after the two minus
348 -- signs, which is an error unless the line ends with two blanks, the
349 -- case of a box comment.
354 while Source (S) not in Line_Terminator loop
358 if Source (S - 1) /= '-' or else Source (S - 2) /= '-' then
359 Error_Space_Required (Scan_Ptr + 3);
369 -- In check token mode (-gnatyt), colon must be surrounded by spaces
371 procedure Check_Dot_Dot is
373 if Style_Check_Tokens then
374 Require_Preceding_Space;
375 Require_Following_Space;
379 -----------------------------------
380 -- Check_Exponentiation_Operator --
381 -----------------------------------
383 -- No spaces are required for the ** operator in GNAT style check mode
385 procedure Check_Exponentiation_Operator is
388 end Check_Exponentiation_Operator;
394 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
396 procedure Check_HT is
398 if Style_Check_Horizontal_Tabs then
399 Error_Msg_S ("(style) horizontal tab not allowed");
403 ----------------------
404 -- Check_Identifier --
405 ----------------------
407 -- In check references mode (-gnatyr), identifier uses must be cased
408 -- the same way as the corresponding identifier declaration.
410 procedure Check_Identifier
411 (Ref : Node_Or_Entity_Id;
412 Def : Node_Or_Entity_Id)
414 Sref : Source_Ptr := Sloc (Ref);
415 Sdef : Source_Ptr := Sloc (Def);
416 Tref : Source_Buffer_Ptr;
417 Tdef : Source_Buffer_Ptr;
422 -- If reference does not come from source, nothing to check
424 if not Comes_From_Source (Ref) then
427 -- Case of definition comes from source
429 elsif Comes_From_Source (Def) then
431 -- Check same casing if we are checking references
433 if Style_Check_References then
434 Tref := Source_Text (Get_Source_File_Index (Sref));
435 Tdef := Source_Text (Get_Source_File_Index (Sdef));
437 -- Ignore operator name case completely. This also catches the
438 -- case of where one is an operator and the other is not. This
439 -- is a phenomenon from rewriting of operators as functions,
440 -- and is to be ignored.
442 if Tref (Sref) = '"' or else Tdef (Sdef) = '"' then
446 while Tref (Sref) = Tdef (Sdef) loop
448 -- If end of identifier, all done
450 if not Identifier_Char (Tref (Sref)) then
453 -- Otherwise loop continues
461 -- Fall through loop when mismatch between identifiers
462 -- If either identifier is not terminated, error.
464 if Identifier_Char (Tref (Sref))
466 Identifier_Char (Tdef (Sdef))
468 Error_Msg_Node_1 := Def;
469 Error_Msg_Sloc := Sloc (Def);
471 ("(style) bad casing of & declared#", Sref);
474 -- Else end of identifiers, and they match
482 -- Case of definition in package Standard
484 elsif Sdef = Standard_Location then
486 -- Check case of identifiers in Standard
488 if Style_Check_Standard then
489 Tref := Source_Text (Get_Source_File_Index (Sref));
493 if Tref (Sref) = '"' then
496 -- Special case of ASCII
499 if Entity (Ref) = Standard_ASCII then
500 Cas := All_Upper_Case;
502 elsif Entity (Ref) in SE (S_LC_A) .. SE (S_LC_Z)
504 Entity (Ref) in SE (S_NUL) .. SE (S_US)
506 Entity (Ref) = SE (S_DEL)
508 Cas := All_Upper_Case;
514 Nlen := Length_Of_Name (Chars (Ref));
517 (Tref (Sref .. Sref + Source_Ptr (Nlen) - 1)) = Cas
522 ("(style) bad casing for entity in Standard", Ref);
527 end Check_Identifier;
529 -----------------------
530 -- Check_Indentation --
531 -----------------------
533 -- In check indentation mode (-gnatyn for n a digit), a new statement or
534 -- declaration is required to start in a column that is a multiple of the
535 -- indentiation amount.
537 procedure Check_Indentation is
539 if Style_Check_Indentation /= 0 then
540 if Token_Ptr = First_Non_Blank_Location
541 and then Start_Column rem Style_Check_Indentation /= 0
543 Error_Msg_SC ("(style) bad indentation");
546 end Check_Indentation;
548 ----------------------
549 -- Check_Left_Paren --
550 ----------------------
552 -- In tone check mode (-gnatyt), left paren must not be preceded by an
553 -- identifier character or digit (a separating space is required) and
554 -- may never be followed by a space.
556 procedure Check_Left_Paren is
560 if Style_Check_Tokens then
561 if Token_Ptr > Source_First (Current_Source_File)
562 and then Identifier_Char (Source (Token_Ptr - 1))
564 Error_Space_Required (Token_Ptr);
567 if Source (Scan_Ptr) = ' ' then
569 -- Allow one or more spaces if followed by comment
573 if Source (S) = '-' and then Source (S + 1) = '-' then
575 elsif Source (S) /= ' ' then
582 Error_Space_Not_Allowed (Scan_Ptr);
585 end Check_Left_Paren;
587 ---------------------------
588 -- Check_Line_Terminator --
589 ---------------------------
591 -- In check blanks at end mode (-gnatyb), lines may not end with a
594 -- In check max line length mode (-gnatym), the line length must
595 -- not exceed the permitted maximum value.
597 -- In check form feeds mode (-gnatyf), the line terminator may not
598 -- be either of the characters FF or VT.
600 procedure Check_Line_Terminator (Len : Int) is
604 -- Check FF/VT terminators
606 if Style_Check_Form_Feeds then
607 if Source (Scan_Ptr) = ASCII.FF then
608 Error_Msg_S ("(style) form feed not allowed");
610 elsif Source (Scan_Ptr) = ASCII.VT then
611 Error_Msg_S ("(style) vertical tab not allowed");
615 -- Check trailing space
617 if Style_Check_Blanks_At_End then
618 if Scan_Ptr >= First_Non_Blank_Location then
619 if Source (Scan_Ptr - 1) = ' ' then
622 while Source (S - 1) = ' ' loop
626 Error_Msg ("(style) trailing spaces not permitted", S);
631 -- Check max line length
633 if Style_Check_Max_Line_Length then
634 if Len > Style_Max_Line_Length then
636 ("(style) this line is too long",
637 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
641 end Check_Line_Terminator;
643 -----------------------
644 -- Check_Pragma_Name --
645 -----------------------
647 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
648 -- case, i.e. start with an upper case letter, and otherwise lower case,
649 -- except after an underline character.
651 procedure Check_Pragma_Name is
653 if Style_Check_Pragma_Casing then
654 if Determine_Token_Casing /= Mixed_Case then
655 Error_Msg_SC ("(style) bad capitalization, mixed case required");
658 end Check_Pragma_Name;
660 -----------------------
661 -- Check_Right_Paren --
662 -----------------------
664 -- In check tokens mode (-gnatyt), right paren must never be preceded by
665 -- a space unless it is the initial non-blank character on the line.
667 procedure Check_Right_Paren is
669 if Style_Check_Tokens then
670 if Token_Ptr > First_Non_Blank_Location
671 and then Source (Token_Ptr - 1) = ' '
673 Error_Space_Not_Allowed (Token_Ptr - 1);
676 end Check_Right_Paren;
678 ---------------------
679 -- Check_Semicolon --
680 ---------------------
682 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
683 -- space and a following space is required.
685 procedure Check_Semicolon is
687 if Style_Check_Tokens then
688 if Scan_Ptr > Source_First (Current_Source_File)
689 and then Source (Token_Ptr - 1) = ' '
691 Error_Space_Not_Allowed (Token_Ptr - 1);
693 elsif Source (Scan_Ptr) > ' ' then
694 Error_Space_Required (Scan_Ptr);
703 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
704 -- to appear either on the same line as the IF, or on a separate line
705 -- after multiple conditions. In any case, it may not appear on the
706 -- line immediately following the line with the IF.
708 procedure Check_Then (If_Loc : Source_Ptr) is
710 if Style_Check_If_Then_Layout then
711 if Get_Physical_Line_Number (Token_Ptr) =
712 Get_Physical_Line_Number (If_Loc) + 1
714 Error_Msg_SC ("(style) misplaced THEN");
719 -------------------------------
720 -- Check_Unary_Plus_Or_Minus --
721 -------------------------------
723 -- In check tokem mode (-gnatyt), unary plus or minus must not be
724 -- followed by a space.
726 procedure Check_Unary_Plus_Or_Minus is
728 if Style_Check_Tokens then
729 if Source (Scan_Ptr) = ' ' then
730 Error_Space_Not_Allowed (Scan_Ptr);
733 end Check_Unary_Plus_Or_Minus;
735 ------------------------
736 -- Check_Vertical_Bar --
737 ------------------------
739 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
741 procedure Check_Vertical_Bar is
743 if Style_Check_Tokens then
744 Require_Preceding_Space;
745 Require_Following_Space;
747 end Check_Vertical_Bar;
749 -----------------------------
750 -- Error_Space_Not_Allowed --
751 -----------------------------
753 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
755 Error_Msg ("(style) space not allowed", S);
756 end Error_Space_Not_Allowed;
758 --------------------------
759 -- Error_Space_Required --
760 --------------------------
762 procedure Error_Space_Required (S : Source_Ptr) is
764 Error_Msg ("(style) space required", S);
765 end Error_Space_Required;
771 -- In check end/exit labels mode (-gnatye), always require the name of
772 -- a subprogram or package to be present on the END, so this is an error.
774 procedure No_End_Name (Name : Node_Id) is
776 if Style_Check_End_Labels then
777 Error_Msg_Node_1 := Name;
778 Error_Msg_SP ("(style) `END &` required");
786 -- In check end/exit labels mode (-gnatye), always require the name of
787 -- the loop to be present on the EXIT when exiting a named loop.
789 procedure No_Exit_Name (Name : Node_Id) is
791 if Style_Check_End_Labels then
792 Error_Msg_Node_1 := Name;
793 Error_Msg_SP ("(style) `EXIT &` required");
797 ----------------------------
798 -- Non_Lower_Case_Keyword --
799 ----------------------------
801 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
802 -- in all lower case (excluding keywords range, access, delta and digits
803 -- used as attribute designators).
805 procedure Non_Lower_Case_Keyword is
807 if Style_Check_Keyword_Casing then
808 Error_Msg_SC ("(style) reserved words must be all lower case");
810 end Non_Lower_Case_Keyword;
812 -----------------------------
813 -- Require_Following_Space --
814 -----------------------------
816 procedure Require_Following_Space is
818 if Source (Scan_Ptr) > ' ' then
819 Error_Space_Required (Scan_Ptr);
821 end Require_Following_Space;
823 -----------------------------
824 -- Require_Preceding_Space --
825 -----------------------------
827 procedure Require_Preceding_Space is
829 if Token_Ptr > Source_First (Current_Source_File)
830 and then Source (Token_Ptr - 1) > ' '
832 Error_Space_Required (Token_Ptr);
834 end Require_Preceding_Space;
836 ---------------------
837 -- RM_Column_Check --
838 ---------------------
840 function RM_Column_Check return Boolean is
842 return Style_Check and Style_Check_Layout;
845 -----------------------------------
846 -- Subprogram_Not_In_Alpha_Order --
847 -----------------------------------
849 procedure Subprogram_Not_In_Alpha_Order (Name : Node_Id) is
851 if Style_Check_Subprogram_Order then
853 ("(style) subprogram body& not in alphabetical order", Name);
855 end Subprogram_Not_In_Alpha_Order;