Skip to content

Commit 89bf881

Browse files
committed
Merge branch 'eyraud/fix_subp_of_interest' into 'master'
Instrument.C: fix logic errors wrt. scopes implementation See merge request eng/cov/gnatcoverage!261 Ref: eng/cov/gnatcoverage#65
2 parents f0f3e78 + ab5faa7 commit 89bf881

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int
2+
bar ()
3+
{
4+
return 0;
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "bar.h"
2+
#include "pkg.h"
3+
4+
int
5+
main ()
6+
{
7+
foo ();
8+
bar ();
9+
return 0;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int
2+
foo ()
3+
{
4+
return 0;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern int foo ();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Check that the subprograms of interest mechanism works as expected on a C
3+
example. It also acts as a regression testcase as gnatcov used to crash when
4+
instrumenting a source including multiple headers, with at last one of them
5+
not having any coverage obligation.
6+
"""
7+
8+
import os
9+
import os.path
10+
11+
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports
12+
from SUITE.context import thistest
13+
from SUITE.cutils import contents_of, Wdir
14+
from SUITE.tutils import gprfor, xcov
15+
from SUITE.gprutils import GPRswitches
16+
17+
tmp = Wdir("tmp_")
18+
19+
cov_args = build_run_and_coverage(
20+
gprsw=GPRswitches(
21+
gprfor(srcdirs=os.path.join("..", "src"), mains=["main.c"])
22+
),
23+
covlevel="stmt",
24+
mains=["main"],
25+
trace_mode='src',
26+
extra_coverage_args=[
27+
"-axcov",
28+
"--subprograms",
29+
f"{os.path.join('..', 'src', 'pkg.c')}:1",
30+
],
31+
)
32+
33+
check_xcov_reports(
34+
"*.xcov",
35+
{
36+
"main.c.xcov": {},
37+
"bar.h.xcov": {},
38+
"pkg.c.xcov": {"+": {4}},
39+
},
40+
"obj",
41+
)
42+
43+
thistest.result()

tools/gnatcov/instrument-c.adb

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ package body Instrument.C is
150150
-- Convert low level SCOs in each scope for each file to high-level SCOs
151151
-- using the mapping in SCO_Map. Set the file's SCO range to cover all of
152152
-- its scopes' SCO ranges.
153+
--
154+
-- Also remove from the Scopes map empty file scopes.
153155

154156
overriding procedure Append_SCO
155157
(Pass : Instrument_Pass_Kind;
@@ -491,10 +493,6 @@ package body Instrument.C is
491493
UIC : in out C_Unit_Inst_Context'Class;
492494
N : Cursor_T)
493495
is
494-
Inserted : Boolean;
495-
496-
File_Scope_Position : Scopes_In_Files_Map.Cursor;
497-
498496
procedure Enter_File_Scope
499497
(UIC : in out C_Unit_Inst_Context'Class;
500498
SFI : Source_File_Index)
@@ -508,7 +506,11 @@ package body Instrument.C is
508506

509507
procedure Enter_File_Scope
510508
(UIC : in out C_Unit_Inst_Context'Class;
511-
SFI : Source_File_Index) is
509+
SFI : Source_File_Index)
510+
is
511+
File_Scope_Position : Scopes_In_Files_Map.Cursor;
512+
513+
Inserted : Boolean;
512514
begin
513515
if not UIC.Scopes.Contains (SFI) then
514516

@@ -629,24 +631,26 @@ package body Instrument.C is
629631

630632
procedure Remap_Scopes
631633
(Scopes : in out Scopes_In_Files_Map.Map;
632-
SCO_Map : LL_HL_SCO_Map) is
634+
SCO_Map : LL_HL_SCO_Map)
635+
is
636+
Res : Scopes_In_Files_Map.Map;
633637
begin
634638
for Cur in Scopes.Iterate loop
635639
declare
636-
Ref : constant Scopes_In_Files_Map.Reference_Type :=
637-
Scopes.Reference (Cur);
638-
File_Scope : Scope_Entity renames
639-
Scope_Entities_Trees.Element (Ref.File_Scope_Entity);
640+
File_Scope : File_Scope_Type :=
641+
Scopes_In_Files_Map.Element (Cur);
642+
File_Scope_Entity : Scope_Entity renames
643+
Scope_Entities_Trees.Element (File_Scope.File_Scope_Entity);
640644
begin
641-
-- If the file scope is empty, remove it
645+
-- If the file scope is empty, do not add it to the resulting map
642646

643-
if File_Scope.To < File_Scope.From then
644-
Ref.Scope_Entities.Delete_Subtree (Ref.File_Scope_Entity);
645-
else
646-
Remap_Scope_Entities (Ref.Scope_Entities, SCO_Map);
647+
if File_Scope_Entity.To >= File_Scope_Entity.From then
648+
Remap_Scope_Entities (File_Scope.Scope_Entities, SCO_Map);
649+
Res.Insert (Scopes_In_Files_Map.Key (Cur), File_Scope);
647650
end if;
648651
end;
649652
end loop;
653+
Scopes := Res;
650654
end Remap_Scopes;
651655

652656
----------------
@@ -3290,23 +3294,23 @@ package body Instrument.C is
32903294
-- Associate these bit maps to the corresponding CU
32913295

32923296
Set_Bit_Maps (UIC.CUs.Element (SFI), Bit_Maps);
3297+
end;
3298+
end loop;
32933299

3294-
-- Iterate through the package level body entities
3300+
-- Iterate through the package level body entities
32953301

3296-
Remap_Scopes (UIC.Scopes, SCO_Map);
3302+
Remap_Scopes (UIC.Scopes, SCO_Map);
32973303

3298-
for C in UIC.Scopes.Iterate loop
3299-
declare
3300-
CU : constant Created_Unit_Maps.Cursor :=
3301-
UIC.CUs.Find (Scopes_In_Files_Map.Key (C));
3302-
begin
3303-
if Created_Unit_Maps.Has_Element (CU) then
3304-
Set_Scope_Entities
3305-
(Created_Unit_Maps.Element (CU),
3306-
Scopes_In_Files_Map.Element (C).Scope_Entities);
3307-
end if;
3308-
end;
3309-
end loop;
3304+
for C in UIC.Scopes.Iterate loop
3305+
declare
3306+
CU : constant Created_Unit_Maps.Cursor :=
3307+
UIC.CUs.Find (Scopes_In_Files_Map.Key (C));
3308+
begin
3309+
if Created_Unit_Maps.Has_Element (CU) then
3310+
Set_Scope_Entities
3311+
(Created_Unit_Maps.Element (CU),
3312+
Scopes_In_Files_Map.Element (C).Scope_Entities);
3313+
end if;
33103314
end;
33113315
end loop;
33123316
end;

0 commit comments

Comments
 (0)