Skip to content

Commit 7d4e246

Browse files
committed
Merge branch 'leo/147-slug_collision' into 'master'
Instrument.adb: Disambiguate hash-based slugs for Ada units Closes #147 See merge request eng/das/cov/gnatcoverage!311 Change the string from which the hash is computed when generating a slug for an Ada unit to avoid collisions. The hash was computed from the symbol string of the unit name, but this transformation can result in the same symbol name being produced for two units: Foo.Bar and Foo_Bar will both be turned into foo_bar. Instead, use the human readable slug as a basis to compute the hash. Closes eng/das/cov/gnatcoverage#147
2 parents dccbbce + 52cd85e commit 7d4e246

File tree

8 files changed

+65
-13
lines changed

8 files changed

+65
-13
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package body Pkg.Child is
2+
3+
function Foo (X : Integer) return Integer is
4+
begin
5+
return X; -- # stmt
6+
end Foo;
7+
8+
end Pkg.Child;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package Pkg.Child is
2+
function Foo (X : Integer) return Integer;
3+
end Pkg.Child;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package Pkg is
2+
end Pkg;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package body Pkg_Child is
2+
3+
function Bar (X : Integer) return Integer is
4+
begin
5+
return X; -- # stmt
6+
end Bar;
7+
8+
end Pkg_Child;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package Pkg_Child is
2+
function Bar (X : Integer) return Integer;
3+
end Pkg_Child;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with Support; use Support;
2+
3+
with Pkg.Child;
4+
with Pkg_Child;
5+
6+
procedure Test_Collision is
7+
begin
8+
Assert (Pkg.Child.Foo (3) = Pkg_Child.Bar (3));
9+
end Test_Collision;
10+
11+
--# pkg-child.adb pkg_child.adb
12+
--
13+
-- /stmt/ l+ ## 0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Regression test: gnatcov used to generated homonym buffer units for a
3+
"Pkg.Child" unit and a "Pkg_Child" unit, as well as duplicate constant
4+
declarations in some generated units, which prevented the build of the
5+
instrumented sources.
6+
"""
7+
8+
from SCOV.tc import TestCase
9+
from SCOV.tctl import CAT
10+
from SUITE.context import thistest
11+
12+
13+
TestCase(category=CAT.stmt).run()
14+
thistest.result()

tools/gnatcov/instrument.adb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,6 @@ package body Instrument is
154154
First : Boolean := True;
155155
Result : Ada_Identifier;
156156
begin
157-
if Use_Hash then
158-
159-
-- Prefix the hash with "z_" to ensure the unit name slug doesn't
160-
-- start with a digit.
161-
162-
return
163-
"z" & Strip_Zero_Padding
164-
(Hex_Image
165-
(Unsigned_32
166-
(Ada.Strings.Hash (To_Symbol_Name (Name)))));
167-
end if;
168-
169157
-- Create a unique slug from the qualified name: replace occurrences of
170158
-- 'z' with 'zz' and insert '_z_' between identifiers.
171159

@@ -187,7 +175,20 @@ package body Instrument is
187175
end;
188176
end loop;
189177
end loop;
190-
return To_String (Result);
178+
179+
if Use_Hash then
180+
181+
-- Prefix the hash with "z" to ensure the unit name slug doesn't
182+
-- start with a digit.
183+
184+
return
185+
"z" & Strip_Zero_Padding
186+
(Hex_Image
187+
(Unsigned_32
188+
(Ada.Strings.Hash (To_String (Result)))));
189+
else
190+
return To_String (Result);
191+
end if;
191192
end Qualified_Name_Slug;
192193

193194
----------------------------

0 commit comments

Comments
 (0)