<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/121368>121368</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[gcov] source file location error due to miss current_working_directory in gcno file
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
PikachuHyA
</td>
</tr>
</table>
<pre>
## Background
I am migrating an internal codebase from GCC to Clang and have encountered a discrepancy in code coverage reporting between the two compilers. Specifically, Clang fails to correctly locate source files when generating coverage data, whereas GCC operates as expected.
## Reproducer
```
// main.cc
#include <iostream>
int main() {
std::cout << "Hello World\n" << std::endl;
return 0;
}
```
Building with GCC (Works Correctly)
GCC Version: 10.2.1
```
g++ main.cc --coverage -c -o main.o
g++ main.o --coverage -o main.bin.gcc
./main.bin.gcc
lcov --directory . --capture --output-file app.info
genhtml app.info -o html
```
Building with Clang (Fails)
Clang Options: `-Xclang -coverage-version=B02A` to ensure compatibility of .gcno and .gcda files
if gcc is 9.2.1, use `-Xclang -coverage-version=A92A`
```
clang++ main.cc -c -o main.o --coverage -Xclang -coverage-version=B02A
clang++ main.o --coverage -o main.bin.clang
./main.bin.clang
lcov --directory . --capture --output-file app.info
genhtml app.info -o html
```
Error Output
```
+ lcov --directory . --capture --output-file app.info
Capturing coverage data from .
Found gcov version: 10.2.1
Using intermediate gcov format
Scanning . for .gcda files ...
Found 1 data files in .
Processing main.gcda
geninfo: WARNING: could not open /main.cc
geninfo: WARNING: some exclusion markers may be ignored
Finished .info-file creation
+ genhtml app.info -o html
Reading data file app.info
Found 1 entries.
No common filename prefix found!
Writing .css and .png files.
Generating output.
Processing file /main.cc
genhtml: ERROR: cannot read /main.cc
```
I also provide a [demo repository](https://github.com/PikachuHyA/cpp_cov_examples) that reproduces the issue using GitHub Actions (Ubuntu 22.04, GCC 11.4.0, lcov 1.14). You can view the workflow run [here](https://github.com/PikachuHyA/cpp_cov_examples/actions/runs/12555011462/job/35004490579).
## Root Cause
The issue arises because the `current_working_directory` field in the `.gcno` file is empty when using Clang. GCC correctly sets this field, allowing gcov and related tools to resolve the relative paths of source files. Clang's omission leads to the inability of lcov and genhtml to locate the source files, resulting in coverage data processing errors.
Relevant Code Snippet from LLVM:
https://github.com/llvm/llvm-project/blob/f590963db836ccbf7c547a3dea9dc719f24444d1/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp#L977
## Fix
```
diff --git a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
index 2ea89be40a3d..eb6771a2b6ee 100644
--- a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -972,8 +972,13 @@ bool GCOVProfiler::emitProfileNotes(
out.write(Tmp, 4);
}
write(Stamp);
- if (Version >= 90)
- writeString(""); // unuseful current_working_directory
+ if (Version >= 90) {
+ SmallString<256> CWD;
+ llvm::sys::fs::current_path(CWD);
+ // the current_working_directory is used by gcov
+ // if the source file is relative path
+ writeString(CWD);
+ }
if (Version >= 80)
write(0); // unuseful has_unexecuted_blocks
```
tested with GCC 9.2.1 and GCC 10.2.1, lcov 1.0.0
## Reference
- https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
- https://github.com/linux-test-project/lcov
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0WN1u2zoSfhrmZmBBpmRbusiF49Q9Bc62RdLT7l4FFDmyeUKRAkk5ydsvhpKT2E1b7HbXMGCbGs7PN8NvhhYh6J1FvGSLK7a4vhBD3Dt_-VnfC7kf_nhaXzROPV0yXjBewJWQ9zvvBqtYvv4AooNO77yI2u5AWNA2orfCgHQKGxEQWu86eL_ZQHSwMSKJKdiLAwJa6QbagAoEKB2kx15Y-QTaJgUg3QG92CF47J1PRhqMD4gW4h4hPjiQruu1QR8yuO1R6lZLYcw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