[clang] [llvm] [sancov] Add -diff and -union options to compute set difference and union of sancov files (PR #171191)

Manuel Carrasco via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 06:24:10 PST 2025


================
@@ -1015,6 +1049,101 @@ static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
   }
 }
 
+static const char *bitnessToString(uint32_t Bitness) {
+  switch (Bitness) {
+  case Bitness64:
+    return "64-bit";
+  case Bitness32:
+    return "32-bit";
+  default:
+    fail("Unsupported bitness: " + std::to_string(Bitness));
+    return nullptr;
+  }
+}
+
+// Compute difference between two coverage files (A - B) and write to output
+// file.
+static void diffRawCoverage(const std::string &FileA, const std::string &FileB,
+                            const std::string &OutputFile) {
+  auto CovA = RawCoverage::read(FileA);
+  failIfError(CovA);
+
+  auto CovB = RawCoverage::read(FileB);
+  failIfError(CovB);
+
+  // Determine bitness from both files
+  ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErrA =
+      MemoryBuffer::getFile(FileA);
+  failIfError(BufOrErrA);
+  const FileHeader *HeaderA =
+      reinterpret_cast<const FileHeader *>(BufOrErrA.get()->getBufferStart());
+
+  ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErrB =
+      MemoryBuffer::getFile(FileB);
+  failIfError(BufOrErrB);
+  const FileHeader *HeaderB =
+      reinterpret_cast<const FileHeader *>(BufOrErrB.get()->getBufferStart());
+
+  // Warn if bitness differs
+  if (HeaderA->Bitness != HeaderB->Bitness) {
+    errs() << "WARNING: Input files have different bitness (File A: "
+           << bitnessToString(HeaderA->Bitness)
+           << ", File B: " << bitnessToString(HeaderB->Bitness)
+           << "). Using bitness from File A.\n";
----------------
mgcarrasco wrote:

Thanks. Done.

https://github.com/llvm/llvm-project/pull/171191


More information about the llvm-commits mailing list