From 46d3790d32be01c9a879c9ac4df0644d8cd1dfaa Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Tue, 14 Oct 2025 12:02:48 +0200 Subject: [PATCH 1/2] [NFC][support]Add operator- to TimeRecord A common use case for the Timer is to measure time duration between two points. Lack of operator- forced an non-intuitive two-step duration computation: get the end time, then decrement it by the start time. Now, as demonstrated on the example of `SyntaxCheckTimer` and `ExprEngineTimer` one can compute duration directly. --- .../StaticAnalyzer/Frontend/AnalysisConsumer.cpp | 14 +++++++------- llvm/include/llvm/Support/Timer.h | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp index 4efde59aab763..5ecaeea5ddf96 100644 --- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -757,9 +757,9 @@ void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode, ++NumFunctionsAnalyzedSyntaxOnly; if (SyntaxCheckTimer) { SyntaxCheckTimer->stopTimer(); - llvm::TimeRecord CheckerEndTime = SyntaxCheckTimer->getTotalTime(); - CheckerEndTime -= CheckerStartTime; - DisplayTime(CheckerEndTime); + llvm::TimeRecord CheckerDuration = + SyntaxCheckTimer->getTotalTime() - CheckerStartTime; + DisplayTime(CheckerDuration); if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) { AnalyzerTimers->clear(); } @@ -804,11 +804,11 @@ void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, Mgr->options.MaxNodesPerTopLevelFunction); if (ExprEngineTimer) { ExprEngineTimer->stopTimer(); - llvm::TimeRecord ExprEngineEndTime = ExprEngineTimer->getTotalTime(); - ExprEngineEndTime -= ExprEngineStartTime; + llvm::TimeRecord ExprEngineDuration = + ExprEngineTimer->getTotalTime() - ExprEngineStartTime; PathRunningTime.set(static_cast( - std::lround(ExprEngineEndTime.getWallTime() * 1000))); - DisplayTime(ExprEngineEndTime); + std::lround(ExprEngineDuration.getWallTime() * 1000))); + DisplayTime(ExprEngineDuration); if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) { AnalyzerTimers->clear(); } diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h index 40709d49db011..8412200e4c981 100644 --- a/llvm/include/llvm/Support/Timer.h +++ b/llvm/include/llvm/Support/Timer.h @@ -66,6 +66,11 @@ class TimeRecord { MemUsed -= RHS.MemUsed; InstructionsExecuted -= RHS.InstructionsExecuted; } + TimeRecord operator-(const TimeRecord &RHS) const { + TimeRecord R = *this; + R -= RHS; + return R; + } /// Print the current time record to \p OS, with a breakdown showing /// contributions to the \p Total time record. From c491d2d225e48f9c7da113f8c61253553b865ab4 Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Tue, 14 Oct 2025 13:30:15 +0200 Subject: [PATCH 2/2] Update llvm/include/llvm/Support/Timer.h --- llvm/include/llvm/Support/Timer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h index 8412200e4c981..0bcc459cbae98 100644 --- a/llvm/include/llvm/Support/Timer.h +++ b/llvm/include/llvm/Support/Timer.h @@ -71,6 +71,7 @@ class TimeRecord { R -= RHS; return R; } + // Feel free to add operator+ if you need it /// Print the current time record to \p OS, with a breakdown showing /// contributions to the \p Total time record.