OSDN Git Service

2008-04-30 Paul Thomas <pault@gcc.gnu.org>
[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-2008, 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 with Hostparm; use Hostparm;
27 with Opt;      use Opt;
28
29 package body Stylesw is
30
31    -------------------------------
32    -- Reset_Style_Check_Options --
33    -------------------------------
34
35    procedure Reset_Style_Check_Options is
36    begin
37       Style_Check_Indentation           := 0;
38       Style_Check_Array_Attribute_Index := False;
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_Separate_Stmt_Lines   := False;
57       Style_Check_Specs                 := False;
58       Style_Check_Standard              := False;
59       Style_Check_Tokens                := False;
60       Style_Check_Xtra_Parens           := False;
61    end Reset_Style_Check_Options;
62
63    ------------------------------
64    -- Save_Style_Check_Options --
65    ------------------------------
66
67    procedure Save_Style_Check_Options (Options : out Style_Check_Options) is
68       P : Natural := 0;
69
70       procedure Add (C : Character; S : Boolean);
71       --  Add given character C to string if switch S is true
72
73       procedure Add_Nat (N : Nat);
74       --  Add given natural number to string
75
76       ---------
77       -- Add --
78       ---------
79
80       procedure Add (C : Character; S : Boolean) is
81       begin
82          if S then
83             P := P + 1;
84             Options (P) := C;
85          end if;
86       end Add;
87
88       -------------
89       -- Add_Nat --
90       -------------
91
92       procedure Add_Nat (N : Nat) is
93       begin
94          if N > 9 then
95             Add_Nat (N / 10);
96          end if;
97
98          P := P + 1;
99          Options (P) := Character'Val (Character'Pos ('0') + N mod 10);
100       end Add_Nat;
101
102    --  Start of processing for Save_Style_Check_Options
103
104    begin
105       for K in Options'Range loop
106          Options (K) := ' ';
107       end loop;
108
109       Add (Character'Val (Style_Check_Indentation + Character'Pos ('0')),
110            Style_Check_Indentation /= 0);
111
112       Add ('a', Style_Check_Attribute_Casing);
113       Add ('A', Style_Check_Array_Attribute_Index);
114       Add ('b', Style_Check_Blanks_At_End);
115       Add ('c', Style_Check_Comments);
116       Add ('d', Style_Check_DOS_Line_Terminator);
117       Add ('e', Style_Check_End_Labels);
118       Add ('f', Style_Check_Form_Feeds);
119       Add ('h', Style_Check_Horizontal_Tabs);
120       Add ('i', Style_Check_If_Then_Layout);
121       Add ('I', Style_Check_Mode_In);
122       Add ('k', Style_Check_Keyword_Casing);
123       Add ('l', Style_Check_Layout);
124       Add ('n', Style_Check_Standard);
125       Add ('o', Style_Check_Order_Subprograms);
126       Add ('p', Style_Check_Pragma_Casing);
127       Add ('r', Style_Check_References);
128       Add ('s', Style_Check_Specs);
129       Add ('S', Style_Check_Separate_Stmt_Lines);
130       Add ('t', Style_Check_Tokens);
131       Add ('u', Style_Check_Blank_Lines);
132       Add ('x', Style_Check_Xtra_Parens);
133
134       if Style_Check_Max_Line_Length then
135          P := P + 1;
136          Options (P) := 'M';
137          Add_Nat (Style_Max_Line_Length);
138       end if;
139
140       if Style_Check_Max_Nesting_Level then
141          P := P + 1;
142          Options (P) := 'L';
143          Add_Nat (Style_Max_Nesting_Level);
144       end if;
145
146       pragma Assert (P <= Options'Last);
147
148       while P < Options'Last loop
149          P := P + 1;
150          Options (P) := ' ';
151       end loop;
152    end Save_Style_Check_Options;
153
154    -------------------------------------
155    -- Set_Default_Style_Check_Options --
156    -------------------------------------
157
158    procedure Set_Default_Style_Check_Options is
159    begin
160       Reset_Style_Check_Options;
161       Set_Style_Check_Options ("3aAbcefhiklmnprst");
162    end Set_Default_Style_Check_Options;
163
164    ----------------------------------
165    -- Set_GNAT_Style_Check_Options --
166    ----------------------------------
167
168    procedure Set_GNAT_Style_Check_Options is
169    begin
170       Reset_Style_Check_Options;
171       Set_Style_Check_Options ("3aAbcdefhiIklmnprsStux");
172    end Set_GNAT_Style_Check_Options;
173
174    -----------------------------
175    -- Set_Style_Check_Options --
176    -----------------------------
177
178    --  Version used when no error checking is required
179
180    procedure Set_Style_Check_Options (Options : String) is
181       OK : Boolean;
182       EC : Natural;
183       pragma Warnings (Off, EC);
184    begin
185       Set_Style_Check_Options (Options, OK, EC);
186       pragma Assert (OK);
187    end Set_Style_Check_Options;
188
189    --  Normal version with error checking
190
191    procedure Set_Style_Check_Options
192      (Options  : String;
193       OK       : out Boolean;
194       Err_Col  : out Natural)
195    is
196       C : Character;
197
198       On : Boolean := True;
199       --  Set to False if minus encountered
200       --  Set to True if plus encountered
201
202       Last_Option : Character := ' ';
203       --  Set to last character encountered
204
205       procedure Add_Img (N : Natural);
206       --  Concatenates image of N at end of Style_Msg_Buf
207
208       procedure Bad_Style_Switch (Msg : String);
209       --  Called if bad style switch found. Msg is set in Style_Msg_Buf and
210       --  Style_Msg_Len. OK is set False.
211
212       -------------
213       -- Add_Img --
214       -------------
215
216       procedure Add_Img (N : Natural) is
217       begin
218          if N >= 10 then
219             Add_Img (N / 10);
220          end if;
221
222          Style_Msg_Len := Style_Msg_Len + 1;
223          Style_Msg_Buf (Style_Msg_Len) :=
224            Character'Val (N mod 10 + Character'Pos ('0'));
225       end Add_Img;
226
227       ----------------------
228       -- Bad_Style_Switch --
229       ----------------------
230
231       procedure Bad_Style_Switch (Msg : String) is
232       begin
233          OK := False;
234          Style_Msg_Len := Msg'Length;
235          Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
236       end Bad_Style_Switch;
237
238    --  Start of processing for Set_Style_Check_Options
239
240    begin
241       Err_Col := Options'First;
242       while Err_Col <= Options'Last loop
243          C := Options (Err_Col);
244          Last_Option := C;
245          Err_Col := Err_Col + 1;
246
247          --  Turning switches on
248
249          if On then
250             case C is
251
252             when '+' =>
253                null;
254
255             when '-' =>
256                On := False;
257
258             when '0' .. '9' =>
259                Style_Check_Indentation :=
260                  Character'Pos (C) - Character'Pos ('0');
261
262             when 'a' =>
263                Style_Check_Attribute_Casing      := True;
264
265             when 'A' =>
266                Style_Check_Array_Attribute_Index := True;
267
268             when 'b' =>
269                Style_Check_Blanks_At_End         := True;
270
271             when 'c' =>
272                Style_Check_Comments              := True;
273
274             when 'd' =>
275                Style_Check_DOS_Line_Terminator   := True;
276
277             when 'e' =>
278                Style_Check_End_Labels            := True;
279
280             when 'f' =>
281                Style_Check_Form_Feeds            := True;
282
283             when 'g' =>
284                Set_GNAT_Style_Check_Options;
285
286             when 'h' =>
287                Style_Check_Horizontal_Tabs       := True;
288
289             when 'i' =>
290                Style_Check_If_Then_Layout        := True;
291
292             when 'I' =>
293                Style_Check_Mode_In               := True;
294
295             when 'k' =>
296                Style_Check_Keyword_Casing        := True;
297
298             when 'l' =>
299                Style_Check_Layout                := True;
300
301             when 'L' =>
302                Style_Max_Nesting_Level := 0;
303
304                if Err_Col > Options'Last
305                  or else Options (Err_Col) not in '0' .. '9'
306                then
307                   Bad_Style_Switch ("invalid nesting level");
308                   return;
309                end if;
310
311                loop
312                   Style_Max_Nesting_Level :=
313                     Style_Max_Nesting_Level * 10 +
314                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
315
316                   if Style_Max_Nesting_Level > 999 then
317                      Bad_Style_Switch
318                        ("max nesting level (999) exceeded in style check");
319                      return;
320                   end if;
321
322                   Err_Col := Err_Col + 1;
323                   exit when Err_Col > Options'Last
324                     or else Options (Err_Col) not in '0' .. '9';
325                end loop;
326
327                Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
328
329             when 'm' =>
330                Style_Check_Max_Line_Length       := True;
331                Style_Max_Line_Length             := 79;
332
333             when 'M' =>
334                Style_Max_Line_Length             := 0;
335
336                if Err_Col > Options'Last
337                  or else Options (Err_Col) not in '0' .. '9'
338                then
339                   Bad_Style_Switch
340                     ("invalid line length in style check");
341                   return;
342                end if;
343
344                loop
345                   Style_Max_Line_Length :=
346                     Style_Max_Line_Length * 10 +
347                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
348
349                   if Style_Max_Line_Length > Int (Max_Line_Length) then
350                      OK := False;
351                      Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
352                      Style_Msg_Len := 27;
353                      Add_Img (Natural (Max_Line_Length));
354                      return;
355                   end if;
356
357                   Err_Col := Err_Col + 1;
358                   exit when Err_Col > Options'Last
359                     or else Options (Err_Col) not in '0' .. '9';
360                end loop;
361
362                Style_Check_Max_Line_Length       := Style_Max_Line_Length /= 0;
363
364             when 'n' =>
365                Style_Check_Standard              := True;
366
367             when 'N' =>
368                Reset_Style_Check_Options;
369
370             when 'o' =>
371                Style_Check_Order_Subprograms     := True;
372
373             when 'p' =>
374                Style_Check_Pragma_Casing         := True;
375
376             when 'r' =>
377                Style_Check_References            := True;
378
379             when 's' =>
380                Style_Check_Specs                 := True;
381
382             when 'S' =>
383                Style_Check_Separate_Stmt_Lines   := True;
384
385             when 't' =>
386                Style_Check_Tokens                := True;
387
388             when 'u' =>
389                Style_Check_Blank_Lines           := True;
390
391             when 'x' =>
392                Style_Check_Xtra_Parens           := True;
393
394             when 'y' =>
395                Set_Default_Style_Check_Options;
396
397             when ' ' =>
398                null;
399
400             when others =>
401                Err_Col := Err_Col - 1;
402                Bad_Style_Switch ("invalid style switch: " & C);
403                return;
404             end case;
405
406          --  Turning switches off
407
408          else
409             case C is
410
411             when '+' =>
412                On := True;
413
414             when '-' =>
415                null;
416
417             when '0' .. '9' =>
418                Style_Check_Indentation := 0;
419
420             when 'a' =>
421                Style_Check_Attribute_Casing      := False;
422
423             when 'A' =>
424                Style_Check_Array_Attribute_Index := False;
425
426             when 'b' =>
427                Style_Check_Blanks_At_End         := False;
428
429             when 'c' =>
430                Style_Check_Comments              := False;
431
432             when 'd' =>
433                Style_Check_DOS_Line_Terminator   := False;
434
435             when 'e' =>
436                Style_Check_End_Labels            := False;
437
438             when 'f' =>
439                Style_Check_Form_Feeds            := False;
440
441             when 'g' =>
442                Reset_Style_Check_Options;
443
444             when 'h' =>
445                Style_Check_Horizontal_Tabs       := False;
446
447             when 'i' =>
448                Style_Check_If_Then_Layout        := False;
449
450             when 'I' =>
451                Style_Check_Mode_In               := False;
452
453             when 'k' =>
454                Style_Check_Keyword_Casing        := False;
455
456             when 'l' =>
457                Style_Check_Layout                := False;
458
459             when 'L' =>
460                Style_Max_Nesting_Level := 0;
461
462             when 'm' =>
463                Style_Check_Max_Line_Length       := False;
464
465             when 'M' =>
466                Style_Max_Line_Length             := 0;
467                Style_Check_Max_Line_Length       := False;
468
469             when 'n' =>
470                Style_Check_Standard              := False;
471
472             when 'o' =>
473                Style_Check_Order_Subprograms     := False;
474
475             when 'p' =>
476                Style_Check_Pragma_Casing         := False;
477
478             when 'r' =>
479                Style_Check_References            := False;
480
481             when 's' =>
482                Style_Check_Specs                 := False;
483
484             when 'S' =>
485                Style_Check_Separate_Stmt_Lines   := False;
486
487             when 't' =>
488                Style_Check_Tokens                := False;
489
490             when 'u' =>
491                Style_Check_Blank_Lines           := False;
492
493             when 'x' =>
494                Style_Check_Xtra_Parens           := False;
495
496             when ' ' =>
497                null;
498
499             when others =>
500                Err_Col := Err_Col - 1;
501                Bad_Style_Switch ("invalid style switch: " & C);
502                return;
503             end case;
504          end if;
505       end loop;
506
507       --  Turn on style checking if other than N at end of string
508
509       Style_Check := (Last_Option /= 'N');
510       OK := True;
511    end Set_Style_Check_Options;
512 end Stylesw;