From 381047c14dfbc3b89a5e87404cb7cf886f10c119 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sun, 31 Aug 2025 15:26:36 -0700
Subject: [PATCH 2/3] chrono: Fix build when using libc++ casting ns to
 system_clock::duration
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

libc++ from LLVM/Clang 21 release requires that time_point::operator+=
receive the exact duration type of the time_point. On many Linux configs,
system_clock::duration is microseconds, so doing:

  t += std::chrono::nanoseconds(...);

fails with:
  error: no viable overloaded '+='
  note: candidate function not viable: no known conversion from
        'duration<..., nano>' to 'const duration<..., micro>' for 1st argument

Cast the nanoseconds to system_clock::duration via duration_cast before
adding them. This builds with Clang 21 + libc++ and remains compatible
with libstdc++. Semantics are unchanged except for truncation to the
clock’s native resolution (which already applies).

No functional change intended.

Upstream-Status: Submitted [https://github.com/bpftrace/bpftrace/pull/4526]

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 src/bpftrace.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/bpftrace.cpp b/src/bpftrace.cpp
index a49a3efe..485267e0 100644
--- a/src/bpftrace.cpp
+++ b/src/bpftrace.cpp
@@ -992,11 +992,13 @@ std::chrono::time_point<std::chrono::system_clock> BPFtrace::resolve_timestamp(
           << "Cannot resolve timestamp due to failed boot time calculation";
     } else {
       t += std::chrono::seconds(boottime_->tv_sec);
-      t += std::chrono::nanoseconds(boottime_->tv_nsec);
+      t += std::chrono::duration_cast<std::chrono::system_clock::duration>(
+               std::chrono::nanoseconds(boottime_->tv_nsec));
     }
   }

-  t += std::chrono::nanoseconds(nsecs);
+  t += std::chrono::duration_cast<std::chrono::system_clock::duration>(
+           std::chrono::nanoseconds(nsecs));
   return t;
 }

