Skip to content

Commit 6eda572

Browse files
committed
Add sources for the src-trace library instrumentation example
1 parent ea4137c commit 6eda572

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

doc/gnatcov/src_traces.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,10 @@ For the sake of the example, we will consider that
10381038
by each new test individually or for the current set of tests
10391039
at a given point in time.
10401040

1041+
The sources for this example can be found under the
1042+
`share/examples/gnatcoverage/doc/library_instr/` directory of the GNATDAS
1043+
distribution.
1044+
10411045
Setting up the coverage runtime
10421046
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10431047

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Project demonstrating how to instrument and get coverage over a library, and
2+
# distribute the instrumented library independently, leveraging the
3+
# Externally_Built GPR attribute.
4+
#
5+
# See paragraph "Example use cases" of the
6+
# "Producing source traces with gnatcov instrument" section for more details
7+
8+
RTS_DIR = rts_install
9+
10+
.PHONY: instrument build run coverage
11+
12+
default: coverage
13+
14+
# Only needs to be called once, can be re-used across multiple GNATcov workflows
15+
$(RTS_DIR)/share/gpr/gnatcov_rts.gpr:
16+
gnatcov setup --prefix=$(RTS_DIR)
17+
18+
instrument-lib: export GPR_PROJECT_PATH := $(shell pwd)/$(RTS_DIR)/share/gpr:$(GPR_PROJECT_PATH)
19+
instrument-lib: $(RTS_DIR)/share/gpr/gnatcov_rts.gpr
20+
@echo 'export GPR_PROJECT_PATH=$$(pwd)/$(RTS_DIR)/share/gpr:$$GPR_PROJECT_PATH'
21+
gnatcov instrument -P code.gpr --level=stmt -XCODE_LIBMODE=instrument
22+
23+
build-lib: export GPR_PROJECT_PATH := $(shell pwd)/$(RTS_DIR)/share/gpr:$(GPR_PROJECT_PATH)
24+
build-lib: instrument
25+
gprbuild -f -p -P code.gpr --src-subdirs=gnatcov-instr --implicit-with=gnatcov_rts.gpr \
26+
-XCODE_LIBMODE=build
27+
28+
instrument-test: export GPR_PROJECT_PATH := $(shell pwd)/$(RTS_DIR)/share/gpr:$(GPR_PROJECT_PATH)
29+
instrument-test: instrument-lib
30+
gnatcov instrument -P tests.gpr --level=stmt --externally-built-projects --dump-trigger=atexit
31+
32+
build-test: export GPR_PROJECT_PATH := $(shell pwd)/$(RTS_DIR)/share/gpr:$(GPR_PROJECT_PATH)
33+
build-test: instrument-test build-lib
34+
gprbuild -P tests.gpr --src-subdirs=gnatcov-instr --implicit-with=gnatcov_rts.gpr
35+
36+
run: build-test
37+
./obj-tests/test_inc
38+
39+
coverage: run
40+
gnatcov coverage -P tests.gpr --level=stmt --annotate=xcov test_inc*.srctrace --externally-built-projects
41+
cat obj-tests/ops.adb.xcov
42+
43+
clean:
44+
rm -rf obj-* $(RTS_DIR) test_inc*.srctrace lib-*
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library project Code is
2+
for Source_Dirs use ("code");
3+
for Object_Dir use "obj-" & Project'Name;
4+
5+
for Library_Dir use "lib-" & project'Name;
6+
for Library_Kind use "static";
7+
for Library_Name use "code";
8+
9+
type Mode is ("build", "instrument", "use");
10+
LIB_MODE : Mode := external ("CODE_LIBMODE", "use");
11+
12+
case LIB_MODE is
13+
when "build" => for Externally_Built use "False";
14+
when "instrument" => for Externally_Built use "False";
15+
when "use" => for Externally_Built use "True";
16+
end case;
17+
18+
end Code;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
------------------------------------------------------------------------------
2+
-- GNATcoverage --
3+
-- Copyright (C) 2012-2025, AdaCore --
4+
------------------------------------------------------------------------------
5+
6+
package body Ops is
7+
procedure Apply (Op : Op_Kind; X : in out Integer) is
8+
begin
9+
case Op is
10+
when Increment => X := X + 1;
11+
when Decrement => X := X - 1;
12+
end case;
13+
end Apply;
14+
end Ops;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
------------------------------------------------------------------------------
2+
-- GNATcoverage --
3+
-- Copyright (C) 2012-2025, AdaCore --
4+
------------------------------------------------------------------------------
5+
6+
package Ops is
7+
type Op_Kind is (Increment, Decrement);
8+
procedure Apply (Op : Op_Kind; X : in out Integer);
9+
end Ops;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
with "code.gpr";
2+
3+
project Tests is
4+
for Source_Dirs use ("tests");
5+
for Object_Dir use "obj-" & Project'Name;
6+
7+
for Main use ("test_inc.adb");
8+
9+
package Coverage is
10+
for Units use ();
11+
end Coverage;
12+
end Tests;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
------------------------------------------------------------------------------
2+
-- GNATcoverage --
3+
-- Copyright (C) 2012-2025, AdaCore --
4+
------------------------------------------------------------------------------
5+
6+
with Ops;
7+
8+
procedure Test_Inc is
9+
X : Integer := 4;
10+
begin
11+
Ops.Apply (Ops.Increment, X);
12+
pragma Assert (X = 5);
13+
end Test_Inc;

0 commit comments

Comments
 (0)