[clang] [llvm] [GCOV] Modify the path parsing of gcov instrument filter (PR #183475)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 23 01:53:55 PDT 2026


https://github.com/LeeLee26 updated https://github.com/llvm/llvm-project/pull/183475

>From 2450691aae2d8063585b22766f9a78a0f52590a6 Mon Sep 17 00:00:00 2001
From: leelee <mengxiaoduo.1 at bytedance.com>
Date: Thu, 26 Feb 2026 16:09:31 +0800
Subject: [PATCH 1/3] [GCOV] Modify the path parsing of gcov instrument filter

---
 .../Instrumentation/GCOVProfiling.cpp         | 20 ++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
index 9f47c43bb9a0b..e3e36e10030ed 100644
--- a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -229,6 +229,17 @@ static SmallString<128> getFilename(const DIScope *SP, vfs::FileSystem &VFS) {
   return Path;
 }
 
+/// Canonicalize a path by making it absolute and removing dots.
+/// Compared to sys::fs::real_path, it will not resolve symlinks.
+static StringRef canonicalizePath(StringRef P) {
+  SmallString<256> Ret = P;
+  std::error_code Err = sys::fs::make_absolute(Ret);
+  if (Err)
+    return P;
+  sys::path::remove_dots(Ret, /*removedotdot*/ true);
+  return Ret;
+}
+
 namespace {
   class GCOVRecord {
   protected:
@@ -471,13 +482,8 @@ bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
 
   // Path can be
   // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for
-  // such a case we must get the real_path.
-  if (VFS.getRealPath(Filename, RealPath)) {
-    // real_path can fail with path like "foo.c".
-    RealFilename = Filename;
-  } else {
-    RealFilename = RealPath;
-  }
+  // such a case we must get the canonicalized path.
+  StringRef RealFilename = canonicalizePath(Filename);
 
   bool ShouldInstrument;
   if (FilterRe.empty()) {

>From b0aaa45adcd4c83cc2bd665a7883aaf2c1b4004b Mon Sep 17 00:00:00 2001
From: leelee <mengxiaoduo.1 at bytedance.com>
Date: Thu, 26 Feb 2026 17:10:39 +0800
Subject: [PATCH 2/3] [GCOV] fix bug

---
 llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
index e3e36e10030ed..a241e27e46372 100644
--- a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -478,7 +478,6 @@ bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
   }
 
   SmallString<256> RealPath;
-  StringRef RealFilename;
 
   // Path can be
   // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for

>From 7f9f569263c7baec8caaa1baf33aa3d5c11a4893 Mon Sep 17 00:00:00 2001
From: leelee <mengxiaoduo.1 at bytedance.com>
Date: Mon, 23 Mar 2026 16:32:21 +0800
Subject: [PATCH 3/3] [GCOV] Add: test for coverage filter without resolving
 symlinks

---
 clang/test/CodeGen/code-coverage-filter.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/test/CodeGen/code-coverage-filter.c b/clang/test/CodeGen/code-coverage-filter.c
index 9248ab5b1c8d0..5f43f58f733d5 100644
--- a/clang/test/CodeGen/code-coverage-filter.c
+++ b/clang/test/CodeGen/code-coverage-filter.c
@@ -14,6 +14,11 @@
 // RUN: FileCheck -check-prefix=NONE < %t %s
 // RUN: %clang -S -emit-llvm --coverage -fprofile-filter-files=".*\.c$" -fprofile-exclude-files=".*\.h$" %s -o %t
 // RUN: FileCheck -check-prefix=JUST-C < %t %s
+// RUN: ln -sf %s %s.symlink.c
+// RUN: %clang -S -emit-llvm --coverage %s.symlink.c -o %t
+// RUN: FileCheck -check-prefix=ALL < %t %s
+// RUN: %clang -S -emit-llvm --coverage -fprofile-filter-files=".*\.symlink.c$" %s.symlink.c -o %t
+// RUN: FileCheck -check-prefix=NO-HEADER < %t %s
 
 #include "Inputs/code-coverage-filter1.h"
 #include "Inputs/code-coverage-filter2.h"



More information about the cfe-commits mailing list