Skip to content

Commit 88b51f5

Browse files
fmeumcopybara-github
authored andcommitted
Emit LLVM coverage for source file paths with a tmp segment
The LLVM LCOV coverage collection logic attempted to filter out source files under `/tmp/`, but instead filtered out all source files with paths containing the substring `/tmp/`. Under macOS, where Bazel's output base lies under `/private/var/tmp`, this matched every output base absolute path. Closes bazelbuild#16852. PiperOrigin-RevId: 500973551 Change-Id: I6c890f38c19eedec83cdf0ea99f021eb85f4e697
1 parent 64d8da6 commit 88b51f5

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh

+91
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,95 @@ end_of_record'
428428
assert_equals "$expected_result" "$(cat bazel-out/_coverage/_coverage_report.dat)"
429429
}
430430

431+
function test_coverage_with_tmp_in_path() {
432+
local -r clang="/usr/bin/clang"
433+
if [[ ! -x ${clang} ]]; then
434+
return
435+
fi
436+
local -r clang_version=$(clang --version | grep -o "clang version [0-9]*" | cut -d " " -f 3)
437+
if [ "$clang_version" -lt 9 ] || [ "$clang_version" -eq 10 ] || [ "$clang_version" -eq 11 ]; then
438+
# No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
439+
echo "clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
440+
fi
441+
442+
local -r llvm_profdata="/usr/bin/llvm-profdata"
443+
if [[ ! -x ${llvm_profdata} ]]; then
444+
return
445+
fi
446+
447+
local -r llvm_cov="/usr/bin/llvm-cov"
448+
if [[ ! -x ${llvm_cov} ]]; then
449+
return
450+
fi
451+
452+
mkdir -p foo/tmp
453+
cat > foo/tmp/BUILD <<'EOF'
454+
cc_library(
455+
name = "a",
456+
srcs = ["a.cc"],
457+
hdrs = ["a.h"],
458+
)
459+
460+
cc_test(
461+
name = "t",
462+
srcs = ["t.cc"],
463+
linkstatic = True,
464+
deps = [":a"],
465+
)
466+
EOF
467+
468+
cat > foo/tmp/a.h <<'EOF'
469+
int a(bool what);
470+
EOF
471+
472+
cat > foo/tmp/a.cc <<'EOF'
473+
#include "a.h"
474+
475+
int a(bool what) {
476+
if (what) {
477+
return 2;
478+
} else {
479+
return 1;
480+
}
481+
}
482+
EOF
483+
484+
cat > foo/tmp/t.cc <<'EOF'
485+
#include <stdio.h>
486+
#include "a.h"
487+
488+
int main(void) {
489+
a(true);
490+
}
491+
EOF
492+
493+
BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
494+
BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
495+
--combined_report=lcov --test_output=all \
496+
//foo/tmp:t --instrumentation_filter=// &>$TEST_log || fail "Coverage failed"
497+
498+
local expected_result='SF:foo/tmp/a.cc
499+
FN:3,_Z1ab
500+
FNDA:1,_Z1ab
501+
FNF:1
502+
FNH:1
503+
BRDA:4,0,0,1
504+
BRDA:4,0,1,0
505+
BRF:2
506+
BRH:1
507+
DA:3,1
508+
DA:4,1
509+
DA:5,1
510+
DA:6,1
511+
DA:7,0
512+
DA:8,0
513+
DA:9,1
514+
LH:5
515+
LF:7
516+
end_of_record'
517+
518+
assert_equals "$expected_result" "$(cat $(get_coverage_file_path_from_test_log))"
519+
assert_equals "$expected_result" "$(cat bazel-out/_coverage/_coverage_report.dat)"
520+
}
521+
431522
run_suite "test tests"

tools/test/collect_cc_coverage.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function llvm_coverage_lcov() {
9393
done < "${COVERAGE_MANIFEST}"
9494

9595
"${LLVM_COV}" export -instr-profile "${output_file}.data" -format=lcov \
96-
-ignore-filename-regex='/tmp/.+' \
96+
-ignore-filename-regex='^/tmp/.+' \
9797
${object_param} | sed 's#/proc/self/cwd/##' > "${output_file}"
9898
}
9999

0 commit comments

Comments
 (0)