Skip to content

Record coverage for templates instantiated outside their defining module - #5319

Open
jmh530 wants to merge 5 commits into
ldc-developers:masterfrom
jmh530:codex/imported-template-coverage-2538
Open

jmh530 wants to merge 5 commits into
ldc-developers:masterfrom
jmh530:codex/imported-template-coverage-2538

Conversation

@jmh530

@jmh530 jmh530 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #2538.

This is the LDC companion to dlang/dmd#24251. Both PRs update druntime to combine coverage registrations for the same source file. This PR also updates LDC’s code generation for imported templates and Boolean coverage.

Investigating why a library’s reported coverage changed under separate compilation led to an existing LDC static-library regression. One module defines a template and another instantiates it. With stock LDC 1.43.0, the program calls the template body twice, yet there is no listing for the defining module. The template is the only code in that module, and its instructions were emitted elsewhere.

LDC now records the hits in the object that emits the instructions and registers them under the defining source file. The bundled druntime combines registrations for that file. LDC uses a separate runtime callback for Boolean instrumentation so an executed line stays at one hit. Atomic and non-atomic modes continue to report execution counts.

With the fix, the same static-library build produces the missing listing. The first execution calls the template twice and uses the default merge:0. A second execution calls it twice more and uses merge:1 to include the first execution’s listing.

Coverage mode First execution Second execution with merging
Atomic 2 4
Non-atomic 2 4
Boolean 1 1

These results were verified on Linux x64. The coverage-mode test also passed with the Boolean callback change built independently of the template fix. The Linux code-generation test selection passed 205 tests, with 13 unsupported. Druntime regressions cover equivalent source paths, conflicting source lengths, and mixed count and Boolean registrations in both orders.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

Retry a failed coverage report open with an extended-length absolute
path. Check the file descriptor before calling fdopen so a report
path failure cannot terminate the process through an invalid CRT call.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Group registrations by source file, add their execution counts, and
combine their executable-line bitmaps before writing a listing.
Recognize different path spellings for the same source. If the line
counts disagree, diagnose the stale objects and finish writing reports
for unaffected files.

Exercise overlapping registrations, path aliases, and incompatible
source lengths without depending on compiler code generation.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Add a registration entry point for hit flags while keeping the existing
counting interface. If any registration supplies a Boolean flag for a
line, report whether that line ran. Keep exact totals for count-only
lines in the same source file.

Test both registration orders and merging with a saved listing.
Document when to start a fresh report when switching modes.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Select the Boolean runtime callback for -cov-increment=boolean. Keep
atomic and non-atomic instrumentation on the counting callback.

Run the Boolean coverage test twice with report merging enabled.
A hit line must still report one after the second execution.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
LDC skips a template body's increment when its source location belongs
to another module. In the static-library regression, the defining
module contains only the template and its listing is missing entirely.

Give each emitting module counters for the source lines it instruments.
Register hits under the defining source file, and keep compile-time hits
and coverage thresholds with the defining module.

Re-enable the static-library regression and check the template body.
Verify that a separately emitted Boolean template remains a single hit
across repeated executions.

Fixes ldc-developers#2538

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve coverage for templates (2.078.1)

1 participant