OSDN Git Service

2007-09-26 Thomas Quinot <quinot@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-2007, 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 ("3aAbcdefhiklmnprsStux");
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    begin
184       Set_Style_Check_Options (Options, OK, EC);
185       pragma Assert (OK);
186    end Set_Style_Check_Options;
187
188    --  Normal version with error checking
189
190    procedure Set_Style_Check_Options
191      (Options  : String;
192       OK       : out Boolean;
193       Err_Col  : out Natural)
194    is
195       C : Character;
196
197       procedure Add_Img (N : Natural);
198       --  Concatenates image of N at end of Style_Msg_Buf
199
200       procedure Bad_Style_Switch (Msg : String);
201       --  Called if bad style switch found. Msg is mset in Style_Msg_Buf and
202       --  Style_Msg_Len. OK is set False.
203
204       -------------
205       -- Add_Img --
206       -------------
207
208       procedure Add_Img (N : Natural) is
209       begin
210          if N >= 10 then
211             Add_Img (N / 10);
212          end if;
213
214          Style_Msg_Len := Style_Msg_Len + 1;
215          Style_Msg_Buf (Style_Msg_Len) :=
216            Character'Val (N mod 10 + Character'Pos ('0'));
217       end Add_Img;
218
219       ----------------------
220       -- Bad_Style_Switch --
221       ----------------------
222
223       procedure Bad_Style_Switch (Msg : String) is
224       begin
225          OK := False;
226          Style_Msg_Len := Msg'Length;
227          Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
228       end Bad_Style_Switch;
229
230    --  Start of processing for Set_Style_Check_Options
231
232    begin
233       Err_Col := Options'First;
234       while Err_Col <= Options'Last loop
235          C := Options (Err_Col);
236          Err_Col := Err_Col + 1;
237
238          case C is
239             when '1' .. '9' =>
240                Style_Check_Indentation :=
241                  Character'Pos (C) - Character'Pos ('0');
242
243             when 'a' =>
244                Style_Check_Attribute_Casing      := True;
245
246             when 'A' =>
247                Style_Check_Array_Attribute_Index := True;
248
249             when 'b' =>
250                Style_Check_Blanks_At_End         := True;
251
252             when 'c' =>
253                Style_Check_Comments              := True;
254
255             when 'd' =>
256                Style_Check_DOS_Line_Terminator   := True;
257
258             when 'e' =>
259                Style_Check_End_Labels            := True;
260
261             when 'f' =>
262                Style_Check_Form_Feeds            := True;
263
264             when 'g' =>
265                Set_GNAT_Style_Check_Options;
266
267             when 'h' =>
268                Style_Check_Horizontal_Tabs       := True;
269
270             when 'i' =>
271                Style_Check_If_Then_Layout        := True;
272
273             when 'I' =>
274                Style_Check_Mode_In               := True;
275
276             when 'k' =>
277                Style_Check_Keyword_Casing        := True;
278
279             when 'l' =>
280                Style_Check_Layout                := True;
281
282             when 'L' =>
283                Style_Max_Nesting_Level := 0;
284
285                if Err_Col > Options'Last
286                  or else Options (Err_Col) not in '0' .. '9'
287                then
288                   Bad_Style_Switch ("invalid nesting level");
289                   return;
290                end if;
291
292                loop
293                   Style_Max_Nesting_Level :=
294                     Style_Max_Nesting_Level * 10 +
295                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
296
297                   if Style_Max_Nesting_Level > 999 then
298                      Bad_Style_Switch
299                        ("max nesting level (999) exceeded in style check");
300                      return;
301                   end if;
302
303                   Err_Col := Err_Col + 1;
304                   exit when Err_Col > Options'Last
305                     or else Options (Err_Col) not in '0' .. '9';
306                end loop;
307
308                Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
309
310             when 'm' =>
311                Style_Check_Max_Line_Length       := True;
312                Style_Max_Line_Length             := 79;
313
314             when 'M' =>
315                Style_Max_Line_Length             := 0;
316
317                if Err_Col > Options'Last
318                  or else Options (Err_Col) not in '0' .. '9'
319                then
320                   Bad_Style_Switch
321                     ("invalid line length in style check");
322                   return;
323                end if;
324
325                loop
326                   Style_Max_Line_Length :=
327                     Style_Max_Line_Length * 10 +
328                       Character'Pos (Options (Err_Col)) - Character'Pos ('0');
329
330                   if Style_Max_Line_Length > Int (Max_Line_Length) then
331                      OK := False;
332                      Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
333                      Style_Msg_Len := 27;
334                      Add_Img (Natural (Max_Line_Length));
335                      return;
336                   end if;
337
338                   Err_Col := Err_Col + 1;
339                   exit when Err_Col > Options'Last
340                     or else Options (Err_Col) not in '0' .. '9';
341                end loop;
342
343                Style_Check_Max_Line_Length       := Style_Max_Line_Length /= 0;
344
345             when 'n' =>
346                Style_Check_Standard              := True;
347
348             when 'N' =>
349                Reset_Style_Check_Options;
350
351             when 'o' =>
352                Style_Check_Order_Subprograms     := True;
353
354             when 'p' =>
355                Style_Check_Pragma_Casing         := True;
356
357             when 'r' =>
358                Style_Check_References            := True;
359
360             when 's' =>
361                Style_Check_Specs                 := True;
362
363             when 'S' =>
364                Style_Check_Separate_Stmt_Lines   := True;
365
366             when 't' =>
367                Style_Check_Tokens                := True;
368
369             when 'u' =>
370                Style_Check_Blank_Lines           := True;
371
372             when 'x' =>
373                Style_Check_Xtra_Parens           := True;
374
375             when ' ' =>
376                null;
377
378             when others =>
379                Err_Col := Err_Col - 1;
380                Style_Msg_Buf (1 .. 22) := "invalid style switch: ";
381                Style_Msg_Len := 23;
382                Style_Msg_Buf (Style_Msg_Len) := C;
383                OK := False;
384                return;
385          end case;
386       end loop;
387
388       Style_Check := True;
389       OK := True;
390    end Set_Style_Check_Options;
391 end Stylesw;