Debian Codegraph @

gcc/ada/diagnostics.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- D I A G N O S T I C S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, 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 Types; use Types;
27 with GNAT.Lists; use GNAT.Lists;
28
29 package Diagnostics is
30
31 type Diagnostic_Id is
32 (No_Diagnostic_Id,
33 GNAT0001,
34 GNAT0002,
35 GNAT0003,
36 GNAT0004,
37 GNAT0005,
38 GNAT0006,
39 GNAT0007,
40 GNAT0008,
41 GNAT0009,
42 GNAT0010,
43 GNAT0011);
44
45 -- Labeled_Span_Type represents a span of source code that is associated
46 -- with a textual label. Primary spans indicate the primary location of the
47 -- diagnostic. Non-primary spans are used to indicate secondary locations.
48 --
49 -- Spans can contain labels that are used to annotate the highlighted span.
50 -- Usually, the label is a short and concise message that provide
51 -- additional allthough non-critical information about the span. This is
52 -- an important since labels are not printed in the brief output and are
53 -- only present in the pretty and structural outputs. That is an important
54 -- distintion when choosing between a label and a sub-diagnostic.
55 type Labeled_Span_Type is record
56 Label : String_Ptr := null;
57 -- Text associated with the span
58
59 Span : Source_Span := (others => No_Location);
60 -- Textual region in the source code
61
62 Is_Primary : Boolean := True;
63 -- Primary spans are used to indicate the primary location of the
64 -- diagnostic. Typically there should just be one primary span per
65 -- diagnostic.
66 -- Non-primary spans are used to indicate secondary locations and
67 -- typically are formatted in a different way or omitted in some
68 -- contexts.
69
70 Is_Region : Boolean := False;
71 -- Regional spans are multiline spans that have a unique way of being
72 -- displayed in the pretty output.
73 end record;
74
75 No_Labeled_Span : constant Labeled_Span_Type := (others => <>);
76
77 procedure Destroy (Elem : in out Labeled_Span_Type);
78 pragma Inline (Destroy);
79
80 package Labeled_Span_Lists is new Doubly_Linked_Lists
81 (Element_Type => Labeled_Span_Type,
82 "=" => "=",
83 Destroy_Element => Destroy,
84 Check_Tampering => False);
85 subtype Labeled_Span_List is Labeled_Span_Lists.Doubly_Linked_List;
86
87 type Sub_Diagnostic_Kind is
88 (Continuation,
89 Help,
90 Note,
91 Suggestion);
92
93 -- Sub_Diagnostic_Type represents a sub-diagnostic message that is meant
94 -- to provide additional information about the primary diagnostic message.
95 --
96 -- Sub-diagnostics are usually constructed with a full sentence as the
97 -- message and provide important context to the main diagnostic message or
98 -- some concrete action to the user.
99 --
100 -- This is different from the labels of labeled spans which are meant to be
101 -- short and concise and are mostly there to annotate the higlighted span.
102
103 type Sub_Diagnostic_Type is record
104 Kind : Sub_Diagnostic_Kind;
105
106 Message : String_Ptr;
107
108 Locations : Labeled_Span_List;
109 end record;
110
111 procedure Add_Location
112 (Diagnostic : in out Sub_Diagnostic_Type; Location : Labeled_Span_Type);
113
114 function Primary_Location
115 (Diagnostic : Sub_Diagnostic_Type) return Labeled_Span_Type;
116
117 procedure Destroy (Elem : in out Sub_Diagnostic_Type);
118 pragma Inline (Destroy);
119
120 package Sub_Diagnostic_Lists is new Doubly_Linked_Lists
121 (Element_Type => Sub_Diagnostic_Type,
122 "=" => "=",
123 Destroy_Element => Destroy,
124 Check_Tampering => False);
125
126 subtype Sub_Diagnostic_List is Sub_Diagnostic_Lists.Doubly_Linked_List;
127
128 -- An Edit_Type represents a textual edit that is associated with a Fix.
129 type Edit_Type is record
130 Span : Source_Span;
131 -- Region of the file to be removed
132
133 Text : String_Ptr;
134 -- Text to be inserted at the start location of the span
135 end record;
136
137 procedure Destroy (Elem : in out Edit_Type);
138 pragma Inline (Destroy);
139
140 package Edit_Lists is new Doubly_Linked_Lists
141 (Element_Type => Edit_Type,
142 "=" => "=",
143 Destroy_Element => Destroy,
144 Check_Tampering => False);
145
146 subtype Edit_List is Edit_Lists.Doubly_Linked_List;
147
148 -- Type Applicability_Type will indicate the state of the resulting code
149 -- after applying a fix.
150 -- * Option Has_Placeholders indicates that the fix contains placeholders
151 -- that the user would need to fill.
152 -- * Option Legal indicates that applying the fix will result in legal Ada
153 -- code.
154 -- * Option Possibly_Illegal indicates that applying the fix will result in
155 -- possibly legal, but also possibly illegal Ada code.
156 type Applicability_Type is
157 (Has_Placeholders,
158 Legal,
159 Possibly_Illegal,
160 Unspecified);
161
162 type Fix_Type is record
163 Description : String_Ptr := null;
164 -- Message describing the fix that will be displayed to the user.
165
166 Applicability : Applicability_Type := Unspecified;
167
168 Edits : Edit_List;
169 -- File changes for the fix.
170 end record;
171
172 procedure Destroy (Elem : in out Fix_Type);
173 pragma Inline (Destroy);
174
175 package Fix_Lists is new Doubly_Linked_Lists
176 (Element_Type => Fix_Type,
177 "=" => "=",
178 Destroy_Element => Destroy,
179 Check_Tampering => False);
180
181 subtype Fix_List is Fix_Lists.Doubly_Linked_List;
182
183 procedure Add_Edit (Fix : in out Fix_Type; Edit : Edit_Type);
184
185 type Status_Type is
186 (Active,
187 Deprecated);
188
189 type Switch_Id is (
190 No_Switch_Id,
191 gnatwb,
192 gnatwc,
193 gnatwd,
194 gnatwf,
195 gnatwg,
196 gnatwh,
197 gnatwi,
198 gnatwj,
199 gnatwk,
200 gnatwl,
201 gnatwm,
202 gnatwo,
203 gnatwp,
204 gnatwq,
205 gnatwr,
206 gnatwt,
207 gnatwu,
208 gnatwv,
209 gnatww,
210 gnatwx,
211 gnatwy,
212 gnatwz,
213 gnatw_dot_a,
214 gnatw_dot_b,
215 gnatw_dot_c,
216 gnatw_dot_f,
217 gnatw_dot_h,
218 gnatw_dot_i,
219 gnatw_dot_j,
220 gnatw_dot_k,
221 gnatw_dot_l,
222 gnatw_dot_m,
223 gnatw_dot_n,
224 gnatw_dot_o,
225 gnatw_dot_p,
226 gnatw_dot_q,
227 gnatw_dot_r,
228 gnatw_dot_s,
229 gnatw_dot_t,
230 gnatw_dot_u,
231 gnatw_dot_v,
232 gnatw_dot_w,
233 gnatw_dot_x,
234 gnatw_dot_y,
235 gnatw_dot_z,
236 gnatw_underscore_a,
237 gnatw_underscore_c,
238 gnatw_underscore_j,
239 gnatw_underscore_l,
240 gnatw_underscore_p,
241 gnatw_underscore_q,
242 gnatw_underscore_r,
243 gnatw_underscore_s,
244 gnaty,
245 gnatya,
246 gnatyb,
247 gnatyc,
248 gnatyd,
249 gnatye,
250 gnatyf,
251 gnatyh,
252 gnatyi,
253 gnatyk,
254 gnatyl,
255 gnatym,
256 gnatyn,
257 gnatyo,
258 gnatyp,
259 gnatyr,
260 gnatys,
261 gnatyu,
262 gnatyx,
263 gnatyz,
264 gnatyaa,
265 gnatybb,
266 gnatycc,
267 gnatydd,
268 gnatyii,
269 gnatyll,
270 gnatymm,
271 gnatyoo,
272 gnatyss,
273 gnatytt,
274 gnatel
275 );
276
277 subtype Active_Switch_Id is Switch_Id range gnatwb .. gnatel;
278 -- The range of switch ids that represent switches that trigger a specific
279 -- diagnostic check.
280
281 type Switch_Type is record
282
283 Status : Status_Type := Active;
284 -- The status will indicate whether the switch is currently active,
285 -- or has been deprecated. A deprecated switch will not control
286 -- diagnostics, and will not be emitted by the GNAT usage.
287
288 Human_Id : String_Ptr := null;
289 -- The Human_Id will be a unique and stable string-based ID which
290 -- identifies the content of the switch within the switch registry.
291 -- This ID will appear in SARIF readers.
292
293 Short_Name : String_Ptr := null;
294 -- The Short_Name will denote the -gnatXX name of the switch.
295
296 Description : String_Ptr := null;
297 -- The description will contain the description of the switch, as it is
298 -- currently emitted by the GNAT usage.
299
300 Documentation_Url : String_Ptr := null;
301 -- The documentation_url will point to the AdaCore documentation site
302 -- for the switch.
303
304 end record;
305
306 type Diagnostic_Kind is
307 (Error,
308 Non_Serious_Error,
309 -- Typically all errors are considered serious and the compiler should
310 -- stop its processing since the tree is essentially invalid. However,
311 -- some errors are not serious and the compiler can continue its
312 -- processing to discover more critical errors.
313 Warning,
314 Default_Warning,
315 -- Warning representing the old warnings created with the '??' insertion
316 -- character. These warning have the [enabled by default] tag.
317 Restriction_Warning,
318 -- Warning representing the old warnings created with the '?*?'
319 -- insertion character. These warning have the [restriction warning]
320 -- tag.
321 Style,
322 Tagless_Warning,
323 -- Warning representing the old warnings created with the '?' insertion
324 -- character.
325 Info
326 );
327
328 type Diagnostic_Entry_Type is record
329 Status : Status_Type := Active;
330
331 Human_Id : String_Ptr := null;
332 -- A human readable code for the diagnostic. If the diagnostic has a
333 -- switch with a human id then the human_id of the switch shall be used
334 -- in SARIF reports.
335
336 Documentation : String_Ptr := null;
337
338 Switch : Switch_Id := No_Switch_Id;
339 -- The switch that controls the diagnostic message.
340 end record;
341
342 type Diagnostic_Type is record
343
344 Id : Diagnostic_Id := No_Diagnostic_Id;
345
346 Kind : Diagnostic_Kind := Error;
347
348 Switch : Switch_Id := No_Switch_Id;
349
350 Message : String_Ptr := null;
351
352 Warn_Err : Boolean := False;
353 -- Signal whether the diagnostic was converted from a warning to an
354 -- error. This needs to be set during the message emission as this
355 -- behavior depends on the context of the code.
356
357 Locations : Labeled_Span_List := Labeled_Span_Lists.Nil;
358
359 Sub_Diagnostics : Sub_Diagnostic_List := Sub_Diagnostic_Lists.Nil;
360
361 Fixes : Fix_List := Fix_Lists.Nil;
362 end record;
363
364 procedure Destroy (Elem : in out Diagnostic_Type);
365 pragma Inline (Destroy);
366
367 package Diagnostics_Lists is new Doubly_Linked_Lists
368 (Element_Type => Diagnostic_Type,
369 "=" => "=",
370 Destroy_Element => Destroy,
371 Check_Tampering => False);
372
373 subtype Diagnostic_List is Diagnostics_Lists.Doubly_Linked_List;
374
375 All_Diagnostics : Diagnostic_List := Diagnostics_Lists.Create;
376
377 procedure Add_Location
378 (Diagnostic : in out Diagnostic_Type; Location : Labeled_Span_Type);
379
380 procedure Add_Sub_Diagnostic
381 (Diagnostic : in out Diagnostic_Type;
382 Sub_Diagnostic : Sub_Diagnostic_Type);
383
384 procedure Add_Fix (Diagnostic : in out Diagnostic_Type; Fix : Fix_Type);
385
386 procedure Record_Diagnostic (Diagnostic : Diagnostic_Type;
387 Update_Count : Boolean := True);
388
389 procedure Print_Diagnostic (Diagnostic : Diagnostic_Type);
390
391 function Primary_Location
392 (Diagnostic : Diagnostic_Type) return Labeled_Span_Type;
393
394 type Labeled_Span_Array is
395 array (Positive range <>) of Labeled_Span_Type;
396 type Sub_Diagnostic_Array is
397 array (Positive range <>) of Sub_Diagnostic_Type;
398 type Fix_Array is
399 array (Positive range <>) of Fix_Type;
400 type Edit_Array is
401 array (Positive range <>) of Edit_Type;
402
403 No_Locations : constant Labeled_Span_Array (1 .. 0) := (others => <>);
404 No_Sub_Diags : constant Sub_Diagnostic_Array (1 .. 0) := (others => <>);
405 No_Fixes : constant Fix_Array (1 .. 0) := (others => <>);
406 No_Edits : constant Edit_Array (1 .. 0) := (others => <>);
407
408 function Make_Diagnostic
409 (Msg : String;
410 Location : Labeled_Span_Type;
411 Id : Diagnostic_Id := No_Diagnostic_Id;
412 Kind : Diagnostic_Kind := Diagnostics.Error;
413 Switch : Switch_Id := No_Switch_Id;
414 Spans : Labeled_Span_Array := No_Locations;
415 Sub_Diags : Sub_Diagnostic_Array := No_Sub_Diags;
416 Fixes : Fix_Array := No_Fixes)
417 return Diagnostic_Type;
418
419 procedure Record_Diagnostic
420 (Msg : String;
421 Location : Labeled_Span_Type;
422 Id : Diagnostic_Id := No_Diagnostic_Id;
423 Kind : Diagnostic_Kind := Diagnostics.Error;
424 Switch : Switch_Id := No_Switch_Id;
425 Spans : Labeled_Span_Array := No_Locations;
426 Sub_Diags : Sub_Diagnostic_Array := No_Sub_Diags;
427 Fixes : Fix_Array := No_Fixes);
428
429 function Labeled_Span (Span : Source_Span;
430 Label : String := "";
431 Is_Primary : Boolean := True;
432 Is_Region : Boolean := False)
433 return Labeled_Span_Type;
434
435 function Primary_Labeled_Span (Span : Source_Span;
436 Label : String := "")
437 return Labeled_Span_Type;
438
439 function Primary_Labeled_Span (N : Node_Or_Entity_Id;
440 Label : String := "")
441 return Labeled_Span_Type;
442
443 function Secondary_Labeled_Span (Span : Source_Span;
444 Label : String := "")
445 return Labeled_Span_Type;
446
447 function Secondary_Labeled_Span (N : Node_Or_Entity_Id;
448 Label : String := "")
449 return Labeled_Span_Type;
450
451 function Sub_Diag (Msg : String;
452 Kind : Sub_Diagnostic_Kind :=
453 Diagnostics.Continuation;
454 Locations : Labeled_Span_Array := No_Locations)
455 return Sub_Diagnostic_Type;
456
457 function Continuation (Msg : String;
458 Locations : Labeled_Span_Array := No_Locations)
459 return Sub_Diagnostic_Type;
460
461 function Help (Msg : String;
462 Locations : Labeled_Span_Array := No_Locations)
463 return Sub_Diagnostic_Type;
464
465 function Suggestion (Msg : String;
466 Locations : Labeled_Span_Array := No_Locations)
467 return Sub_Diagnostic_Type;
468
469 function Fix (Description : String;
470 Edits : Edit_Array;
471 Applicability : Applicability_Type := Unspecified)
472 return Fix_Type;
473
474 function Edit (Text : String;
475 Span : Source_Span)
476 return Edit_Type;
477 end Diagnostics;