-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclang-extensions.adb
327 lines (279 loc) · 10.9 KB
/
clang-extensions.adb
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
------------------------------------------------------------------------------
-- --
-- GNATcoverage --
-- --
-- Copyright (C) 2021-2024, AdaCore --
-- --
-- GNATcoverage is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be useful --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY 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 this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
-- Wrap the clang extension functions into thicker bindings, similarly to what
-- is done for the libclang Ada bindings themselves (to avoid having to use
-- the String_T type that must be memory managed by the user for instance).
with Clang.CX_File; use Clang.CX_File;
with Clang.CX_String; use Clang.CX_String;
with Files_Table; use Files_Table;
package body Clang.Extensions is
function To_Sloc
(File : File_T; Line, Column : unsigned) return Source_Location
is
((Source_File => Get_Index_From_Generic_Name
(Name => Get_File_Name (File),
Kind => Source_File),
L => To_Sloc (Line, Column)));
-- Convert a Clang source location to gnatcov's own format
-----------------------
-- Get_Decl_Name_Str --
-----------------------
function Get_Decl_Name_Str (C : Cursor_T) return String is
function Get_Decl_Name_Str_C (C : Cursor_T) return String_T
with
Import, Convention => C,
External_Name => "clang_getDeclName";
DeclName_Str_C : constant String_T := Get_Decl_Name_Str_C (C);
DeclName : constant String := Get_C_String (DeclName_Str_C);
begin
Dispose_String (DeclName_Str_C);
return DeclName;
end Get_Decl_Name_Str;
-------------------------
-- Get_Callee_Name_Str --
-------------------------
function Get_Callee_Name_Str (C : Cursor_T) return String is
function Get_Callee_Name_Str_C (C : Cursor_T) return String_T
with
Import, Convention => C,
External_Name => "clang_getCalleeName";
CalleeName_Str_C : constant String_T := Get_Callee_Name_Str_C (C);
CalleeName : constant String := Get_C_String (CalleeName_Str_C);
begin
Dispose_String (CalleeName_Str_C);
return CalleeName;
end Get_Callee_Name_Str;
---------------------------------
-- Is_Instrumentable_Call_Expr --
---------------------------------
function Is_Instrumentable_Call_Expr (C : Cursor_T) return Boolean is
function Is_Instrumentable_Call_Expr_C (C : Cursor_T) return unsigned
with
Import, Convention => C,
External_Name => "clang_isInstrumentableCallExpr";
begin
return Is_Instrumentable_Call_Expr_C (C) /= 0;
end Is_Instrumentable_Call_Expr;
--------------------
-- Get_Opcode_Str --
--------------------
function Get_Opcode_Str (C : Cursor_T) return String is
function Get_Opcode_Str_C (C : Cursor_T) return String_T
with
Import, Convention => C,
External_Name => "clang_getOpcodeStr";
Opcode_Str_C : constant String_T := Get_Opcode_Str_C (C);
Opcode_Str : constant String := Get_C_String (Opcode_Str_C);
begin
Dispose_String (Opcode_Str_C);
return Opcode_Str;
end Get_Opcode_Str;
--------------------------------------
-- Is_This_Declaration_A_Definition --
--------------------------------------
function Is_This_Declaration_A_Definition (C : Cursor_T) return Boolean
is
function Is_This_Declaration_A_Definition_C
(C : Cursor_T) return unsigned
with
Import, Convention => C,
External_Name => "clang_isThisDeclarationADefinition";
begin
return Is_This_Declaration_A_Definition_C (C) /= 0;
end Is_This_Declaration_A_Definition;
------------------
-- Is_Constexpr --
------------------
function Is_Constexpr (C : Cursor_T) return Boolean
is
function Is_Constexpr_C (C : Cursor_T) return unsigned
with
Import, Convention => C,
External_Name => "clang_isConstexpr";
begin
return Is_Constexpr_C (C) /= 0;
end Is_Constexpr;
-----------------------------------
-- CX_Rewriter_Insert_Text_After --
-----------------------------------
procedure CX_Rewriter_Insert_Text_After
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
is
procedure CX_Rewriter_Insert_Text_After_C
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
with
Import, Convention => C,
External_Name => "clang_CXRewriter_insertTextAfter";
begin
CX_Rewriter_Insert_Text_After_C (Rew, Loc, Insert & ASCII.NUL);
end CX_Rewriter_Insert_Text_After;
-----------------------------------------
-- CX_Rewriter_Insert_Text_After_Token --
-----------------------------------------
procedure CX_Rewriter_Insert_Text_After_Token
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
is
procedure CX_Rewriter_Insert_Text_After_Token_C
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
with
Import, Convention => C,
External_Name => "clang_CXRewriter_insertTextAfterToken";
begin
CX_Rewriter_Insert_Text_After_Token_C (Rew, Loc, Insert & ASCII.NUL);
end CX_Rewriter_Insert_Text_After_Token;
------------------------------------------
-- CX_Rewriter_Insert_Text_Before_Token --
------------------------------------------
procedure CX_Rewriter_Insert_Text_Before_Token
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
is
procedure CX_Rewriter_Insert_Text_Before_Token_C
(Rew : Rewriter_T;
Loc : Source_Location_T;
Insert : String)
with
Import, Convention => C,
External_Name => "clang_CXRewriter_insertTextBeforeToken";
begin
CX_Rewriter_Insert_Text_Before_Token_C (Rew, Loc, Insert & ASCII.NUL);
end CX_Rewriter_Insert_Text_Before_Token;
------------------------------------
-- CX_Rewriter_Get_Rewritten_Text --
------------------------------------
function CX_Rewriter_Get_Rewritten_Text
(Rew : Rewriter_T;
R : Source_Range_T) return String
is
function CX_Rewriter_Get_Rewritten_Text
(Rew : Rewriter_T;
R : Source_Range_T) return String_T
with
Import, Convention => C,
External_Name => "clang_CXRewriter_getRewrittenText";
Rewritten_Text_C : constant String_T :=
CX_Rewriter_Get_Rewritten_Text (Rew, R);
Rewritten_Text : constant String := Get_C_String (Rewritten_Text_C);
begin
Dispose_String (Rewritten_Text_C);
return Rewritten_Text;
end CX_Rewriter_Get_Rewritten_Text;
-----------------------
-- Spelling_Location --
-----------------------
function Spelling_Location (Loc : Source_Location_T) return Source_Location
is
File : File_T;
Line, Column, Offset : aliased unsigned;
begin
Get_Spelling_Location
(Loc, File'Address, Line'Access, Column'Access, Offset'Access);
return To_Sloc (File, Line, Column);
end Spelling_Location;
-------------------
-- File_Location --
-------------------
function File_Location
(Loc : Source_Location_T) return Local_Source_Location
is
File : File_T;
Line, Column, Offset : aliased unsigned;
begin
Get_File_Location
(Loc, File'Address, Line'Access, Column'Access, Offset'Access);
return To_Sloc (Line, Column);
end File_Location;
-----------------------
-- Presumed_Location --
-----------------------
function Presumed_Location
(Loc : Source_Location_T) return Local_Source_Location
is
Filename : aliased String_T;
Line, Column : aliased unsigned;
begin
Get_Presumed_Location (Loc, Filename'Access, Line'Access, Column'Access);
Dispose_String (Filename);
return To_Sloc (Line, Column);
end Presumed_Location;
-----------------------
-- Is_Macro_Location --
-----------------------
function Is_Macro_Location (Loc : Source_Location_T) return Boolean
is
function Is_Macro_Location_C (Loc : Source_Location_T) return unsigned
with
Import, Convention => C,
External_Name => "clang_isMacroLocation";
begin
return Is_Macro_Location_C (Loc) /= 0;
end Is_Macro_Location;
----------------------------
-- Is_Macro_Arg_Expansion --
----------------------------
function Is_Macro_Arg_Expansion
(Loc : Source_Location_T;
Start_Loc : out Source_Location_T;
TU : Translation_Unit_T) return Boolean
is
function Is_Macro_Arg_Expansion
(Loc : Source_Location_T;
Start_Loc : access Source_Location_T;
TU : Translation_Unit_T) return unsigned
with
Import, Convention => C,
External_Name => "clang_isMacroArgExpansion";
C_Start_Loc : aliased Source_Location_T;
begin
return Result : constant Boolean :=
Is_Macro_Arg_Expansion (Loc, C_Start_Loc'Access, TU) /= 0
do
Start_Loc := C_Start_Loc;
end return;
end Is_Macro_Arg_Expansion;
----------------------------------------------
-- Get_Immediate_Macro_Name_For_Diagnostics --
----------------------------------------------
function Get_Immediate_Macro_Name_For_Diagnostics
(Loc : Source_Location_T;
TU : Translation_Unit_T) return String
is
function Get_Immediate_Macro_Name_For_Diagnostics_C
(Loc : Source_Location_T;
TU : Translation_Unit_T) return String_T
with
Import, Convention => C,
External_Name => "clang_getImmediateMacroNameForDiagnostics";
Macro_Name_C : constant String_T :=
Get_Immediate_Macro_Name_For_Diagnostics_C (Loc, TU);
Macro_Name : constant String :=
Get_C_String (Macro_Name_C);
begin
Dispose_String (Macro_Name_C);
return Macro_Name;
end Get_Immediate_Macro_Name_For_Diagnostics;
end Clang.Extensions;