Enable Source code coverage feature:
authorOleg Lekarev <o.lekarev@samsung.com>
Thu, 13 Jul 2023 09:17:28 +0000 (12:17 +0300)
committerAlexander Soldatov/Platform Lab /SRR/Staff Engineer/Samsung Electronics <soldatov.a@samsung.com>
Thu, 31 Aug 2023 13:41:01 +0000 (16:41 +0300)
https://clang.llvm.org/docs/SourceBasedCodeCoverage.html

README.md
compileoptions.cmake
test-suite/llvm-gcov.sh [new file with mode: 0755]
test-suite/run_tests.sh

index a7ae3b201c30ea59ba2998d9e06aef013099c763..19f45ba2396f78f5dfff08e58eb14fb1086d045d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -52,6 +52,9 @@ user@build$ CC=clang CXX=clang++ cmake ..
 
 For running tests after build has succeed you need to add option `-DCMAKE_INSTALL_PREFIX=$PWD/../bin`.
 
+To enable the Source-based code coverage feature (https://clang.llvm.org/docs/SourceBasedCodeCoverage.html)
+add `-DCLR_CMAKE_ENABLE_CODE_COVERAGE` option.
+
 If you have previously downloaded .NET SDK or CoreCLR sources, then you should modify command line and add following options: `-DDOTNET_DIR=/path/to/sdk/dir -DCORECLR_DIR=/path/to/coreclr/sources`
 
 If cmake tries to download .NET SDK or CoreCLR sources and fails -- see bullets 6 and 7 above. You can download required files manually.
@@ -194,6 +197,10 @@ $ /path/to/netcoredbg --interpreter=TYPE -- /path/to/dotnet /path/to/program.dll
 You can find detailed instruction how to run tests in `test-suite` directory, see [test-suite/README.md](test-suite/README.md).
 Basically you just need to build and install Netcoredbg into `bin` directory (in Netcoredbg source tree) and then change directory to `test-suite` and run script `/run_tests.sh`
 
+If you wish to get "Source-based code coverage" report, you can add an -c or --coverage option to the command line, i.e.:
+`./run_tests.sh -c [[testname1][testname2]..]`
+Note, for that case your build configuration should be done with `-DCLR_CMAKE_ENABLE_CODE_COVERAGE` option (see above). This feature is currently supported on Unix-like platforms only.
+
 
 ### Building and running unit tests
 
index 4056c653091f7d9e6b78591c598e5b73eff7baf4..661145a4763616a8461c53e43fc960ed6be1a6d0 100644 (file)
@@ -138,8 +138,7 @@ if(CLR_CMAKE_ENABLE_CODE_COVERAGE)
       message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
     endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
 
-    add_compile_options(-fprofile-arcs)
-    add_compile_options(-ftest-coverage)
+    add_compile_options(--coverage)
     set(CLANG_COVERAGE_LINK_FLAGS  "--coverage")
     set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
     set(CMAKE_EXE_LINKER_FLAGS     "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
diff --git a/test-suite/llvm-gcov.sh b/test-suite/llvm-gcov.sh
new file mode 100755 (executable)
index 0000000..fdfc21c
--- /dev/null
@@ -0,0 +1 @@
+llvm-cov gcov $@
index 67edb4da23d6d57b6086ba4dae97156ba6cd6dd1..4515c8dac0e960cf0f54c2d5a31e37cc75c32042 100755 (executable)
@@ -7,12 +7,11 @@ generate_xml()
     local xml_path=$1
     local testnames=$2
 
-    echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
-        <testsuites>
-            <testsuite name=\"Tests\" tests=\"\" failures=\"\" errors=\"\" time=\"\">
-                ${testnames}
-            </testsuite>
-        </testsuites>" > "${xml_path}/test-results.xml"
+    echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+    <testsuites>
+        <testsuite name=\"Tests\" tests=\"$(($test_pass + $test_fail))\" failures=\"$test_fail\" errors=\"0\" time=\"\">
+${testnames}        </testsuite>
+    </testsuites>" > "${xml_path}/test-results.xml"
 }
 
 ALL_TEST_NAMES=(
@@ -90,6 +89,10 @@ case $i in
     generate_report=true
     shift
     ;;
+    -c|--coverage)
+    code_coverage_report=true
+    shift
+    ;;
     *)
         TEST_NAMES="$TEST_NAMES *"
     ;;
@@ -102,8 +105,14 @@ if [[ -z $NETCOREDBG ]]; then
     NETCOREDBG="../bin/netcoredbg"
 fi
 
+if [[ -z $OBJS_DIR ]]; then
+    OBJS_DIR="../build/src/CMakeFiles/netcoredbg.dir/"
+fi
+
 if [[ -z $TEST_NAMES ]]; then
     TEST_NAMES="${ALL_TEST_NAMES[@]}"
+    # delete all accumulated coverage data
+    find $OBJS_DIR -name '*.gcda' -delete
 fi
 
 dotnet build TestRunner || exit $?
@@ -187,17 +196,27 @@ for TEST_NAME in $TEST_NAMES; do
     if [ "$res" -ne "0" ]; then
         test_fail=$(($test_fail + 1))
         test_list="$test_list$TEST_NAME ... failed res=$res\n"
-        test_xml+="<testcase name=\"$TEST_NAME\"><failure></failure></testcase>"
+        test_xml+="            <testcase name=\"$TEST_NAME\"><failure></failure></testcase>\n"
     else
         test_pass=$(($test_pass + 1))
         test_list="$test_list$TEST_NAME ... passed\n"
-        test_xml+="<testcase name=\"$TEST_NAME\"></testcase>"
+        test_xml+="            <testcase name=\"$TEST_NAME\"></testcase>\n"
     fi
 done
 
+if [[ $code_coverage_report == true ]]; then
+     lcov --capture --derive-func-data --gcov-tool $PWD/llvm-gcov.sh --directory $OBJS_DIR --output-file coverage.info
+     cd ..
+     lcov --remove test-suite/coverage.info $PWD/'.coreclr/*' $PWD/'build/*' '/usr/*' $PWD/'third_party/*' -o coverage.info
+     genhtml  -o ./cov_html coverage.info
+     zip -r coverage.zip cov_html/ coverage.info
+     cd -
+fi
+
 if [[ $generate_report == true ]]; then
     #Generate xml test file to current directory
     generate_xml "${XML_ABS_PATH}" "${test_xml}"
+    zip test.zip test-results.xml
 fi
 
 echo ""