Skip to content

Commit bb6f1a7

Browse files
fmeumcopybara-github
authored andcommitted
Collect C++ lcov coverage if runtime object not in runfiles
Before this commit, collecting C++ coverage in lcov format would fail at the llvm-cov export step if a shared library listed in the runtime_objects_list.txt was not contained in the runfiles of the top- level target. This can happen e.g. if a cc_library depends on a java_binary that has a cc_binary shared library in its resources. This is fixed by not including objects that don't exist at runtime in the llvm-cov invocation. Fixes bazelbuild#15121. Closes bazelbuild#15118. PiperOrigin-RevId: 442799461
1 parent 9ad3511 commit bb6f1a7

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,77 @@ LH:5
146146
LF:7
147147
end_of_record"
148148

149-
assert_equals "$(cat $(get_coverage_file_path_from_test_log))" "$expected_result"
149+
assert_equals "$expected_result" "$(cat $(get_coverage_file_path_from_test_log))"
150150
}
151151

152+
function test_cc_test_with_runtime_objects_not_in_runfiles() {
153+
local -r llvm_profdata="/usr/bin/llvm-profdata-9"
154+
if [[ ! -x ${llvm_profdata} ]]; then
155+
return
156+
fi
157+
158+
local -r clang="/usr/bin/clang-9"
159+
if [[ ! -x ${clang} ]]; then
160+
return
161+
fi
162+
163+
local -r llvm_cov="/usr/bin/llvm-cov-9"
164+
if [[ ! -x ${llvm_cov} ]]; then
165+
return
166+
fi
167+
168+
cat << EOF > BUILD
169+
cc_test(
170+
name = "main",
171+
srcs = ["main.cpp"],
172+
data = [":jar"],
173+
)
174+
175+
java_binary(
176+
name = "jar",
177+
resources = [":shared_lib"],
178+
create_executable = False,
179+
)
180+
181+
cc_binary(
182+
name = "shared_lib",
183+
linkshared = True,
184+
)
185+
EOF
186+
187+
cat << EOF > main.cpp
188+
#include <iostream>
189+
190+
int main(int argc, char const *argv[])
191+
{
192+
if (argc < 5) {
193+
std::cout << "Hello World!" << std::endl;
194+
}
195+
}
196+
EOF
197+
198+
199+
BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
200+
BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
201+
--test_output=all --instrument_test_targets \
202+
//:main &>$TEST_log || fail "Coverage for //:main failed"
203+
204+
local expected_result="SF:main.cpp
205+
FN:4,main
206+
FNDA:1,main
207+
FNF:1
208+
FNH:1
209+
DA:4,1
210+
DA:5,1
211+
DA:6,1
212+
DA:7,1
213+
DA:8,1
214+
LH:5
215+
LF:5
216+
end_of_record"
217+
218+
assert_equals "$expected_result" "$(cat $(get_coverage_file_path_from_test_log))"
219+
}
220+
221+
152222
run_suite "test tests"

tools/test/collect_cc_coverage.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ function llvm_coverage_lcov() {
8585
while read -r line; do
8686
if [[ ${line: -24} == "runtime_objects_list.txt" ]]; then
8787
while read -r line_runtime_object; do
88+
if [[ -e "${RUNFILES_DIR}/${TEST_WORKSPACE}/${line_runtime_object}" ]]; then
8889
object_param+=" -object ${RUNFILES_DIR}/${TEST_WORKSPACE}/${line_runtime_object}"
90+
fi
8991
done < "${line}"
9092
fi
9193
done < "${COVERAGE_MANIFEST}"

0 commit comments

Comments
 (0)