[PATCH 02/14] meson: Add support for clang/LLVM coverage instrumentation

Rayhan Faizel posted 14 patches 3 months ago
[PATCH 02/14] meson: Add support for clang/LLVM coverage instrumentation
Posted by Rayhan Faizel 3 months ago
Clang coverage is the de-facto standard for generating coverage reports
in libFuzzer based fuzzers, so add support for the same.

__LLVM_PROFILE_RT_INIT_ONCE is added automatically which breaks commandhelper
so filter the same.

Note that the existing gcov support can also be used to generate coverage.

Signed-off-by: Rayhan Faizel <rayhan.faizel@gmail.com>
---
 meson.build           | 11 +++++++++++
 meson_options.txt     |  3 ++-
 tests/commandhelper.c |  8 ++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/meson.build b/meson.build
index f31485c395..904524abc6 100644
--- a/meson.build
+++ b/meson.build
@@ -2141,6 +2141,17 @@ if get_option('test_coverage')
   ]
 endif
 
+if get_option('test_coverage_clang')
+  if cc.get_id() != 'clang'
+    error('test_coverage_clang can only be used with the Clang compiler.')
+  endif
+
+  coverage_flags = [
+    '-fprofile-instr-generate',
+    '-fcoverage-mapping',
+  ]
+endif
+
 
 # Various definitions
 
diff --git a/meson_options.txt b/meson_options.txt
index 2d440c63d8..153e325cb5 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -8,7 +8,8 @@ option('unitdir', type: 'string', value: '', description: 'directory for systemd
 option('sysusersdir', type: 'string', value: '', description: 'directory for sysusers files')
 # dep:tests
 option('expensive_tests', type: 'feature', value: 'auto', description: 'set the default for enabling expensive tests (long timeouts)')
-option('test_coverage', type: 'boolean', value: false, description: 'turn on code coverage instrumentation')
+option('test_coverage', type: 'boolean', value: false, description: 'turn on code coverage instrumentation (gcov)')
+option('test_coverage_clang', type: 'boolean', value: false, description: 'turn on code coverage instrumentation (clang)')
 option('git_werror', type: 'feature', value: 'auto', description: 'use -Werror if building from GIT')
 option('rpath', type: 'feature', value: 'auto', description: 'whether to include rpath information in installed binaries and libraries')
 option('docdir', type: 'string', value: '', description: 'documentation installation directory')
diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index d4629d824e..6f562c3e28 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -172,9 +172,13 @@ static int printEnvironment(FILE *log)
     for (i = 0; i < length; i++) {
         /* Ignore the variables used to instruct the loader into
          * behaving differently, as they could throw the tests off.
-         * Also ignore __CF_USER_TEXT_ENCODING, which is set by macOS. */
+         * Also ignore __CF_USER_TEXT_ENCODING, which is set by macOS.
+         *
+         * If LLVM coverage is enabled, __LLVM_PROFILE_RT_INIT_ONCE is
+         * automatically set, which should be ignored. */
         if (!STRPREFIX(newenv[i], "LD_") &&
-            !STRPREFIX(newenv[i], "__CF_USER_TEXT_ENCODING=")) {
+            !STRPREFIX(newenv[i], "__CF_USER_TEXT_ENCODING=") &&
+            !STRPREFIX(newenv[i], "__LLVM_PROFILE_RT_INIT_ONCE=")) {
             fprintf(log, "ENV:%s\n", newenv[i]);
         }
     }
-- 
2.34.1