OSDN Git Service

2010-06-22 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / styleg.adb
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-2010, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 --  This version of the Style package implements the standard GNAT style
27 --  checking rules. For documentation of these rules, see comments on the
28 --  individual procedures.
29
30 with Atree;    use Atree;
31 with Casing;   use Casing;
32 with Csets;    use Csets;
33 with Einfo;    use Einfo;
34 with Err_Vars; use Err_Vars;
35 with Opt;      use Opt;
36 with Scans;    use Scans;
37 with Sinfo;    use Sinfo;
38 with Sinput;   use Sinput;
39 with Stylesw;  use Stylesw;
40
41 package body Styleg is
42
43    use ASCII;
44
45    Blank_Lines : Nat := 0;
46    --  Counts number of empty lines seen. Reset to zero if a non-empty line
47    --  is encountered. Used to check for trailing blank lines in Check_EOF,
48    --  and for multiple blank lines.
49
50    Blank_Line_Location : Source_Ptr;
51    --  Remembers location of first blank line in a series. Used to issue an
52    --  appropriate diagnostic if subsequent blank lines or the end of file
53    --  is encountered.
54
55    -----------------------
56    -- Local Subprograms --
57    -----------------------
58
59    procedure Check_No_Space_After;
60    --  Checks that there is a non-white space character after the current
61    --  token, or white space followed by a comment, or the end of line.
62    --  Issue error message if not.
63
64    procedure Check_No_Space_Before;
65    --  Check that token is first token on line, or else is not preceded
66    --  by white space. Signal error of space not allowed if not.
67
68    procedure Check_Separate_Stmt_Lines_Cont;
69    --  Non-inlined continuation of Check_Separate_Stmt_Lines
70
71    function Determine_Token_Casing return Casing_Type;
72    --  Determine casing of current token
73
74    procedure Error_Space_Not_Allowed (S : Source_Ptr);
75    --  Posts an error message indicating that a space is not allowed
76    --  at the given source location.
77
78    procedure Error_Space_Required (S : Source_Ptr);
79    --  Posts an error message indicating that a space is required at
80    --  the given source location.
81
82    function Is_White_Space (C : Character) return Boolean;
83    pragma Inline (Is_White_Space);
84    --  Returns True for space, HT, VT or FF, False otherwise
85
86    procedure Require_Following_Space;
87    pragma Inline (Require_Following_Space);
88    --  Require token to be followed by white space. Used only if in GNAT
89    --  style checking mode.
90
91    procedure Require_Preceding_Space;
92    pragma Inline (Require_Preceding_Space);
93    --  Require token to be preceded by white space. Used only if in GNAT
94    --  style checking mode.
95
96    ----------------------
97    -- Check_Abs_Or_Not --
98    ----------------------
99
100    --  In check tokens mode (-gnatyt), ABS/NOT must be followed by a space
101
102    procedure Check_Abs_Not is
103    begin
104       if Style_Check_Tokens then
105          if Source (Scan_Ptr) > ' ' then
106             Error_Space_Required (Scan_Ptr);
107          end if;
108       end if;
109    end Check_Abs_Not;
110
111    ----------------------
112    -- Check_Apostrophe --
113    ----------------------
114
115    --  Do not allow space before or after apostrophe
116
117    procedure Check_Apostrophe is
118    begin
119       if Style_Check_Tokens then
120          Check_No_Space_After;
121       end if;
122    end Check_Apostrophe;
123
124    -----------------
125    -- Check_Arrow --
126    -----------------
127
128    --  In check tokens mode (-gnatys), arrow must be surrounded by spaces
129
130    procedure Check_Arrow is
131    begin
132       if Style_Check_Tokens then
133          Require_Preceding_Space;
134          Require_Following_Space;
135       end if;
136    end Check_Arrow;
137
138    --------------------------
139    -- Check_Attribute_Name --
140    --------------------------
141
142    --  In check attribute casing mode (-gnatya), attribute names must be
143    --  mixed case, i.e. start with an upper case letter, and otherwise
144    --  lower case, except after an underline character.
145
146    procedure Check_Attribute_Name (Reserved : Boolean) is
147       pragma Warnings (Off, Reserved);
148    begin
149       if Style_Check_Attribute_Casing then
150          if Determine_Token_Casing /= Mixed_Case then
151             Error_Msg_SC -- CODEFIX
152               ("(style) bad capitalization, mixed case required");
153          end if;
154       end if;
155    end Check_Attribute_Name;
156
157    ---------------------------
158    -- Check_Binary_Operator --
159    ---------------------------
160
161    --  In check token mode (-gnatyt), binary operators other than the special
162    --  case of exponentiation require surrounding space characters.
163
164    procedure Check_Binary_Operator is
165    begin
166       if Style_Check_Tokens then
167          Require_Preceding_Space;
168          Require_Following_Space;
169       end if;
170    end Check_Binary_Operator;
171
172    ----------------------------
173    -- Check_Boolean_Operator --
174    ----------------------------
175
176    procedure Check_Boolean_Operator (Node : Node_Id) is
177
178       function OK_Boolean_Operand (N : Node_Id) return Boolean;
179       --  Returns True for simple variable, or "not X1" or "X1 and X2" or
180       --  "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
181
182       ------------------------
183       -- OK_Boolean_Operand --
184       ------------------------
185
186       function OK_Boolean_Operand (N : Node_Id) return Boolean is
187       begin
188          if Nkind_In (N, N_Identifier, N_Expanded_Name) then
189             return True;
190
191          elsif Nkind (N) = N_Op_Not then
192             return OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
193
194          elsif Nkind_In (N, N_Op_And, N_Op_Or) then
195             return OK_Boolean_Operand (Original_Node (Left_Opnd (N)))
196                      and then
197                    OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
198
199          else
200             return False;
201          end if;
202       end OK_Boolean_Operand;
203
204    --  Start of processig for Check_Boolean_Operator
205    begin
206       if Style_Check_Boolean_And_Or
207         and then Comes_From_Source (Node)
208       then
209          declare
210             Orig : constant Node_Id := Original_Node (Node);
211
212          begin
213             if Nkind_In (Orig, N_Op_And, N_Op_Or) then
214                declare
215                   L : constant Node_Id := Original_Node (Left_Opnd  (Orig));
216                   R : constant Node_Id := Original_Node (Right_Opnd (Orig));
217
218                begin
219                   --  First OK case, simple boolean constants/identifiers
220
221                   if OK_Boolean_Operand (L)
222                        and then
223                      OK_Boolean_Operand (R)
224                   then
225                      return;
226
227                   --  Second OK case, modular types
228
229                   elsif Is_Modular_Integer_Type (Etype (Node)) then
230                      return;
231
232                   --  Third OK case, array types
233
234                   elsif Is_Array_Type (Etype (Node)) then
235                      return;
236
237                   --  Otherwise we have an error
238
239                   elsif Nkind (Orig) = N_Op_And then
240                      Error_Msg -- CODEFIX
241                        ("(style) `AND THEN` required", Sloc (Orig));
242                   else
243                      Error_Msg -- CODEFIX
244                        ("(style) `OR ELSE` required", Sloc (Orig));
245                   end if;
246                end;
247             end if;
248          end;
249       end if;
250    end Check_Boolean_Operator;
251
252    ---------------
253    -- Check_Box --
254    ---------------
255
256    --  In check token mode (-gnatyt), box must be preceded by a space or by
257    --  a left parenthesis. Spacing checking on the surrounding tokens takes
258    --  care of the remaining checks.
259
260    procedure Check_Box is
261    begin
262       if Style_Check_Tokens then
263          if Prev_Token /= Tok_Left_Paren then
264             Require_Preceding_Space;
265          end if;
266       end if;
267    end Check_Box;
268
269    -----------------
270    -- Check_Colon --
271    -----------------
272
273    --  In check token mode (-gnatyt), colon must be surrounded by spaces
274
275    procedure Check_Colon is
276    begin
277       if Style_Check_Tokens then
278          Require_Preceding_Space;
279          Require_Following_Space;
280       end if;
281    end Check_Colon;
282
283    -----------------------
284    -- Check_Colon_Equal --
285    -----------------------
286
287    --  In check token mode (-gnatyt), := must be surrounded by spaces
288
289    procedure Check_Colon_Equal is
290    begin
291       if Style_Check_Tokens then
292          Require_Preceding_Space;
293          Require_Following_Space;
294       end if;
295    end Check_Colon_Equal;
296
297    -----------------
298    -- Check_Comma --
299    -----------------
300
301    --  In check token mode (-gnatyt), comma must be either the first
302    --  token on a line, or be preceded by a non-blank character.
303    --  It must also always be followed by a blank.
304
305    procedure Check_Comma is
306    begin
307       if Style_Check_Tokens then
308          Check_No_Space_Before;
309
310          if Source (Scan_Ptr) > ' ' then
311             Error_Space_Required (Scan_Ptr);
312          end if;
313       end if;
314    end Check_Comma;
315
316    -------------------
317    -- Check_Comment --
318    -------------------
319
320    --  In check comment mode (-gnatyc) there are several requirements on the
321    --  format of comments. The following are permissible comment formats:
322
323    --    1. Any comment that is not at the start of a line, i.e. where the
324    --       initial minuses are not the first non-blank characters on the
325    --       line must have at least one blank after the second minus or a
326    --       special character as defined in rule 5.
327
328    --    2. A row of all minuses of any length is permitted (see procedure
329    --       box above in the source of this routine).
330
331    --    3. A comment line starting with two minuses and a space, and ending
332    --       with a space and two minuses. Again see the procedure title box
333    --       immediately above in the source.
334
335    --    4. A full line comment where two spaces follow the two minus signs.
336    --       This is the normal comment format in GNAT style, as typified by
337    --       the comments you are reading now.
338
339    --    5. A full line comment where the first character after the second
340    --       minus is a special character, i.e. a character in the ASCII
341    --       range 16#21#..16#2F# or 16#3A#..16#3F#. This allows special
342    --       comments, such as those generated by gnatprep, or those that
343    --       appear in the SPARK annotation language to be accepted.
344
345    --       Note: for GNAT internal files (-gnatg switch set on for the
346    --       compilation), the only special sequence recognized and allowed
347    --       is --! as generated by gnatprep.
348
349    --    6. In addition, the comment must be properly indented if comment
350    --       indentation checking is active (Style_Check_Indentation non-zero).
351    --       Either the start column must be a multiple of this indentation,
352    --       or the indentation must match that of the next non-blank line.
353
354    procedure Check_Comment is
355       S : Source_Ptr;
356       C : Character;
357
358       function Is_Box_Comment return Boolean;
359       --  Returns True if the last two characters on the line are -- which
360       --  characterizes a box comment (as for example follows this spec).
361
362       function Is_Special_Character (C : Character) return Boolean;
363       --  Determines if C is a special character (see rule 5 above)
364
365       function Same_Column_As_Next_Non_Blank_Line return Boolean;
366       --  Called for a full line comment. If the indentation of this comment
367       --  matches that of the next non-blank line in the source, then True is
368       --  returned, otherwise False.
369
370       --------------------
371       -- Is_Box_Comment --
372       --------------------
373
374       function Is_Box_Comment return Boolean is
375          S : Source_Ptr;
376
377       begin
378          --  Do we need to worry about UTF_32 line terminators here ???
379
380          S := Scan_Ptr + 3;
381          while Source (S) not in Line_Terminator loop
382             S := S + 1;
383          end loop;
384
385          return Source (S - 1) = '-' and then Source (S - 2) = '-';
386       end Is_Box_Comment;
387
388       --------------------------
389       -- Is_Special_Character --
390       --------------------------
391
392       function Is_Special_Character (C : Character) return Boolean is
393       begin
394          if GNAT_Mode then
395             return C = '!';
396          else
397             return
398               Character'Pos (C) in 16#21# .. 16#2F#
399                 or else
400               Character'Pos (C) in 16#3A# .. 16#3F#;
401          end if;
402       end Is_Special_Character;
403
404       ----------------------------------------
405       -- Same_Column_As_Next_Non_Blank_Line --
406       ----------------------------------------
407
408       function Same_Column_As_Next_Non_Blank_Line return Boolean is
409          P : Source_Ptr;
410
411       begin
412          --  Step to end of line
413
414          P := Scan_Ptr + 2;
415          while Source (P) not in Line_Terminator loop
416             P := P + 1;
417          end loop;
418
419          --  Step past blanks, and line terminators (UTF_32 case???)
420
421          while Source (P) <= ' ' and then Source (P) /= EOF loop
422             P := P + 1;
423          end loop;
424
425          --  Compare columns
426
427          return Get_Column_Number (Scan_Ptr) = Get_Column_Number (P);
428       end Same_Column_As_Next_Non_Blank_Line;
429
430    --  Start of processing for Check_Comment
431
432    begin
433       --  Can never have a non-blank character preceding the first minus
434
435       if Style_Check_Comments then
436          if Scan_Ptr > Source_First (Current_Source_File)
437            and then Source (Scan_Ptr - 1) > ' '
438          then
439             Error_Msg_S -- CODEFIX
440               ("(style) space required");
441          end if;
442       end if;
443
444       --  For a comment that is not at the start of the line, the only
445       --  requirement is that we cannot have a non-blank character after
446       --  the second minus sign or a special character.
447
448       if Scan_Ptr /= First_Non_Blank_Location then
449          if Style_Check_Comments then
450             if Source (Scan_Ptr + 2) > ' '
451               and then not Is_Special_Character (Source (Scan_Ptr + 2))
452             then
453                Error_Msg -- CODEFIX
454                  ("(style) space required", Scan_Ptr + 2);
455             end if;
456          end if;
457
458          return;
459
460       --  Case of a comment that is at the start of a line
461
462       else
463          --  First check, must be in appropriately indented column
464
465          if Style_Check_Indentation /= 0 then
466             if Start_Column rem Style_Check_Indentation /= 0 then
467                if not Same_Column_As_Next_Non_Blank_Line then
468                   Error_Msg_S -- CODEFIX
469                     ("(style) bad column");
470                end if;
471
472                return;
473             end if;
474          end if;
475
476          --  If we are not checking comments, nothing more to do
477
478          if not Style_Check_Comments then
479             return;
480          end if;
481
482          --  Case of not followed by a blank. Usually wrong, but there are
483          --  some exceptions that we permit.
484
485          if Source (Scan_Ptr + 2) /= ' ' then
486             C := Source (Scan_Ptr + 2);
487
488             --  Case of -- all on its own on a line is OK
489
490             if C < ' ' then
491                return;
492             end if;
493
494             --  Case of --x, x special character is OK (gnatprep/SPARK/etc.)
495             --  This is not permitted in internal GNAT implementation units
496             --  except for the case of --! as used by gnatprep output.
497
498             if Is_Special_Character (C) then
499                return;
500             end if;
501
502             --  The only other case in which we allow a character after
503             --  the -- other than a space is when we have a row of minus
504             --  signs (case of header lines for a box comment for example).
505
506             S := Scan_Ptr + 2;
507             while Source (S) >= ' ' loop
508                if Source (S) /= '-' then
509                   if Is_Box_Comment then
510                      Error_Space_Required (Scan_Ptr + 2);
511                   else
512                      Error_Msg -- CODEFIX
513                        ("(style) two spaces required", Scan_Ptr + 2);
514                   end if;
515
516                   return;
517                end if;
518
519                S := S + 1;
520             end loop;
521
522          --  If we are followed by a blank, then the comment is OK if the
523          --  character following this blank is another blank or a format
524          --  effector.
525
526          elsif Source (Scan_Ptr + 3) <= ' ' then
527             return;
528
529          --  Here is the case where we only have one blank after the two
530          --  minus signs, which is an error unless the line ends with two
531          --  minus signs, the case of a box comment.
532
533          elsif not Is_Box_Comment then
534             Error_Space_Required (Scan_Ptr + 3);
535          end if;
536       end if;
537    end Check_Comment;
538
539    -------------------
540    -- Check_Dot_Dot --
541    -------------------
542
543    --  In check token mode (-gnatyt), colon must be surrounded by spaces
544
545    procedure Check_Dot_Dot is
546    begin
547       if Style_Check_Tokens then
548          Require_Preceding_Space;
549          Require_Following_Space;
550       end if;
551    end Check_Dot_Dot;
552
553    ---------------
554    -- Check_EOF --
555    ---------------
556
557    --  In check blanks at end mode, check no blank lines precede the EOF
558
559    procedure Check_EOF is
560    begin
561       if Style_Check_Blank_Lines then
562
563          --  We expect one blank line, from the EOF, but no more than one
564
565          if Blank_Lines = 2 then
566             Error_Msg -- CODEFIX
567               ("(style) blank line not allowed at end of file",
568                Blank_Line_Location);
569
570          elsif Blank_Lines >= 3 then
571             Error_Msg -- CODEFIX
572               ("(style) blank lines not allowed at end of file",
573                Blank_Line_Location);
574          end if;
575       end if;
576    end Check_EOF;
577
578    -----------------------------------
579    -- Check_Exponentiation_Operator --
580    -----------------------------------
581
582    --  No spaces are required for the ** operator in GNAT style check mode
583
584    procedure Check_Exponentiation_Operator is
585    begin
586       null;
587    end Check_Exponentiation_Operator;
588
589    --------------
590    -- Check_HT --
591    --------------
592
593    --  In check horizontal tab mode (-gnatyh), tab characters are not allowed
594
595    procedure Check_HT is
596    begin
597       if Style_Check_Horizontal_Tabs then
598          Error_Msg_S -- CODEFIX
599            ("(style) horizontal tab not allowed");
600       end if;
601    end Check_HT;
602
603    -----------------------
604    -- Check_Indentation --
605    -----------------------
606
607    --  In check indentation mode (-gnatyn for n a digit), a new statement or
608    --  declaration is required to start in a column that is a multiple of the
609    --  indentation amount.
610
611    procedure Check_Indentation is
612    begin
613       if Style_Check_Indentation /= 0 then
614          if Token_Ptr = First_Non_Blank_Location
615            and then Start_Column rem Style_Check_Indentation /= 0
616          then
617             Error_Msg_SC -- CODEFIX
618               ("(style) bad indentation");
619          end if;
620       end if;
621    end Check_Indentation;
622
623    ----------------------
624    -- Check_Left_Paren --
625    ----------------------
626
627    --  In tone check mode (-gnatyt), left paren must not be preceded by an
628    --  identifier character or digit (a separating space is required) and
629    --  may never be followed by a space.
630
631    procedure Check_Left_Paren is
632    begin
633       if Style_Check_Tokens then
634          if Token_Ptr > Source_First (Current_Source_File)
635            and then Identifier_Char (Source (Token_Ptr - 1))
636          then
637             Error_Space_Required (Token_Ptr);
638          end if;
639
640          Check_No_Space_After;
641       end if;
642    end Check_Left_Paren;
643
644    ---------------------------
645    -- Check_Line_Max_Length --
646    ---------------------------
647
648    --  In check max line length mode (-gnatym), the line length must
649    --  not exceed the permitted maximum value.
650
651    procedure Check_Line_Max_Length (Len : Int) is
652    begin
653       if Style_Check_Max_Line_Length then
654          if Len > Style_Max_Line_Length then
655             Error_Msg
656               ("(style) this line is too long",
657                Current_Line_Start + Source_Ptr (Style_Max_Line_Length));
658          end if;
659       end if;
660    end Check_Line_Max_Length;
661
662    ---------------------------
663    -- Check_Line_Terminator --
664    ---------------------------
665
666    --  In check blanks at end mode (-gnatyb), lines may not end with a
667    --  trailing space.
668
669    --  In check form feeds mode (-gnatyf), the line terminator may not
670    --  be either of the characters FF or VT.
671
672    --  In check DOS line terminators node (-gnatyd), the line terminator
673    --  must be a single LF, without a following CR.
674
675    procedure Check_Line_Terminator (Len : Int) is
676       S : Source_Ptr;
677
678       L : Int := Len;
679       --  Length of line (adjusted down for blanks at end of line)
680
681    begin
682       --  Reset count of blank lines if first line
683
684       if Get_Logical_Line_Number (Scan_Ptr) = 1 then
685          Blank_Lines := 0;
686       end if;
687
688       --  Check FF/VT terminators
689
690       if Style_Check_Form_Feeds then
691          if Source (Scan_Ptr) = ASCII.FF then
692             Error_Msg_S -- CODEFIX
693               ("(style) form feed not allowed");
694          elsif Source (Scan_Ptr) = ASCII.VT then
695             Error_Msg_S -- CODEFIX
696               ("(style) vertical tab not allowed");
697          end if;
698       end if;
699
700       --  Check DOS line terminator
701
702       if Style_Check_DOS_Line_Terminator then
703
704       --  Ignore EOF, since we only get called with an EOF if it is the last
705       --  character in the buffer (and was therefore not in the source file),
706       --  since the terminating EOF is added to stop the scan.
707
708          if Source (Scan_Ptr) = EOF then
709             null;
710
711          --  Bad terminator if we don't have an LF
712
713          elsif Source (Scan_Ptr) /= LF then
714             Error_Msg_S ("(style) incorrect line terminator");
715          end if;
716       end if;
717
718       --  Remove trailing spaces
719
720       S := Scan_Ptr;
721       while L > 0 and then Is_White_Space (Source (S - 1)) loop
722          S := S - 1;
723          L := L - 1;
724       end loop;
725
726       --  Issue message for blanks at end of line if option enabled
727
728       if Style_Check_Blanks_At_End and then L < Len then
729          Error_Msg -- CODEFIX
730            ("(style) trailing spaces not permitted", S);
731       end if;
732
733       --  Deal with empty (blank) line
734
735       if L = 0 then
736
737          --  Increment blank line count
738
739          Blank_Lines := Blank_Lines + 1;
740
741          --  If first blank line, record location for later error message
742
743          if Blank_Lines = 1 then
744             Blank_Line_Location := Scan_Ptr;
745          end if;
746
747       --  Non-blank line, check for previous multiple blank lines
748
749       else
750          if Style_Check_Blank_Lines and then Blank_Lines > 1 then
751             Error_Msg -- CODEFIX
752               ("(style) multiple blank lines", Blank_Line_Location);
753          end if;
754
755          --  And reset blank line count
756
757          Blank_Lines := 0;
758       end if;
759    end Check_Line_Terminator;
760
761    --------------------------
762    -- Check_No_Space_After --
763    --------------------------
764
765    procedure Check_No_Space_After is
766       S : Source_Ptr;
767
768    begin
769       if Is_White_Space (Source (Scan_Ptr)) then
770
771          --  Allow one or more spaces if followed by comment
772
773          S := Scan_Ptr + 1;
774          loop
775             if Source (S) = '-' and then Source (S + 1) = '-' then
776                return;
777
778             elsif Is_White_Space (Source (S)) then
779                S := S + 1;
780
781             else
782                exit;
783             end if;
784          end loop;
785
786          Error_Space_Not_Allowed (Scan_Ptr);
787       end if;
788    end Check_No_Space_After;
789
790    ---------------------------
791    -- Check_No_Space_Before --
792    ---------------------------
793
794    procedure Check_No_Space_Before is
795    begin
796       if Token_Ptr > First_Non_Blank_Location
797          and then Source (Token_Ptr - 1) <= ' '
798       then
799          Error_Space_Not_Allowed (Token_Ptr - 1);
800       end if;
801    end Check_No_Space_Before;
802
803    -----------------------
804    -- Check_Pragma_Name --
805    -----------------------
806
807    --  In check pragma casing mode (-gnatyp), pragma names must be mixed
808    --  case, i.e. start with an upper case letter, and otherwise lower case,
809    --  except after an underline character.
810
811    procedure Check_Pragma_Name is
812    begin
813       if Style_Check_Pragma_Casing then
814          if Determine_Token_Casing /= Mixed_Case then
815             Error_Msg_SC -- CODEFIX
816               ("(style) bad capitalization, mixed case required");
817          end if;
818       end if;
819    end Check_Pragma_Name;
820
821    -----------------------
822    -- Check_Right_Paren --
823    -----------------------
824
825    --  In check tokens mode (-gnatyt), right paren must not be immediately
826    --  followed by an identifier character, and must never be preceded by
827    --  a space unless it is the initial non-blank character on the line.
828
829    procedure Check_Right_Paren is
830    begin
831       if Style_Check_Tokens then
832          if Identifier_Char (Source (Token_Ptr + 1)) then
833             Error_Space_Required (Token_Ptr + 1);
834          end if;
835
836          Check_No_Space_Before;
837       end if;
838    end Check_Right_Paren;
839
840    ---------------------
841    -- Check_Semicolon --
842    ---------------------
843
844    --  In check tokens mode (-gnatyt), semicolon does not permit a preceding
845    --  space and a following space is required.
846
847    procedure Check_Semicolon is
848    begin
849       if Style_Check_Tokens then
850          Check_No_Space_Before;
851
852          if Source (Scan_Ptr) > ' ' then
853             Error_Space_Required (Scan_Ptr);
854          end if;
855       end if;
856    end Check_Semicolon;
857
858    -------------------------------
859    -- Check_Separate_Stmt_Lines --
860    -------------------------------
861
862    procedure Check_Separate_Stmt_Lines is
863    begin
864       if Style_Check_Separate_Stmt_Lines then
865          Check_Separate_Stmt_Lines_Cont;
866       end if;
867    end Check_Separate_Stmt_Lines;
868
869    ------------------------------------
870    -- Check_Separate_Stmt_Lines_Cont --
871    ------------------------------------
872
873    procedure Check_Separate_Stmt_Lines_Cont is
874       S : Source_Ptr;
875
876    begin
877       --  Skip past white space
878
879       S := Scan_Ptr;
880       while Is_White_Space (Source (S)) loop
881          S := S + 1;
882       end loop;
883
884       --  Line terminator is OK
885
886       if Source (S) in Line_Terminator then
887          return;
888
889       --  Comment is OK
890
891       elsif Source (S) = '-' and then Source (S + 1) = '-' then
892          return;
893
894       --  ABORT keyword is OK after THEN (THEN ABORT case)
895
896       elsif Token = Tok_Then
897         and then (Source (S + 0) = 'a' or else Source (S + 0) = 'A')
898         and then (Source (S + 1) = 'b' or else Source (S + 1) = 'B')
899         and then (Source (S + 2) = 'o' or else Source (S + 2) = 'O')
900         and then (Source (S + 3) = 'r' or else Source (S + 3) = 'R')
901         and then (Source (S + 4) = 't' or else Source (S + 4) = 'T')
902         and then (Source (S + 5) in Line_Terminator
903                    or else Is_White_Space (Source (S + 5)))
904       then
905          return;
906
907       --  PRAGMA keyword is OK after ELSE
908
909       elsif Token = Tok_Else
910         and then (Source (S + 0) = 'p' or else Source (S + 0) = 'P')
911         and then (Source (S + 1) = 'r' or else Source (S + 1) = 'R')
912         and then (Source (S + 2) = 'a' or else Source (S + 2) = 'A')
913         and then (Source (S + 3) = 'g' or else Source (S + 3) = 'G')
914         and then (Source (S + 4) = 'm' or else Source (S + 4) = 'M')
915         and then (Source (S + 5) = 'a' or else Source (S + 5) = 'A')
916         and then (Source (S + 6) in Line_Terminator
917                    or else Is_White_Space (Source (S + 6)))
918       then
919          return;
920
921          --  Otherwise we have the style violation we are looking for
922
923       else
924          if Token = Tok_Then then
925             Error_Msg -- CODEFIX
926               ("(style) no statements may follow THEN on same line", S);
927          else
928             Error_Msg
929               ("(style) no statements may follow ELSE on same line", S);
930          end if;
931       end if;
932    end Check_Separate_Stmt_Lines_Cont;
933
934    ----------------
935    -- Check_Then --
936    ----------------
937
938    --  In check if then layout mode (-gnatyi), we expect a THEN keyword
939    --  to appear either on the same line as the IF, or on a separate line
940    --  after multiple conditions. In any case, it may not appear on the
941    --  line immediately following the line with the IF.
942
943    procedure Check_Then (If_Loc : Source_Ptr) is
944    begin
945       if Style_Check_If_Then_Layout then
946          if Get_Physical_Line_Number (Token_Ptr) =
947             Get_Physical_Line_Number (If_Loc) + 1
948          then
949             Error_Msg_SC ("(style) misplaced THEN");
950          end if;
951       end if;
952    end Check_Then;
953
954    -------------------------------
955    -- Check_Unary_Plus_Or_Minus --
956    -------------------------------
957
958    --  In check token mode (-gnatyt), unary plus or minus must not be
959    --  followed by a space.
960
961    procedure Check_Unary_Plus_Or_Minus is
962    begin
963       if Style_Check_Tokens then
964          Check_No_Space_After;
965       end if;
966    end Check_Unary_Plus_Or_Minus;
967
968    ------------------------
969    -- Check_Vertical_Bar --
970    ------------------------
971
972    --  In check token mode (-gnatyt), vertical bar must be surrounded by spaces
973
974    procedure Check_Vertical_Bar is
975    begin
976       if Style_Check_Tokens then
977          Require_Preceding_Space;
978          Require_Following_Space;
979       end if;
980    end Check_Vertical_Bar;
981
982    -----------------------
983    -- Check_Xtra_Parens --
984    -----------------------
985
986    procedure Check_Xtra_Parens (Loc : Source_Ptr) is
987    begin
988       if Style_Check_Xtra_Parens then
989          Error_Msg -- CODEFIX
990            ("redundant parentheses?", Loc);
991       end if;
992    end Check_Xtra_Parens;
993
994    ----------------------------
995    -- Determine_Token_Casing --
996    ----------------------------
997
998    function Determine_Token_Casing return Casing_Type is
999    begin
1000       return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
1001    end Determine_Token_Casing;
1002
1003    -----------------------------
1004    -- Error_Space_Not_Allowed --
1005    -----------------------------
1006
1007    procedure Error_Space_Not_Allowed (S : Source_Ptr) is
1008    begin
1009       Error_Msg -- CODEFIX
1010         ("(style) space not allowed", S);
1011    end Error_Space_Not_Allowed;
1012
1013    --------------------------
1014    -- Error_Space_Required --
1015    --------------------------
1016
1017    procedure Error_Space_Required (S : Source_Ptr) is
1018    begin
1019       Error_Msg -- CODEFIX
1020         ("(style) space required", S);
1021    end Error_Space_Required;
1022
1023    --------------------
1024    -- Is_White_Space --
1025    --------------------
1026
1027    function Is_White_Space (C : Character) return Boolean is
1028    begin
1029       return C = ' ' or else C = HT;
1030    end Is_White_Space;
1031
1032    -------------------
1033    -- Mode_In_Check --
1034    -------------------
1035
1036    function Mode_In_Check return Boolean is
1037    begin
1038       return Style_Check and Style_Check_Mode_In;
1039    end Mode_In_Check;
1040
1041    -----------------
1042    -- No_End_Name --
1043    -----------------
1044
1045    --  In check end/exit labels mode (-gnatye), always require the name of
1046    --  a subprogram or package to be present on the END, so this is an error.
1047
1048    procedure No_End_Name (Name : Node_Id) is
1049    begin
1050       if Style_Check_End_Labels then
1051          Error_Msg_Node_1 := Name;
1052          Error_Msg_SP -- CODEFIX
1053            ("(style) `END &` required");
1054       end if;
1055    end No_End_Name;
1056
1057    ------------------
1058    -- No_Exit_Name --
1059    ------------------
1060
1061    --  In check end/exit labels mode (-gnatye), always require the name of
1062    --  the loop to be present on the EXIT when exiting a named loop.
1063
1064    procedure No_Exit_Name (Name : Node_Id) is
1065    begin
1066       if Style_Check_End_Labels then
1067          Error_Msg_Node_1 := Name;
1068          Error_Msg_SP -- CODEFIX
1069            ("(style) `EXIT &` required");
1070       end if;
1071    end No_Exit_Name;
1072
1073    ----------------------------
1074    -- Non_Lower_Case_Keyword --
1075    ----------------------------
1076
1077    --  In check casing mode (-gnatyk), reserved keywords must be spelled
1078    --  in all lower case (excluding keywords range, access, delta and digits
1079    --  used as attribute designators).
1080
1081    procedure Non_Lower_Case_Keyword is
1082    begin
1083       if Style_Check_Keyword_Casing then
1084          Error_Msg_SC -- CODEFIX
1085            ("(style) reserved words must be all lower case");
1086       end if;
1087    end Non_Lower_Case_Keyword;
1088
1089    -----------------------------
1090    -- Require_Following_Space --
1091    -----------------------------
1092
1093    procedure Require_Following_Space is
1094    begin
1095       if Source (Scan_Ptr) > ' ' then
1096          Error_Space_Required (Scan_Ptr);
1097       end if;
1098    end Require_Following_Space;
1099
1100    -----------------------------
1101    -- Require_Preceding_Space --
1102    -----------------------------
1103
1104    procedure Require_Preceding_Space is
1105    begin
1106       if Token_Ptr > Source_First (Current_Source_File)
1107         and then Source (Token_Ptr - 1) > ' '
1108       then
1109          Error_Space_Required (Token_Ptr);
1110       end if;
1111    end Require_Preceding_Space;
1112
1113 end Styleg;