Skip to content

Commit f64a4b3

Browse files
committed
Merge branch 'eyraud/118' into 'master'
Instrument.C: fix instrumentation when no SCO can be instrumented Closes #118 See merge request eng/cov/gnatcoverage!254 gnatcov used to crash when instrumenting source code that consisted solely of non-instrumentable constructs. This was due to an implicit assumption that every source of interest should be present in the UIC.Instrumented_Entities map, which wasn't the case in this scenario. Fix the code to ensure the assumption's validity. Closes eng/cov/gnatcoverage#118
2 parents 88ad677 + 21f5650 commit f64a4b3

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
constexpr int
2+
bar ()
3+
{
4+
return 0; // # ce-header
5+
}

testsuite/tests/C++/mcdc/ConstExpr/src/test_constexpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "pkg.hh"
2+
13
constexpr int
24
foo ()
35
{

tools/gnatcov/instrument-c.adb

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,17 @@ package body Instrument.C is
896896
Pragma_Aspect_Name : Name_Id := Namet.No_Name)
897897
is
898898
begin
899+
-- Insert a new entry to the UIC.Instrumented_Entities maps: even if
900+
-- this SOI does not possess instrumented SCOs (it can be the case if
901+
-- all of the code constructs are not instrumentable), we want an empty
902+
-- Instrumented_Entities entry rather than no entry at all, to have
903+
-- proper initialization of checkpoints structures (e.g. Statement_Bits)
904+
-- later on.
905+
906+
if not UIC.Instrumented_Entities.Contains (From.Source_File) then
907+
UIC.Instrumented_Entities.Insert (From.Source_File, (others => <>));
908+
end if;
909+
899910
Append_SCO
900911
(C1, C2, From.L, To.L, From.Source_File, Last, Pragma_Aspect_Name);
901912

@@ -950,13 +961,9 @@ package body Instrument.C is
950961
SFI : Valid_Source_File_Index)
951962
return C_Instrumented_Entities_Maps.Reference_Type
952963
is
953-
use C_Instrumented_Entities_Maps;
954-
Cur : Cursor;
955964
Dummy : Boolean;
956965
begin
957-
UIC.Instrumented_Entities.Insert
958-
(SFI, (others => <>), Cur, Dummy);
959-
return UIC.Instrumented_Entities.Reference (Cur);
966+
return UIC.Instrumented_Entities.Reference (SFI);
960967
end Find_Instrumented_Entities;
961968

962969
-----------------------
@@ -3185,7 +3192,7 @@ package body Instrument.C is
31853192
UIC.Allocated_Bits.Reference (Ent.Buffers_Index);
31863193
Bit_Maps : CU_Bit_Maps;
31873194
begin
3188-
-- Allocate bits in covearge buffers and insert the
3195+
-- Allocate bits in coverage buffers and insert the
31893196
-- corresponding witness calls.
31903197

31913198
for SS of Ent.Statements loop
@@ -4567,11 +4574,15 @@ package body Instrument.C is
45674574
-- switches.
45684575

45694576
if UIC.Files_Of_Interest.Contains (File) then
4570-
SOI :=
4571-
(Of_Interest => True,
4572-
SFI => Get_Index_From_Generic_Name
4573-
(+File, Source_File),
4574-
CU_Name => CU_Name_For_File (File));
4577+
declare
4578+
SFI : constant Source_File_Index :=
4579+
Get_Index_From_Generic_Name (+File, Source_File);
4580+
begin
4581+
SOI :=
4582+
(Of_Interest => True,
4583+
SFI => SFI,
4584+
CU_Name => CU_Name_For_File (File));
4585+
end;
45754586
else
45764587
SOI := (Of_Interest => False);
45774588
end if;

tools/gnatcov/instrument-c.ads

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ private
397397
function Find_Instrumented_Entities
398398
(UIC : in out C_Unit_Inst_Context'Class;
399399
SFI : Valid_Source_File_Index)
400-
return C_Instrumented_Entities_Maps.Reference_Type;
400+
return C_Instrumented_Entities_Maps.Reference_Type
401+
with Pre => UIC.Instrumented_Entities.Contains (SFI);
401402
-- Return a reference to the UIC.Instrumented_Entities entry
402-
-- corresponding to the source file that SFI designates. If there is no
403-
-- such entry yet, create it.
403+
-- corresponding to the source file that SFI designates.
404404

405405
type Pass_Kind is abstract tagged null record;
406406

0 commit comments

Comments
 (0)