Skip to content

Commit 61bf0d0

Browse files
committed
Instrument.C: fix handling of double quotes in filenames
1 parent 2937587 commit 61bf0d0

File tree

8 files changed

+91
-52
lines changed

8 files changed

+91
-52
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int
2+
bar (int a, int b, int c)
3+
{
4+
if (a)
5+
return b;
6+
else
7+
return c;
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
extern int foo (int i);
2+
extern int bar (int a, int b, int c);
23

34
int
45
main (void)
56
{
6-
int result = foo (0);
7+
int result = foo (0) + bar (1, 0, 2);
78
return result;
89
}

testsuite/tests/instr-cov/c_special_filenames/test.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,56 @@
33
characters produces valid instrumented sources.
44
"""
55

6+
import os.path
7+
8+
from e3.fs import cp
9+
610
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports
11+
from SUITE.control import env
712
from SUITE.context import thistest
813
from SUITE.cutils import Wdir
914
from SUITE.tutils import gprfor
1015
from SUITE.gprutils import GPRswitches
1116

1217

13-
tmp = Wdir('tmp_')
18+
tmp = Wdir("tmp_")
19+
20+
# Copy the sources in the temporary directory. Note that we cannot test the
21+
# case of a filename containing a double quote or a backslash on Windows
22+
# because of filename restrictions on that platform.
23+
copy_map = {
24+
"ada_main.adb": "ada_main.adb",
25+
"bar.c": "src bar.c" if env.build.os.name == "windows" else 'src\\"bar.c',
26+
"foo.c": "src [email protected]",
27+
"main.c": "main.c",
28+
}
29+
for src, dest in copy_map.items():
30+
cp(os.path.join("..", src), dest)
31+
32+
# Compute the expected coverage report from the actual source filenames. Note
33+
# that in xcov filenames, "gnatcov coverage" first turns '\' to '/' (during
34+
# path separator canonicalization) and then the unique filename machinery turns
35+
# '/' to '-'.
36+
coverage_data = {
37+
"ada_main.adb": {"+": {7, 9}},
38+
"bar.c": {"+": {4, 5}, "-": {7}},
39+
"foo.c": {"+": {4}},
40+
"main.c": {"+": {7, 8}},
41+
}
42+
expected_report = {
43+
"{}.xcov".format(copy_map[filename].replace("\\", "-")): report
44+
for filename, report in coverage_data.items()
45+
}
1446

1547
build_run_and_coverage(
1648
gprsw=GPRswitches(
17-
root_project=gprfor(srcdirs=['..'], mains=['main.c', 'ada_main.adb'])
49+
root_project=gprfor(srcdirs=["."], mains=["main.c", "ada_main.adb"])
1850
),
19-
covlevel='stmt',
20-
mains=['main', 'ada_main'],
21-
extra_coverage_args=['-axcov', '--output-dir=xcov'],
22-
trace_mode='src',
23-
)
24-
check_xcov_reports(
25-
'*.xcov',
26-
{
27-
'main.c.xcov': {'+': {6}},
28-
'$foo@bar$.c.xcov': {'+': {4}},
29-
'ada_main.adb.xcov': {'+': {9}},
30-
},
31-
'xcov',
51+
covlevel="stmt",
52+
mains=["main", "ada_main"],
53+
extra_coverage_args=["-axcov", "--output-dir=xcov"],
54+
trace_mode="src",
3255
)
56+
check_xcov_reports("*.xcov", expected_report, "xcov")
3357

3458
thistest.result()

tools/gnatcov/instrument-c.adb

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,7 +3585,7 @@ package body Instrument.C is
35853585
-- filenames.
35863586

35873587
& " .unit_name = "
3588-
& " {""" & Escape_Backslashes (CU_Filename) & """, "
3588+
& " {" & C_String_Literal (CU_Filename) & ", "
35893589
& CU_Filename'Length'Image & "}"
35903590
& ","
35913591
& ASCII.LF
@@ -3777,11 +3777,12 @@ package body Instrument.C is
37773777
File.Put_Line (Indent2 & Simple & "),");
37783778

37793779
File.Put_Line (Indent2
3780-
& "STR ("""
3781-
& (if Dump_Config.Trigger = Manual
3782-
then +Prj.Prj_Name
3783-
else Escape_Backslashes (+Main.Filename))
3784-
& """),");
3780+
& "STR ("
3781+
& C_String_Literal
3782+
(if Dump_Config.Trigger = Manual
3783+
then +Prj.Prj_Name
3784+
else +Main.Filename)
3785+
& "),");
37853786
File.Put_Line (Indent2 & "gnatcov_rts_time_to_uint64()" & ",");
37863787
File.Put_Line (Indent2 & "STR ("""")");
37873788
end;
@@ -3793,11 +3794,12 @@ package body Instrument.C is
37933794
-- program name is the name of the main, and there is no way to
37943795
-- get the current execution time.
37953796

3796-
File.Put_Line (Indent2 & "STR ("""
3797-
& (if Dump_Config.Trigger = Manual
3798-
then +Prj.Prj_Name
3799-
else Escape_Backslashes (+Main.Filename))
3800-
& """),");
3797+
File.Put_Line (Indent2 & "STR ("
3798+
& C_String_Literal
3799+
(if Dump_Config.Trigger = Manual
3800+
then +Prj.Prj_Name
3801+
else +Main.Filename)
3802+
& "),");
38013803
File.Put_Line (Indent2 & "0,");
38023804
File.Put_Line (Indent2 & "STR ("""")");
38033805

@@ -4605,4 +4607,25 @@ package body Instrument.C is
46054607
end;
46064608
end Is_Source_Of_Interest;
46074609

4610+
----------------------
4611+
-- C_String_Literal --
4612+
----------------------
4613+
4614+
function C_String_Literal (Str : String) return String is
4615+
Result : Unbounded_String;
4616+
begin
4617+
Append (Result, '"');
4618+
for C of Str loop
4619+
if C = '\' then
4620+
Append (Result, "\\");
4621+
elsif C = '"' then
4622+
Append (Result, "\""");
4623+
else
4624+
Append (Result, C);
4625+
end if;
4626+
end loop;
4627+
Append (Result, '"');
4628+
return +Result;
4629+
end C_String_Literal;
4630+
46084631
end Instrument.C;

tools/gnatcov/instrument-c.ads

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ package Instrument.C is
395395
-- UIC.Sources_Of_Interest. Return whether this source file is a source of
396396
-- interest.
397397

398+
function C_String_Literal (Str : String) return String;
399+
-- Turn Str into the corresponding C string literal. For instance:
400+
--
401+
-- C_String_Literal ("foo") = """foo"""
402+
-- C_String_Literal ("a\b") = """a\\b"""
403+
-- C_String_Literal ("a\b") = """a\\b"""
404+
-- C_String_Literal ("a""b") = """a\""b"""
405+
398406
private
399407

400408
function Find_Instrumented_Entities

tools/gnatcov/paths.adb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ with Ada.Strings.Unbounded;
2222
with GNAT.OS_Lib;
2323
with GNAT.Regpat;
2424

25-
with Strings; use Strings;
26-
2725
package body Paths is
2826

2927
On_Windows : constant Boolean := GNAT.OS_Lib.Directory_Separator = '\';
@@ -319,23 +317,4 @@ package body Paths is
319317
and then Path (Path'First + 1) = ':';
320318
end Starts_With_Drive_Pattern;
321319

322-
-------------------------
323-
-- Escape_Windows_Path --
324-
-------------------------
325-
326-
function Escape_Backslashes (Str : String) return String
327-
is
328-
use Ada.Strings.Unbounded;
329-
Result : Unbounded_String;
330-
begin
331-
for C of Str loop
332-
if C = '\' then
333-
Append (Result, "\\");
334-
else
335-
Append (Result, C);
336-
end if;
337-
end loop;
338-
return +Result;
339-
end Escape_Backslashes;
340-
341320
end Paths;

tools/gnatcov/paths.ads

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ package Paths is
6464
-- of the service, this one always matches both Windows or Unix file path
6565
-- flavors.
6666

67-
function Escape_Backslashes (Str : String) return String;
68-
-- Escape every backslash in the given Str, to turn it into a C-compatible
69-
-- string.
70-
7167
-- TODO??? Handle Unicode file names
7268

7369
end Paths;

0 commit comments

Comments
 (0)