Skip to content

Commit 6dbf39e

Browse files
committed
instrument-projects.adb: show progress for coverage instrumentation
1 parent 80bd98f commit 6dbf39e

File tree

9 files changed

+172
-21
lines changed

9 files changed

+172
-21
lines changed

testsuite/SCOV/instr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def default_dump_channel():
3838
return 'base64-stdout'
3939

4040

41-
def xcov_instrument(gprsw, covlevel, extra_args=[], dump_trigger="auto",
42-
dump_channel="auto", gpr_obj_dir=None,
41+
def xcov_instrument(gprsw, covlevel, quiet=True, extra_args=[],
42+
dump_trigger="auto", dump_channel="auto", gpr_obj_dir=None,
4343
runtime_project=None, out=None, err=None,
4444
tolerate_messages=None, register_failure=True,
4545
auto_config_args=True, auto_target_args=True,
@@ -50,6 +50,7 @@ def xcov_instrument(gprsw, covlevel, extra_args=[], dump_trigger="auto",
5050
:param GPRswitches gprsw: Project file command line switches to honor.
5151
:param None|str covlevel: Coverage level for the instrumentation
5252
(--level argument). Not passed if None.
53+
:param bool quiet: Whether to pass the "--quiet" flag.
5354
:param list[str] extra_args: Extra arguments to append to the command line.
5455
:param None|str dump_trigger: If None, do not pass the --dump-trigger
5556
argument. If "auto", pass the result of default_dump_trigger().
@@ -97,6 +98,8 @@ def xcov_instrument(gprsw, covlevel, extra_args=[], dump_trigger="auto",
9798
mains = [main.strip(' "') for main in mains]
9899

99100
args = ['instrument'] + covlevel_args
101+
if quiet:
102+
args.append("--quiet")
100103

101104
if dump_trigger:
102105
if dump_trigger == "auto":

testsuite/SCOV/minicheck.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626

2727
COV_RE = re.compile(r'^ *(\d+) (.):.*$')
2828

29-
def build_and_run(gprsw, covlevel, mains, extra_coverage_args, scos=None,
30-
gpr_obj_dir=None, gpr_exe_dir=None, ignored_source_files=[],
31-
separate_coverage=None, extra_args=[], extra_run_args=None,
32-
extra_instr_args=None, extra_gprbuild_args=[],
33-
extra_gprbuild_cargs=[], extra_gprbuild_largs=[],
34-
absolute_paths=False, dump_trigger="auto",
35-
dump_channel="auto", check_gprbuild_output=False,
36-
trace_mode=None, runtime_project=None,
37-
gprsw_for_coverage=None, scos_for_run=True,
38-
register_failure=True, program_env=None,
29+
def build_and_run(gprsw, covlevel, mains, extra_coverage_args, quiet=True,
30+
scos=None, gpr_obj_dir=None, gpr_exe_dir=None,
31+
ignored_source_files=[], separate_coverage=None,
32+
extra_args=[], extra_run_args=None, extra_instr_args=None,
33+
extra_gprbuild_args=[], extra_gprbuild_cargs=[],
34+
extra_gprbuild_largs=[], absolute_paths=False,
35+
dump_trigger="auto", dump_channel="auto",
36+
check_gprbuild_output=False, trace_mode=None,
37+
runtime_project=None, gprsw_for_coverage=None,
38+
scos_for_run=True, register_failure=True, program_env=None,
3939
tolerate_instrument_messages=None, exec_args=None,
4040
auto_languages=True, manual_prj_name=None):
4141
"""
@@ -56,6 +56,7 @@ def build_and_run(gprsw, covlevel, mains, extra_coverage_args, scos=None,
5656
"gnatcov coverage" command-line returned. This is just for convenience:
5757
one can pass an empty list here and manually append extra arguments to
5858
the result.
59+
:param bool quiet: Whether to pass "--quiet" to "gnatcov.
5960
:param None|list[str] scos: Optional list of SCOs files (ALI or SID) must
6061
be passed to gnatcov. These files must have no extension (for instance:
6162
'obj/foo' instead of 'obj/foo.ali'. If absent, we pass "-P" to "gnatcov
@@ -170,6 +171,8 @@ def gprbuild_wrapper(root_project):
170171
for pattern in ignored_source_files])
171172
if separate_coverage:
172173
cov_or_instr_args.extend(['-S', separate_coverage])
174+
if quiet:
175+
cov_or_instr_args.append("--quiet")
173176

174177
# Compute arguments to specify units of interest.
175178
if trace_mode == 'bin':
@@ -213,14 +216,20 @@ def gprbuild_wrapper(root_project):
213216

214217
# Instrument the project and build the result
215218
extra_instr_args = cov_or_instr_args + list(extra_instr_args or [])
216-
xcov_instrument(gprsw, covlevel, extra_args=extra_instr_args,
217-
gpr_obj_dir=gpr_obj_dir, dump_trigger=dump_trigger,
218-
dump_channel=dump_channel,
219-
runtime_project=runtime_project,
220-
out='instrument.log',
221-
register_failure=register_failure,
222-
tolerate_messages=tolerate_instrument_messages,
223-
auto_languages=auto_languages)
219+
xcov_instrument(
220+
gprsw,
221+
covlevel,
222+
quiet=False,
223+
extra_args=extra_instr_args,
224+
gpr_obj_dir=gpr_obj_dir,
225+
dump_trigger=dump_trigger,
226+
dump_channel=dump_channel,
227+
runtime_project=runtime_project,
228+
out='instrument.log',
229+
register_failure=register_failure,
230+
tolerate_messages=tolerate_instrument_messages,
231+
auto_languages=auto_languages,
232+
)
224233
gprbuild_wrapper(gprsw.root_project)
225234

226235
# Retrieve the dump_channel that "gnatcov instrument" actually used,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
extern void cpp_func (void);
4+
5+
void
6+
c_func (void)
7+
{
8+
puts ("c:c_func");
9+
cpp_func ();
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
3+
extern "C" {
4+
extern void cpp_func (void);
5+
}
6+
7+
void
8+
cpp_func (void)
9+
{
10+
std::cout << "cpp:cpp_func" << std::endl;
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with Ada.Text_IO; use Ada.Text_IO;
2+
3+
procedure Main is
4+
procedure C_Func;
5+
pragma Import (C, C_Func, "c_func");
6+
begin
7+
Put_Line ("Ada:main");
8+
C_Func;
9+
end Main;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!C++ DEAD Testcase uses a Ada/C/C++ mixed project
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Check that "gnatcov instrument" correctly reports progress about the
3+
instrumented units.
4+
"""
5+
6+
import os
7+
import os.path
8+
9+
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports
10+
from SUITE.context import thistest
11+
from SUITE.cutils import Wdir, lines_of
12+
from SUITE.tutils import gprfor
13+
from SUITE.gprutils import GPRswitches
14+
15+
16+
tmp = Wdir("tmp_")
17+
18+
# Avoid "creating output path" info messages
19+
os.mkdir("obj")
20+
21+
build_run_and_coverage(
22+
gprsw=GPRswitches(
23+
root_project=gprfor(
24+
langs=["Ada", "C", "C++"],
25+
mains=["main.adb"],
26+
srcdirs=[".."],
27+
)
28+
),
29+
covlevel="stmt",
30+
mains=["main"],
31+
extra_coverage_args=["-axcov", "--output-dir=xcov"],
32+
quiet=False,
33+
trace_mode="src",
34+
)
35+
36+
# Sanity check: the insrument-build-coverage process completed with the
37+
# expected results.
38+
check_xcov_reports(
39+
"*xcov",
40+
{
41+
"main.adb.xcov": {"+": {7, 8}},
42+
"c_unit.c.xcov": {"+": {8}},
43+
"cpp_unit.cpp.xcov": {"+": {10}},
44+
},
45+
cwd="xcov",
46+
)
47+
48+
# Units are not instrumented in a particular order: we only want to check that
49+
# all of them are listed with the expected formatting.
50+
output = lines_of("instrument.log")
51+
thistest.fail_if_not_equal(
52+
"First line of 'gnatcov instrument' output",
53+
"".join(output[:1]),
54+
"Coverage instrumentation",
55+
)
56+
thistest.fail_if_not_equal(
57+
"'gnatcov instrument' output",
58+
"\n".join([
59+
" [Ada] main",
60+
" [C++] cpp_unit.cpp",
61+
" [C] c_unit.c",
62+
]),
63+
"\n".join(sorted(output[1:])),
64+
)
65+
66+
thistest.result()

testsuite/tests/TC11-052-internal-error/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def check(args, trigger, info):
1717
out = f"{trigger}.log"
1818
env = dict(os.environ)
1919
env["GNATCOV_INTERNAL_ERROR_TRIGGER"] = trigger
20-
p = xcov(args, out=out, env=env, register_failure=False)
20+
p = xcov(args + ["-q"], out=out, env=env, register_failure=False)
2121
thistest.fail_if(
2222
p.status == 0,
2323
"gnatcov returned success exit code, error expected"

tools/gnatcov/instrument-projects.adb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with Ada.Strings;
2828
with Ada.Strings.Fixed;
2929
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3030
with Ada.Strings.Unbounded.Hash;
31+
with Ada.Text_IO; use Ada.Text_IO;
3132
with Ada.Unchecked_Deallocation;
3233

3334
with GNAT.Exception_Actions;
@@ -266,6 +267,10 @@ is
266267
-- Clean the instrumentation directories and print any relevant information
267268
-- regarding the instrumentation context.
268269

270+
procedure Show_Progress (Language : Some_Language; Unit_Name : String);
271+
-- If quiet mode is not enabled, show instrumentation progress by printing
272+
-- the language/unit name that are being instrumented.
273+
269274
-----------------------
270275
-- Load_From_Project --
271276
-----------------------
@@ -987,6 +992,37 @@ is
987992
Outputs.Print_Internal_Error (Exc);
988993
end Clean_And_Print;
989994

995+
-------------------
996+
-- Show_Progress --
997+
-------------------
998+
999+
procedure Show_Progress (Language : Some_Language; Unit_Name : String) is
1000+
begin
1001+
if Quiet then
1002+
return;
1003+
end if;
1004+
1005+
declare
1006+
Language_Name : constant String :=
1007+
"[" & Image (Language) & "]";
1008+
Filename_Indentation : constant String :=
1009+
(1 .. 16 - Language_Name'Length => ' ');
1010+
begin
1011+
Put (" ");
1012+
Put (Language_Name);
1013+
Put (Filename_Indentation);
1014+
1015+
-- To keep progress logs readable, use source basenames for
1016+
-- file-based languages.
1017+
1018+
if Language_Kind (Language) = File_Based_Language then
1019+
Put_Line (Ada.Directories.Simple_Name (Unit_Name));
1020+
else
1021+
Put_Line (Unit_Name);
1022+
end if;
1023+
end;
1024+
end Show_Progress;
1025+
9901026
Mains_To_Instrument : array (Src_Supported_Language)
9911027
of Main_To_Instrument_Vectors.Vector;
9921028
-- For each supported language, list of mains to instrument. Note that
@@ -1203,6 +1239,10 @@ begin
12031239
Sources_Of_Interest_File.Close;
12041240
end;
12051241

1242+
if not Quiet then
1243+
Put_Line ("Coverage instrumentation");
1244+
end if;
1245+
12061246
-- Instrument every unit of interest asynchronously
12071247

12081248
declare
@@ -1255,6 +1295,8 @@ begin
12551295

12561296
Unit_Args.Append (+Unit_Name);
12571297

1298+
Show_Progress (LU_Info.Language, Unit_Name);
1299+
12581300
-- According to the set parallelism level, instrument in
12591301
-- the same process (thus reusing the libadalang context, which
12601302
-- is a big gain of time), or spawn another instrumentation

0 commit comments

Comments
 (0)