aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/diagnostics-converter.adb
blob: 45bb19c0a84093d5a74d8a3295e718ae7c08b64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                D I A G N O S T I C S . C O N V E R T E R                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1992-2024, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------
with Erroutc; use Erroutc;
with Debug;   use Debug;
with Diagnostics.Repository;        use Diagnostics.Repository;
with Diagnostics.SARIF_Emitter;     use Diagnostics.SARIF_Emitter;
with Diagnostics.Switch_Repository; use Diagnostics.Switch_Repository;
with Opt;    use Opt;
with Osint;  use Osint;
with Output; use Output;
use Diagnostics.Diagnostics_Lists;
with System.OS_Lib; use System.OS_Lib;

package body Diagnostics.Converter is

   function Convert (E_Id : Error_Msg_Id) return Diagnostic_Type;

   function Convert_Sub_Diagnostic
     (E_Id : Error_Msg_Id) return Sub_Diagnostic_Type;

   function Get_Warning_Kind (E_Msg : Error_Msg_Object) return Diagnostic_Kind
   is (if E_Msg.Info then Info_Warning
       elsif E_Msg.Warn_Chr = "* " then Restriction_Warning
       elsif E_Msg.Warn_Chr = "? " then Default_Warning
       elsif E_Msg.Warn_Chr = "  " then Tagless_Warning
       else Warning);
   --  NOTE: Some messages have both info and warning set to true. The old
   --  printer added the warning switch label but treated the message as
   --  an info message.

   -----------------------------------
   -- Convert_Errors_To_Diagnostics --
   -----------------------------------

   procedure Convert_Errors_To_Diagnostics
   is
      E : Error_Msg_Id;
   begin
      E := First_Error_Msg;
      while E /= No_Error_Msg loop

         if not Errors.Table (E).Deleted
           and then not Errors.Table (E).Msg_Cont
         then

            --  We do not need to update the count of converted error messages
            --  since they are accounted for in their creation.

            Record_Diagnostic (Convert (E), Update_Count => False);
         end if;

         E := Errors.Table (E).Next;
      end loop;

   end Convert_Errors_To_Diagnostics;

   ----------------------------
   -- Convert_Sub_Diagnostic --
   ----------------------------

   function Convert_Sub_Diagnostic
     (E_Id : Error_Msg_Id) return Sub_Diagnostic_Type
   is
      E_Msg : constant Error_Msg_Object := Errors.Table (E_Id);
      D : Sub_Diagnostic_Type;
   begin
      D.Message := E_Msg.Text;

      --  All converted sub-diagnostics are continuations. When emitted they
      --  shall be printed with the same kind token as the main diagnostic.
      D.Kind := Continuation;

      declare
         L : Labeled_Span_Type;
      begin
         if E_Msg.Insertion_Sloc /= No_Location then
            L.Span := To_Span (E_Msg.Insertion_Sloc);
         else
            L.Span := E_Msg.Sptr;
         end if;

         L.Is_Primary := True;
         Add_Location (D, L);
      end;

      if E_Msg.Optr.Ptr /= E_Msg.Sptr.Ptr then
         declare
            L : Labeled_Span_Type;
         begin
            L.Span       := E_Msg.Optr;
            L.Is_Primary := False;
            Add_Location (D, L);
         end;
      end if;

      return D;
   end Convert_Sub_Diagnostic;

   -------------
   -- Convert --
   -------------

   function Convert (E_Id : Error_Msg_Id) return Diagnostic_Type is

      E_Next_Id : Error_Msg_Id;

      E_Msg : constant Error_Msg_Object := Errors.Table (E_Id);
      D : Diagnostic_Type;
   begin
      D.Message := E_Msg.Text;

      if E_Msg.Warn then
         D.Kind   := Get_Warning_Kind (E_Msg);
         D.Switch := Get_Switch_Id (E_Msg);
      elsif E_Msg.Style then
         D.Kind   := Style;
         D.Switch := Get_Switch_Id (E_Msg);
      elsif E_Msg.Info then
         D.Kind := Info;
         D.Switch := Get_Switch_Id (E_Msg);
      else
         D.Kind := Error;
      end if;

      D.Warn_Err := E_Msg.Warn_Err;

      D.Serious := E_Msg.Serious;

      --  Convert the primary location

      declare
         L : Labeled_Span_Type;
      begin
         L.Span       := E_Msg.Sptr;
         L.Is_Primary := True;
         Add_Location (D, L);
      end;

      --  Convert the secondary location if it is different from the primary

      if E_Msg.Optr.Ptr /= E_Msg.Sptr.Ptr then
         declare
            L : Labeled_Span_Type;
         begin
            L.Span       := E_Msg.Optr;
            L.Is_Primary := False;
            Add_Location (D, L);
         end;
      end if;

      E_Next_Id := Errors.Table (E_Id).Next;
      while E_Next_Id /= No_Error_Msg
        and then Errors.Table (E_Next_Id).Msg_Cont
      loop
         Add_Sub_Diagnostic (D, Convert_Sub_Diagnostic (E_Next_Id));
         E_Next_Id := Errors.Table (E_Next_Id).Next;
      end loop;

      return D;
   end Convert;

   ----------------------
   -- Emit_Diagnostics --
   ----------------------

   procedure Emit_Diagnostics is
      D : Diagnostic_Type;

      It : Iterator := Iterate (All_Diagnostics);

      Sarif_File_Name : constant String :=
        Get_First_Main_File_Name & ".gnat.sarif";

      Switches_File_Name : constant String := "gnat_switches.json";

      Diagnostics_File_Name : constant String := "gnat_diagnostics.json";

      Dummy : Boolean;
   begin
      if Opt.SARIF_Output then
         Set_Standard_Error;

         Print_SARIF_Report (All_Diagnostics);

         Set_Standard_Output;
      elsif Opt.SARIF_File then
         Delete_File (Sarif_File_Name, Dummy);
         declare
            Output_FD : constant File_Descriptor :=
              Create_New_File
                (Sarif_File_Name,
                 Fmode => Text);

         begin
            Set_Output (Output_FD);

            Print_SARIF_Report (All_Diagnostics);

            Set_Standard_Output;

            Close (Output_FD);
         end;
      else
         Set_Standard_Error;

         while Has_Next (It) loop
            Next (It, D);

            Print_Diagnostic (D);
         end loop;

         Set_Standard_Output;
      end if;

      if Debug_Flag_Underscore_EE then

         --  Print the switch repository to a file

         Delete_File (Switches_File_Name, Dummy);
         declare
            Output_FD : constant File_Descriptor :=
              Create_New_File
                (Switches_File_Name,
                 Fmode => Text);

         begin
            Set_Output (Output_FD);

            Print_Switch_Repository;

            Set_Standard_Output;

            Close (Output_FD);
         end;

         --  Print the diagnostics repository to a file

         Delete_File (Diagnostics_File_Name, Dummy);
         declare
            Output_FD : constant File_Descriptor :=
              Create_New_File
                (Diagnostics_File_Name,
                 Fmode => Text);

         begin
            Set_Output (Output_FD);

            Print_Diagnostic_Repository;

            Set_Standard_Output;

            Close (Output_FD);
         end;
      end if;

      Destroy (All_Diagnostics);
   end Emit_Diagnostics;

end Diagnostics.Converter;