Skip to content

Commit

Permalink
Emit LLVM coverage for source file paths with a tmp segment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmeum authored and copybara-github committed Jan 10, 2023
1 parent 64d8da6 commit 88b51f5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,95 @@ end_of_record'
assert_equals "$expected_result" "$(cat bazel-out/_coverage/_coverage_report.dat)"
}

function test_coverage_with_tmp_in_path() {
local -r clang="/usr/bin/clang"
if [[ ! -x ${clang} ]]; then
return
fi
local -r clang_version=$(clang --version | grep -o "clang version [0-9]*" | cut -d " " -f 3)
if [ "$clang_version" -lt 9 ] || [ "$clang_version" -eq 10 ] || [ "$clang_version" -eq 11 ]; then
# No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
echo "clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
fi

local -r llvm_profdata="/usr/bin/llvm-profdata"
if [[ ! -x ${llvm_profdata} ]]; then
return
fi

local -r llvm_cov="/usr/bin/llvm-cov"
if [[ ! -x ${llvm_cov} ]]; then
return
fi

mkdir -p foo/tmp
cat > foo/tmp/BUILD <<'EOF'
cc_library(
name = "a",
srcs = ["a.cc"],
hdrs = ["a.h"],
)
cc_test(
name = "t",
srcs = ["t.cc"],
linkstatic = True,
deps = [":a"],
)
EOF

cat > foo/tmp/a.h <<'EOF'
int a(bool what);
EOF

cat > foo/tmp/a.cc <<'EOF'
#include "a.h"
int a(bool what) {
if (what) {
return 2;
} else {
return 1;
}
}
EOF

cat > foo/tmp/t.cc <<'EOF'
#include <stdio.h>
#include "a.h"
int main(void) {
a(true);
}
EOF

BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
--combined_report=lcov --test_output=all \
//foo/tmp:t --instrumentation_filter=// &>$TEST_log || fail "Coverage failed"

local expected_result='SF:foo/tmp/a.cc
FN:3,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:4,0,0,1
BRDA:4,0,1,0
BRF:2
BRH:1
DA:3,1
DA:4,1
DA:5,1
DA:6,1
DA:7,0
DA:8,0
DA:9,1
LH:5
LF:7
end_of_record'

assert_equals "$expected_result" "$(cat $(get_coverage_file_path_from_test_log))"
assert_equals "$expected_result" "$(cat bazel-out/_coverage/_coverage_report.dat)"
}

run_suite "test tests"
2 changes: 1 addition & 1 deletion tools/test/collect_cc_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function llvm_coverage_lcov() {
done < "${COVERAGE_MANIFEST}"

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

Expand Down

0 comments on commit 88b51f5

Please sign in to comment.