Skip to content

Commit c0f56d1

Browse files
committed
Introduce wrapper to avoid access types in instrument-c.adb
Using 'Access or 'Address should be avoided when used to implement OUT parameters. Create subprograms that wrap thin C bindings to avoid this, and refactor instrument-c.adb accordingly. Change-Id: I40ca1268e18e7099bce6604ff94128a559582fec TN: V916-015
1 parent 278c41f commit c0f56d1

File tree

4 files changed

+113
-120
lines changed

4 files changed

+113
-120
lines changed

tools/gnatcov/clang-extensions.adb

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,23 @@
2222

2323
with Clang.CX_String; use Clang.CX_String;
2424

25+
with Files_Table; use Files_Table;
26+
2527
package body Clang.Extensions is
2628

29+
function To_Sloc (Line, Column : unsigned) return Local_Source_Location is
30+
((Natural (Line), Natural (Column)));
31+
-- Convert a Clang local source location to gnatcov's own format
32+
33+
function To_Sloc
34+
(File : File_T; Line, Column : unsigned) return Source_Location
35+
is
36+
((Source_File => Get_Index_From_Generic_Name
37+
(Name => Get_File_Name (File),
38+
Kind => Source_File),
39+
L => To_Sloc (Line, Column)));
40+
-- Convert a Clang source location to gnatcov's own format
41+
2742
--------------------
2843
-- Get_Opcode_Str --
2944
--------------------
@@ -82,6 +97,50 @@ package body Clang.Extensions is
8297
CX_Rewriter_Insert_Text_After_Token_C (Rew, Loc, Insert & ASCII.NUL);
8398
end CX_Rewriter_Insert_Text_After_Token;
8499

100+
-----------------------
101+
-- Spelling_Location --
102+
-----------------------
103+
104+
function Spelling_Location (Loc : Source_Location_T) return Source_Location
105+
is
106+
File : File_T;
107+
Line, Column, Offset : aliased unsigned;
108+
begin
109+
Get_Spelling_Location
110+
(Loc, File'Address, Line'Access, Column'Access, Offset'Access);
111+
112+
return To_Sloc (File, Line, Column);
113+
end Spelling_Location;
114+
115+
-------------------
116+
-- File_Location --
117+
-------------------
118+
119+
function File_Location
120+
(Loc : Source_Location_T) return Local_Source_Location
121+
is
122+
File : File_T;
123+
Line, Column, Offset : aliased unsigned;
124+
begin
125+
Get_File_Location
126+
(Loc, File'Address, Line'Access, Column'Access, Offset'Access);
127+
return To_Sloc (Line, Column);
128+
end File_Location;
129+
130+
-----------------------
131+
-- Presumed_Location --
132+
-----------------------
133+
134+
function Presumed_Location
135+
(Loc : Source_Location_T) return Local_Source_Location
136+
is
137+
Filename : aliased String_T;
138+
Line, Column : aliased unsigned;
139+
begin
140+
Get_Presumed_Location (Loc, Filename'Access, Line'Access, Column'Access);
141+
return To_Sloc (Line, Column);
142+
end Presumed_Location;
143+
85144
-----------------------
86145
-- Is_Macro_Location --
87146
-----------------------
@@ -102,7 +161,7 @@ package body Clang.Extensions is
102161

103162
function Is_Macro_Arg_Expansion
104163
(Loc : Source_Location_T;
105-
Start_Loc : access Source_Location_T := null;
164+
Start_Loc : out Source_Location_T;
106165
TU : Translation_Unit_T) return Boolean
107166
is
108167
function Is_Macro_Arg_Expansion
@@ -112,8 +171,14 @@ package body Clang.Extensions is
112171
with
113172
Import, Convention => C,
114173
External_Name => "clang_isMacroArgExpansion";
174+
175+
C_Start_Loc : aliased Source_Location_T;
115176
begin
116-
return Is_Macro_Arg_Expansion (Loc, Start_Loc, TU) /= 0;
177+
return Result : constant Boolean :=
178+
Is_Macro_Arg_Expansion (Loc, C_Start_Loc'Access, TU) /= 0
179+
do
180+
Start_Loc := C_Start_Loc;
181+
end return;
117182
end Is_Macro_Arg_Expansion;
118183

119184
----------------------------------------------

tools/gnatcov/clang-extensions.ads

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ with Interfaces.C; use Interfaces.C;
2525
with Clang.Index; use Clang.Index;
2626
with Clang.Rewrite; use Clang.Rewrite;
2727

28+
with Slocs; use Slocs;
29+
2830
package Clang.Extensions is
2931

3032
function Get_Body (C : Cursor_T) return Cursor_T
@@ -114,13 +116,26 @@ package Clang.Extensions is
114116
-- The functions below are simply bindings around clang functions, which
115117
-- are exhaustively documented in the clang sources.
116118

119+
function Spelling_Location (Loc : Source_Location_T) return Source_Location
120+
with Inline;
121+
-- Thick binding for Clang.Index.Get_Spelling_Location
122+
123+
function File_Location
124+
(Loc : Source_Location_T) return Local_Source_Location
125+
with Inline;
126+
-- Thick binding for Clang.Index.Get_File_Location
127+
128+
function Presumed_Location
129+
(Loc : Source_Location_T) return Local_Source_Location;
130+
-- Thick binding for Clang.Index.Get Presumed_Location
131+
117132
function Is_Macro_Location (Loc : Source_Location_T) return Boolean
118133
with Inline;
119134
-- See isMacroID in clang/Basic/SourceLocation.h.
120135

121136
function Is_Macro_Arg_Expansion
122137
(Loc : Source_Location_T;
123-
Start_Loc : access Source_Location_T := null;
138+
Start_Loc : out Source_Location_T;
124139
TU : Translation_Unit_T) return Boolean
125140
with Inline;
126141
-- See isMacroArgExpansion in clang/Basic/SourceManager.h.

tools/gnatcov/instrument-c.adb

Lines changed: 26 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,8 @@ package body Instrument.C is
565565
Last : Boolean;
566566
Pragma_Aspect_Name : Name_Id := Namet.No_Name)
567567
is
568-
Loc : Source_Location_T :=
569-
Get_Range_Start (Get_Cursor_Extent (N));
570-
571-
Line, Column, Offset : aliased unsigned;
572-
File : File_T;
573-
Info : PP_Info;
568+
Loc : Source_Location_T := Get_Range_Start (Get_Cursor_Extent (N));
569+
Info : PP_Info;
574570
begin
575571
Append_SCO (C1, C2, From, To, Last, Pragma_Aspect_Name);
576572

@@ -591,14 +587,10 @@ package body Instrument.C is
591587
Expansion_Stack : Expansion_Lists.List;
592588
Definition_Info : Expansion_Info;
593589

594-
Macro_Expansion_Name : US.Unbounded_String;
595-
Immediate_Expansion_Loc_C : Source_Location_T;
596-
Immediate_Expansion_Loc : Source_Location;
597-
598-
Macro_Arg_Expanded_Loc_C : aliased Source_Location_T;
599-
Macro_Arg_Expanded_Loc : Source_Location;
590+
Macro_Expansion_Name : US.Unbounded_String;
591+
Immediate_Expansion_Loc : Source_Location_T;
592+
Macro_Arg_Expanded_Loc : Source_Location_T;
600593
begin
601-
602594
-- Note: macro arguments are completely macro-expanded before they
603595
-- are substituted in a macro body, unless they are stringified or
604596
-- pasted with other tokens.
@@ -662,53 +654,26 @@ package body Instrument.C is
662654
-- So we have to get the macro argument expansion location, and
663655
-- get its spelling location.
664656

665-
if Is_Macro_Arg_Expansion
666-
(Loc, Macro_Arg_Expanded_Loc_C'Access, UIC.TU)
657+
if Is_Macro_Arg_Expansion (Loc, Macro_Arg_Expanded_Loc, UIC.TU)
667658
then
668-
Get_Spelling_Location
669-
(Macro_Arg_Expanded_Loc_C,
670-
File'Address,
671-
Line'Access,
672-
Column'Access,
673-
Offset'Access);
674-
Macro_Arg_Expanded_Loc :=
675-
(Source_File =>
676-
Get_Index_From_Generic_Name
677-
(Name => Get_File_Name (File),
678-
Kind => Source_File),
679-
L => (Natural (Line), Natural (Column)));
680-
681659
Macro_Expansion_Name :=
682660
+Get_Immediate_Macro_Name_For_Diagnostics
683-
(Macro_Arg_Expanded_Loc_C, UIC.TU);
661+
(Macro_Arg_Expanded_Loc, UIC.TU);
684662

685663
Definition_Info :=
686664
(Macro_Name => Macro_Expansion_Name,
687-
Sloc => Macro_Arg_Expanded_Loc);
665+
Sloc => Spelling_Location (Macro_Arg_Expanded_Loc));
688666
else
689-
Get_Spelling_Location
690-
(Loc,
691-
File'Address,
692-
Line'Access,
693-
Column'Access,
694-
Offset'Access);
695-
696-
Immediate_Expansion_Loc :=
697-
(Source_File =>
698-
Get_Index_From_Generic_Name
699-
(Name => Get_File_Name (File),
700-
Kind => Source_File),
701-
L => (Natural (Line), Natural (Column)));
702667
Macro_Expansion_Name :=
703668
+Get_Immediate_Macro_Name_For_Diagnostics (Loc, UIC.TU);
704669
Definition_Info :=
705670
(Macro_Name => Macro_Expansion_Name,
706-
Sloc => Immediate_Expansion_Loc);
671+
Sloc => Spelling_Location (Loc));
707672
end if;
708673

709674
while Is_Macro_Location (Loc) loop
710675

711-
Immediate_Expansion_Loc_C := Loc;
676+
Immediate_Expansion_Loc := Loc;
712677

713678
-- Find the location of the immediately expanded macro. Getting
714679
-- the immediate expansion location yields a location in the
@@ -747,9 +712,7 @@ package body Instrument.C is
747712
-- as implemented in clang.
748713

749714
while Is_Macro_Arg_Expansion
750-
(Immediate_Expansion_Loc_C,
751-
Macro_Arg_Expanded_Loc_C'Access,
752-
UIC.TU)
715+
(Immediate_Expansion_Loc, Macro_Arg_Expanded_Loc, UIC.TU)
753716
loop
754717
-- TODO??? Document why it is needed to loop while we are
755718
-- in a macro argument expansion (did not manage to make an
@@ -758,31 +721,17 @@ package body Instrument.C is
758721
-- Get_Immediate_Macro_Name_For_Diagnostics implemented in
759722
-- clang.
760723

761-
Immediate_Expansion_Loc_C :=
724+
Immediate_Expansion_Loc :=
762725
Get_Immediate_Expansion_Loc
763-
(Immediate_Expansion_Loc_C, UIC.TU);
726+
(Immediate_Expansion_Loc, UIC.TU);
764727
end loop;
765728

766-
-- Immediate_Expansion_Loc is the location of the token in
767-
-- the immediate expanded macro definition. To get to the
768-
-- expansion point, go up one level.
769-
770-
Immediate_Expansion_Loc_C :=
771-
Get_Immediate_Expansion_Loc
772-
(Immediate_Expansion_Loc_C, UIC.TU);
773-
Get_Spelling_Location
774-
(Immediate_Expansion_Loc_C,
775-
File'Address,
776-
Line'Access,
777-
Column'Access,
778-
Offset'Access);
729+
-- Immediate_Expansion_Loc is the location of the token in the
730+
-- immediate expanded macro definition. To get to the expansion
731+
-- point, go up one level.
779732

780733
Immediate_Expansion_Loc :=
781-
(Source_File =>
782-
Get_Index_From_Generic_Name
783-
(Name => Get_File_Name (File),
784-
Kind => Source_File),
785-
L => (Natural (Line), Natural (Column)));
734+
Get_Immediate_Expansion_Loc (Immediate_Expansion_Loc, UIC.TU);
786735
Macro_Expansion_Name :=
787736
+Get_Immediate_Macro_Name_For_Diagnostics (Loc, UIC.TU);
788737

@@ -797,7 +746,8 @@ package body Instrument.C is
797746
if Length (Macro_Expansion_Name) /= 0 then
798747
Expansion_Stack.Append
799748
((Macro_Name => Macro_Expansion_Name,
800-
Sloc => Immediate_Expansion_Loc));
749+
Sloc => Spelling_Location
750+
(Immediate_Expansion_Loc)));
801751
end if;
802752
end loop;
803753

@@ -844,9 +794,6 @@ package body Instrument.C is
844794
End_Loc : constant Source_Location_T :=
845795
Get_Range_End (Cursor_Source_Range_C);
846796

847-
Line, Column, Offset : aliased unsigned;
848-
File : File_T;
849-
850797
Cursor_Source_Range : Slocs.Local_Source_Location_Range;
851798

852799
procedure Update (LL_SCO : Nat; Info : in out PP_Info);
@@ -864,28 +811,13 @@ package body Instrument.C is
864811
end Update;
865812

866813
begin
867-
-- Get start of the range
868-
869-
Get_File_Location
870-
(Start_Loc,
871-
File'Address,
872-
Line'Access,
873-
Column'Access,
874-
Offset'Access);
875-
Cursor_Source_Range.First_Sloc :=
876-
(Line => Natural (Line), Column => Natural (Column));
877-
878-
-- Get end of the range. Note: end column is exclusive
879-
880-
Get_File_Location
881-
(End_Loc,
882-
File'Address,
883-
Line'Access,
884-
Column'Access,
885-
Offset'Access);
886-
887-
Cursor_Source_Range.Last_Sloc :=
888-
(Line => Natural (Line), Column => Natural (Column) - 1);
814+
-- Get start and end of the range. Note: End_Loc is exclusive,
815+
-- whereas we need Cursor_Source_Range.Last_Sloc to be inclusive.
816+
817+
Cursor_Source_Range.First_Sloc := File_Location (Start_Loc);
818+
Cursor_Source_Range.Last_Sloc := File_Location (End_Loc);
819+
Cursor_Source_Range.Last_Sloc.Column :=
820+
Cursor_Source_Range.Last_Sloc.Column - 1;
889821

890822
UIC.LL_PP_Info_Map.Update_Element
891823
(UIC.LL_PP_Info_Map.Find (SCOs.SCO_Table.Last), Update'Access);

tools/gnatcov/instrument-c_utils.adb

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,30 @@ package body Instrument.C_Utils is
3434
----------
3535

3636
function Sloc (Loc : Source_Location_T) return Local_Source_Location is
37-
Line, Column : aliased Interfaces.C.unsigned;
3837
begin
39-
Get_Presumed_Location (Location => Loc,
40-
Filename => null,
41-
Line => Line'Access,
42-
Column => Column'Access);
43-
return (Natural (Line), Natural (Column));
38+
return Presumed_Location (Loc);
4439
end Sloc;
4540

4641
----------------
4742
-- Start_Sloc --
4843
----------------
4944

5045
function Start_Sloc (N : Cursor_T) return Local_Source_Location is
51-
Line, Column : aliased Interfaces.C.unsigned;
52-
Loc : constant Source_Location_T :=
53-
Get_Range_Start (Get_Cursor_Extent (N));
5446
begin
55-
Get_Presumed_Location (Location => Loc,
56-
Filename => null,
57-
Line => Line'Access,
58-
Column => Column'Access);
59-
return (Natural (Line), Natural (Column));
47+
return Presumed_Location (Get_Range_Start (Get_Cursor_Extent (N)));
6048
end Start_Sloc;
6149

6250
--------------
6351
-- End_Sloc --
6452
--------------
6553

6654
function End_Sloc (N : Cursor_T) return Local_Source_Location is
67-
Line, Column : aliased Interfaces.C.unsigned;
68-
Loc : Source_Location_T :=
69-
Get_Range_End (Get_Cursor_Extent (N));
55+
Loc : Source_Location_T := Get_Range_End (Get_Cursor_Extent (N));
7056
begin
7157
if Is_Macro_Location (Loc) then
7258
Loc := Get_Expansion_End (Get_Cursor_TU (N), Loc);
7359
end if;
74-
Get_Presumed_Location
75-
(Location => Loc,
76-
Filename => null,
77-
Line => Line'Access,
78-
Column => Column'Access);
79-
return (Natural (Line), Natural (Column));
60+
return Presumed_Location (Loc);
8061
end End_Sloc;
8162

8263
----------

0 commit comments

Comments
 (0)