]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/stylesw.adb
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / ada / stylesw.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S T Y L E S W --
6-- --
7-- B o d y --
8-- --
8d0d46f4 9-- Copyright (C) 1992-2021, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
23d0d17f
TQ
26with Hostparm; use Hostparm;
27with Opt; use Opt;
d48cd424 28with Output; use Output;
996ae0b0
RK
29
30package body Stylesw is
31
09494c32
AC
32 -- The following constant defines the default style options for -gnaty
33
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
52
53 -- The following constant defines the GNAT style options, showing them
54 -- as additions to the standard default style check options.
55
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
498d1b80
AC
63 -- Note: we intend GNAT_Style to also include the following, but we do
64 -- not yet have the whole tool suite clean with respect to this.
65
66 -- "B" & -- check boolean operators
498d1b80 67
996ae0b0
RK
68 -------------------------------
69 -- Reset_Style_Check_Options --
70 -------------------------------
71
72 procedure Reset_Style_Check_Options is
73 begin
a9a5b8ac
RD
74 Style_Check_Indentation := 0;
75 Style_Check_Array_Attribute_Index := False;
76 Style_Check_Attribute_Casing := False;
77 Style_Check_Blanks_At_End := False;
78 Style_Check_Blank_Lines := False;
a36c1c3e 79 Style_Check_Boolean_And_Or := False;
a9a5b8ac
RD
80 Style_Check_Comments := False;
81 Style_Check_DOS_Line_Terminator := False;
c4487c3b 82 Style_Check_Mixed_Case_Decls := False;
a9a5b8ac
RD
83 Style_Check_End_Labels := False;
84 Style_Check_Form_Feeds := False;
85 Style_Check_Horizontal_Tabs := False;
86 Style_Check_If_Then_Layout := False;
87 Style_Check_Keyword_Casing := False;
88 Style_Check_Layout := False;
89 Style_Check_Max_Line_Length := False;
90 Style_Check_Max_Nesting_Level := False;
235f4375 91 Style_Check_Missing_Overriding := False;
a9a5b8ac
RD
92 Style_Check_Mode_In := False;
93 Style_Check_Order_Subprograms := False;
94 Style_Check_Pragma_Casing := False;
95 Style_Check_References := False;
835d23b2 96 Style_Check_Separate_Stmt_Lines := False;
a9a5b8ac
RD
97 Style_Check_Specs := False;
98 Style_Check_Standard := False;
99 Style_Check_Tokens := False;
100 Style_Check_Xtra_Parens := False;
996ae0b0
RK
101 end Reset_Style_Check_Options;
102
c75c4293
AC
103 ---------------------
104 -- RM_Column_Check --
105 ---------------------
106
107 function RM_Column_Check return Boolean is
108 begin
109 return Style_Check and Style_Check_Layout;
110 end RM_Column_Check;
111
996ae0b0
RK
112 ------------------------------
113 -- Save_Style_Check_Options --
114 ------------------------------
115
116 procedure Save_Style_Check_Options (Options : out Style_Check_Options) is
835d23b2 117 P : Natural := 0;
996ae0b0
RK
118
119 procedure Add (C : Character; S : Boolean);
120 -- Add given character C to string if switch S is true
121
0da2c8ac
AC
122 procedure Add_Nat (N : Nat);
123 -- Add given natural number to string
124
125 ---------
126 -- Add --
127 ---------
128
996ae0b0
RK
129 procedure Add (C : Character; S : Boolean) is
130 begin
131 if S then
132 P := P + 1;
133 Options (P) := C;
134 end if;
135 end Add;
136
0da2c8ac
AC
137 -------------
138 -- Add_Nat --
139 -------------
140
141 procedure Add_Nat (N : Nat) is
142 begin
143 if N > 9 then
144 Add_Nat (N / 10);
145 end if;
146
147 P := P + 1;
148 Options (P) := Character'Val (Character'Pos ('0') + N mod 10);
149 end Add_Nat;
150
996ae0b0
RK
151 -- Start of processing for Save_Style_Check_Options
152
153 begin
996ae0b0
RK
154 Add (Character'Val (Style_Check_Indentation + Character'Pos ('0')),
155 Style_Check_Indentation /= 0);
156
157 Add ('a', Style_Check_Attribute_Casing);
a9a5b8ac 158 Add ('A', Style_Check_Array_Attribute_Index);
996ae0b0 159 Add ('b', Style_Check_Blanks_At_End);
a36c1c3e 160 Add ('B', Style_Check_Boolean_And_Or);
a2773bd3 161
6577bef9
AC
162 if Style_Check_Comments then
163 if Style_Check_Comments_Spacing = 2 then
164 Add ('c', Style_Check_Comments);
3ec54569
PMR
165 else
166 pragma Assert (Style_Check_Comments_Spacing = 1);
6577bef9
AC
167 Add ('C', Style_Check_Comments);
168 end if;
a2773bd3
AC
169 end if;
170
debe0ab6 171 Add ('d', Style_Check_DOS_Line_Terminator);
c4487c3b 172 Add ('D', Style_Check_Mixed_Case_Decls);
996ae0b0
RK
173 Add ('e', Style_Check_End_Labels);
174 Add ('f', Style_Check_Form_Feeds);
175 Add ('h', Style_Check_Horizontal_Tabs);
176 Add ('i', Style_Check_If_Then_Layout);
4f73f89c 177 Add ('I', Style_Check_Mode_In);
996ae0b0
RK
178 Add ('k', Style_Check_Keyword_Casing);
179 Add ('l', Style_Check_Layout);
996ae0b0 180 Add ('n', Style_Check_Standard);
7fe16580 181 Add ('o', Style_Check_Order_Subprograms);
235f4375 182 Add ('O', Style_Check_Missing_Overriding);
996ae0b0
RK
183 Add ('p', Style_Check_Pragma_Casing);
184 Add ('r', Style_Check_References);
185 Add ('s', Style_Check_Specs);
835d23b2 186 Add ('S', Style_Check_Separate_Stmt_Lines);
996ae0b0 187 Add ('t', Style_Check_Tokens);
357ac4df 188 Add ('u', Style_Check_Blank_Lines);
62b63164 189 Add ('x', Style_Check_Xtra_Parens);
996ae0b0
RK
190
191 if Style_Check_Max_Line_Length then
0da2c8ac 192 P := P + 1;
996ae0b0 193 Options (P) := 'M';
0da2c8ac
AC
194 Add_Nat (Style_Max_Line_Length);
195 end if;
196
197 if Style_Check_Max_Nesting_Level then
198 P := P + 1;
199 Options (P) := 'L';
200 Add_Nat (Style_Max_Nesting_Level);
996ae0b0
RK
201 end if;
202
0da2c8ac
AC
203 pragma Assert (P <= Options'Last);
204
205 while P < Options'Last loop
206 P := P + 1;
207 Options (P) := ' ';
208 end loop;
996ae0b0
RK
209 end Save_Style_Check_Options;
210
211 -------------------------------------
212 -- Set_Default_Style_Check_Options --
213 -------------------------------------
214
215 procedure Set_Default_Style_Check_Options is
216 begin
217 Reset_Style_Check_Options;
09494c32 218 Set_Style_Check_Options (Default_Style);
996ae0b0
RK
219 end Set_Default_Style_Check_Options;
220
94a198aa
RD
221 ----------------------------------
222 -- Set_GNAT_Style_Check_Options --
223 ----------------------------------
224
225 procedure Set_GNAT_Style_Check_Options is
226 begin
227 Reset_Style_Check_Options;
09494c32 228 Set_Style_Check_Options (GNAT_Style);
94a198aa
RD
229 end Set_GNAT_Style_Check_Options;
230
996ae0b0
RK
231 -----------------------------
232 -- Set_Style_Check_Options --
233 -----------------------------
234
235 -- Version used when no error checking is required
236
237 procedure Set_Style_Check_Options (Options : String) is
238 OK : Boolean;
239 EC : Natural;
67ce0d7e 240 pragma Warnings (Off, EC);
996ae0b0
RK
241 begin
242 Set_Style_Check_Options (Options, OK, EC);
23d0d17f 243 pragma Assert (OK);
996ae0b0
RK
244 end Set_Style_Check_Options;
245
246 -- Normal version with error checking
247
248 procedure Set_Style_Check_Options
249 (Options : String;
250 OK : out Boolean;
251 Err_Col : out Natural)
252 is
996ae0b0
RK
253 C : Character;
254
30179374
RD
255 On : Boolean := True;
256 -- Set to False if minus encountered
257 -- Set to True if plus encountered
258
259 Last_Option : Character := ' ';
260 -- Set to last character encountered
261
23d0d17f
TQ
262 procedure Add_Img (N : Natural);
263 -- Concatenates image of N at end of Style_Msg_Buf
264
265 procedure Bad_Style_Switch (Msg : String);
3354f96d 266 -- Called if bad style switch found. Msg is set in Style_Msg_Buf and
23d0d17f
TQ
267 -- Style_Msg_Len. OK is set False.
268
269 -------------
270 -- Add_Img --
271 -------------
272
273 procedure Add_Img (N : Natural) is
274 begin
275 if N >= 10 then
276 Add_Img (N / 10);
277 end if;
278
279 Style_Msg_Len := Style_Msg_Len + 1;
280 Style_Msg_Buf (Style_Msg_Len) :=
281 Character'Val (N mod 10 + Character'Pos ('0'));
282 end Add_Img;
283
284 ----------------------
285 -- Bad_Style_Switch --
286 ----------------------
287
288 procedure Bad_Style_Switch (Msg : String) is
289 begin
290 OK := False;
291 Style_Msg_Len := Msg'Length;
292 Style_Msg_Buf (1 .. Style_Msg_Len) := Msg;
293 end Bad_Style_Switch;
294
295 -- Start of processing for Set_Style_Check_Options
296
996ae0b0 297 begin
23d0d17f
TQ
298 Err_Col := Options'First;
299 while Err_Col <= Options'Last loop
300 C := Options (Err_Col);
30179374 301 Last_Option := C;
23d0d17f 302 Err_Col := Err_Col + 1;
996ae0b0 303
30179374
RD
304 -- Turning switches on
305
306 if On then
307 case C is
30179374
RD
308 when '+' =>
309 null;
310
311 when '-' =>
312 On := False;
313
314 when '0' .. '9' =>
23d0d17f
TQ
315 Style_Check_Indentation :=
316 Character'Pos (C) - Character'Pos ('0');
996ae0b0
RK
317
318 when 'a' =>
a9a5b8ac
RD
319 Style_Check_Attribute_Casing := True;
320
321 when 'A' =>
322 Style_Check_Array_Attribute_Index := True;
996ae0b0
RK
323
324 when 'b' =>
a9a5b8ac 325 Style_Check_Blanks_At_End := True;
996ae0b0 326
a36c1c3e
RD
327 when 'B' =>
328 Style_Check_Boolean_And_Or := True;
329
996ae0b0 330 when 'c' =>
a9a5b8ac 331 Style_Check_Comments := True;
a2773bd3
AC
332 Style_Check_Comments_Spacing := 2;
333
334 when 'C' =>
335 Style_Check_Comments := True;
336 Style_Check_Comments_Spacing := 1;
debe0ab6
RD
337
338 when 'd' =>
a9a5b8ac 339 Style_Check_DOS_Line_Terminator := True;
996ae0b0 340
c4487c3b
BD
341 when 'D' =>
342 Style_Check_Mixed_Case_Decls := True;
343
996ae0b0 344 when 'e' =>
a9a5b8ac 345 Style_Check_End_Labels := True;
996ae0b0
RK
346
347 when 'f' =>
a9a5b8ac 348 Style_Check_Form_Feeds := True;
996ae0b0 349
94a198aa
RD
350 when 'g' =>
351 Set_GNAT_Style_Check_Options;
352
996ae0b0 353 when 'h' =>
a9a5b8ac 354 Style_Check_Horizontal_Tabs := True;
996ae0b0
RK
355
356 when 'i' =>
a9a5b8ac 357 Style_Check_If_Then_Layout := True;
996ae0b0 358
4f73f89c 359 when 'I' =>
a9a5b8ac 360 Style_Check_Mode_In := True;
4f73f89c 361
996ae0b0 362 when 'k' =>
a9a5b8ac 363 Style_Check_Keyword_Casing := True;
996ae0b0
RK
364
365 when 'l' =>
a9a5b8ac 366 Style_Check_Layout := True;
996ae0b0 367
0da2c8ac
AC
368 when 'L' =>
369 Style_Max_Nesting_Level := 0;
370
23d0d17f
TQ
371 if Err_Col > Options'Last
372 or else Options (Err_Col) not in '0' .. '9'
0da2c8ac 373 then
23d0d17f 374 Bad_Style_Switch ("invalid nesting level");
0da2c8ac
AC
375 return;
376 end if;
377
378 loop
379 Style_Max_Nesting_Level :=
380 Style_Max_Nesting_Level * 10 +
23d0d17f 381 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
0da2c8ac
AC
382
383 if Style_Max_Nesting_Level > 999 then
23d0d17f
TQ
384 Bad_Style_Switch
385 ("max nesting level (999) exceeded in style check");
0da2c8ac
AC
386 return;
387 end if;
388
23d0d17f
TQ
389 Err_Col := Err_Col + 1;
390 exit when Err_Col > Options'Last
391 or else Options (Err_Col) not in '0' .. '9';
0da2c8ac
AC
392 end loop;
393
394 Style_Check_Max_Nesting_Level := Style_Max_Nesting_Level /= 0;
395
996ae0b0 396 when 'm' =>
a9a5b8ac
RD
397 Style_Check_Max_Line_Length := True;
398 Style_Max_Line_Length := 79;
996ae0b0 399
996ae0b0 400 when 'M' =>
a9a5b8ac 401 Style_Max_Line_Length := 0;
996ae0b0 402
23d0d17f
TQ
403 if Err_Col > Options'Last
404 or else Options (Err_Col) not in '0' .. '9'
996ae0b0 405 then
23d0d17f
TQ
406 Bad_Style_Switch
407 ("invalid line length in style check");
996ae0b0
RK
408 return;
409 end if;
410
411 loop
412 Style_Max_Line_Length :=
413 Style_Max_Line_Length * 10 +
23d0d17f 414 Character'Pos (Options (Err_Col)) - Character'Pos ('0');
fbf5a39b 415
23d0d17f 416 if Style_Max_Line_Length > Int (Max_Line_Length) then
fbf5a39b 417 OK := False;
23d0d17f
TQ
418 Style_Msg_Buf (1 .. 27) := "max line length allowed is ";
419 Style_Msg_Len := 27;
420 Add_Img (Natural (Max_Line_Length));
fbf5a39b
AC
421 return;
422 end if;
423
23d0d17f
TQ
424 Err_Col := Err_Col + 1;
425 exit when Err_Col > Options'Last
426 or else Options (Err_Col) not in '0' .. '9';
996ae0b0
RK
427 end loop;
428
a9a5b8ac 429 Style_Check_Max_Line_Length := Style_Max_Line_Length /= 0;
996ae0b0 430
23d0d17f 431 when 'n' =>
a9a5b8ac 432 Style_Check_Standard := True;
23d0d17f
TQ
433
434 when 'N' =>
435 Reset_Style_Check_Options;
436
996ae0b0 437 when 'o' =>
a9a5b8ac 438 Style_Check_Order_Subprograms := True;
996ae0b0 439
235f4375
AC
440 when 'O' =>
441 Style_Check_Missing_Overriding := True;
442
996ae0b0 443 when 'p' =>
a9a5b8ac 444 Style_Check_Pragma_Casing := True;
996ae0b0
RK
445
446 when 'r' =>
a9a5b8ac 447 Style_Check_References := True;
996ae0b0
RK
448
449 when 's' =>
a9a5b8ac 450 Style_Check_Specs := True;
996ae0b0 451
835d23b2
RD
452 when 'S' =>
453 Style_Check_Separate_Stmt_Lines := True;
454
996ae0b0 455 when 't' =>
a9a5b8ac 456 Style_Check_Tokens := True;
996ae0b0 457
357ac4df 458 when 'u' =>
a9a5b8ac 459 Style_Check_Blank_Lines := True;
357ac4df 460
62b63164 461 when 'x' =>
a9a5b8ac 462 Style_Check_Xtra_Parens := True;
62b63164 463
30179374
RD
464 when 'y' =>
465 Set_Default_Style_Check_Options;
466
467 when ' ' =>
468 null;
469
470 when others =>
d48cd424
RD
471 if Ignore_Unrecognized_VWY_Switches then
472 Write_Line ("unrecognized switch -gnaty" & C & " ignored");
473 else
474 Err_Col := Err_Col - 1;
e11b776b 475 Bad_Style_Switch ("invalid style switch");
d48cd424
RD
476 return;
477 end if;
30179374
RD
478 end case;
479
480 -- Turning switches off
481
482 else
483 case C is
30179374
RD
484 when '+' =>
485 On := True;
486
487 when '-' =>
488 null;
489
490 when '0' .. '9' =>
491 Style_Check_Indentation := 0;
492
493 when 'a' =>
494 Style_Check_Attribute_Casing := False;
495
496 when 'A' =>
497 Style_Check_Array_Attribute_Index := False;
498
499 when 'b' =>
500 Style_Check_Blanks_At_End := False;
501
a36c1c3e
RD
502 when 'B' =>
503 Style_Check_Boolean_And_Or := False;
504
a2773bd3 505 when 'c' | 'C' =>
30179374
RD
506 Style_Check_Comments := False;
507
508 when 'd' =>
509 Style_Check_DOS_Line_Terminator := False;
510
c4487c3b
BD
511 when 'D' =>
512 Style_Check_Mixed_Case_Decls := False;
513
30179374
RD
514 when 'e' =>
515 Style_Check_End_Labels := False;
516
517 when 'f' =>
518 Style_Check_Form_Feeds := False;
519
520 when 'g' =>
521 Reset_Style_Check_Options;
522
523 when 'h' =>
524 Style_Check_Horizontal_Tabs := False;
525
526 when 'i' =>
527 Style_Check_If_Then_Layout := False;
528
529 when 'I' =>
530 Style_Check_Mode_In := False;
531
532 when 'k' =>
533 Style_Check_Keyword_Casing := False;
534
535 when 'l' =>
536 Style_Check_Layout := False;
537
538 when 'L' =>
539 Style_Max_Nesting_Level := 0;
540
541 when 'm' =>
542 Style_Check_Max_Line_Length := False;
543
544 when 'M' =>
545 Style_Max_Line_Length := 0;
546 Style_Check_Max_Line_Length := False;
547
548 when 'n' =>
549 Style_Check_Standard := False;
550
551 when 'o' =>
552 Style_Check_Order_Subprograms := False;
a20f4389
AC
553
554 when 'O' =>
555 Style_Check_Missing_Overriding := False;
30179374
RD
556
557 when 'p' =>
558 Style_Check_Pragma_Casing := False;
559
560 when 'r' =>
561 Style_Check_References := False;
562
563 when 's' =>
564 Style_Check_Specs := False;
565
566 when 'S' =>
567 Style_Check_Separate_Stmt_Lines := False;
568
569 when 't' =>
570 Style_Check_Tokens := False;
571
572 when 'u' =>
573 Style_Check_Blank_Lines := False;
574
575 when 'x' =>
576 Style_Check_Xtra_Parens := False;
577
996ae0b0
RK
578 when ' ' =>
579 null;
580
581 when others =>
d48cd424
RD
582 if Ignore_Unrecognized_VWY_Switches then
583 Write_Line ("unrecognized switch -gnaty-" & C & " ignored");
584 else
585 Err_Col := Err_Col - 1;
e11b776b 586 Bad_Style_Switch ("invalid style switch");
d48cd424
RD
587 return;
588 end if;
30179374
RD
589 end case;
590 end if;
996ae0b0
RK
591 end loop;
592
30179374
RD
593 -- Turn on style checking if other than N at end of string
594
595 Style_Check := (Last_Option /= 'N');
996ae0b0 596 OK := True;
996ae0b0 597 end Set_Style_Check_Options;
996ae0b0 598end Stylesw;