| Issue |
243423
|
| Summary |
[Coverage] Coverage mapping reports ghost execution for unchosen C11 _Generic associations
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Durgesh-Shelke
|
### Summary
Under ISO C11 §6.5.1.1 (Generic selection, paragraph 3):
> *"Only the _expression_ from the chosen generic association is evaluated. None of the other expressions from other generic associations are evaluated."*
However, Clang's source-based code coverage (`-fprofile-instr-generate -fcoverage-mapping`) lacks a dedicated visitor for `GenericSelectionExpr` in `CoverageMappingGen.cpp`. As a result, the entire `_Generic` _expression_ inherits the enclosing function's entry execution counter (`#0`).
Any unchosen type associations—even multi-line blocks containing function calls or side effects—are reported by `llvm-cov` as **100% EXECUTED**, presenting false-positive "ghost coverage".
### Reproducer
#include <stdio.h>
int eval(int x) {
int res = _Generic(x,
int:
x + 10,
float:
(
printf("Float branch executed!\n"),
x * 100
),
double:
(
printf("Double branch executed!\n"),
x * 1000
)
);
return res;
}
int main(void) {
eval(1); // x is int; float and double associations are never evaluated
return 0;
}
Commands to compile and view coverage:
clang -O0 -fprofile-instr-generate -fcoverage-mapping test.c -o test
./test
llvm-profdata merge default.profraw -o test.profdata
llvm-cov show ./test -instr-profile="" test.c
### Observed Output
1| |#include <stdio.h>
2| |
3| 1|int eval(int x) {
4| 1| int res = _Generic(x,
5| 1| int:
6| 1| x + 10,
7| 1| float:
8| 1| (
9| 1| printf("Float branch executed!\n"),
10| 1| x * 100
11| 1| ),
12| 1| double:
13| 1| (
14| 1| printf("Double branch executed!\n"),
15| 1| x * 1000
16| 1| )
17| 1| );
18| 1| return res;
19| 1|}
Notice that lines 7–16 are reported with execution count 1, even though:
• The binary executed eval(1) where x is int.
• Terminal output is completely empty (neither printf was ever compiled or called).
• Inspecting binary strings (strings test | grep -E "Float|Double") confirms that string literals in unchosen associations were never emitted into the object file.
### Coverage Mapping Dump (-Xclang -dump-coverage-mapping)
eval:
File 0, 3:17 -> 19:2 = #0
main:
File 0, 21:16 -> 24:2 = #0
Clang generates a single monolithic region (#0) covering the entire function; no sub-regions or skipped regions are instantiated for unchosen generic associations.
### Contrast with GCC (gcov)
Under GCC (gcc -O0 --coverage), lines 4–16 are correctly identified as uninstrumented (-:), preventing false-positive coverage reporting.
### Root Cause
In clang/lib/CodeGen/CoverageMappingGen.cpp, CounterCoverageMappingBuilder does not implement VisitGenericSelectionExpr(const GenericSelectionExpr *E). Traversal falls back to the default statement visitor, which visits all child AST nodes under the enclosing function region (#0).
### Proposed Solution
CounterCoverageMappingBuilder should implement VisitGenericSelectionExpr:
• Traverse and map the selected _expression_ (E->getResultExpr()) using the current counter.
• Mark the source ranges of all unchosen generic association expressions as skipped regions (SourceMappingRegion::SkippedRegion).
Note on Skipped vs. Count 0: Unchosen associations should be marked as skipped regions, mirroring how preprocessor #if or compile-time branches are handled. Marking them with count 0 would unfairly penalize coverage on generic type-dispatching macros (such as <tgmath.h>), whereas a skipped region accurately reflects compile-time elision.
### Environment
• Clang versions verified:
- Clang 14.0.0 (Ubuntu 14.0.0-1ubuntu1.1, x86_64)
- Clang 16.0.6 (x86_64)
- Clang trunk (24.0.0git, verified on Compiler Explorer)
• OS: Linux x86_64
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs