Skip to content

Commit 0c455ca

Browse files
committed
Merge branch 'pmderodat/subprograms' into 'master'
Reject --subprograms arguments that designate no known subprogram See merge request eng/das/cov/gnatcoverage!318 For eng/das/cov/gnatcoverage#148
2 parents e520096 + fb6f7b6 commit 0c455ca

File tree

9 files changed

+247
-82
lines changed

9 files changed

+247
-82
lines changed

testsuite/tests/subp_of_interest/test.py

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,32 @@
1313
from SUITE.tutils import gprfor, xcov
1414
from SUITE.gprutils import GPRswitches
1515

16+
17+
src_traces = thistest.options.trace_mode == "src"
18+
19+
20+
def check_xcov(label, args, expected_output=""):
21+
"""
22+
Run xcov with the given aruments and check its output.
23+
24+
Also pass it --output-dir={label}, and create that directory beforehand.
25+
"""
26+
log = f"{label}.log"
27+
os.mkdir(label)
28+
xcov(args + [f"--output-dir={label}"], out=log)
29+
thistest.fail_if_not_equal(
30+
"'gnatcov coverage' output",
31+
expected_output,
32+
contents_of(log).strip(),
33+
)
34+
35+
1636
tmp = Wdir("tmp_")
1737

38+
pkg_spec = os.path.join("..", "src", "pkg.ads")
39+
pkg_body = os.path.join("..", "src", "pkg.adb")
40+
41+
thistest.log("== Checkpoint creation ==")
1842
cov_args = build_and_run(
1943
gprsw=GPRswitches(
2044
gprfor(srcdirs=os.path.join("..", "src"), mains=["main.adb"])
@@ -28,30 +52,24 @@
2852
# that the coverage report contains only coverage data for the specified
2953
# subprograms for source traces. For binary traces, simply check that the
3054
# gnatcov coverage invocation yields the expected warning.
31-
os.mkdir("xcov_subp")
32-
xcov(
55+
check_xcov(
56+
"xcov_subp",
3357
cov_args
3458
+ [
3559
"--save-checkpoint",
3660
"trace.ckpt",
37-
"--subprograms",
38-
f"{os.path.join('..', 'src', 'pkg.ads')}:4",
39-
"--subprograms",
40-
f"{os.path.join('..', 'src', 'pkg.adb')}:10",
41-
"--subprograms",
42-
f"{os.path.join('..', 'src', 'pkg.adb')}:12",
43-
"--output-dir=xcov_subp",
61+
f"--subprograms={pkg_spec}:4",
62+
f"--subprograms={pkg_body}:10",
63+
f"--subprograms={pkg_body}:12",
4464
],
45-
out="coverage.log",
65+
expected_output=(
66+
""
67+
if src_traces else
68+
"warning: Ignoring --subprograms switches as this is not supported"
69+
" with binary traces."
70+
),
4671
)
47-
if thistest.options.trace_mode == "bin":
48-
thistest.fail_if_no_match(
49-
"gnatcov coverage output",
50-
"warning: Ignoring --subprograms switches as this is not supported with"
51-
" binary traces.",
52-
contents_of("coverage.log"),
53-
)
54-
else:
72+
if src_traces:
5573
check_xcov_reports(
5674
"*.xcov",
5775
{
@@ -61,12 +79,14 @@
6179
"xcov_subp",
6280
)
6381

64-
# Then check that the checkpoint contains only coverage data for the specific
65-
# subprogram. To do this, produce a new coverage report from the checkpoint
66-
# without using the --subprograms switch.
67-
xcov(
82+
# Then check that the checkpoint contains only coverage data for the
83+
# specific subprogram. To do this, produce a new coverage report from the
84+
# checkpoint without using the --subprograms switch.
85+
thistest.log("== xcov_no_subp ==")
86+
check_xcov(
87+
"xcov_no_subp",
6888
cov_args[:-1]
69-
+ ["--checkpoint", "trace.ckpt", "--output-dir=xcov_no_subp"]
89+
+ ["--checkpoint", "trace.ckpt"],
7090
)
7191
check_xcov_reports(
7292
"*.xcov",
@@ -80,6 +100,7 @@
80100
# Also check the warnings when the subprogram switch is ill-formed
81101

82102
# Case 1: missing colon in the argument
103+
thistest.log("== Missing colon ==")
83104
xcov(
84105
cov_args + ["--subprograms", "no-colon"],
85106
out="xcov-wrong1.txt",
@@ -90,10 +111,11 @@
90111
regexp=r".*Wrong argument passed to --subprograms: .*",
91112
actual=contents_of("xcov-wrong1.txt"),
92113
)
93-
114+
94115
# Case 2: line number is not a number
116+
thistest.log("== Bad line number ==")
95117
xcov(
96-
cov_args + ["--subprograms", f"{os.path.join('..', 'src', 'pkg.ads')}:b",],
118+
cov_args + ["--subprograms", f"{pkg_spec}:b"],
97119
out="xcov-wrong2.txt",
98120
register_failure=False,
99121
)
@@ -102,20 +124,38 @@
102124
regexp=r".*Wrong argument passed to --subprograms: .*",
103125
actual=contents_of("xcov-wrong2.txt"),
104126
)
105-
127+
106128
# Case 3: file does not exist
129+
thistest.log("== No such file ==")
107130
xcov(
108131
cov_args + ["--subprograms", "dumb-file-name:4"],
109132
out="xcov-wrong3.txt",
110133
register_failure=False,
111134
)
112135
thistest.fail_if_no_match(
113136
what="unexpected coverage output",
114-
regexp=r".*Error when parsing --subprograms argument dumb-file-name:4:"
115-
r".*dumb-file-name does not exist",
137+
regexp=(
138+
r".*Error when parsing --subprograms argument dumb-file-name:4:"
139+
r" unknown source file"
140+
),
116141
actual=contents_of("xcov-wrong3.txt"),
117142
)
118143

144+
# Case 4: scope does not exist
145+
thistest.log("== No such scope ==")
146+
xcov(
147+
cov_args + [f"--subprograms={pkg_body}:14"],
148+
out="xcov-wrong3.txt",
149+
register_failure=False,
150+
)
151+
thistest.fail_if_no_match(
152+
what="unexpected coverage output",
153+
regexp=(
154+
f".*Error when parsing --subprograms argument {pkg_body}:14:"
155+
" unknown subprogram"
156+
),
157+
actual=contents_of("xcov-wrong3.txt"),
158+
)
119159

120160

121161
thistest.result()

testsuite/tests/subp_of_interest_c/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports
1212
from SUITE.context import thistest
13-
from SUITE.cutils import contents_of, Wdir
14-
from SUITE.tutils import gprfor, xcov
13+
from SUITE.cutils import Wdir
14+
from SUITE.tutils import gprfor
1515
from SUITE.gprutils import GPRswitches
1616

1717
tmp = Wdir("tmp_")

tools/gnatcov/coverage-source.adb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,7 @@ package body Coverage.Source is
19481948
is
19491949
CU : CU_Id;
19501950
BM : CU_Bit_Maps;
1951+
ST : Scope_Traversal_Type;
19511952

19521953
procedure Set_Executed (SCI : in out Source_Coverage_Info);
19531954
-- Mark SCI as executed
@@ -1962,6 +1963,12 @@ package body Coverage.Source is
19621963
when File_Based_Language => +CU_Name.Filename);
19631964
-- Helper to refer to the instrumented unit in an error message
19641965

1966+
procedure Update_SCI_Wrapper
1967+
(SCO : SCO_Id;
1968+
Tag : SC_Tag;
1969+
Process : access procedure (SCI : in out Source_Coverage_Info));
1970+
-- Execute Process on the SCI for the given SCO and tag
1971+
19651972
------------------
19661973
-- Set_Executed --
19671974
------------------
@@ -1985,13 +1992,9 @@ package body Coverage.Source is
19851992
when Unit_Separate => "separate");
19861993
end Part_Image;
19871994

1988-
ST : Scope_Traversal_Type;
1989-
1990-
procedure Update_SCI_Wrapper
1991-
(SCO : SCO_Id;
1992-
Tag : SC_Tag;
1993-
Process : access procedure (SCI : in out Source_Coverage_Info));
1994-
-- Execute Process on the SCI for the given SCO and tag
1995+
------------------------
1996+
-- Update_SCI_Wrapper --
1997+
------------------------
19951998

19961999
procedure Update_SCI_Wrapper
19972000
(SCO : SCO_Id;

tools/gnatcov/gnatcov_bits_specific.adb

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ with Traces_Files; use Traces_Files;
8787
with Traces_Files_Registry; use Traces_Files_Registry;
8888
with Traces_Names; use Traces_Names;
8989
with Traces_Source;
90+
with Types; use Types;
9091
with Version;
9192

9293
procedure GNATcov_Bits_Specific is
@@ -367,32 +368,39 @@ procedure GNATcov_Bits_Specific is
367368
-- Process_Subp_Input --
368369
------------------------
369370

370-
procedure Process_Subp_Input (Subp_Input : String)
371-
is
372-
Colon_Index : constant Natural :=
371+
procedure Process_Subp_Input (Subp_Input : String) is
372+
Column_Index : constant Natural :=
373373
Ada.Strings.Fixed.Index (Subp_Input, Ada.Strings.Maps.To_Set (':'));
374-
Filename : constant String :=
375-
Subp_Input (Subp_Input'First .. Colon_Index - 1);
374+
Filename : String renames
375+
Subp_Input (Subp_Input'First .. Column_Index - 1);
376+
Column : String renames
377+
Subp_Input (Column_Index + 1 .. Subp_Input'Last);
378+
379+
Identifier : Scope_Entity_Identifier;
376380
begin
377-
if Colon_Index = 0 then
381+
if Column_Index = 0 then
378382
raise Constraint_Error;
379383
end if;
380-
if not Exists (Filename) then
384+
Identifier.Decl_SFI :=
385+
Get_Index_From_Full_Name
386+
(Full_Name => Full_Name (Filename),
387+
Kind => Source_File,
388+
Insert => False);
389+
if Identifier.Decl_SFI = No_Source_File then
381390
Outputs.Fatal_Error
382391
("Error when parsing --subprograms argument "
383-
& Subp_Input & ": file " & Filename & " does not exist");
392+
& Subp_Input & ": unknown source file");
384393
end if;
385-
Subps_Of_Interest.Include
386-
(Scope_Entity_Identifier'
387-
(Decl_SFI =>
388-
Get_Index_From_Full_Name
389-
(Full_Name (Filename), Source_File),
390-
Decl_Line =>
391-
Natural'Value
392-
(Subp_Input (Colon_Index + 1 .. Subp_Input'Last))));
394+
Identifier.Decl_Line := Natural'Value (Column);
393395

396+
if not Available_Subps_Of_Interest.Contains (Identifier) then
397+
Outputs.Fatal_Error
398+
("Error when parsing --subprograms argument "
399+
& Subp_Input & ": unknown subprogram");
400+
end if;
401+
Subps_Of_Interest.Include (Identifier);
394402
exception
395-
-- Deal gracefully with parsing errors
403+
-- Deal gracefully with parsing errors
396404

397405
when Constraint_Error =>
398406
Outputs.Fatal_Error
@@ -421,6 +429,16 @@ procedure GNATcov_Bits_Specific is
421429
Create_Matcher (Ignored_Source_Files, Matcher, Has_Matcher);
422430
Inputs.Iterate (SID_Inputs, SID_Load_Wrapper'Access);
423431

432+
-- Now that all the scope entities that can be referenced by
433+
-- --subprograms are known, dump them in verbose mode.
434+
435+
if Verbose then
436+
for CU in 1 .. Last_CU loop
437+
Put_Line ("Scopes for " & Image (CU) & ":");
438+
Dump (Get_Scope_Entities (CU), Line_Prefix => "| ");
439+
end loop;
440+
end if;
441+
424442
-- Parse the listed subprograms of interest
425443

426444
Copy_Arg_List (Opt_Subp_Of_Interest, Subprograms_Inputs);
@@ -1771,8 +1789,7 @@ begin
17711789
-- Warn when the user hasn't explicitly set a coverage level and
17721790
-- default to stmt.
17731791

1774-
if not (Source_Coverage_Enabled or else Object_Coverage_Enabled)
1775-
then
1792+
if not (Source_Coverage_Enabled or else Object_Coverage_Enabled) then
17761793
Warn ("Coverage level not specified on the command line or in the"
17771794
& " project file (--level=" & Source_Level_Options
17781795
& "|" & Object_Level_Options ("|") & "), defaulting to"

0 commit comments

Comments
 (0)