Skip to content

Commit 39b3280

Browse files
committed
Instrument.C_Utils: reformattings and refactorings
Fix and harmonize the coding style, add missing comments and get rid of the pragma Warnings. TN: V916-015 Change-Id: I0c3a38ed411206951daa45ccd57c58879168ac8d
1 parent 6c1940b commit 39b3280

File tree

2 files changed

+92
-40
lines changed

2 files changed

+92
-40
lines changed

tools/gnatcov/instrument-c_utils.adb

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,24 @@ package body Instrument.C_Utils is
7878
return Cursor_Is_Null (N);
7979
end Is_Null;
8080

81-
pragma Warnings (Off);
82-
8381
--------------------
8482
-- Visit_Children --
8583
--------------------
8684

8785
procedure Visit_Children
88-
(Parent : Cursor_T;
89-
Visitor : Cursor_Visitor_Function) is
90-
86+
(Parent : Cursor_T; Visitor : Cursor_Visitor_Function)
87+
is
9188
function Visitor_Wrapper
9289
(Node : Cursor_T;
9390
Parent : Cursor_T;
9491
Client_Data : Client_Data_T) return Child_Visit_Result_T
9592
with Convention => C;
93+
-- Callback for Clang.Index.Visit_Children. Just a wrapper around
94+
-- Visitor.
95+
96+
---------------------
97+
-- Visitor_Wrapper --
98+
---------------------
9699

97100
function Visitor_Wrapper
98101
(Node : Cursor_T;
@@ -104,24 +107,33 @@ package body Instrument.C_Utils is
104107
return Visitor (Node);
105108
end Visitor_Wrapper;
106109

107-
Data : Client_Data_T;
108-
Result : unsigned;
110+
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+
109117
begin
110-
Result :=
111-
Visit_Children (Parent, Visitor_Wrapper'Unrestricted_Access, Data);
118+
null;
112119
end Visit_Children;
113120

114121
-----------
115122
-- Visit --
116123
-----------
117124

118125
procedure Visit (Parent : Cursor_T; Visitor : Cursor_Visitor_Function) is
119-
120126
function Visitor_Wrapper
121127
(Node : Cursor_T;
122128
Parent : Cursor_T;
123129
Client_Data : Client_Data_T) return Child_Visit_Result_T
124130
with Convention => C;
131+
-- Callback for Clang.Index.Visit_Children. Just a wrapper around
132+
-- Visitor.
133+
134+
---------------------
135+
-- Visitor_Wrapper --
136+
---------------------
125137

126138
function Visitor_Wrapper
127139
(Node : Cursor_T;
@@ -133,34 +145,41 @@ package body Instrument.C_Utils is
133145
return Visitor (Node);
134146
end Visitor_Wrapper;
135147

136-
Data : Client_Data_T;
137-
Result : unsigned;
148+
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+
138155
begin
139-
Result := Visit (Parent, Visitor_Wrapper'Unrestricted_Access, Data);
156+
null;
140157
end Visit;
141-
pragma Warnings (On);
142158

143159
------------------
144160
-- Get_Children --
145161
------------------
146162

147163
function Get_Children (N : Cursor_T) return Cursor_Vectors.Vector is
148-
149164
Res : Vector;
150165

151-
-- Append the children to the result vector and continue the traversal
152-
-- until all children have been appended.
153-
154166
function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T
155167
with Convention => C;
168+
-- Callback for Visit_Children. Append Cursor to Res and continue the
169+
-- traversal.
156170

157-
function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T
158-
is
171+
------------------
172+
-- Append_Child --
173+
------------------
174+
175+
function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T is
159176
begin
160177
Res.Append (Cursor);
161178
return Child_Visit_Continue;
162179
end Append_Child;
163180

181+
-- Start of processing for Get_Children
182+
164183
begin
165184
Visit_Children (N, Get_Children.Append_Child'Unrestricted_Access);
166185
return Res;
@@ -190,6 +209,9 @@ package body Instrument.C_Utils is
190209
end if;
191210
return Child_Visit_Recurse;
192211
end Process;
212+
213+
-- Start of processing for Get_Lambda_Exprs
214+
193215
begin
194216
Visit_Children (N, Process'Unrestricted_Access);
195217
return Res;
@@ -223,6 +245,10 @@ package body Instrument.C_Utils is
223245
end;
224246
end Is_Unit_Of_Interest;
225247

248+
---------------
249+
-- To_Vector --
250+
---------------
251+
226252
function To_Vector (N : Cursor_T) return Cursor_Vectors.Vector is
227253
Res : Cursor_Vectors.Vector;
228254
begin
@@ -235,28 +261,36 @@ package body Instrument.C_Utils is
235261
--------------
236262

237263
function Get_Main (TU : Translation_Unit_T) return Cursor_T is
238-
239-
Main_Decl_Cursor : Cursor_T := Get_Null_Cursor;
264+
Result : Cursor_T := Get_Null_Cursor;
240265

241266
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
242267
with Convention => C;
268+
-- Callback for Visit_Children. Set Result and break the iteration if
269+
-- Cursor is the "main" function definition, continue the iteration to
270+
-- find the main otherwise.
243271

244-
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
245-
is
272+
----------------
273+
-- Visit_Decl --
274+
----------------
275+
276+
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T is
246277
begin
247278
if Kind (Cursor) = Cursor_Translation_Unit then
248279
return Child_Visit_Recurse;
249280
end if;
250281
if Cursor_Get_Mangling (Cursor) = "main" then
251-
Main_Decl_Cursor := Cursor;
282+
Result := Cursor;
252283
return Child_Visit_Break;
253284
end if;
254285
return Child_Visit_Continue;
255286
end Visit_Decl;
287+
288+
-- Start of processing for Get_Main
289+
256290
begin
257291
Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
258292
Visitor => Visit_Decl'Unrestricted_Access);
259-
return Main_Decl_Cursor;
293+
return Result;
260294
end Get_Main;
261295

262296
-- Rewriting utilities
@@ -268,20 +302,23 @@ package body Instrument.C_Utils is
268302
procedure Add_Statement_In_Main
269303
(TU : Translation_Unit_T;
270304
Rew : Rewriter_T;
271-
Statement : String) is
272-
305+
Statement : String)
306+
is
273307
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
274308
with Convention => C;
275309
-- Traverse the tree until the main function is found, and insert a
276310
-- statement.
277311

278-
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
279-
is
312+
----------------
313+
-- Visit_Decl --
314+
----------------
315+
316+
function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T is
280317
begin
281318
if Kind (Cursor) = Cursor_Translation_Unit then
282319
return Child_Visit_Recurse;
283-
end if;
284-
if Cursor_Get_Mangling (Cursor) = "main" then
320+
321+
elsif Cursor_Get_Mangling (Cursor) = "main" then
285322
declare
286323
Fun_Children : constant Vector := Get_Children (Cursor);
287324
Body_Cursor : constant Cursor_T :=
@@ -303,9 +340,14 @@ package body Instrument.C_Utils is
303340
Insert => Statement);
304341
end;
305342
return Child_Visit_Break;
343+
344+
else
345+
return Child_Visit_Continue;
306346
end if;
307-
return Child_Visit_Continue;
308347
end Visit_Decl;
348+
349+
-- Start of processing for Add_Statement_In_Main
350+
309351
begin
310352
Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
311353
Visitor => Visit_Decl'Unrestricted_Access);
@@ -443,6 +485,8 @@ package body Instrument.C_Utils is
443485

444486
procedure Print_Tokens (TU : Translation_Unit_T; N : Cursor_T) is
445487
procedure Put_Token (Tok : Token_T);
488+
-- Callback for Iterate_Tokens. Put the spelling of Tok on the standard
489+
-- output.
446490

447491
---------------
448492
-- Put_Token --
@@ -452,6 +496,9 @@ package body Instrument.C_Utils is
452496
begin
453497
Put (Get_Token_Spelling (TU, Tok));
454498
end Put_Token;
499+
500+
-- Start of processing for Print_Tokens
501+
455502
begin
456503
Iterate_Tokens (TU, N, Put_Token'Access);
457504
New_Line;

tools/gnatcov/instrument-c_utils.ads

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ package Instrument.C_Utils is
3333
type Cursor_Visitor_Function is access function
3434
(Node : Cursor_T) return Child_Visit_Result_T with Convention => C;
3535

36-
-------------------------
37-
-- Location utilities --
38-
-------------------------
36+
------------------------
37+
-- Location utilities --
38+
------------------------
3939

4040
function Sloc (Loc : Source_Location_T) return Local_Source_Location;
4141
-- Convert a Source_Location_T to a Source_Location
@@ -53,8 +53,7 @@ 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;
57-
Visitor : Cursor_Visitor_Function);
56+
(Parent : Cursor_T; Visitor : Cursor_Visitor_Function);
5857
-- Wrapper for the Visit_Children clang procedure
5958

6059
procedure Visit (Parent : Cursor_T; Visitor : Cursor_Visitor_Function);
@@ -90,8 +89,12 @@ package Instrument.C_Utils is
9089
-- Turn the node N into a single element node vector
9190

9291
function Get_Main (TU : Translation_Unit_T) return Cursor_T;
92+
-- Return the cursor corresponding to the definition of the "main"
93+
-- function, or the null cursor if there is no main function.
9394

94-
-- Rewriting utilities
95+
-------------------------
96+
-- Rewriting utilities --
97+
-------------------------
9598

9699
procedure Add_Statement_In_Main
97100
(TU : Translation_Unit_T;
@@ -151,7 +154,9 @@ package Instrument.C_Utils is
151154
Process : not null access procedure (Tok : Token_T));
152155
-- Call Process on all of the tokens within the source range of N
153156

154-
-- Debugging
157+
---------------
158+
-- Debugging --
159+
---------------
155160

156161
procedure Print_Tokens (TU : Translation_Unit_T; N : Cursor_T);
157162
-- Print the tokens associated to the given node

0 commit comments

Comments
 (0)