Skip to content

Commit 89329ff

Browse files
committed
Instrument.C_Utils: use anonymous functions as callbacks
Expose anonymous functions with Ada convention as callbacks for the Visit and Visit_Children procedures. This also avoids the use of 'Unrestricted_Access. TN: V916-015 Change-Id: If8abd7d5cc8e23b74d230d5992e6fd525fccc8ef
1 parent 39b3280 commit 89329ff

File tree

3 files changed

+64
-86
lines changed

3 files changed

+64
-86
lines changed

tools/gnatcov/instrument-c.adb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,7 @@ package body Instrument.C is
13271327
-- output for the complex decision. It process the suboperands of the
13281328
-- decision looking for nested decisions.
13291329

1330-
function Process_Node (N : Cursor_T) return Child_Visit_Result_T
1331-
with Convention => C;
1330+
function Process_Node (N : Cursor_T) return Child_Visit_Result_T;
13321331
-- Processes one node in the traversal, looking for logical operators,
13331332
-- and if one is found, outputs the appropriate table entries.
13341333

@@ -1581,7 +1580,7 @@ package body Instrument.C is
15811580
return;
15821581
end if;
15831582
Hash_Entries.Init;
1584-
Visit (N, Process_Node'Unrestricted_Access);
1583+
Visit (N, Process_Node'Access);
15851584
Hash_Entries.Free;
15861585
end Process_Decisions;
15871586

@@ -1609,8 +1608,7 @@ package body Instrument.C is
16091608

16101609
function Has_Decision (T : Cursor_T) return Boolean is
16111610

1612-
function Visitor (N : Cursor_T) return Child_Visit_Result_T
1613-
with Convention => C;
1611+
function Visitor (N : Cursor_T) return Child_Visit_Result_T;
16141612
-- If N's kind indicates the presence of a decision, return
16151613
-- Child_Visit_Break, otherwise return Child_Visit_Recurse.
16161614
--
@@ -1645,7 +1643,7 @@ package body Instrument.C is
16451643
-- Start of processing for Has_Decision
16461644

16471645
begin
1648-
Visit (T, Visitor'Unrestricted_Access);
1646+
Visit (T, Visitor'Access);
16491647
return Has_Decision;
16501648
end Has_Decision;
16511649

@@ -3318,8 +3316,7 @@ package body Instrument.C is
33183316
when Main_End | Ravenscar_Task_Termination =>
33193317
declare
33203318
function Process
3321-
(Cursor : Cursor_T) return Child_Visit_Result_T
3322-
with Convention => C;
3319+
(Cursor : Cursor_T) return Child_Visit_Result_T;
33233320
-- Callback for Visit_Children. Insert calls to dump buffers
33243321
-- before the function return.
33253322

@@ -3398,9 +3395,8 @@ package body Instrument.C is
33983395
end Process;
33993396

34003397
begin
3401-
Visit_Children
3402-
(Parent => Get_Main (Rew.TU),
3403-
Visitor => Process'Unrestricted_Access);
3398+
Visit_Children (Parent => Get_Main (Rew.TU),
3399+
Visitor => Process'Access);
34043400
end;
34053401

34063402
when At_Exit =>
@@ -3578,8 +3574,7 @@ package body Instrument.C is
35783574
is
35793575
Location : Source_Location_T := Get_Null_Location;
35803576

3581-
function Visit_Decl
3582-
(Cursor : Cursor_T) return Child_Visit_Result_T with Convention => C;
3577+
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T;
35833578
-- Callback for Visit_Children
35843579

35853580
----------------
@@ -3608,7 +3603,7 @@ package body Instrument.C is
36083603

36093604
begin
36103605
Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
3611-
Visitor => Visit_Decl'Unrestricted_Access);
3606+
Visitor => Visit_Decl'Access);
36123607
return Location;
36133608
end Find_First_Insert_Location;
36143609

tools/gnatcov/instrument-c_utils.adb

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ package body Instrument.C_Utils is
2929

3030
use Cursor_Vectors;
3131

32+
function Visitor_Wrapper
33+
(Node : Cursor_T;
34+
Parent : Cursor_T;
35+
Client_Data : Client_Data_T) return Child_Visit_Result_T
36+
with Convention => C;
37+
-- Helper for Visit_Children and Visit procedures.
38+
--
39+
-- Interpret Client_Data as the Visitor anonymous function for these
40+
-- procedures and call it on Node.
41+
--
42+
-- Note: this has convention C in order to be a callback for the
43+
-- Clang.Index.Visit_Children and Clang.Index.Visit functions.
44+
3245
----------
3346
-- Sloc --
3447
----------
@@ -78,42 +91,34 @@ package body Instrument.C_Utils is
7891
return Cursor_Is_Null (N);
7992
end Is_Null;
8093

94+
---------------------
95+
-- Visitor_Wrapper --
96+
---------------------
97+
98+
function Visitor_Wrapper
99+
(Node : Cursor_T;
100+
Parent : Cursor_T;
101+
Client_Data : Client_Data_T) return Child_Visit_Result_T
102+
is
103+
pragma Unreferenced (Parent);
104+
Callback : access function
105+
(Node : Cursor_T) return Child_Visit_Result_T
106+
with Import, Address => System.Address (Client_Data);
107+
begin
108+
return Callback.all (Node);
109+
end Visitor_Wrapper;
110+
81111
--------------------
82112
-- Visit_Children --
83113
--------------------
84114

85115
procedure Visit_Children
86-
(Parent : Cursor_T; Visitor : Cursor_Visitor_Function)
116+
(Parent : Cursor_T;
117+
Visitor : not null access function
118+
(Node : Cursor_T) return Child_Visit_Result_T)
87119
is
88-
function Visitor_Wrapper
89-
(Node : Cursor_T;
90-
Parent : Cursor_T;
91-
Client_Data : Client_Data_T) return Child_Visit_Result_T
92-
with Convention => C;
93-
-- Callback for Clang.Index.Visit_Children. Just a wrapper around
94-
-- Visitor.
95-
96-
---------------------
97-
-- Visitor_Wrapper --
98-
---------------------
99-
100-
function Visitor_Wrapper
101-
(Node : Cursor_T;
102-
Parent : Cursor_T;
103-
Client_Data : Client_Data_T) return Child_Visit_Result_T
104-
is
105-
pragma Unreferenced (Parent, Client_Data);
106-
begin
107-
return Visitor (Node);
108-
end Visitor_Wrapper;
109-
110120
Dummy : constant unsigned := Visit_Children
111-
(Parent,
112-
Visitor_Wrapper'Unrestricted_Access,
113-
Client_Data_T (System.Null_Address));
114-
115-
-- Start of processing for Visit_Children
116-
121+
(Parent, Visitor_Wrapper'Access, Client_Data_T (Visitor'Address));
117122
begin
118123
null;
119124
end Visit_Children;
@@ -122,36 +127,13 @@ package body Instrument.C_Utils is
122127
-- Visit --
123128
-----------
124129

125-
procedure Visit (Parent : Cursor_T; Visitor : Cursor_Visitor_Function) is
126-
function Visitor_Wrapper
127-
(Node : Cursor_T;
128-
Parent : Cursor_T;
129-
Client_Data : Client_Data_T) return Child_Visit_Result_T
130-
with Convention => C;
131-
-- Callback for Clang.Index.Visit_Children. Just a wrapper around
132-
-- Visitor.
133-
134-
---------------------
135-
-- Visitor_Wrapper --
136-
---------------------
137-
138-
function Visitor_Wrapper
139-
(Node : Cursor_T;
140-
Parent : Cursor_T;
141-
Client_Data : Client_Data_T) return Child_Visit_Result_T
142-
is
143-
pragma Unreferenced (Parent, Client_Data);
144-
begin
145-
return Visitor (Node);
146-
end Visitor_Wrapper;
147-
130+
procedure Visit
131+
(Parent : Cursor_T;
132+
Visitor : not null access function
133+
(Node : Cursor_T) return Child_Visit_Result_T)
134+
is
148135
Dummy : constant unsigned := Visit
149-
(Parent,
150-
Visitor_Wrapper'Unrestricted_Access,
151-
Client_Data_T (System.Null_Address));
152-
153-
-- Start of processing for Visit
154-
136+
(Parent, Visitor_Wrapper'Access, Client_Data_T (Visitor'Address));
155137
begin
156138
null;
157139
end Visit;
@@ -163,8 +145,7 @@ package body Instrument.C_Utils is
163145
function Get_Children (N : Cursor_T) return Cursor_Vectors.Vector is
164146
Res : Vector;
165147

166-
function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T
167-
with Convention => C;
148+
function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T;
168149
-- Callback for Visit_Children. Append Cursor to Res and continue the
169150
-- traversal.
170151

@@ -181,7 +162,7 @@ package body Instrument.C_Utils is
181162
-- Start of processing for Get_Children
182163

183164
begin
184-
Visit_Children (N, Get_Children.Append_Child'Unrestricted_Access);
165+
Visit_Children (N, Get_Children.Append_Child'Access);
185166
return Res;
186167
end Get_Children;
187168

@@ -192,8 +173,7 @@ package body Instrument.C_Utils is
192173
function Get_Lambda_Exprs (N : Cursor_T) return Cursor_Vectors.Vector is
193174
Res : Vector;
194175

195-
function Process (Cursor : Cursor_T) return Child_Visit_Result_T
196-
with Convention => C;
176+
function Process (Cursor : Cursor_T) return Child_Visit_Result_T;
197177
-- Helper for Visit_Children. Add every lambda expr under Cursor to Res,
198178
-- _but_ the lambda expressions nested in other lambda expressions.
199179

@@ -213,7 +193,7 @@ package body Instrument.C_Utils is
213193
-- Start of processing for Get_Lambda_Exprs
214194

215195
begin
216-
Visit_Children (N, Process'Unrestricted_Access);
196+
Visit_Children (N, Process'Access);
217197
return Res;
218198
end Get_Lambda_Exprs;
219199

@@ -263,8 +243,7 @@ package body Instrument.C_Utils is
263243
function Get_Main (TU : Translation_Unit_T) return Cursor_T is
264244
Result : Cursor_T := Get_Null_Cursor;
265245

266-
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
267-
with Convention => C;
246+
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T;
268247
-- Callback for Visit_Children. Set Result and break the iteration if
269248
-- Cursor is the "main" function definition, continue the iteration to
270249
-- find the main otherwise.
@@ -289,7 +268,7 @@ package body Instrument.C_Utils is
289268

290269
begin
291270
Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
292-
Visitor => Visit_Decl'Unrestricted_Access);
271+
Visitor => Visit_Decl'Access);
293272
return Result;
294273
end Get_Main;
295274

@@ -304,8 +283,7 @@ package body Instrument.C_Utils is
304283
Rew : Rewriter_T;
305284
Statement : String)
306285
is
307-
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
308-
with Convention => C;
286+
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T;
309287
-- Traverse the tree until the main function is found, and insert a
310288
-- statement.
311289

@@ -350,7 +328,7 @@ package body Instrument.C_Utils is
350328

351329
begin
352330
Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
353-
Visitor => Visit_Decl'Unrestricted_Access);
331+
Visitor => Visit_Decl'Access);
354332
end Add_Statement_In_Main;
355333

356334
--------------------------------

tools/gnatcov/instrument-c_utils.ads

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ package Instrument.C_Utils is
5353
-- Return True if the node N is a null cursor, false otherwise
5454

5555
procedure Visit_Children
56-
(Parent : Cursor_T; Visitor : Cursor_Visitor_Function);
56+
(Parent : Cursor_T;
57+
Visitor : not null access function
58+
(Node : Cursor_T) return Child_Visit_Result_T);
5759
-- Wrapper for the Visit_Children clang procedure
5860

59-
procedure Visit (Parent : Cursor_T; Visitor : Cursor_Visitor_Function);
61+
procedure Visit
62+
(Parent : Cursor_T;
63+
Visitor : not null access function
64+
(Node : Cursor_T) return Child_Visit_Result_T);
6065
-- Wrapper for the Visit clang procedure
6166

6267
function Get_Children (N : Cursor_T) return Cursor_Vectors.Vector;

0 commit comments

Comments
 (0)