Merge from mainline
[official-gcc.git] / gcc / ada / stylesw.adb
blobe1eda4489453a237c8491f12348123e820fd5caf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S T Y L E S W --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, 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 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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Hostparm; use Hostparm;
28 with Opt; use Opt;
30 package body Stylesw is
32 -------------------------------
33 -- Reset_Style_Check_Options --
34 -------------------------------
36 procedure Reset_Style_Check_Options is
37 begin
38 Style_Check_Indentation := 0;
39 Style_Check_Attribute_Casing := False;
40 Style_Check_Blanks_At_End := False;
41 Style_Check_Blank_Lines := False;
42 Style_Check_Comments := False;
43 Style_Check_DOS_Line_Terminator := False;
44 Style_Check_End_Labels := False;
45 Style_Check_Form_Feeds := False;
46 Style_Check_Horizontal_Tabs := False;
47 Style_Check_If_Then_Layout := False;
48 Style_Check_Keyword_Casing := False;
49 Style_Check_Layout := False;
50 Style_Check_Max_Line_Length := False;
51 Style_Check_Max_Nesting_Level := False;
52 Style_Check_Mode_In := False;
53 Style_Check_Order_Subprograms := False;
54 Style_Check_Pragma_Casing := False;
55 Style_Check_References := False;
56 Style_Check_Specs := False;
57 Style_Check_Standard := False;
58 Style_Check_Tokens := False;
59 Style_Check_Xtra_Parens := False;
60 end Reset_Style_Check_Options;
62 ------------------------------
63 -- Save_Style_Check_Options --
64 ------------------------------
66 procedure Save_Style_Check_Options (Options : out Style_Check_Options) is
67 P : Natural := 0;
69 procedure Add (C : Character; S : Boolean);
70 -- Add given character C to string if switch S is true
72 procedure Add_Nat (N : Nat);
73 -- Add given natural number to string
75 ---------
76 -- Add --
77 ---------
79 procedure Add (C : Character; S : Boolean) is
80 begin
81 if S then
82 P := P + 1;
83 Options (P) := C;
84 end if;
85 end Add;
87 -------------
88 -- Add_Nat --
89 -------------
91 procedure Add_Nat (N : Nat) is
92 begin
93 if N > 9 then
94 Add_Nat (N / 10);
95 end if;
97 P := P + 1;
98 Options (P) := Character'Val (Character'Pos ('0') + N mod 10);
99 end Add_Nat;
101 -- Start of processing for Save_Style_Check_Options
103 begin
104 for K in Options'Range loop
105 Options (K) := ' ';
106 end loop;
108 Add (Character'Val (Style_Check_Indentation + Character'Pos ('0')),
109 Style_Check_Indentation /= 0);
111 Add ('a', Style_Check_Attribute_Casing);
112 Add ('b', Style_Check_Blanks_At_End);
113 Add ('c', Style_Check_Comments);
114 Add ('d', Style_Check_DOS_Line_Terminator);
115 Add ('e', Style_Check_End_Labels);
116 Add ('f', Style_Check_Form_Feeds);
117 Add ('h', Style_Check_Horizontal_Tabs);
118 Add ('i', Style_Check_If_Then_Layout);
119 Add ('I', Style_Check_Mode_In);
120 Add ('k', Style_Check_Keyword_Casing);
121 Add ('l', Style_Check_Layout);
122 Add ('n', Style_Check_Standard);
123 Add ('o', Style_Check_Order_Subprograms);
124 Add ('p', Style_Check_Pragma_Casing);
125 Add ('r', Style_Check_References);
126 Add ('s', Style_Check_Specs);
127 Add ('t', Style_Check_Tokens);
128 Add ('u', Style_Check_Blank_Lines);
129 Add ('x', Style_Check_Xtra_Parens);
131 if Style_Check_Max_Line_Length then
132 P := P + 1;
133 Options (P) := 'M';
134 Add_Nat (Style_Max_Line_Length);
135 end if;
137 if Style_Check_Max_Nesting_Level then
138 P := P + 1;
139 Options (P) := 'L';
140 Add_Nat (Style_Max_Nesting_Level);
141 end if;
143 pragma Assert (P <= Options'Last);
145 while P < Options'Last loop
146 P := P + 1;
147 Options (P) := ' ';
148 end loop;
149 end Save_Style_Check_Options;
151 -------------------------------------
152 -- Set_Default_Style_Check_Options --
153 -------------------------------------
155 procedure Set_Default_Style_Check_Options is
156 begin
157 Reset_Style_Check_Options;
158 Set_Style_Check_Options ("3abcefhiklmnprst");
159 end Set_Default_Style_Check_Options;
161 -----------------------------
162 -- Set_Style_Check_Options --
163 -----------------------------
165 -- Version used when no error checking is required
167 procedure Set_Style_Check_Options (Options : String) is
168 OK : Boolean;
169 EC : Natural;
170 begin
171 Set_Style_Check_Options (Options, OK, EC);
172 pragma Assert (OK);
173 end Set_Style_Check_Options;
175 -- Normal version with error checking
177 procedure Set_Style_Check_Options
178 (Options : String;
179 OK : out Boolean;
180 Err_Col : out Natural)
182 C : Character;
184 procedure Add_Img (N : Natural);
185 -- Concatenates image of N at end of Style_Msg_Buf
187 procedure Bad_Style_Switch (Msg : String);
188 -- Called if bad style switch found. Msg is mset in Style_Msg_Buf and
189 -- Style_Msg_Len. OK is set False.
191 -------------
192 -- Add_Img --
193 -------------
195 procedure Add_Img (N : Natural) is
196 begin
197 if N >= 10 then
198 Add_Img (N / 10);
199 end if;
201 Style_Msg_Len := Style_Msg_Len + 1;
202 Style_Msg_Buf (Style_Msg_Len) :=
203 Character'Val (N mod 10 + Character'Pos ('0'));
204 end Add_Img;
206 ----------------------
207 -- Bad_Style_Switch --
208 ----------------------
210 procedure Bad_Style_Switch (Msg : String) is
211 begin
212 OK := False;
213 Style_Msg_Len := Msg'Length;
214 Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
215 end Bad_Style_Switch;
217 -- Start of processing for Set_Style_Check_Options
219 begin
220 Err_Col := Options'First;
221 while Err_Col <= Options'Last loop
222 C := Options (Err_Col);
223 Err_Col := Err_Col + 1;
225 case C is
226 when '1' .. '9' =>
227 Style_Check_Indentation :=
228 Character'Pos (C) - Character'Pos ('0');
230 when 'a' =>
231 Style_Check_Attribute_Casing := True;
233 when 'b' =>
234 Style_Check_Blanks_At_End := True;
236 when 'c' =>
237 Style_Check_Comments := True;
239 when 'd' =>
240 Style_Check_DOS_Line_Terminator := True;
242 when 'e' =>
243 Style_Check_End_Labels := True;
245 when 'f' =>
246 Style_Check_Form_Feeds := True;
248 when 'h' =>
249 Style_Check_Horizontal_Tabs := True;
251 when 'i' =>
252 Style_Check_If_Then_Layout := True;
254 when 'I' =>
255 Style_Check_Mode_In := True;
257 when 'k' =>
258 Style_Check_Keyword_Casing := True;
260 when 'l' =>
261 Style_Check_Layout := True;
263 when 'L' =>
264 Style_Max_Nesting_Level := 0;
266 if Err_Col > Options'Last
267 or else Options (Err_Col) not in '0' .. '9'
268 then
269 Bad_Style_Switch ("invalid nesting level");
270 return;
271 end if;
273 loop
274 Style_Max_Nesting_Level :=
275 Style_Max_Nesting_Level * 10 +
276 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
278 if Style_Max_Nesting_Level > 999 then
279 Bad_Style_Switch
280 ("max nesting level (999) exceeded in style check");
281 return;
282 end if;
284 Err_Col := Err_Col + 1;
285 exit when Err_Col > Options'Last
286 or else Options (Err_Col) not in '0' .. '9';
287 end loop;
289 Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
291 when 'm' =>
292 Style_Check_Max_Line_Length := True;
293 Style_Max_Line_Length := 79;
295 when 'M' =>
296 Style_Max_Line_Length := 0;
298 if Err_Col > Options'Last
299 or else Options (Err_Col) not in '0' .. '9'
300 then
301 Bad_Style_Switch
302 ("invalid line length in style check");
303 return;
304 end if;
306 loop
307 Style_Max_Line_Length :=
308 Style_Max_Line_Length * 10 +
309 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
311 if Style_Max_Line_Length > Int (Max_Line_Length) then
312 OK := False;
313 Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
314 Style_Msg_Len := 27;
315 Add_Img (Natural (Max_Line_Length));
316 return;
317 end if;
319 Err_Col := Err_Col + 1;
320 exit when Err_Col > Options'Last
321 or else Options (Err_Col) not in '0' .. '9';
322 end loop;
324 Style_Check_Max_Line_Length := Style_Max_Line_Length /= 0;
326 when 'n' =>
327 Style_Check_Standard := True;
329 when 'N' =>
330 Reset_Style_Check_Options;
332 when 'o' =>
333 Style_Check_Order_Subprograms := True;
335 when 'p' =>
336 Style_Check_Pragma_Casing := True;
338 when 'r' =>
339 Style_Check_References := True;
341 when 's' =>
342 Style_Check_Specs := True;
344 when 't' =>
345 Style_Check_Tokens := True;
347 when 'u' =>
348 Style_Check_Blank_Lines := True;
350 when 'x' =>
351 Style_Check_Xtra_Parens := True;
353 when ' ' =>
354 null;
356 when others =>
357 Err_Col := Err_Col - 1;
358 Style_Msg_Buf (1 .. 21) := "invalid style switch:";
359 Style_Msg_Len := 22;
360 Style_Msg_Buf (Style_Msg_Len) := C;
361 OK := False;
362 return;
363 end case;
364 end loop;
366 Style_Check := True;
367 OK := True;
368 end Set_Style_Check_Options;
369 end Stylesw;