Debian Codegraph @

gcc/ada/diagnostics-converter.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- D I A G N O S T I C S . C O N V E R T E R --
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 with Erroutc; use Erroutc;
26 with Debug; use Debug;
27 with Diagnostics.Repository; use Diagnostics.Repository;
28 with Diagnostics.SARIF_Emitter; use Diagnostics.SARIF_Emitter;
29 with Diagnostics.Switch_Repository; use Diagnostics.Switch_Repository;
30 with Opt; use Opt;
31 with Osint; use Osint;
32 with Output; use Output;
33 use Diagnostics.Diagnostics_Lists;
34 with System.OS_Lib; use System.OS_Lib;
35
36 package body Diagnostics.Converter is
37
38 function Convert (E_Id : Error_Msg_Id) return Diagnostic_Type;
39
40 function Convert_Sub_Diagnostic
41 (E_Id : Error_Msg_Id) return Sub_Diagnostic_Type;
42
43 function Get_Warning_Kind (E_Msg : Error_Msg_Object) return Diagnostic_Kind
44 is (if E_Msg.Warn_Chr = "* " then Restriction_Warning
45 elsif E_Msg.Warn_Chr = "? " then Default_Warning
46 elsif E_Msg.Warn_Chr = " " then Tagless_Warning
47 else Warning);
48 -- NOTE: Some messages have both info and warning set to true. The old
49 -- printer added the warning switch label but treated the message as
50 -- an info message.
51
52 function Get_Diagnostics_Kind (E_Msg : Error_Msg_Object)
53 return Diagnostic_Kind
54 is (if E_Msg.Kind = Erroutc.Warning then Get_Warning_Kind (E_Msg)
55 elsif E_Msg.Kind = Erroutc.Style then Style
56 elsif E_Msg.Kind = Erroutc.Info then Info
57 elsif E_Msg.Kind = Erroutc.Non_Serious_Error then Non_Serious_Error
58 else Error);
59
60 -----------------------------------
61 -- Convert_Errors_To_Diagnostics --
62 -----------------------------------
63
64 procedure Convert_Errors_To_Diagnostics
65 is
66 E : Error_Msg_Id;
67 begin
68 E := First_Error_Msg;
69 while E /= No_Error_Msg loop
70
71 if not Errors.Table (E).Deleted
72 and then not Errors.Table (E).Msg_Cont
73 then
74
75 -- We do not need to update the count of converted error messages
76 -- since they are accounted for in their creation.
77
78 Record_Diagnostic (Convert (E), Update_Count => False);
79 end if;
80
81 E := Errors.Table (E).Next;
82 end loop;
83
84 end Convert_Errors_To_Diagnostics;
85
86 ----------------------------
87 -- Convert_Sub_Diagnostic --
88 ----------------------------
89
90 function Convert_Sub_Diagnostic
91 (E_Id : Error_Msg_Id) return Sub_Diagnostic_Type
92 is
93 E_Msg : constant Error_Msg_Object := Errors.Table (E_Id);
94 D : Sub_Diagnostic_Type;
95 begin
96 D.Message := E_Msg.Text;
97
98 -- All converted sub-diagnostics are continuations. When emitted they
99 -- shall be printed with the same kind token as the main diagnostic.
100 D.Kind := Continuation;
101
102 Add_Location (D,
103 Primary_Labeled_Span
104 (if E_Msg.Insertion_Sloc /= No_Location
105 then To_Span (E_Msg.Insertion_Sloc)
106 else E_Msg.Sptr));
107
108 if E_Msg.Optr.Ptr /= E_Msg.Sptr.Ptr then
109 Add_Location (D, Secondary_Labeled_Span (E_Msg.Optr));
110 end if;
111
112 return D;
113 end Convert_Sub_Diagnostic;
114
115 -------------
116 -- Convert --
117 -------------
118
119 function Convert (E_Id : Error_Msg_Id) return Diagnostic_Type is
120
121 E_Next_Id : Error_Msg_Id;
122
123 E_Msg : constant Error_Msg_Object := Errors.Table (E_Id);
124 D : Diagnostic_Type;
125 begin
126 D.Message := E_Msg.Text;
127
128 D.Kind := Get_Diagnostics_Kind (E_Msg);
129
130 if E_Msg.Kind in Erroutc.Warning | Erroutc.Style | Erroutc.Info then
131 D.Switch := Get_Switch_Id (E_Msg);
132 end if;
133
134 D.Warn_Err := E_Msg.Warn_Err;
135
136 -- Convert the primary location
137
138 Add_Location (D, Primary_Labeled_Span (E_Msg.Sptr));
139
140 -- Convert the secondary location if it is different from the primary
141
142 if E_Msg.Optr.Ptr /= E_Msg.Sptr.Ptr then
143 Add_Location (D, Secondary_Labeled_Span (E_Msg.Optr));
144 end if;
145
146 E_Next_Id := Errors.Table (E_Id).Next;
147 while E_Next_Id /= No_Error_Msg
148 and then Errors.Table (E_Next_Id).Msg_Cont
149 loop
150 Add_Sub_Diagnostic (D, Convert_Sub_Diagnostic (E_Next_Id));
151 E_Next_Id := Errors.Table (E_Next_Id).Next;
152 end loop;
153
154 return D;
155 end Convert;
156
157 ----------------------
158 -- Emit_Diagnostics --
159 ----------------------
160
161 procedure Emit_Diagnostics is
162 D : Diagnostic_Type;
163
164 It : Iterator := Iterate (All_Diagnostics);
165
166 Sarif_File_Name : constant String :=
167 Get_First_Main_File_Name & ".gnat.sarif";
168
169 Switches_File_Name : constant String := "gnat_switches.json";
170
171 Diagnostics_File_Name : constant String := "gnat_diagnostics.json";
172
173 Dummy : Boolean;
174 begin
175 if Opt.SARIF_Output then
176 Set_Standard_Error;
177
178 Print_SARIF_Report (All_Diagnostics);
179
180 Set_Standard_Output;
181 elsif Opt.SARIF_File then
182 Delete_File (Sarif_File_Name, Dummy);
183 declare
184 Output_FD : constant File_Descriptor :=
185 Create_New_File
186 (Sarif_File_Name,
187 Fmode => Text);
188
189 begin
190 Set_Output (Output_FD);
191
192 Print_SARIF_Report (All_Diagnostics);
193
194 Set_Standard_Output;
195
196 Close (Output_FD);
197 end;
198 else
199 Set_Standard_Error;
200
201 while Has_Next (It) loop
202 Next (It, D);
203
204 Print_Diagnostic (D);
205 end loop;
206
207 Set_Standard_Output;
208 end if;
209
210 if Debug_Flag_Underscore_EE then
211
212 -- Print the switch repository to a file
213
214 Delete_File (Switches_File_Name, Dummy);
215 declare
216 Output_FD : constant File_Descriptor :=
217 Create_New_File
218 (Switches_File_Name,
219 Fmode => Text);
220
221 begin
222 Set_Output (Output_FD);
223
224 Print_Switch_Repository;
225
226 Set_Standard_Output;
227
228 Close (Output_FD);
229 end;
230
231 -- Print the diagnostics repository to a file
232
233 Delete_File (Diagnostics_File_Name, Dummy);
234 declare
235 Output_FD : constant File_Descriptor :=
236 Create_New_File
237 (Diagnostics_File_Name,
238 Fmode => Text);
239
240 begin
241 Set_Output (Output_FD);
242
243 Print_Diagnostic_Repository;
244
245 Set_Standard_Output;
246
247 Close (Output_FD);
248 end;
249 end if;
250
251 Destroy (All_Diagnostics);
252 end Emit_Diagnostics;
253
254 end Diagnostics.Converter;