RISC-V: Bugfix vec_extract vls mode iterator restriction mismatch
[official-gcc.git] / gcc / ada / stylesw.adb
blob76004455b104c735e8d5d8f0e434835295a38ddd
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-2024, 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 ------------------------------------------------------------------------------
26 with Hostparm; use Hostparm;
27 with Opt; use Opt;
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
62 "z"; -- check parens not required by precedence rules.
64 -------------------------------
65 -- Reset_Style_Check_Options --
66 -------------------------------
68 procedure Reset_Style_Check_Options is
69 begin
70 Style_Check_Indentation := 0;
71 Style_Check_Array_Attribute_Index := False;
72 Style_Check_Attribute_Casing := False;
73 Style_Check_Blanks_At_End := False;
74 Style_Check_Blank_Lines := False;
75 Style_Check_Boolean_And_Or := False;
76 Style_Check_Comments := False;
77 Style_Check_DOS_Line_Terminator := False;
78 Style_Check_Mixed_Case_Decls := False;
79 Style_Check_End_Labels := False;
80 Style_Check_Form_Feeds := False;
81 Style_Check_Horizontal_Tabs := False;
82 Style_Check_If_Then_Layout := False;
83 Style_Check_Keyword_Casing := False;
84 Style_Check_Layout := False;
85 Style_Check_Max_Line_Length := False;
86 Style_Check_Max_Nesting_Level := False;
87 Style_Check_Missing_Overriding := False;
88 Style_Check_Mode_In := False;
89 Style_Check_Order_Subprograms := False;
90 Style_Check_Pragma_Casing := False;
91 Style_Check_References := False;
92 Style_Check_Separate_Stmt_Lines := False;
93 Style_Check_Specs := False;
94 Style_Check_Standard := False;
95 Style_Check_Tokens := False;
96 Style_Check_Xtra_Parens := False;
97 Style_Check_Xtra_Parens_Precedence := False;
98 end Reset_Style_Check_Options;
100 ---------------------
101 -- RM_Column_Check --
102 ---------------------
104 function RM_Column_Check return Boolean is
105 begin
106 return Style_Check and Style_Check_Layout;
107 end RM_Column_Check;
109 ------------------------------
110 -- Save_Style_Check_Options --
111 ------------------------------
113 procedure Save_Style_Check_Options (Options : out Style_Check_Options) is
114 P : Natural := 0;
116 procedure Add (C : Character; S : Boolean);
117 -- Add given character C to string if switch S is true
119 procedure Add_Nat (N : Nat);
120 -- Add given natural number to string
122 ---------
123 -- Add --
124 ---------
126 procedure Add (C : Character; S : Boolean) is
127 begin
128 if S then
129 P := P + 1;
130 Options (P) := C;
131 end if;
132 end Add;
134 -------------
135 -- Add_Nat --
136 -------------
138 procedure Add_Nat (N : Nat) is
139 begin
140 if N > 9 then
141 Add_Nat (N / 10);
142 end if;
144 P := P + 1;
145 Options (P) := Character'Val (Character'Pos ('0') + N mod 10);
146 end Add_Nat;
148 -- Start of processing for Save_Style_Check_Options
150 begin
151 Add (Character'Val (Style_Check_Indentation + Character'Pos ('0')),
152 Style_Check_Indentation /= 0);
154 Add ('a', Style_Check_Attribute_Casing);
155 Add ('A', Style_Check_Array_Attribute_Index);
156 Add ('b', Style_Check_Blanks_At_End);
157 Add ('B', Style_Check_Boolean_And_Or);
159 if Style_Check_Comments then
160 if Style_Check_Comments_Spacing = 2 then
161 Add ('c', Style_Check_Comments);
162 else
163 pragma Assert (Style_Check_Comments_Spacing = 1);
164 Add ('C', Style_Check_Comments);
165 end if;
166 end if;
168 Add ('d', Style_Check_DOS_Line_Terminator);
169 Add ('D', Style_Check_Mixed_Case_Decls);
170 Add ('e', Style_Check_End_Labels);
171 Add ('f', Style_Check_Form_Feeds);
172 Add ('h', Style_Check_Horizontal_Tabs);
173 Add ('i', Style_Check_If_Then_Layout);
174 Add ('I', Style_Check_Mode_In);
175 Add ('k', Style_Check_Keyword_Casing);
176 Add ('l', Style_Check_Layout);
177 Add ('n', Style_Check_Standard);
178 Add ('o', Style_Check_Order_Subprograms);
179 Add ('O', Style_Check_Missing_Overriding);
180 Add ('p', Style_Check_Pragma_Casing);
181 Add ('r', Style_Check_References);
182 Add ('s', Style_Check_Specs);
183 Add ('S', Style_Check_Separate_Stmt_Lines);
184 Add ('t', Style_Check_Tokens);
185 Add ('u', Style_Check_Blank_Lines);
186 Add ('x', Style_Check_Xtra_Parens);
187 Add ('z', Style_Check_Xtra_Parens_Precedence);
189 if Style_Check_Max_Line_Length then
190 P := P + 1;
191 Options (P) := 'M';
192 Add_Nat (Style_Max_Line_Length);
193 end if;
195 if Style_Check_Max_Nesting_Level then
196 P := P + 1;
197 Options (P) := 'L';
198 Add_Nat (Style_Max_Nesting_Level);
199 end if;
201 pragma Assert (P <= Options'Last);
203 while P < Options'Last loop
204 P := P + 1;
205 Options (P) := ' ';
206 end loop;
207 end Save_Style_Check_Options;
209 -------------------------------------
210 -- Set_Default_Style_Check_Options --
211 -------------------------------------
213 procedure Set_Default_Style_Check_Options is
214 begin
215 Reset_Style_Check_Options;
216 Set_Style_Check_Options (Default_Style);
217 end Set_Default_Style_Check_Options;
219 ----------------------------------
220 -- Set_GNAT_Style_Check_Options --
221 ----------------------------------
223 procedure Set_GNAT_Style_Check_Options is
224 begin
225 Reset_Style_Check_Options;
226 Set_Style_Check_Options (GNAT_Style);
227 end Set_GNAT_Style_Check_Options;
229 -----------------------------
230 -- Set_Style_Check_Options --
231 -----------------------------
233 -- Version used when no error checking is required
235 procedure Set_Style_Check_Options (Options : String) is
236 OK : Boolean;
237 EC : Natural;
238 pragma Warnings (Off, EC);
239 begin
240 Set_Style_Check_Options (Options, OK, EC);
241 pragma Assert (OK);
242 end Set_Style_Check_Options;
244 -- Normal version with error checking
246 procedure Set_Style_Check_Options
247 (Options : String;
248 OK : out Boolean;
249 Err_Col : out Natural)
251 C : Character;
253 On : Boolean := True;
254 -- Set to False if minus encountered
255 -- Set to True if plus encountered
257 Last_Option : Character := ' ';
258 -- Set to last character encountered
260 procedure Add_Img (N : Natural);
261 -- Concatenates image of N at end of Style_Msg_Buf
263 procedure Bad_Style_Switch (Msg : String);
264 -- Called if bad style switch found. Msg is set in Style_Msg_Buf and
265 -- Style_Msg_Len. OK is set False.
267 -------------
268 -- Add_Img --
269 -------------
271 procedure Add_Img (N : Natural) is
272 begin
273 if N >= 10 then
274 Add_Img (N / 10);
275 end if;
277 Style_Msg_Len := Style_Msg_Len + 1;
278 Style_Msg_Buf (Style_Msg_Len) :=
279 Character'Val (N mod 10 + Character'Pos ('0'));
280 end Add_Img;
282 ----------------------
283 -- Bad_Style_Switch --
284 ----------------------
286 procedure Bad_Style_Switch (Msg : String) is
287 begin
288 OK := False;
289 Style_Msg_Len := Msg'Length;
290 Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
291 end Bad_Style_Switch;
293 -- Start of processing for Set_Style_Check_Options
295 begin
296 Err_Col := Options'First;
297 while Err_Col <= Options'Last loop
298 C := Options (Err_Col);
299 Last_Option := C;
300 Err_Col := Err_Col + 1;
302 -- Turning switches on
304 if On then
305 case C is
306 when '+' =>
307 null;
309 when '-' =>
310 On := False;
312 when '0' .. '9' =>
313 Style_Check_Indentation :=
314 Character'Pos (C) - Character'Pos ('0');
316 when 'a' =>
317 Style_Check_Attribute_Casing := True;
319 when 'A' =>
320 Style_Check_Array_Attribute_Index := True;
322 when 'b' =>
323 Style_Check_Blanks_At_End := True;
325 when 'B' =>
326 Style_Check_Boolean_And_Or := True;
328 when 'c' =>
329 Style_Check_Comments := True;
330 Style_Check_Comments_Spacing := 2;
332 when 'C' =>
333 Style_Check_Comments := True;
334 Style_Check_Comments_Spacing := 1;
336 when 'd' =>
337 Style_Check_DOS_Line_Terminator := True;
339 when 'D' =>
340 Style_Check_Mixed_Case_Decls := True;
342 when 'e' =>
343 Style_Check_End_Labels := True;
345 when 'f' =>
346 Style_Check_Form_Feeds := True;
348 when 'g' =>
349 Set_GNAT_Style_Check_Options;
351 when 'h' =>
352 Style_Check_Horizontal_Tabs := True;
354 when 'i' =>
355 Style_Check_If_Then_Layout := True;
357 when 'I' =>
358 Style_Check_Mode_In := True;
360 when 'k' =>
361 Style_Check_Keyword_Casing := True;
363 when 'l' =>
364 Style_Check_Layout := True;
366 when 'L' =>
367 Style_Max_Nesting_Level := 0;
369 if Err_Col > Options'Last
370 or else Options (Err_Col) not in '0' .. '9'
371 then
372 Bad_Style_Switch ("invalid nesting level");
373 return;
374 end if;
376 loop
377 Style_Max_Nesting_Level :=
378 Style_Max_Nesting_Level * 10 +
379 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
381 if Style_Max_Nesting_Level > 999 then
382 Bad_Style_Switch
383 ("max nesting level (999) exceeded in style check");
384 return;
385 end if;
387 Err_Col := Err_Col + 1;
388 exit when Err_Col > Options'Last
389 or else Options (Err_Col) not in '0' .. '9';
390 end loop;
392 Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
394 when 'm' =>
395 Style_Check_Max_Line_Length := True;
396 Style_Max_Line_Length := 79;
398 when 'M' =>
399 Style_Max_Line_Length := 0;
401 if Err_Col > Options'Last
402 or else Options (Err_Col) not in '0' .. '9'
403 then
404 Bad_Style_Switch
405 ("invalid line length in style check");
406 return;
407 end if;
409 loop
410 Style_Max_Line_Length :=
411 Style_Max_Line_Length * 10 +
412 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
414 if Style_Max_Line_Length > Int (Max_Line_Length) then
415 OK := False;
416 Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
417 Style_Msg_Len := 27;
418 Add_Img (Natural (Max_Line_Length));
419 return;
420 end if;
422 Err_Col := Err_Col + 1;
423 exit when Err_Col > Options'Last
424 or else Options (Err_Col) not in '0' .. '9';
425 end loop;
427 Style_Check_Max_Line_Length := Style_Max_Line_Length /= 0;
429 when 'n' =>
430 Style_Check_Standard := True;
432 when 'N' =>
433 Reset_Style_Check_Options;
435 when 'o' =>
436 Style_Check_Order_Subprograms := True;
438 when 'O' =>
439 Style_Check_Missing_Overriding := True;
441 when 'p' =>
442 Style_Check_Pragma_Casing := True;
444 when 'r' =>
445 Style_Check_References := True;
447 when 's' =>
448 Style_Check_Specs := True;
450 when 'S' =>
451 Style_Check_Separate_Stmt_Lines := True;
453 when 't' =>
454 Style_Check_Tokens := True;
456 when 'u' =>
457 Style_Check_Blank_Lines := True;
459 when 'x' =>
460 Style_Check_Xtra_Parens := True;
462 when 'y' =>
463 Set_Default_Style_Check_Options;
465 when 'z' =>
466 Style_Check_Xtra_Parens_Precedence := True;
468 when ' ' =>
469 null;
471 when others =>
472 if Ignore_Unrecognized_VWY_Switches then
473 Write_Line ("unrecognized switch -gnaty" & C & " ignored");
474 else
475 Err_Col := Err_Col - 1;
476 Bad_Style_Switch ("invalid style switch");
477 return;
478 end if;
479 end case;
481 -- Turning switches off
483 else
484 case C is
485 when '+' =>
486 On := True;
488 when '-' =>
489 null;
491 when '0' .. '9' =>
492 Style_Check_Indentation := 0;
494 when 'a' =>
495 Style_Check_Attribute_Casing := False;
497 when 'A' =>
498 Style_Check_Array_Attribute_Index := False;
500 when 'b' =>
501 Style_Check_Blanks_At_End := False;
503 when 'B' =>
504 Style_Check_Boolean_And_Or := False;
506 when 'c' | 'C' =>
507 Style_Check_Comments := False;
509 when 'd' =>
510 Style_Check_DOS_Line_Terminator := False;
512 when 'D' =>
513 Style_Check_Mixed_Case_Decls := False;
515 when 'e' =>
516 Style_Check_End_Labels := False;
518 when 'f' =>
519 Style_Check_Form_Feeds := False;
521 when 'g' =>
522 Reset_Style_Check_Options;
524 when 'h' =>
525 Style_Check_Horizontal_Tabs := False;
527 when 'i' =>
528 Style_Check_If_Then_Layout := False;
530 when 'I' =>
531 Style_Check_Mode_In := False;
533 when 'k' =>
534 Style_Check_Keyword_Casing := False;
536 when 'l' =>
537 Style_Check_Layout := False;
539 when 'L' =>
540 Style_Max_Nesting_Level := 0;
542 when 'm' =>
543 Style_Check_Max_Line_Length := False;
545 when 'M' =>
546 Style_Max_Line_Length := 0;
547 Style_Check_Max_Line_Length := False;
549 when 'n' =>
550 Style_Check_Standard := False;
552 when 'o' =>
553 Style_Check_Order_Subprograms := False;
555 when 'O' =>
556 Style_Check_Missing_Overriding := False;
558 when 'p' =>
559 Style_Check_Pragma_Casing := False;
561 when 'r' =>
562 Style_Check_References := False;
564 when 's' =>
565 Style_Check_Specs := False;
567 when 'S' =>
568 Style_Check_Separate_Stmt_Lines := False;
570 when 't' =>
571 Style_Check_Tokens := False;
573 when 'u' =>
574 Style_Check_Blank_Lines := False;
576 when 'x' =>
577 Style_Check_Xtra_Parens := False;
579 when 'z' =>
580 Style_Check_Xtra_Parens_Precedence := False;
582 when ' ' =>
583 null;
585 when others =>
586 if Ignore_Unrecognized_VWY_Switches then
587 Write_Line ("unrecognized switch -gnaty-" & C & " ignored");
588 else
589 Err_Col := Err_Col - 1;
590 Bad_Style_Switch ("invalid style switch");
591 return;
592 end if;
593 end case;
594 end if;
595 end loop;
597 -- Turn on style checking if other than N at end of string
599 Style_Check := (Last_Option /= 'N');
600 OK := True;
601 end Set_Style_Check_Options;
602 end Stylesw;