| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- D I A G N O S T I C S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 Atree; use Atree; |
| 27 |
with Debug; use Debug; |
| 28 |
with Diagnostics.Brief_Emitter; |
| 29 |
with Diagnostics.Pretty_Emitter; |
| 30 |
with Diagnostics.Repository; use Diagnostics.Repository; |
| 31 |
with Diagnostics.Utils; use Diagnostics.Utils; |
| 32 |
with Lib; use Lib; |
| 33 |
with Opt; use Opt; |
| 34 |
with Sinput; use Sinput; |
| 35 |
with Warnsw; |
| 36 |
|
| 37 |
package body Diagnostics is |
| 38 |
|
| 39 |
------------- |
| 40 |
-- Destroy -- |
| 41 |
------------- |
| 42 |
|
| 43 |
procedure Destroy (Elem : in out Labeled_Span_Type) is |
| 44 |
begin |
| 45 |
Free (Elem.Label); |
| 46 |
end Destroy; |
| 47 |
|
| 48 |
------------- |
| 49 |
-- Destroy -- |
| 50 |
------------- |
| 51 |
|
| 52 |
procedure Destroy (Elem : in out Sub_Diagnostic_Type) is |
| 53 |
begin |
| 54 |
Free (Elem.Message); |
| 55 |
if Labeled_Span_Lists.Present (Elem.Locations) then |
| 56 |
Labeled_Span_Lists.Destroy (Elem.Locations); |
| 57 |
end if; |
| 58 |
end Destroy; |
| 59 |
|
| 60 |
------------- |
| 61 |
-- Destroy -- |
| 62 |
------------- |
| 63 |
|
| 64 |
procedure Destroy (Elem : in out Edit_Type) is |
| 65 |
begin |
| 66 |
Free (Elem.Text); |
| 67 |
end Destroy; |
| 68 |
|
| 69 |
------------- |
| 70 |
-- Destroy -- |
| 71 |
------------- |
| 72 |
|
| 73 |
procedure Destroy (Elem : in out Fix_Type) is |
| 74 |
begin |
| 75 |
Free (Elem.Description); |
| 76 |
if Edit_Lists.Present (Elem.Edits) then |
| 77 |
Edit_Lists.Destroy (Elem.Edits); |
| 78 |
end if; |
| 79 |
end Destroy; |
| 80 |
|
| 81 |
------------- |
| 82 |
-- Destroy -- |
| 83 |
------------- |
| 84 |
|
| 85 |
procedure Destroy (Elem : in out Diagnostic_Type) is |
| 86 |
begin |
| 87 |
Free (Elem.Message); |
| 88 |
if Labeled_Span_Lists.Present (Elem.Locations) then |
| 89 |
Labeled_Span_Lists.Destroy (Elem.Locations); |
| 90 |
end if; |
| 91 |
if Sub_Diagnostic_Lists.Present (Elem.Sub_Diagnostics) then |
| 92 |
Sub_Diagnostic_Lists.Destroy (Elem.Sub_Diagnostics); |
| 93 |
end if; |
| 94 |
if Fix_Lists.Present (Elem.Fixes) then |
| 95 |
Fix_Lists.Destroy (Elem.Fixes); |
| 96 |
end if; |
| 97 |
end Destroy; |
| 98 |
|
| 99 |
------------------ |
| 100 |
-- Add_Location -- |
| 101 |
------------------ |
| 102 |
|
| 103 |
procedure Add_Location |
| 104 |
(Diagnostic : in out Sub_Diagnostic_Type; Location : Labeled_Span_Type) |
| 105 |
is |
| 106 |
use Labeled_Span_Lists; |
| 107 |
begin |
| 108 |
if not Present (Diagnostic.Locations) then |
| 109 |
Diagnostic.Locations := Create; |
| 110 |
end if; |
| 111 |
|
| 112 |
Append (Diagnostic.Locations, Location); |
| 113 |
end Add_Location; |
| 114 |
|
| 115 |
---------------------- |
| 116 |
-- Primary_Location -- |
| 117 |
---------------------- |
| 118 |
|
| 119 |
function Primary_Location |
| 120 |
(Diagnostic : Sub_Diagnostic_Type) return Labeled_Span_Type |
| 121 |
is |
| 122 |
begin |
| 123 |
return Get_Primary_Labeled_Span (Diagnostic.Locations); |
| 124 |
end Primary_Location; |
| 125 |
|
| 126 |
------------------ |
| 127 |
-- Add_Location -- |
| 128 |
------------------ |
| 129 |
|
| 130 |
procedure Add_Location |
| 131 |
(Diagnostic : in out Diagnostic_Type; Location : Labeled_Span_Type) |
| 132 |
is |
| 133 |
use Labeled_Span_Lists; |
| 134 |
begin |
| 135 |
if not Present (Diagnostic.Locations) then |
| 136 |
Diagnostic.Locations := Create; |
| 137 |
end if; |
| 138 |
|
| 139 |
Append (Diagnostic.Locations, Location); |
| 140 |
end Add_Location; |
| 141 |
|
| 142 |
------------------------ |
| 143 |
-- Add_Sub_Diagnostic -- |
| 144 |
------------------------ |
| 145 |
|
| 146 |
procedure Add_Sub_Diagnostic |
| 147 |
(Diagnostic : in out Diagnostic_Type; |
| 148 |
Sub_Diagnostic : Sub_Diagnostic_Type) |
| 149 |
is |
| 150 |
use Sub_Diagnostic_Lists; |
| 151 |
begin |
| 152 |
if not Present (Diagnostic.Sub_Diagnostics) then |
| 153 |
Diagnostic.Sub_Diagnostics := Create; |
| 154 |
end if; |
| 155 |
|
| 156 |
Append (Diagnostic.Sub_Diagnostics, Sub_Diagnostic); |
| 157 |
end Add_Sub_Diagnostic; |
| 158 |
|
| 159 |
procedure Add_Edit (Fix : in out Fix_Type; Edit : Edit_Type) is |
| 160 |
use Edit_Lists; |
| 161 |
begin |
| 162 |
if not Present (Fix.Edits) then |
| 163 |
Fix.Edits := Create; |
| 164 |
end if; |
| 165 |
|
| 166 |
Append (Fix.Edits, Edit); |
| 167 |
end Add_Edit; |
| 168 |
|
| 169 |
------------- |
| 170 |
-- Add_Fix -- |
| 171 |
------------- |
| 172 |
|
| 173 |
procedure Add_Fix (Diagnostic : in out Diagnostic_Type; Fix : Fix_Type) is |
| 174 |
use Fix_Lists; |
| 175 |
begin |
| 176 |
if not Present (Diagnostic.Fixes) then |
| 177 |
Diagnostic.Fixes := Create; |
| 178 |
end if; |
| 179 |
|
| 180 |
Append (Diagnostic.Fixes, Fix); |
| 181 |
end Add_Fix; |
| 182 |
|
| 183 |
----------------------- |
| 184 |
-- Record_Diagnostic -- |
| 185 |
----------------------- |
| 186 |
|
| 187 |
procedure Record_Diagnostic (Diagnostic : Diagnostic_Type; |
| 188 |
Update_Count : Boolean := True) |
| 189 |
is |
| 190 |
|
| 191 |
procedure Update_Diagnostic_Count (Diagnostic : Diagnostic_Type); |
| 192 |
|
| 193 |
----------------------------- |
| 194 |
-- Update_Diagnostic_Count -- |
| 195 |
----------------------------- |
| 196 |
|
| 197 |
procedure Update_Diagnostic_Count (Diagnostic : Diagnostic_Type) is |
| 198 |
|
| 199 |
begin |
| 200 |
case Diagnostic.Kind is |
| 201 |
when Error => |
| 202 |
Total_Errors_Detected := Total_Errors_Detected + 1; |
| 203 |
Serious_Errors_Detected := Serious_Errors_Detected + 1; |
| 204 |
|
| 205 |
when Non_Serious_Error => |
| 206 |
Total_Errors_Detected := Total_Errors_Detected + 1; |
| 207 |
|
| 208 |
when Warning |
| 209 |
| Default_Warning |
| 210 |
| Tagless_Warning |
| 211 |
| Restriction_Warning |
| 212 |
| Style |
| 213 |
=> |
| 214 |
Warnings_Detected := Warnings_Detected + 1; |
| 215 |
|
| 216 |
if Diagnostic.Warn_Err then |
| 217 |
Warnings_Treated_As_Errors := Warnings_Treated_As_Errors + 1; |
| 218 |
end if; |
| 219 |
|
| 220 |
when Info => |
| 221 |
Info_Messages := Info_Messages + 1; |
| 222 |
end case; |
| 223 |
end Update_Diagnostic_Count; |
| 224 |
|
| 225 |
procedure Handle_Serious_Error; |
| 226 |
-- Internal procedure to do all error message handling for a serious |
| 227 |
-- error message, other than bumping the error counts and arranging |
| 228 |
-- for the message to be output. |
| 229 |
|
| 230 |
procedure Handle_Serious_Error is |
| 231 |
begin |
| 232 |
-- Turn off code generation if not done already |
| 233 |
|
| 234 |
if Operating_Mode = Generate_Code then |
| 235 |
Operating_Mode := Check_Semantics; |
| 236 |
Expander_Active := False; |
| 237 |
end if; |
| 238 |
|
| 239 |
-- Set the fatal error flag in the unit table unless we are in |
| 240 |
-- Try_Semantics mode (in which case we set ignored mode if not |
| 241 |
-- currently set. This stops the semantics from being performed |
| 242 |
-- if we find a serious error. This is skipped if we are currently |
| 243 |
-- dealing with the configuration pragma file. |
| 244 |
|
| 245 |
if Current_Source_Unit /= No_Unit then |
| 246 |
declare |
| 247 |
U : constant Unit_Number_Type := |
| 248 |
Get_Source_Unit |
| 249 |
(Primary_Location (Diagnostic).Span.Ptr); |
| 250 |
begin |
| 251 |
if Try_Semantics then |
| 252 |
if Fatal_Error (U) = None then |
| 253 |
Set_Fatal_Error (U, Error_Ignored); |
| 254 |
end if; |
| 255 |
else |
| 256 |
Set_Fatal_Error (U, Error_Detected); |
| 257 |
end if; |
| 258 |
end; |
| 259 |
end if; |
| 260 |
|
| 261 |
-- Disable warnings on unused use clauses and the like. Otherwise, an |
| 262 |
-- error might hide a reference to an entity in a used package, so |
| 263 |
-- after fixing the error, the use clause no longer looks like it was |
| 264 |
-- unused. |
| 265 |
|
| 266 |
Warnsw.Check_Unreferenced := False; |
| 267 |
Warnsw.Check_Unreferenced_Formals := False; |
| 268 |
end Handle_Serious_Error; |
| 269 |
begin |
| 270 |
Insert_Based_On_Location (All_Diagnostics, Diagnostic); |
| 271 |
|
| 272 |
if Update_Count then |
| 273 |
Update_Diagnostic_Count (Diagnostic); |
| 274 |
end if; |
| 275 |
|
| 276 |
if Diagnostic.Kind = Error then |
| 277 |
Handle_Serious_Error; |
| 278 |
end if; |
| 279 |
end Record_Diagnostic; |
| 280 |
|
| 281 |
---------------------- |
| 282 |
-- Print_Diagnostic -- |
| 283 |
---------------------- |
| 284 |
|
| 285 |
procedure Print_Diagnostic (Diagnostic : Diagnostic_Type) is |
| 286 |
|
| 287 |
begin |
| 288 |
if Debug_Flag_FF then |
| 289 |
Diagnostics.Pretty_Emitter.Print_Diagnostic (Diagnostic); |
| 290 |
else |
| 291 |
Diagnostics.Brief_Emitter.Print_Diagnostic (Diagnostic); |
| 292 |
end if; |
| 293 |
end Print_Diagnostic; |
| 294 |
|
| 295 |
---------------------- |
| 296 |
-- Primary_Location -- |
| 297 |
---------------------- |
| 298 |
|
| 299 |
function Primary_Location |
| 300 |
(Diagnostic : Diagnostic_Type) return Labeled_Span_Type |
| 301 |
is |
| 302 |
begin |
| 303 |
return Get_Primary_Labeled_Span (Diagnostic.Locations); |
| 304 |
end Primary_Location; |
| 305 |
|
| 306 |
--------------------- |
| 307 |
-- Make_Diagnostic -- |
| 308 |
--------------------- |
| 309 |
|
| 310 |
function Make_Diagnostic |
| 311 |
(Msg : String; |
| 312 |
Location : Labeled_Span_Type; |
| 313 |
Id : Diagnostic_Id := No_Diagnostic_Id; |
| 314 |
Kind : Diagnostic_Kind := Diagnostics.Error; |
| 315 |
Switch : Switch_Id := No_Switch_Id; |
| 316 |
Spans : Labeled_Span_Array := No_Locations; |
| 317 |
Sub_Diags : Sub_Diagnostic_Array := No_Sub_Diags; |
| 318 |
Fixes : Fix_Array := No_Fixes) |
| 319 |
return Diagnostic_Type |
| 320 |
is |
| 321 |
D : Diagnostic_Type; |
| 322 |
begin |
| 323 |
D.Message := new String'(Msg); |
| 324 |
D.Id := Id; |
| 325 |
D.Kind := Kind; |
| 326 |
|
| 327 |
if Id /= No_Diagnostic_Id then |
| 328 |
pragma Assert (Switch = Diagnostic_Entries (Id).Switch, |
| 329 |
"Provided switch must be the same as in the registry"); |
| 330 |
end if; |
| 331 |
D.Switch := Switch; |
| 332 |
|
| 333 |
pragma Assert (Location.Is_Primary, "Main location must be primary"); |
| 334 |
Add_Location (D, Location); |
| 335 |
|
| 336 |
for I in Spans'Range loop |
| 337 |
Add_Location (D, Spans (I)); |
| 338 |
end loop; |
| 339 |
|
| 340 |
for I in Sub_Diags'Range loop |
| 341 |
Add_Sub_Diagnostic (D, Sub_Diags (I)); |
| 342 |
end loop; |
| 343 |
|
| 344 |
for I in Fixes'Range loop |
| 345 |
Add_Fix (D, Fixes (I)); |
| 346 |
end loop; |
| 347 |
|
| 348 |
return D; |
| 349 |
end Make_Diagnostic; |
| 350 |
|
| 351 |
----------------------- |
| 352 |
-- Record_Diagnostic -- |
| 353 |
----------------------- |
| 354 |
|
| 355 |
procedure Record_Diagnostic |
| 356 |
(Msg : String; |
| 357 |
Location : Labeled_Span_Type; |
| 358 |
Id : Diagnostic_Id := No_Diagnostic_Id; |
| 359 |
Kind : Diagnostic_Kind := Diagnostics.Error; |
| 360 |
Switch : Switch_Id := No_Switch_Id; |
| 361 |
Spans : Labeled_Span_Array := No_Locations; |
| 362 |
Sub_Diags : Sub_Diagnostic_Array := No_Sub_Diags; |
| 363 |
Fixes : Fix_Array := No_Fixes) |
| 364 |
is |
| 365 |
|
| 366 |
begin |
| 367 |
Record_Diagnostic |
| 368 |
(Make_Diagnostic |
| 369 |
(Msg => Msg, |
| 370 |
Location => Location, |
| 371 |
Id => Id, |
| 372 |
Kind => Kind, |
| 373 |
Switch => Switch, |
| 374 |
Spans => Spans, |
| 375 |
Sub_Diags => Sub_Diags, |
| 376 |
Fixes => Fixes)); |
| 377 |
end Record_Diagnostic; |
| 378 |
|
| 379 |
------------------ |
| 380 |
-- Labeled_Span -- |
| 381 |
------------------ |
| 382 |
|
| 383 |
function Labeled_Span (Span : Source_Span; |
| 384 |
Label : String := ""; |
| 385 |
Is_Primary : Boolean := True; |
| 386 |
Is_Region : Boolean := False) |
| 387 |
return Labeled_Span_Type |
| 388 |
is |
| 389 |
L : Labeled_Span_Type; |
| 390 |
begin |
| 391 |
L.Span := Span; |
| 392 |
if Label /= "" then |
| 393 |
L.Label := new String'(Label); |
| 394 |
end if; |
| 395 |
L.Is_Primary := Is_Primary; |
| 396 |
L.Is_Region := Is_Region; |
| 397 |
|
| 398 |
return L; |
| 399 |
end Labeled_Span; |
| 400 |
|
| 401 |
-------------------------- |
| 402 |
-- Primary_Labeled_Span -- |
| 403 |
-------------------------- |
| 404 |
|
| 405 |
function Primary_Labeled_Span (Span : Source_Span; |
| 406 |
Label : String := "") |
| 407 |
return Labeled_Span_Type |
| 408 |
is begin |
| 409 |
return Labeled_Span (Span => Span, Label => Label, Is_Primary => True); |
| 410 |
end Primary_Labeled_Span; |
| 411 |
|
| 412 |
-------------------------- |
| 413 |
-- Primary_Labeled_Span -- |
| 414 |
-------------------------- |
| 415 |
|
| 416 |
function Primary_Labeled_Span (N : Node_Or_Entity_Id; |
| 417 |
Label : String := "") |
| 418 |
return Labeled_Span_Type |
| 419 |
is |
| 420 |
begin |
| 421 |
return Primary_Labeled_Span (To_Full_Span (N), Label); |
| 422 |
end Primary_Labeled_Span; |
| 423 |
|
| 424 |
---------------------------- |
| 425 |
-- Secondary_Labeled_Span -- |
| 426 |
---------------------------- |
| 427 |
|
| 428 |
function Secondary_Labeled_Span |
| 429 |
(Span : Source_Span; |
| 430 |
Label : String := "") |
| 431 |
return Labeled_Span_Type |
| 432 |
is |
| 433 |
begin |
| 434 |
return Labeled_Span (Span => Span, Label => Label, Is_Primary => False); |
| 435 |
end Secondary_Labeled_Span; |
| 436 |
|
| 437 |
---------------------------- |
| 438 |
-- Secondary_Labeled_Span -- |
| 439 |
---------------------------- |
| 440 |
|
| 441 |
function Secondary_Labeled_Span (N : Node_Or_Entity_Id; |
| 442 |
Label : String := "") |
| 443 |
return Labeled_Span_Type |
| 444 |
is |
| 445 |
begin |
| 446 |
return Secondary_Labeled_Span (To_Full_Span (N), Label); |
| 447 |
end Secondary_Labeled_Span; |
| 448 |
|
| 449 |
-------------- |
| 450 |
-- Sub_Diag -- |
| 451 |
-------------- |
| 452 |
|
| 453 |
function Sub_Diag (Msg : String; |
| 454 |
Kind : Sub_Diagnostic_Kind := |
| 455 |
Diagnostics.Continuation; |
| 456 |
Locations : Labeled_Span_Array := No_Locations) |
| 457 |
return Sub_Diagnostic_Type |
| 458 |
is |
| 459 |
S : Sub_Diagnostic_Type; |
| 460 |
begin |
| 461 |
S.Message := new String'(Msg); |
| 462 |
S.Kind := Kind; |
| 463 |
|
| 464 |
for I in Locations'Range loop |
| 465 |
Add_Location (S, Locations (I)); |
| 466 |
end loop; |
| 467 |
|
| 468 |
return S; |
| 469 |
end Sub_Diag; |
| 470 |
|
| 471 |
------------------ |
| 472 |
-- Continuation -- |
| 473 |
------------------ |
| 474 |
|
| 475 |
function Continuation (Msg : String; |
| 476 |
Locations : Labeled_Span_Array := No_Locations) |
| 477 |
return Sub_Diagnostic_Type |
| 478 |
is |
| 479 |
begin |
| 480 |
return Sub_Diag (Msg, Diagnostics.Continuation, Locations); |
| 481 |
end Continuation; |
| 482 |
|
| 483 |
---------- |
| 484 |
-- Help -- |
| 485 |
---------- |
| 486 |
|
| 487 |
function Help (Msg : String; |
| 488 |
Locations : Labeled_Span_Array := No_Locations) |
| 489 |
return Sub_Diagnostic_Type |
| 490 |
is |
| 491 |
begin |
| 492 |
return Sub_Diag (Msg, Diagnostics.Help, Locations); |
| 493 |
end Help; |
| 494 |
|
| 495 |
---------------- |
| 496 |
-- Suggestion -- |
| 497 |
---------------- |
| 498 |
|
| 499 |
function Suggestion (Msg : String; |
| 500 |
Locations : Labeled_Span_Array := No_Locations) |
| 501 |
return Sub_Diagnostic_Type |
| 502 |
is |
| 503 |
begin |
| 504 |
return Sub_Diag (Msg, Diagnostics.Suggestion, Locations); |
| 505 |
end Suggestion; |
| 506 |
|
| 507 |
--------- |
| 508 |
-- Fix -- |
| 509 |
--------- |
| 510 |
|
| 511 |
function Fix |
| 512 |
(Description : String; |
| 513 |
Edits : Edit_Array; |
| 514 |
Applicability : Applicability_Type := Unspecified) return Fix_Type |
| 515 |
is |
| 516 |
F : Fix_Type; |
| 517 |
begin |
| 518 |
F.Description := new String'(Description); |
| 519 |
|
| 520 |
for I in Edits'Range loop |
| 521 |
Add_Edit (F, Edits (I)); |
| 522 |
end loop; |
| 523 |
|
| 524 |
F.Applicability := Applicability; |
| 525 |
|
| 526 |
return F; |
| 527 |
end Fix; |
| 528 |
|
| 529 |
---------- |
| 530 |
-- Edit -- |
| 531 |
---------- |
| 532 |
|
| 533 |
function Edit (Text : String; Span : Source_Span) return Edit_Type is |
| 534 |
|
| 535 |
begin |
| 536 |
return (Text => new String'(Text), Span => Span); |
| 537 |
end Edit; |
| 538 |
|
| 539 |
end Diagnostics; |