OSDN Git Service

2006-02-13 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / stylesw.adb
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-2005, 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 ------------------------------------------------------------------------------
26
27 with Hostparm; use Hostparm;
28 with Opt;      use Opt;
29
30 package body Stylesw is
31
32    -------------------------------
33    -- Reset_Style_Check_Options --
34    -------------------------------
35
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_Order_Subprograms   := False;
53       Style_Check_Pragma_Casing       := False;
54       Style_Check_References          := False;
55       Style_Check_Specs               := False;
56       Style_Check_Standard            := False;
57       Style_Check_Tokens              := False;
58       Style_Check_Xtra_Parens         := False;
59    end Reset_Style_Check_Options;
60
61    ------------------------------
62    -- Save_Style_Check_Options --
63    ------------------------------
64
65    procedure Save_Style_Check_Options (Options : out Style_Check_Options) is
66       P : Natural := 0;
67
68       procedure Add (C : Character; S : Boolean);
69       --  Add given character C to string if switch S is true
70
71       procedure Add_Nat (N : Nat);
72       --  Add given natural number to string
73
74       ---------
75       -- Add --
76       ---------
77
78       procedure Add (C : Character; S : Boolean) is
79       begin
80          if S then
81             P := P + 1;
82             Options (P) := C;
83          end if;
84       end Add;
85
86       -------------
87       -- Add_Nat --
88       -------------
89
90       procedure Add_Nat (N : Nat) is
91       begin
92          if N > 9 then
93             Add_Nat (N / 10);
94          end if;
95
96          P := P + 1;
97          Options (P) := Character'Val (Character'Pos ('0') + N mod 10);
98       end Add_Nat;
99
100    --  Start of processing for Save_Style_Check_Options
101
102    begin
103       for K in Options'Range loop
104          Options (K) := ' ';
105       end loop;
106
107       Add (Character'Val (Style_Check_Indentation + Character'Pos ('0')),
108            Style_Check_Indentation /= 0);
109
110       Add ('a', Style_Check_Attribute_Casing);
111       Add ('b', Style_Check_Blanks_At_End);
112       Add ('c', Style_Check_Comments);
113       Add ('d', Style_Check_DOS_Line_Terminator);
114       Add ('e', Style_Check_End_Labels);
115       Add ('f', Style_Check_Form_Feeds);
116       Add ('h', Style_Check_Horizontal_Tabs);
117       Add ('i', Style_Check_If_Then_Layout);
118       Add ('k', Style_Check_Keyword_Casing);
119       Add ('l', Style_Check_Layout);
120       Add ('n', Style_Check_Standard);
121       Add ('o', Style_Check_Order_Subprograms);
122       Add ('p', Style_Check_Pragma_Casing);
123       Add ('r', Style_Check_References);
124       Add ('s', Style_Check_Specs);
125       Add ('t', Style_Check_Tokens);
126       Add ('u', Style_Check_Blank_Lines);
127       Add ('x', Style_Check_Xtra_Parens);
128
129       if Style_Check_Max_Line_Length then
130          P := P + 1;
131          Options (P) := 'M';
132          Add_Nat (Style_Max_Line_Length);
133       end if;
134
135       if Style_Check_Max_Nesting_Level then
136          P := P + 1;
137          Options (P) := 'L';
138          Add_Nat (Style_Max_Nesting_Level);
139       end if;
140
141       pragma Assert (P <= Options'Last);
142
143       while P < Options'Last loop
144          P := P + 1;
145          Options (P) := ' ';
146       end loop;
147    end Save_Style_Check_Options;
148
149    -------------------------------------
150    -- Set_Default_Style_Check_Options --
151    -------------------------------------
152
153    procedure Set_Default_Style_Check_Options is
154    begin
155       Reset_Style_Check_Options;
156       Set_Style_Check_Options ("3abcefhiklmnprst");
157    end Set_Default_Style_Check_Options;
158
159    -----------------------------
160    -- Set_Style_Check_Options --
161    -----------------------------
162
163    --  Version used when no error checking is required
164
165    procedure Set_Style_Check_Options (Options : String) is
166       OK : Boolean;
167       EC : Natural;
168    begin
169       Set_Style_Check_Options (Options, OK, EC);
170       pragma Assert (OK);
171    end Set_Style_Check_Options;
172
173    --  Normal version with error checking
174
175    procedure Set_Style_Check_Options
176      (Options  : String;
177       OK       : out Boolean;
178       Err_Col  : out Natural)
179    is
180       C : Character;
181
182       procedure Add_Img (N : Natural);
183       --  Concatenates image of N at end of Style_Msg_Buf
184
185       procedure Bad_Style_Switch (Msg : String);
186       --  Called if bad style switch found. Msg is mset in Style_Msg_Buf and
187       --  Style_Msg_Len. OK is set False.
188
189       -------------
190       -- Add_Img --
191       -------------
192
193       procedure Add_Img (N : Natural) is
194       begin
195          if N >= 10 then
196             Add_Img (N / 10);
197          end if;
198
199          Style_Msg_Len := Style_Msg_Len + 1;
200          Style_Msg_Buf (Style_Msg_Len) :=
201            Character'Val (N mod 10 + Character'Pos ('0'));
202       end Add_Img;
203
204       ----------------------
205       -- Bad_Style_Switch --
206       ----------------------
207
208       procedure Bad_Style_Switch (Msg : String) is
209       begin
210          OK := False;
211          Style_Msg_Len := Msg'Length;
212          Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
213       end Bad_Style_Switch;
214
215    --  Start of processing for Set_Style_Check_Options
216
217    begin
218       Err_Col := Options'First;
219       while Err_Col <= Options'Last loop
220          C := Options (Err_Col);
221          Err_Col := Err_Col + 1;
222
223          case C is
224             when '1' .. '9' =>
225                Style_Check_Indentation :=
226                  Character'Pos (C) - Character'Pos ('0');
227
228             when 'a' =>
229                Style_Check_Attribute_Casing    := True;
230
231             when 'b' =>
232                Style_Check_Blanks_At_End       := True;
233
234             when 'c' =>
235                Style_Check_Comments            := True;
236
237             when 'd' =>
238                Style_Check_DOS_Line_Terminator := True;
239
240             when 'e' =>
241                Style_Check_End_Labels          := True;
242
243             when 'f' =>
244                Style_Check_Form_Feeds          := True;
245
246             when 'h' =>
247                Style_Check_Horizontal_Tabs     := True;
248
249             when 'i' =>
250                Style_Check_If_Then_Layout      := True;
251
252             when 'k' =>
253                Style_Check_Keyword_Casing      := True;
254
255             when 'l' =>
256                Style_Check_Layout              := True;
257
258             when 'L' =>
259                Style_Max_Nesting_Level := 0;
260
261                if Err_Col > Options'Last
262                  or else Options (Err_Col) not in '0' .. '9'
263                then
264                   Bad_Style_Switch ("invalid nesting level");
265                   return;
266                end if;
267
268                loop
269                   Style_Max_Nesting_Level :=
270                     Style_Max_Nesting_Level * 10 +
271                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
272
273                   if Style_Max_Nesting_Level > 999 then
274                      Bad_Style_Switch
275                        ("max nesting level (999) exceeded in style check");
276                      return;
277                   end if;
278
279                   Err_Col := Err_Col + 1;
280                   exit when Err_Col > Options'Last
281                     or else Options (Err_Col) not in '0' .. '9';
282                end loop;
283
284                Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
285
286             when 'm' =>
287                Style_Check_Max_Line_Length     := True;
288                Style_Max_Line_Length           := 79;
289
290             when 'M' =>
291                Style_Max_Line_Length := 0;
292
293                if Err_Col > Options'Last
294                  or else Options (Err_Col) not in '0' .. '9'
295                then
296                   Bad_Style_Switch
297                     ("invalid line length in style check");
298                   return;
299                end if;
300
301                loop
302                   Style_Max_Line_Length :=
303                     Style_Max_Line_Length * 10 +
304                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
305
306                   if Style_Max_Line_Length > Int (Max_Line_Length) then
307                      OK := False;
308                      Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
309                      Style_Msg_Len := 27;
310                      Add_Img (Natural (Max_Line_Length));
311                      return;
312                   end if;
313
314                   Err_Col := Err_Col + 1;
315                   exit when Err_Col > Options'Last
316                     or else Options (Err_Col) not in '0' .. '9';
317                end loop;
318
319                Style_Check_Max_Line_Length   := Style_Max_Line_Length /= 0;
320
321             when 'n' =>
322                Style_Check_Standard            := True;
323
324             when 'N' =>
325                Reset_Style_Check_Options;
326
327             when 'o' =>
328                Style_Check_Order_Subprograms   := True;
329
330             when 'p' =>
331                Style_Check_Pragma_Casing       := True;
332
333             when 'r' =>
334                Style_Check_References          := True;
335
336             when 's' =>
337                Style_Check_Specs               := True;
338
339             when 't' =>
340                Style_Check_Tokens              := True;
341
342             when 'u' =>
343                Style_Check_Blank_Lines         := True;
344
345             when 'x' =>
346                Style_Check_Xtra_Parens         := True;
347
348             when ' ' =>
349                null;
350
351             when others =>
352                Err_Col := Err_Col - 1;
353                Style_Msg_Buf (1 .. 21) := "invalid style switch:";
354                Style_Msg_Len := 22;
355                Style_Msg_Buf (Style_Msg_Len) := C;
356                OK := False;
357                return;
358          end case;
359       end loop;
360
361       Style_Check := True;
362       OK := True;
363    end Set_Style_Check_Options;
364 end Stylesw;