Skip to content

Commit ffc4e7e

Browse files
committed
Merge branch 'topic/401-M-preprocessor-flags' into 'master'
Integrated instr: Properly handle -M family of switches Closes #401 See merge request eng/das/cov/gnatcoverage!817 These preprocessor flags imply -E, so there is nothing to do on the gnatcov side. Closes eng/das/cov/gnatcoverage#401
2 parents b95f28f + 0a36626 commit ffc4e7e

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const int special_0 = 0;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "special_0.h"
2+
3+
int
4+
main ()
5+
{
6+
return special_0;
7+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Test that the output of the -M family of flags of the GCC preprocessor is not
3+
clobbered with references to instrumentation artifacts.
4+
"""
5+
6+
import os
7+
import os.path
8+
9+
from e3.fs import cp
10+
11+
from SUITE.control import env
12+
from SUITE.cutils import contents_of, Wdir
13+
from SUITE.tutils import cmdrun, thistest, xcov
14+
15+
Wdir("tmp_")
16+
17+
cwd = os.getcwd()
18+
19+
# Copy the sources in the temporary directory
20+
cp(os.path.join("..", "test.c"), ".")
21+
cp(os.path.join("..", "special_0.h"), ".")
22+
23+
# Then, setup the instrumentation process
24+
xcov(
25+
[
26+
"setup-integration",
27+
"--level=stmt",
28+
f"--files={os.path.join(cwd, 'test.c')}",
29+
"--compilers=gcc",
30+
f"--output-dir={cwd}",
31+
]
32+
)
33+
baseline_dep_file = "test.dep.expected"
34+
compile_cmd = [
35+
"gcc",
36+
"test.c",
37+
"-MM",
38+
"-MF",
39+
baseline_dep_file,
40+
]
41+
42+
# Generate the baseline dep file with the regular compiler
43+
cmdrun(compile_cmd, for_pgm=False)
44+
45+
# Shadow the compiler driver with the generated wrapper
46+
env.add_search_path(env_var="PATH", path=cwd)
47+
48+
# Ask gcc to produce a dep file, using the gnatcov wrapper this time
49+
dep_file = "test.dep.actual"
50+
compile_cmd.pop()
51+
compile_cmd.append(dep_file)
52+
cmdrun(compile_cmd, for_pgm=False)
53+
54+
thistest.fail_if_not_equal(
55+
what="Difference in generated dep file",
56+
expected=contents_of(baseline_dep_file),
57+
actual=contents_of(dep_file),
58+
)
59+
60+
thistest.result()

tools/gnatcov/instrument-gcc_wrapper.adb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,24 @@ is
655655
Arg : constant Unbounded_String := Element (Cur);
656656
Arg_Str : constant String := +Arg;
657657
begin
658+
-- Include family switches will have already had an effect during
659+
-- the initial preprocessing, skip them here
660+
658661
if Arg_Str in "-include" | "--include" then
659662
Cur := Next (Cur);
660663
elsif Starts_With (Arg, "--include=")
661664
or else Starts_With (Arg, "-include")
662665
then
663666
null;
667+
668+
-- The -M family of switches should not be propagated as it
669+
-- otherwise leads to the inclusion of coverage artifacts in the
670+
-- generated dependency files.
671+
672+
elsif Arg_Str in "-MF" | "-MT" | "-MQ" then
673+
Next (Cur);
674+
elsif Starts_With (Arg, "-M") then
675+
null;
664676
else
665677
Result.Append (Element (Cur));
666678
end if;
@@ -758,9 +770,13 @@ begin
758770

759771
-- If this driver invocation is not meant to compile a source file, there
760772
-- is no instrumentation to do: just run the original driver and exit.
773+
--
774+
-- The -M family of flags imply -E, so there nothing to do for us in that
775+
-- case too, EXCEPT FOR -MD and -MMD, which do not imply -E. See section
776+
-- 13 of the C preprocessor manual.
761777

762778
for Arg of Cargs loop
763-
if +Arg in "-###" | "-E" then
779+
if +Arg in "-###" | "-E" | "-M" | "-MM" then
764780
Run_Original_Compiler (Context, Cargs);
765781
return;
766782
end if;

0 commit comments

Comments
 (0)