| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- D I A G N O S T I C S . U T I L 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 Diagnostics.Repository; use Diagnostics.Repository; |
| 27 |
with Diagnostics.Switch_Repository; use Diagnostics.Switch_Repository; |
| 28 |
with Errout; use Errout; |
| 29 |
with Erroutc; use Erroutc; |
| 30 |
with Namet; use Namet; |
| 31 |
with Opt; use Opt; |
| 32 |
with Sinput; use Sinput; |
| 33 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 34 |
with Warnsw; use Warnsw; |
| 35 |
|
| 36 |
package body Diagnostics.Utils is |
| 37 |
|
| 38 |
------------------ |
| 39 |
-- Get_Human_Id -- |
| 40 |
------------------ |
| 41 |
|
| 42 |
function Get_Human_Id (D : Diagnostic_Type) return String_Ptr is |
| 43 |
begin |
| 44 |
if D.Switch = No_Switch_Id then |
| 45 |
return Diagnostic_Entries (D.Id).Human_Id; |
| 46 |
else |
| 47 |
return Get_Switch (D).Human_Id; |
| 48 |
end if; |
| 49 |
end Get_Human_Id; |
| 50 |
|
| 51 |
------------------ |
| 52 |
-- To_File_Name -- |
| 53 |
------------------ |
| 54 |
|
| 55 |
function To_File_Name (Sptr : Source_Ptr) return String is |
| 56 |
Sfile : constant Source_File_Index := Get_Source_File_Index (Sptr); |
| 57 |
Ref_Name : constant File_Name_Type := |
| 58 |
(if Full_Path_Name_For_Brief_Errors then Full_Ref_Name (Sfile) |
| 59 |
else Reference_Name (Sfile)); |
| 60 |
|
| 61 |
begin |
| 62 |
return Get_Name_String (Ref_Name); |
| 63 |
end To_File_Name; |
| 64 |
|
| 65 |
-------------------- |
| 66 |
-- Line_To_String -- |
| 67 |
-------------------- |
| 68 |
|
| 69 |
function Line_To_String (Sptr : Source_Ptr) return String is |
| 70 |
Line : constant Logical_Line_Number := Get_Logical_Line_Number (Sptr); |
| 71 |
Img_Raw : constant String := Int'Image (Int (Line)); |
| 72 |
|
| 73 |
begin |
| 74 |
return Img_Raw (Img_Raw'First + 1 .. Img_Raw'Last); |
| 75 |
end Line_To_String; |
| 76 |
|
| 77 |
---------------------- |
| 78 |
-- Column_To_String -- |
| 79 |
---------------------- |
| 80 |
|
| 81 |
function Column_To_String (Sptr : Source_Ptr) return String is |
| 82 |
Col : constant Column_Number := Get_Column_Number (Sptr); |
| 83 |
Img_Raw : constant String := Int'Image (Int (Col)); |
| 84 |
|
| 85 |
begin |
| 86 |
return |
| 87 |
(if Col < 10 then "0" else "") |
| 88 |
& Img_Raw (Img_Raw'First + 1 .. Img_Raw'Last); |
| 89 |
end Column_To_String; |
| 90 |
|
| 91 |
--------------- |
| 92 |
-- To_String -- |
| 93 |
--------------- |
| 94 |
|
| 95 |
function To_String (Sptr : Source_Ptr) return String is |
| 96 |
begin |
| 97 |
return |
| 98 |
To_File_Name (Sptr) & ":" & Line_To_String (Sptr) & ":" |
| 99 |
& Column_To_String (Sptr); |
| 100 |
end To_String; |
| 101 |
|
| 102 |
-------------------- |
| 103 |
-- Sloc_To_String -- |
| 104 |
-------------------- |
| 105 |
|
| 106 |
function Sloc_To_String |
| 107 |
(N : Node_Or_Entity_Id; Ref : Source_Ptr) return String |
| 108 |
is |
| 109 |
|
| 110 |
begin |
| 111 |
return Sloc_To_String (Sloc (N), Ref); |
| 112 |
end Sloc_To_String; |
| 113 |
|
| 114 |
-------------------- |
| 115 |
-- Sloc_To_String -- |
| 116 |
-------------------- |
| 117 |
|
| 118 |
function Sloc_To_String (Sptr : Source_Ptr; Ref : Source_Ptr) return String |
| 119 |
is |
| 120 |
|
| 121 |
begin |
| 122 |
if Sptr = No_Location then |
| 123 |
return "at unknown location"; |
| 124 |
|
| 125 |
elsif Sptr = System_Location then |
| 126 |
return "in package System"; |
| 127 |
|
| 128 |
elsif Sptr = Standard_Location then |
| 129 |
return "in package Standard"; |
| 130 |
|
| 131 |
elsif Sptr = Standard_ASCII_Location then |
| 132 |
return "in package Standard.ASCII"; |
| 133 |
|
| 134 |
else |
| 135 |
if Full_File_Name (Get_Source_File_Index (Sptr)) |
| 136 |
/= Full_File_Name (Get_Source_File_Index (Ref)) |
| 137 |
then |
| 138 |
return "at " & To_String (Sptr); |
| 139 |
else |
| 140 |
return "at line " & Line_To_String (Sptr); |
| 141 |
end if; |
| 142 |
end if; |
| 143 |
end Sloc_To_String; |
| 144 |
|
| 145 |
------------------ |
| 146 |
-- To_Full_Span -- |
| 147 |
------------------ |
| 148 |
|
| 149 |
function To_Full_Span (N : Node_Id) return Source_Span |
| 150 |
is |
| 151 |
Fst, Lst : Node_Id; |
| 152 |
begin |
| 153 |
First_And_Last_Nodes (N, Fst, Lst); |
| 154 |
return To_Span (Ptr => Sloc (N), |
| 155 |
First => First_Sloc (Fst), |
| 156 |
Last => Last_Sloc (Lst)); |
| 157 |
end To_Full_Span; |
| 158 |
|
| 159 |
--------------- |
| 160 |
-- To_String -- |
| 161 |
--------------- |
| 162 |
|
| 163 |
function To_String (Id : Diagnostic_Id) return String is |
| 164 |
begin |
| 165 |
if Id = No_Diagnostic_Id then |
| 166 |
return "GNAT0000"; |
| 167 |
else |
| 168 |
return Id'Img; |
| 169 |
end if; |
| 170 |
end To_String; |
| 171 |
|
| 172 |
------------- |
| 173 |
-- To_Name -- |
| 174 |
------------- |
| 175 |
|
| 176 |
function To_Name (E : Entity_Id) return String is |
| 177 |
begin |
| 178 |
-- The name of the node operator "&" has many special cases. Reuse the |
| 179 |
-- node to name conversion implementation from the errout package for |
| 180 |
-- now. |
| 181 |
|
| 182 |
Error_Msg_Node_1 := E; |
| 183 |
Set_Msg_Text ("&", Sloc (E)); |
| 184 |
|
| 185 |
return Msg_Buffer (1 .. Msglen); |
| 186 |
end To_Name; |
| 187 |
|
| 188 |
------------------ |
| 189 |
-- To_Type_Name -- |
| 190 |
------------------ |
| 191 |
|
| 192 |
function To_Type_Name (E : Entity_Id) return String is |
| 193 |
begin |
| 194 |
Error_Msg_Node_1 := E; |
| 195 |
Set_Msg_Text ("}", Sloc (E)); |
| 196 |
|
| 197 |
return Msg_Buffer (1 .. Msglen); |
| 198 |
end To_Type_Name; |
| 199 |
|
| 200 |
-------------------- |
| 201 |
-- Kind_To_String -- |
| 202 |
-------------------- |
| 203 |
|
| 204 |
function Kind_To_String |
| 205 |
(D : Sub_Diagnostic_Type; |
| 206 |
Parent : Diagnostic_Type) return String |
| 207 |
is |
| 208 |
(case D.Kind is |
| 209 |
when Continuation => Kind_To_String (Parent), |
| 210 |
when Help => "help", |
| 211 |
when Note => "note", |
| 212 |
when Suggestion => "suggestion"); |
| 213 |
|
| 214 |
-------------------- |
| 215 |
-- Kind_To_String -- |
| 216 |
-------------------- |
| 217 |
|
| 218 |
function Kind_To_String (D : Diagnostic_Type) return String is |
| 219 |
(if D.Warn_Err then "error" |
| 220 |
else |
| 221 |
(case D.Kind is |
| 222 |
when Diagnostics.Error | Non_Serious_Error => "error", |
| 223 |
when Warning | Restriction_Warning | Default_Warning | |
| 224 |
Tagless_Warning => "warning", |
| 225 |
when Style => "style", |
| 226 |
when Info => "info")); |
| 227 |
|
| 228 |
------------------------------ |
| 229 |
-- Get_Primary_Labeled_Span -- |
| 230 |
------------------------------ |
| 231 |
|
| 232 |
function Get_Primary_Labeled_Span (Spans : Labeled_Span_List) |
| 233 |
return Labeled_Span_Type |
| 234 |
is |
| 235 |
use Labeled_Span_Lists; |
| 236 |
|
| 237 |
S : Labeled_Span_Type; |
| 238 |
It : Iterator; |
| 239 |
begin |
| 240 |
if Present (Spans) then |
| 241 |
It := Iterate (Spans); |
| 242 |
while Has_Next (It) loop |
| 243 |
Next (It, S); |
| 244 |
if S.Is_Primary then |
| 245 |
return S; |
| 246 |
end if; |
| 247 |
end loop; |
| 248 |
end if; |
| 249 |
|
| 250 |
return No_Labeled_Span; |
| 251 |
end Get_Primary_Labeled_Span; |
| 252 |
|
| 253 |
-------------------- |
| 254 |
-- Get_Doc_Switch -- |
| 255 |
-------------------- |
| 256 |
|
| 257 |
function Get_Doc_Switch (Diag : Diagnostic_Type) return String is |
| 258 |
begin |
| 259 |
if Warning_Doc_Switch |
| 260 |
and then Diag.Kind in Default_Warning |
| 261 |
| Info |
| 262 |
| Restriction_Warning |
| 263 |
| Style |
| 264 |
| Warning |
| 265 |
then |
| 266 |
if Diag.Switch = No_Switch_Id then |
| 267 |
if Diag.Kind = Restriction_Warning then |
| 268 |
return "[restriction warning]"; |
| 269 |
|
| 270 |
-- Info messages can have a switch tag but they should not have |
| 271 |
-- a default switch tag. |
| 272 |
|
| 273 |
elsif Diag.Kind /= Info then |
| 274 |
|
| 275 |
-- For Default_Warning |
| 276 |
|
| 277 |
return "[enabled by default]"; |
| 278 |
end if; |
| 279 |
else |
| 280 |
declare |
| 281 |
S : constant Switch_Type := Get_Switch (Diag); |
| 282 |
begin |
| 283 |
return "[-" & S.Short_Name.all & "]"; |
| 284 |
end; |
| 285 |
end if; |
| 286 |
end if; |
| 287 |
|
| 288 |
return ""; |
| 289 |
end Get_Doc_Switch; |
| 290 |
|
| 291 |
-------------------- |
| 292 |
-- Appears_Before -- |
| 293 |
-------------------- |
| 294 |
|
| 295 |
function Appears_Before (D1, D2 : Diagnostic_Type) return Boolean is |
| 296 |
|
| 297 |
begin |
| 298 |
return Appears_Before (Primary_Location (D1).Span.Ptr, |
| 299 |
Primary_Location (D2).Span.Ptr); |
| 300 |
end Appears_Before; |
| 301 |
|
| 302 |
-------------------- |
| 303 |
-- Appears_Before -- |
| 304 |
-------------------- |
| 305 |
|
| 306 |
function Appears_Before (P1, P2 : Source_Ptr) return Boolean is |
| 307 |
|
| 308 |
begin |
| 309 |
if Get_Source_File_Index (P1) = Get_Source_File_Index (P2) then |
| 310 |
if Get_Logical_Line_Number (P1) = Get_Logical_Line_Number (P2) then |
| 311 |
return Get_Column_Number (P1) < Get_Column_Number (P2); |
| 312 |
else |
| 313 |
return Get_Logical_Line_Number (P1) < Get_Logical_Line_Number (P2); |
| 314 |
end if; |
| 315 |
else |
| 316 |
return Get_Source_File_Index (P1) < Get_Source_File_Index (P2); |
| 317 |
end if; |
| 318 |
end Appears_Before; |
| 319 |
|
| 320 |
------------------------------ |
| 321 |
-- Insert_Based_On_Location -- |
| 322 |
------------------------------ |
| 323 |
|
| 324 |
procedure Insert_Based_On_Location |
| 325 |
(List : Diagnostic_List; |
| 326 |
Diagnostic : Diagnostic_Type) |
| 327 |
is |
| 328 |
use Diagnostics_Lists; |
| 329 |
|
| 330 |
It : Iterator := Iterate (List); |
| 331 |
D : Diagnostic_Type; |
| 332 |
begin |
| 333 |
-- This is the common scenario where the error is reported at the |
| 334 |
-- natural order the tree is processed. This saves a lot of time when |
| 335 |
-- looking for the correct position in the list when there are a lot of |
| 336 |
-- diagnostics. |
| 337 |
|
| 338 |
if Present (List) and then |
| 339 |
not Is_Empty (List) and then |
| 340 |
Appears_Before (Last (List), Diagnostic) |
| 341 |
then |
| 342 |
Append (List, Diagnostic); |
| 343 |
else |
| 344 |
while Has_Next (It) loop |
| 345 |
Next (It, D); |
| 346 |
|
| 347 |
if Appears_Before (Diagnostic, D) then |
| 348 |
Insert_Before (List, D, Diagnostic); |
| 349 |
return; |
| 350 |
end if; |
| 351 |
end loop; |
| 352 |
|
| 353 |
Append (List, Diagnostic); |
| 354 |
end if; |
| 355 |
end Insert_Based_On_Location; |
| 356 |
|
| 357 |
end Diagnostics.Utils; |