[llvm] [llvm-cov] Fix semi-random coverage report file order (PR #136517)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 20 16:10:07 PDT 2025


https://github.com/L3337 created https://github.com/llvm/llvm-project/pull/136517

On Linux, ext4, files are enumerated in an order other than case-insensitive sorted.  This may not be a problem on Windows and macOS.

Ensure that the files in the report are sorted using a case-insensitive string comparison.

>From de54a328797370c5b35ee2d48c4a12e716facc05 Mon Sep 17 00:00:00 2001
From: Anon McHacker <anonymous at noreply.L3337.github.com>
Date: Sun, 20 Apr 2025 16:03:05 -0700
Subject: [PATCH] [llvm-cov] Fix semi-random coverage report file order

On Linux, ext4, files are enumerated in an order other than
case-insensitive sorted.  This may not be a problem on Windows and
macOS.

Ensure that the files in the report are sorted using a case-insensitive
string comparison.
---
 llvm/tools/llvm-cov/CodeCoverage.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index c828e25de4b02..44d30a74a5dc1 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -1144,7 +1144,17 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
       if (!IgnoreFilenameFilters.matchesFilename(Filename))
         SourceFiles.push_back(std::string(Filename));
     }
-
+  // Some file systems return directories and files in a random order,
+  // ensure case-insensitive sorting
+  std::sort(
+    SourceFiles.begin(),
+    SourceFiles.end(),
+    [](const auto& A, const auto& B) {
+      StringRef Sra(A);
+      StringRef Srb(B);
+      return Sra.compare_insensitive(Srb) == -1 ? true : false;
+    }
+  );
   // Create an index out of the source files.
   if (ViewOpts.hasOutputDirectory()) {
     if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {



More information about the llvm-commits mailing list