1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Hostparm
; use Hostparm
;
28 with Output
; use Output
;
30 package body Stylesw
is
32 -- The following constant defines the default style options for -gnaty
34 Default_Style
: constant String :=
35 "3" & -- indentation level is 3
36 "a" & -- check attribute casing
37 "A" & -- check array attribute indexes
38 "b" & -- check no blanks at end of lines
39 "c" & -- check comment formats
40 "e" & -- check end/exit labels present
41 "f" & -- check no form/feeds vertical tabs in source
42 "h" & -- check no horizontal tabs in source
43 "i" & -- check if-then layout
44 "k" & -- check casing rules for keywords
45 "l" & -- check reference manual layout
46 "m" & -- check line length <= 79 characters
47 "n" & -- check casing of package Standard idents
48 "p" & -- check pragma casing
49 "r" & -- check casing for identifier references
50 "s" & -- check separate subprogram specs present
51 "t"; -- check token separation rules
53 -- The following constant defines the GNAT style options, showing them
54 -- as additions to the standard default style check options.
56 GNAT_Style
: constant String := Default_Style
&
57 "d" & -- check no DOS line terminators
58 "I" & -- check mode IN
59 "S" & -- check separate lines after THEN or ELSE
60 "u" & -- check no unnecessary blank lines
61 "x"; -- check extra parentheses around conditionals
63 -- Note: we intend GNAT_Style to also include the following, but we do
64 -- not yet have the whole tool suite clean with respect to this.
66 -- "B" & -- check boolean operators
68 -------------------------------
69 -- Reset_Style_Check_Options --
70 -------------------------------
72 procedure Reset_Style_Check_Options
is
74 Style_Check_Indentation
:= 0;
75 Style_Check_Array_Attribute_Index
:= False;
76 Style_Check_Attribute_Casing
:= False;
77 Style_Check_Blanks_At_End
:= False;
78 Style_Check_Blank_Lines
:= False;
79 Style_Check_Boolean_And_Or
:= False;
80 Style_Check_Comments
:= False;
81 Style_Check_DOS_Line_Terminator
:= False;
82 Style_Check_End_Labels
:= False;
83 Style_Check_Form_Feeds
:= False;
84 Style_Check_Horizontal_Tabs
:= False;
85 Style_Check_If_Then_Layout
:= False;
86 Style_Check_Keyword_Casing
:= False;
87 Style_Check_Layout
:= False;
88 Style_Check_Max_Line_Length
:= False;
89 Style_Check_Max_Nesting_Level
:= False;
90 Style_Check_Missing_Overriding
:= False;
91 Style_Check_Mode_In
:= False;
92 Style_Check_Order_Subprograms
:= False;
93 Style_Check_Pragma_Casing
:= False;
94 Style_Check_References
:= False;
95 Style_Check_Separate_Stmt_Lines
:= False;
96 Style_Check_Specs
:= False;
97 Style_Check_Standard
:= False;
98 Style_Check_Tokens
:= False;
99 Style_Check_Xtra_Parens
:= False;
100 end Reset_Style_Check_Options
;
102 ---------------------
103 -- RM_Column_Check --
104 ---------------------
106 function RM_Column_Check
return Boolean is
108 return Style_Check
and Style_Check_Layout
;
111 ------------------------------
112 -- Save_Style_Check_Options --
113 ------------------------------
115 procedure Save_Style_Check_Options
(Options
: out Style_Check_Options
) is
118 procedure Add
(C
: Character; S
: Boolean);
119 -- Add given character C to string if switch S is true
121 procedure Add_Nat
(N
: Nat
);
122 -- Add given natural number to string
128 procedure Add
(C
: Character; S
: Boolean) is
140 procedure Add_Nat
(N
: Nat
) is
147 Options
(P
) := Character'Val (Character'Pos ('0') + N
mod 10);
150 -- Start of processing for Save_Style_Check_Options
153 for K
in Options
'Range loop
157 Add
(Character'Val (Style_Check_Indentation
+ Character'Pos ('0')),
158 Style_Check_Indentation
/= 0);
160 Add
('a', Style_Check_Attribute_Casing
);
161 Add
('A', Style_Check_Array_Attribute_Index
);
162 Add
('b', Style_Check_Blanks_At_End
);
163 Add
('B', Style_Check_Boolean_And_Or
);
165 if Style_Check_Comments
then
166 if Style_Check_Comments_Spacing
= 2 then
167 Add
('c', Style_Check_Comments
);
168 elsif Style_Check_Comments_Spacing
= 1 then
169 Add
('C', Style_Check_Comments
);
173 Add
('d', Style_Check_DOS_Line_Terminator
);
174 Add
('e', Style_Check_End_Labels
);
175 Add
('f', Style_Check_Form_Feeds
);
176 Add
('h', Style_Check_Horizontal_Tabs
);
177 Add
('i', Style_Check_If_Then_Layout
);
178 Add
('I', Style_Check_Mode_In
);
179 Add
('k', Style_Check_Keyword_Casing
);
180 Add
('l', Style_Check_Layout
);
181 Add
('n', Style_Check_Standard
);
182 Add
('o', Style_Check_Order_Subprograms
);
183 Add
('O', Style_Check_Missing_Overriding
);
184 Add
('p', Style_Check_Pragma_Casing
);
185 Add
('r', Style_Check_References
);
186 Add
('s', Style_Check_Specs
);
187 Add
('S', Style_Check_Separate_Stmt_Lines
);
188 Add
('t', Style_Check_Tokens
);
189 Add
('u', Style_Check_Blank_Lines
);
190 Add
('x', Style_Check_Xtra_Parens
);
192 if Style_Check_Max_Line_Length
then
195 Add_Nat
(Style_Max_Line_Length
);
198 if Style_Check_Max_Nesting_Level
then
201 Add_Nat
(Style_Max_Nesting_Level
);
204 pragma Assert
(P
<= Options
'Last);
206 while P
< Options
'Last loop
210 end Save_Style_Check_Options
;
212 -------------------------------------
213 -- Set_Default_Style_Check_Options --
214 -------------------------------------
216 procedure Set_Default_Style_Check_Options
is
218 Reset_Style_Check_Options
;
219 Set_Style_Check_Options
(Default_Style
);
220 end Set_Default_Style_Check_Options
;
222 ----------------------------------
223 -- Set_GNAT_Style_Check_Options --
224 ----------------------------------
226 procedure Set_GNAT_Style_Check_Options
is
228 Reset_Style_Check_Options
;
229 Set_Style_Check_Options
(GNAT_Style
);
230 end Set_GNAT_Style_Check_Options
;
232 -----------------------------
233 -- Set_Style_Check_Options --
234 -----------------------------
236 -- Version used when no error checking is required
238 procedure Set_Style_Check_Options
(Options
: String) is
241 pragma Warnings
(Off
, EC
);
243 Set_Style_Check_Options
(Options
, OK
, EC
);
245 end Set_Style_Check_Options
;
247 -- Normal version with error checking
249 procedure Set_Style_Check_Options
252 Err_Col
: out Natural)
256 On
: Boolean := True;
257 -- Set to False if minus encountered
258 -- Set to True if plus encountered
260 Last_Option
: Character := ' ';
261 -- Set to last character encountered
263 procedure Add_Img
(N
: Natural);
264 -- Concatenates image of N at end of Style_Msg_Buf
266 procedure Bad_Style_Switch
(Msg
: String);
267 -- Called if bad style switch found. Msg is set in Style_Msg_Buf and
268 -- Style_Msg_Len. OK is set False.
274 procedure Add_Img
(N
: Natural) is
280 Style_Msg_Len
:= Style_Msg_Len
+ 1;
281 Style_Msg_Buf
(Style_Msg_Len
) :=
282 Character'Val (N
mod 10 + Character'Pos ('0'));
285 ----------------------
286 -- Bad_Style_Switch --
287 ----------------------
289 procedure Bad_Style_Switch
(Msg
: String) is
292 Style_Msg_Len
:= Msg
'Length;
293 Style_Msg_Buf
(1 .. Style_Msg_Len
) := Msg
;
294 end Bad_Style_Switch
;
296 -- Start of processing for Set_Style_Check_Options
299 Err_Col
:= Options
'First;
300 while Err_Col
<= Options
'Last loop
301 C
:= Options
(Err_Col
);
303 Err_Col
:= Err_Col
+ 1;
305 -- Turning switches on
317 Style_Check_Indentation
:=
318 Character'Pos (C
) - Character'Pos ('0');
321 Style_Check_Attribute_Casing
:= True;
324 Style_Check_Array_Attribute_Index
:= True;
327 Style_Check_Blanks_At_End
:= True;
330 Style_Check_Boolean_And_Or
:= True;
333 Style_Check_Comments
:= True;
334 Style_Check_Comments_Spacing
:= 2;
337 Style_Check_Comments
:= True;
338 Style_Check_Comments_Spacing
:= 1;
341 Style_Check_DOS_Line_Terminator
:= True;
344 Style_Check_End_Labels
:= True;
347 Style_Check_Form_Feeds
:= True;
350 Set_GNAT_Style_Check_Options
;
353 Style_Check_Horizontal_Tabs
:= True;
356 Style_Check_If_Then_Layout
:= True;
359 Style_Check_Mode_In
:= True;
362 Style_Check_Keyword_Casing
:= True;
365 Style_Check_Layout
:= True;
368 Style_Max_Nesting_Level
:= 0;
370 if Err_Col
> Options
'Last
371 or else Options
(Err_Col
) not in '0' .. '9'
373 Bad_Style_Switch
("invalid nesting level");
378 Style_Max_Nesting_Level
:=
379 Style_Max_Nesting_Level
* 10 +
380 Character'Pos (Options
(Err_Col
)) - Character'Pos ('0');
382 if Style_Max_Nesting_Level
> 999 then
384 ("max nesting level (999) exceeded in style check");
388 Err_Col
:= Err_Col
+ 1;
389 exit when Err_Col
> Options
'Last
390 or else Options
(Err_Col
) not in '0' .. '9';
393 Style_Check_Max_Nesting_Level
:= Style_Max_Nesting_Level
/= 0;
396 Style_Check_Max_Line_Length
:= True;
397 Style_Max_Line_Length
:= 79;
400 Style_Max_Line_Length
:= 0;
402 if Err_Col
> Options
'Last
403 or else Options
(Err_Col
) not in '0' .. '9'
406 ("invalid line length in style check");
411 Style_Max_Line_Length
:=
412 Style_Max_Line_Length
* 10 +
413 Character'Pos (Options
(Err_Col
)) - Character'Pos ('0');
415 if Style_Max_Line_Length
> Int
(Max_Line_Length
) then
417 Style_Msg_Buf
(1 .. 27) := "max line length allowed is ";
419 Add_Img
(Natural (Max_Line_Length
));
423 Err_Col
:= Err_Col
+ 1;
424 exit when Err_Col
> Options
'Last
425 or else Options
(Err_Col
) not in '0' .. '9';
428 Style_Check_Max_Line_Length
:= Style_Max_Line_Length
/= 0;
431 Style_Check_Standard
:= True;
434 Reset_Style_Check_Options
;
437 Style_Check_Order_Subprograms
:= True;
440 Style_Check_Missing_Overriding
:= True;
443 Style_Check_Pragma_Casing
:= True;
446 Style_Check_References
:= True;
449 Style_Check_Specs
:= True;
452 Style_Check_Separate_Stmt_Lines
:= True;
455 Style_Check_Tokens
:= True;
458 Style_Check_Blank_Lines
:= True;
461 Style_Check_Xtra_Parens
:= True;
464 Set_Default_Style_Check_Options
;
470 if Ignore_Unrecognized_VWY_Switches
then
471 Write_Line
("unrecognized switch -gnaty" & C
& " ignored");
473 Err_Col
:= Err_Col
- 1;
474 Bad_Style_Switch
("invalid style switch: " & C
);
479 -- Turning switches off
491 Style_Check_Indentation
:= 0;
494 Style_Check_Attribute_Casing
:= False;
497 Style_Check_Array_Attribute_Index
:= False;
500 Style_Check_Blanks_At_End
:= False;
503 Style_Check_Boolean_And_Or
:= False;
506 Style_Check_Comments
:= False;
509 Style_Check_DOS_Line_Terminator
:= False;
512 Style_Check_End_Labels
:= False;
515 Style_Check_Form_Feeds
:= False;
518 Reset_Style_Check_Options
;
521 Style_Check_Horizontal_Tabs
:= False;
524 Style_Check_If_Then_Layout
:= False;
527 Style_Check_Mode_In
:= False;
530 Style_Check_Keyword_Casing
:= False;
533 Style_Check_Layout
:= False;
536 Style_Max_Nesting_Level
:= 0;
539 Style_Check_Max_Line_Length
:= False;
542 Style_Max_Line_Length
:= 0;
543 Style_Check_Max_Line_Length
:= False;
546 Style_Check_Standard
:= False;
549 Style_Check_Order_Subprograms
:= False;
552 Style_Check_Missing_Overriding
:= False;
555 Style_Check_Pragma_Casing
:= False;
558 Style_Check_References
:= False;
561 Style_Check_Specs
:= False;
564 Style_Check_Separate_Stmt_Lines
:= False;
567 Style_Check_Tokens
:= False;
570 Style_Check_Blank_Lines
:= False;
573 Style_Check_Xtra_Parens
:= False;
579 if Ignore_Unrecognized_VWY_Switches
then
580 Write_Line
("unrecognized switch -gnaty-" & C
& " ignored");
582 Err_Col
:= Err_Col
- 1;
583 Bad_Style_Switch
("invalid style switch: " & C
);
590 -- Turn on style checking if other than N at end of string
592 Style_Check
:= (Last_Option
/= 'N');
594 end Set_Style_Check_Options
;