Skip to content

Commit fb6f7b6

Browse files
committed
Reject --subprograms arguments that designate no known subprogram
1 parent b26c4b9 commit fb6f7b6

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

testsuite/tests/subp_of_interest/test.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def check_xcov(label, args, expected_output=""):
3535

3636
tmp = Wdir("tmp_")
3737

38+
pkg_spec = os.path.join("..", "src", "pkg.ads")
39+
pkg_body = os.path.join("..", "src", "pkg.adb")
40+
3841
thistest.log("== Checkpoint creation ==")
3942
cov_args = build_and_run(
4043
gprsw=GPRswitches(
@@ -55,12 +58,9 @@ def check_xcov(label, args, expected_output=""):
5558
+ [
5659
"--save-checkpoint",
5760
"trace.ckpt",
58-
"--subprograms",
59-
f"{os.path.join('..', 'src', 'pkg.ads')}:4",
60-
"--subprograms",
61-
f"{os.path.join('..', 'src', 'pkg.adb')}:10",
62-
"--subprograms",
63-
f"{os.path.join('..', 'src', 'pkg.adb')}:12",
61+
f"--subprograms={pkg_spec}:4",
62+
f"--subprograms={pkg_body}:10",
63+
f"--subprograms={pkg_body}:12",
6464
],
6565
expected_output=(
6666
""
@@ -115,9 +115,7 @@ def check_xcov(label, args, expected_output=""):
115115
# Case 2: line number is not a number
116116
thistest.log("== Bad line number ==")
117117
xcov(
118-
cov_args + [
119-
"--subprograms", f"{os.path.join('..', 'src', 'pkg.ads')}:b",
120-
],
118+
cov_args + ["--subprograms", f"{pkg_spec}:b"],
121119
out="xcov-wrong2.txt",
122120
register_failure=False,
123121
)
@@ -138,7 +136,23 @@ def check_xcov(label, args, expected_output=""):
138136
what="unexpected coverage output",
139137
regexp=(
140138
r".*Error when parsing --subprograms argument dumb-file-name:4:"
141-
r".*dumb-file-name does not exist"
139+
r" unknown source file"
140+
),
141+
actual=contents_of("xcov-wrong3.txt"),
142+
)
143+
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"
142156
),
143157
actual=contents_of("xcov-wrong3.txt"),
144158
)

tools/gnatcov/gnatcov_bits_specific.adb

Lines changed: 18 additions & 7 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
@@ -374,20 +375,30 @@ procedure GNATcov_Bits_Specific is
374375
Subp_Input (Subp_Input'First .. Column_Index - 1);
375376
Column : String renames
376377
Subp_Input (Column_Index + 1 .. Subp_Input'Last);
378+
379+
Identifier : Scope_Entity_Identifier;
377380
begin
378381
if Column_Index = 0 then
379382
raise Constraint_Error;
380383
end if;
381-
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
390+
Outputs.Fatal_Error
391+
("Error when parsing --subprograms argument "
392+
& Subp_Input & ": unknown source file");
393+
end if;
394+
Identifier.Decl_Line := Natural'Value (Column);
395+
396+
if not Available_Subps_Of_Interest.Contains (Identifier) then
382397
Outputs.Fatal_Error
383398
("Error when parsing --subprograms argument "
384-
& Subp_Input & ": file " & Filename & " does not exist");
399+
& Subp_Input & ": unknown subprogram");
385400
end if;
386-
Subps_Of_Interest.Include
387-
(Scope_Entity_Identifier'
388-
(Decl_SFI =>
389-
Get_Index_From_Full_Name (Full_Name (Filename), Source_File),
390-
Decl_Line => Natural'Value (Column)));
401+
Subps_Of_Interest.Include (Identifier);
391402
exception
392403
-- Deal gracefully with parsing errors
393404

tools/gnatcov/sc_obligations.adb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,11 @@ package body SC_Obligations is
12361236
Remap_SCO_Id (Scope_Ent.From);
12371237
Remap_SCO_Id (Scope_Ent.To);
12381238
Remap_SFI (Relocs, Scope_Ent.Identifier.Decl_SFI);
1239+
1240+
-- Register each scope identifiers to make them available to users
1241+
-- on the command line.
1242+
1243+
Available_Subps_Of_Interest.Include (Scope_Ent.Identifier);
12391244
end loop;
12401245

12411246
end if;

tools/gnatcov/sc_obligations.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ package SC_Obligations is
214214
(Element_Type => Scope_Entity_Identifier);
215215
subtype Scope_Id_Set is Scope_Id_Sets.Set;
216216

217+
Available_Subps_Of_Interest : SC_Obligations.Scope_Id_Set;
218+
-- Set of subprograms of interest known so far. This is used to validate
219+
-- that entries added to Switches.Subp_Of_Interest do exist, i.e. raise
220+
-- errors when a requested subprogram of interest is unknown.
221+
217222
package Scope_Entities_Trees is new Ada.Containers.Multiway_Trees
218223
(Element_Type => Scope_Entity);
219224
subtype Scope_Entities_Tree is Scope_Entities_Trees.Tree;

tools/gnatcov/switches.ads

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ package Switches is
134134
-- instead of hashes.
135135

136136
Subps_Of_Interest : SC_Obligations.Scope_Id_Set;
137-
-- List of subprograms of interest
137+
-- List of subprograms of interest requested by users on the command line
138138

139139
type Separated_Source_Coverage_Type is (None, Routines, Instances);
140140
Separated_Source_Coverage : Separated_Source_Coverage_Type := None;

0 commit comments

Comments
 (0)