1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
35 with Scans; use Scans;
36 with Sinput; use Sinput;
37 with Stylesw; use Stylesw;
39 package body Styleg is
43 Blank_Lines : Nat := 0;
44 -- Counts number of empty lines seen. Reset to zero if a non-empty line
45 -- is encountered. Used to check for trailing blank lines in Check_EOF,
46 -- and for multiple blank lines.
48 Blank_Line_Location : Source_Ptr;
49 -- Remembers location of first blank line in a series. Used to issue an
50 -- appropriate diagnostic if subsequent blank lines or the end of file
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Check_No_Space_After;
58 -- Checks that there is a non-white space character after the current
59 -- token, or white space followed by a comment, or the end of line.
60 -- Issue error message if not.
62 procedure Check_No_Space_Before;
63 -- Check that token is first token on line, or else is not preceded
64 -- by white space. Signal error of space not allowed if not.
66 procedure Check_Separate_Stmt_Lines_Cont;
67 -- Non-inlined continuation of Check_Separate_Stmt_Lines
69 function Determine_Token_Casing return Casing_Type;
70 -- Determine casing of current token
72 procedure Error_Space_Not_Allowed (S : Source_Ptr);
73 -- Posts an error message indicating that a space is not allowed
74 -- at the given source location.
76 procedure Error_Space_Required (S : Source_Ptr);
77 -- Posts an error message indicating that a space is required at
78 -- the given source location.
80 function Is_White_Space (C : Character) return Boolean;
81 pragma Inline (Is_White_Space);
82 -- Returns True for space, HT, VT or FF, False otherwise
84 procedure Require_Following_Space;
85 pragma Inline (Require_Following_Space);
86 -- Require token to be followed by white space. Used only if in GNAT
87 -- style checking mode.
89 procedure Require_Preceding_Space;
90 pragma Inline (Require_Preceding_Space);
91 -- Require token to be preceded by white space. Used only if in GNAT
92 -- style checking mode.
94 ----------------------
95 -- Check_Abs_Or_Not --
96 ----------------------
98 -- In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
100 procedure Check_Abs_Not is
102 if Style_Check_Tokens then
103 if Source (Scan_Ptr) > ' ' then
104 Error_Space_Required (Scan_Ptr);
109 ----------------------
110 -- Check_Apostrophe --
111 ----------------------
113 -- Do not allow space before or after apostrophe
115 procedure Check_Apostrophe is
117 if Style_Check_Tokens then
118 Check_No_Space_After;
120 end Check_Apostrophe;
126 -- In check tokens mode (-gnatys), arrow must be surrounded by spaces
128 procedure Check_Arrow is
130 if Style_Check_Tokens then
131 Require_Preceding_Space;
132 Require_Following_Space;
136 --------------------------
137 -- Check_Attribute_Name --
138 --------------------------
140 -- In check attribute casing mode (-gnatya), attribute names must be
141 -- mixed case, i.e. start with an upper case letter, and otherwise
142 -- lower case, except after an underline character.
144 procedure Check_Attribute_Name (Reserved : Boolean) is
145 pragma Warnings (Off, Reserved);
147 if Style_Check_Attribute_Casing then
148 if Determine_Token_Casing /= Mixed_Case then
149 Error_Msg_SC ("(style) bad capitalization, mixed case required");
152 end Check_Attribute_Name;
154 ---------------------------
155 -- Check_Binary_Operator --
156 ---------------------------
158 -- In check token mode (-gnatyt), binary operators other than the special
159 -- case of exponentiation require surrounding space characters.
161 procedure Check_Binary_Operator is
163 if Style_Check_Tokens then
164 Require_Preceding_Space;
165 Require_Following_Space;
167 end Check_Binary_Operator;
173 -- In check token mode (-gnatyt), box must be preceded by a space or by
174 -- a left parenthesis. Spacing checking on the surrounding tokens takes
175 -- care of the remaining checks.
177 procedure Check_Box is
179 if Style_Check_Tokens then
180 if Prev_Token /= Tok_Left_Paren then
181 Require_Preceding_Space;
190 -- In check token mode (-gnatyt), colon must be surrounded by spaces
192 procedure Check_Colon is
194 if Style_Check_Tokens then
195 Require_Preceding_Space;
196 Require_Following_Space;
200 -----------------------
201 -- Check_Colon_Equal --
202 -----------------------
204 -- In check token mode (-gnatyt), := must be surrounded by spaces
206 procedure Check_Colon_Equal is
208 if Style_Check_Tokens then
209 Require_Preceding_Space;
210 Require_Following_Space;
212 end Check_Colon_Equal;
218 -- In check token mode (-gnatyt), comma must be either the first
219 -- token on a line, or be preceded by a non-blank character.
220 -- It must also always be followed by a blank.
222 procedure Check_Comma is
224 if Style_Check_Tokens then
225 Check_No_Space_Before;
227 if Source (Scan_Ptr) > ' ' then
228 Error_Space_Required (Scan_Ptr);
237 -- In check comment mode (-gnatyc) there are several requirements on the
238 -- format of comments. The following are permissible comment formats:
240 -- 1. Any comment that is not at the start of a line, i.e. where the
241 -- initial minuses are not the first non-blank characters on the
242 -- line must have at least one blank after the second minus.
244 -- 2. A row of all minuses of any length is permitted (see procedure
245 -- box above in the source of this routine).
247 -- 3. A comment line starting with two minuses and a space, and ending
248 -- with a space and two minuses. Again see the procedure title box
249 -- immediately above in the source.
251 -- 4. A full line comment where two spaces follow the two minus signs.
252 -- This is the normal comment format in GNAT style, as typified by
253 -- the comments you are reading now.
255 -- 5. A full line comment where the first character after the second
256 -- minus is a special character, i.e. a character in the ASCII
257 -- range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
258 -- comments, such as those generated by gnatprep, or those that
259 -- appear in the SPARK annotation language to be accepted.
261 -- Note: for GNAT internal files (-gnatg switch set on for the
262 -- compilation), the only special sequence recognized and allowed
263 -- is --! as generated by gnatprep.
265 procedure Check_Comment is
269 function Is_Box_Comment return Boolean;
270 -- Returns True if the last two characters on the line are -- which
271 -- characterizes a box comment (as for example follows this spec).
277 function Is_Box_Comment return Boolean is
281 -- Do we need to worry about UTF_32 line terminators here ???
284 while Source (S) not in Line_Terminator loop
288 return Source (S - 1) = '-' and then Source (S - 2) = '-';
291 -- Start of processing for Check_Comment
294 -- Can never have a non-blank character preceding the first minus
296 if Style_Check_Comments then
297 if Scan_Ptr > Source_First (Current_Source_File)
298 and then Source (Scan_Ptr - 1) > ' '
300 Error_Msg_S ("(style) space required");
304 -- For a comment that is not at the start of the line, the only
305 -- requirement is that we cannot have a non-blank character after
306 -- the second minus sign.
308 if Scan_Ptr /= First_Non_Blank_Location then
309 if Style_Check_Comments then
310 if Source (Scan_Ptr + 2) > ' ' then
311 Error_Msg ("(style) space required", Scan_Ptr + 2);
317 -- Case of a comment that is at the start of a line
320 -- First check, must be in appropriately indented column
322 if Style_Check_Indentation /= 0 then
323 if Start_Column rem Style_Check_Indentation /= 0 then
324 Error_Msg_S ("(style) bad column");
329 -- If we are not checking comments, nothing to do
331 if not Style_Check_Comments then
335 -- Case of not followed by a blank. Usually wrong, but there are
336 -- some exceptions that we permit.
338 if Source (Scan_Ptr + 2) /= ' ' then
339 C := Source (Scan_Ptr + 2);
341 -- Case of -- all on its own on a line is OK
347 -- Case of --x, x special character is OK (gnatprep/SPARK/etc.)
348 -- This is not permitted in internal GNAT implementation units
349 -- except for the case of --! as used by gnatprep output.
357 if Character'Pos (C) in 16#21# .. 16#2F#
359 Character'Pos (C) in 16#3A# .. 16#3F#
365 -- The only other case in which we allow a character after
366 -- the -- other than a space is when we have a row of minus
367 -- signs (case of header lines for a box comment for example).
370 while Source (S) >= ' ' loop
371 if Source (S) /= '-' then
372 if Is_Box_Comment then
373 Error_Space_Required (Scan_Ptr + 2);
375 Error_Msg ("(style) two spaces required", Scan_Ptr + 2);
384 -- If we are followed by a blank, then the comment is OK if the
385 -- character following this blank is another blank or a format
388 elsif Source (Scan_Ptr + 3) <= ' ' then
391 -- Here is the case where we only have one blank after the two
392 -- minus signs, which is an error unless the line ends with two
393 -- minus signs, the case of a box comment.
395 elsif not Is_Box_Comment then
396 Error_Space_Required (Scan_Ptr + 3);
405 -- In check token mode (-gnatyt), colon must be surrounded by spaces
407 procedure Check_Dot_Dot is
409 if Style_Check_Tokens then
410 Require_Preceding_Space;
411 Require_Following_Space;
419 -- In check blanks at end mode, check no blank lines precede the EOF
421 procedure Check_EOF is
423 if Style_Check_Blank_Lines then
425 -- We expect one blank line, from the EOF, but no more than one
427 if Blank_Lines = 2 then
429 ("(style) blank line not allowed at end of file",
430 Blank_Line_Location);
432 elsif Blank_Lines >= 3 then
434 ("(style) blank lines not allowed at end of file",
435 Blank_Line_Location);
440 -----------------------------------
441 -- Check_Exponentiation_Operator --
442 -----------------------------------
444 -- No spaces are required for the ** operator in GNAT style check mode
446 procedure Check_Exponentiation_Operator is
449 end Check_Exponentiation_Operator;
455 -- In check horizontal tab mode (-gnatyh), tab characters are not allowed
457 procedure Check_HT is
459 if Style_Check_Horizontal_Tabs then
460 Error_Msg_S ("(style) horizontal tab not allowed");
464 -----------------------
465 -- Check_Indentation --
466 -----------------------
468 -- In check indentation mode (-gnatyn for n a digit), a new statement or
469 -- declaration is required to start in a column that is a multiple of the
470 -- indentiation amount.
472 procedure Check_Indentation is
474 if Style_Check_Indentation /= 0 then
475 if Token_Ptr = First_Non_Blank_Location
476 and then Start_Column rem Style_Check_Indentation /= 0
478 Error_Msg_SC ("(style) bad indentation");
481 end Check_Indentation;
483 ----------------------
484 -- Check_Left_Paren --
485 ----------------------
487 -- In tone check mode (-gnatyt), left paren must not be preceded by an
488 -- identifier character or digit (a separating space is required) and
489 -- may never be followed by a space.
491 procedure Check_Left_Paren is
493 if Style_Check_Tokens then
494 if Token_Ptr > Source_First (Current_Source_File)
495 and then Identifier_Char (Source (Token_Ptr - 1))
497 Error_Space_Required (Token_Ptr);
500 Check_No_Space_After;
502 end Check_Left_Paren;
504 ---------------------------
505 -- Check_Line_Max_Length --
506 ---------------------------
508 -- In check max line length mode (-gnatym), the line length must
509 -- not exceed the permitted maximum value.
511 procedure Check_Line_Max_Length (Len : Int) is
513 if Style_Check_Max_Line_Length then
514 if Len > Style_Max_Line_Length then
516 ("(style) this line is too long",
517 Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
520 end Check_Line_Max_Length;
522 ---------------------------
523 -- Check_Line_Terminator --
524 ---------------------------
526 -- In check blanks at end mode (-gnatyb), lines may not end with a
529 -- In check form feeds mode (-gnatyf), the line terminator may not
530 -- be either of the characters FF or VT.
532 -- In check DOS line terminators node (-gnatyd), the line terminator
533 -- must be a single LF, without a following CR.
535 procedure Check_Line_Terminator (Len : Int) is
539 -- Length of line (adjusted down for blanks at end of line)
542 -- Reset count of blank lines if first line
544 if Get_Logical_Line_Number (Scan_Ptr) = 1 then
548 -- Check FF/VT terminators
550 if Style_Check_Form_Feeds then
551 if Source (Scan_Ptr) = ASCII.FF then
552 Error_Msg_S ("(style) form feed not allowed");
553 elsif Source (Scan_Ptr) = ASCII.VT then
554 Error_Msg_S ("(style) vertical tab not allowed");
558 -- Check DOS line terminator (ignore EOF, since we only get called
559 -- with an EOF if it is the last character in the buffer, and was
560 -- therefore not present in the sources
562 if Style_Check_DOS_Line_Terminator then
563 if Source (Scan_Ptr) = EOF then
565 elsif Source (Scan_Ptr) /= LF
566 or else Source (Scan_Ptr + 1) = CR
568 Error_Msg_S ("(style) incorrect line terminator");
572 -- Remove trailing spaces
575 while L > 0 and then Is_White_Space (Source (S - 1)) loop
580 -- Issue message for blanks at end of line if option enabled
582 if Style_Check_Blanks_At_End and then L < Len then
584 ("(style) trailing spaces not permitted", S);
587 -- Deal with empty (blank) line
591 -- Increment blank line count
593 Blank_Lines := Blank_Lines + 1;
595 -- If first blank line, record location for later error message
597 if Blank_Lines = 1 then
598 Blank_Line_Location := Scan_Ptr;
601 -- Non-blank line, check for previous multiple blank lines
604 if Style_Check_Blank_Lines and then Blank_Lines > 1 then
606 ("(style) multiple blank lines", Blank_Line_Location);
609 -- And reset blank line count
613 end Check_Line_Terminator;
615 --------------------------
616 -- Check_No_Space_After --
617 --------------------------
619 procedure Check_No_Space_After is
623 if Is_White_Space (Source (Scan_Ptr)) then
625 -- Allow one or more spaces if followed by comment
629 if Source (S) = '-' and then Source (S + 1) = '-' then
632 elsif Is_White_Space (Source (S)) then
640 Error_Space_Not_Allowed (Scan_Ptr);
642 end Check_No_Space_After;
644 ---------------------------
645 -- Check_No_Space_Before --
646 ---------------------------
648 procedure Check_No_Space_Before is
650 if Token_Ptr > First_Non_Blank_Location
651 and then Source (Token_Ptr - 1) <= ' '
653 Error_Space_Not_Allowed (Token_Ptr - 1);
655 end Check_No_Space_Before;
657 -----------------------
658 -- Check_Pragma_Name --
659 -----------------------
661 -- In check pragma casing mode (-gnatyp), pragma names must be mixed
662 -- case, i.e. start with an upper case letter, and otherwise lower case,
663 -- except after an underline character.
665 procedure Check_Pragma_Name is
667 if Style_Check_Pragma_Casing then
668 if Determine_Token_Casing /= Mixed_Case then
669 Error_Msg_SC ("(style) bad capitalization, mixed case required");
672 end Check_Pragma_Name;
674 -----------------------
675 -- Check_Right_Paren --
676 -----------------------
678 -- In check tokens mode (-gnatyt), right paren must never be preceded by
679 -- a space unless it is the initial non-blank character on the line.
681 procedure Check_Right_Paren is
683 if Style_Check_Tokens then
684 Check_No_Space_Before;
686 end Check_Right_Paren;
688 ---------------------
689 -- Check_Semicolon --
690 ---------------------
692 -- In check tokens mode (-gnatyt), semicolon does not permit a preceding
693 -- space and a following space is required.
695 procedure Check_Semicolon is
697 if Style_Check_Tokens then
698 Check_No_Space_Before;
700 if Source (Scan_Ptr) > ' ' then
701 Error_Space_Required (Scan_Ptr);
706 -------------------------------
707 -- Check_Separate_Stmt_Lines --
708 -------------------------------
710 procedure Check_Separate_Stmt_Lines is
712 if Style_Check_Separate_Stmt_Lines then
713 Check_Separate_Stmt_Lines_Cont;
715 end Check_Separate_Stmt_Lines;
717 ------------------------------------
718 -- Check_Separate_Stmt_Lines_Cont --
719 ------------------------------------
721 procedure Check_Separate_Stmt_Lines_Cont is
725 -- Skip past white space
728 while Is_White_Space (Source (S)) loop
732 -- Line terminator is OK
734 if Source (S) in Line_Terminator then
739 elsif Source (S) = '-' and then Source (S + 1) = '-' then
742 -- ABORT keyword is OK after THEN (THEN ABORT case)
744 elsif Token = Tok_Then
745 and then (Source (S + 0) = 'a' or else Source (S + 0) = 'A')
746 and then (Source (S + 1) = 'b' or else Source (S + 1) = 'B')
747 and then (Source (S + 2) = 'o' or else Source (S + 2) = 'O')
748 and then (Source (S + 3) = 'r' or else Source (S + 3) = 'R')
749 and then (Source (S + 4) = 't' or else Source (S + 4) = 'T')
750 and then (Source (S + 5) in Line_Terminator
751 or else Is_White_Space (Source (S + 5)))
755 -- PRAGMA keyword is OK after ELSE
757 elsif Token = Tok_Else
758 and then (Source (S + 0) = 'p' or else Source (S + 0) = 'P')
759 and then (Source (S + 1) = 'r' or else Source (S + 1) = 'R')
760 and then (Source (S + 2) = 'a' or else Source (S + 2) = 'A')
761 and then (Source (S + 3) = 'g' or else Source (S + 3) = 'G')
762 and then (Source (S + 4) = 'm' or else Source (S + 4) = 'M')
763 and then (Source (S + 5) = 'a' or else Source (S + 5) = 'A')
764 and then (Source (S + 6) in Line_Terminator
765 or else Is_White_Space (Source (S + 6)))
769 -- Otherwise we have the style violation we are looking for
772 if Token = Tok_Then then
774 ("(style) no statements may follow THEN on same line", S);
777 ("(style) no statements may follow ELSE on same line", S);
780 end Check_Separate_Stmt_Lines_Cont;
786 -- In check if then layout mode (-gnatyi), we expect a THEN keyword
787 -- to appear either on the same line as the IF, or on a separate line
788 -- after multiple conditions. In any case, it may not appear on the
789 -- line immediately following the line with the IF.
791 procedure Check_Then (If_Loc : Source_Ptr) is
793 if Style_Check_If_Then_Layout then
794 if Get_Physical_Line_Number (Token_Ptr) =
795 Get_Physical_Line_Number (If_Loc) + 1
797 Error_Msg_SC ("(style) misplaced THEN");
802 -------------------------------
803 -- Check_Unary_Plus_Or_Minus --
804 -------------------------------
806 -- In check tokem mode (-gnatyt), unary plus or minus must not be
807 -- followed by a space.
809 procedure Check_Unary_Plus_Or_Minus is
811 if Style_Check_Tokens then
812 Check_No_Space_After;
814 end Check_Unary_Plus_Or_Minus;
816 ------------------------
817 -- Check_Vertical_Bar --
818 ------------------------
820 -- In check token mode (-gnatyt), vertical bar must be surrounded by spaces
822 procedure Check_Vertical_Bar is
824 if Style_Check_Tokens then
825 Require_Preceding_Space;
826 Require_Following_Space;
828 end Check_Vertical_Bar;
830 -----------------------
831 -- Check_Xtra_Parens --
832 -----------------------
834 procedure Check_Xtra_Parens (Loc : Source_Ptr) is
836 if Style_Check_Xtra_Parens then
837 Error_Msg ("redundant parentheses?", Loc);
839 end Check_Xtra_Parens;
841 ----------------------------
842 -- Determine_Token_Casing --
843 ----------------------------
845 function Determine_Token_Casing return Casing_Type is
847 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
848 end Determine_Token_Casing;
850 -----------------------------
851 -- Error_Space_Not_Allowed --
852 -----------------------------
854 procedure Error_Space_Not_Allowed (S : Source_Ptr) is
856 Error_Msg ("(style) space not allowed", S);
857 end Error_Space_Not_Allowed;
859 --------------------------
860 -- Error_Space_Required --
861 --------------------------
863 procedure Error_Space_Required (S : Source_Ptr) is
865 Error_Msg ("(style) space required", S);
866 end Error_Space_Required;
872 function Is_White_Space (C : Character) return Boolean is
874 return C = ' ' or else C = HT;
881 function Mode_In_Check return Boolean is
883 return Style_Check and Style_Check_Mode_In;
890 -- In check end/exit labels mode (-gnatye), always require the name of
891 -- a subprogram or package to be present on the END, so this is an error.
893 procedure No_End_Name (Name : Node_Id) is
895 if Style_Check_End_Labels then
896 Error_Msg_Node_1 := Name;
897 Error_Msg_SP ("(style) `END &` required");
905 -- In check end/exit labels mode (-gnatye), always require the name of
906 -- the loop to be present on the EXIT when exiting a named loop.
908 procedure No_Exit_Name (Name : Node_Id) is
910 if Style_Check_End_Labels then
911 Error_Msg_Node_1 := Name;
912 Error_Msg_SP ("(style) `EXIT &` required");
916 ----------------------------
917 -- Non_Lower_Case_Keyword --
918 ----------------------------
920 -- In check casing mode (-gnatyk), reserved keywords must be be spelled
921 -- in all lower case (excluding keywords range, access, delta and digits
922 -- used as attribute designators).
924 procedure Non_Lower_Case_Keyword is
926 if Style_Check_Keyword_Casing then
927 Error_Msg_SC ("(style) reserved words must be all lower case");
929 end Non_Lower_Case_Keyword;
931 -----------------------------
932 -- Require_Following_Space --
933 -----------------------------
935 procedure Require_Following_Space is
937 if Source (Scan_Ptr) > ' ' then
938 Error_Space_Required (Scan_Ptr);
940 end Require_Following_Space;
942 -----------------------------
943 -- Require_Preceding_Space --
944 -----------------------------
946 procedure Require_Preceding_Space is
948 if Token_Ptr > Source_First (Current_Source_File)
949 and then Source (Token_Ptr - 1) > ' '
951 Error_Space_Required (Token_Ptr);
953 end Require_Preceding_Space;
955 ---------------------
956 -- RM_Column_Check --
957 ---------------------
959 function RM_Column_Check return Boolean is
961 return Style_Check and Style_Check_Layout;