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

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 01:24:08 PST 2025


================
@@ -277,6 +284,33 @@ raw_ostream &operator<<(raw_ostream &OS, const RawCoverage &CoverageData) {
   return OS;
 }
 
+// Write coverage addresses in binary format.
+void RawCoverage::write(const std::string &FileName,
+                        const RawCoverage &Coverage, FileHeader Header) {
+  std::error_code EC;
+  raw_fd_ostream OS(FileName, EC, sys::fs::OF_None);
+  failIfError(EC);
+
+  OS.write(reinterpret_cast<const char *>(&Header), sizeof(Header));
+
+  switch (Header.Bitness) {
+  case Bitness64:
+    for (auto Addr : *Coverage.Addrs) {
+      uint64_t Addr64 = Addr;
+      OS.write(reinterpret_cast<const char *>(&Addr64), sizeof(Addr64));
+    }
+    break;
+  case Bitness32:
+    for (auto Addr : *Coverage.Addrs) {
+      uint32_t Addr32 = static_cast<uint32_t>(Addr);
+      OS.write(reinterpret_cast<const char *>(&Addr32), sizeof(Addr32));
+    }
----------------
zhaoqi5 wrote:

Each uint64_t `Addr` is static_cast to uint32_t before writing, which may cause silent truncation when `Addr` is lager than uint32. 

I am not sure if this is expected. If so, it is better add tests. Otherwise, a warning or error message when the truncation indeed happens is great. Or maybe add an `--allow-truncate` option?

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


More information about the llvm-commits mailing list