Skip to content

Conversation

@necto
Copy link
Contributor

@necto necto commented Oct 14, 2025

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.

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.
@necto necto requested review from NagyDonat and steakhal October 14, 2025 10:22
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer llvm:support labels Oct 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Arseniy Zaostrovnykh (necto)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/163361.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp (+7-7)
  • (modified) llvm/include/llvm/Support/Timer.h (+5)
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<unsigned>(
-        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.

@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-llvm-support

Author: Arseniy Zaostrovnykh (necto)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/163361.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp (+7-7)
  • (modified) llvm/include/llvm/Support/Timer.h (+5)
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<unsigned>(
-        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.

@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-clang

Author: Arseniy Zaostrovnykh (necto)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/163361.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp (+7-7)
  • (modified) llvm/include/llvm/Support/Timer.h (+5)
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<unsigned>(
-        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.

@necto
Copy link
Contributor Author

necto commented Oct 14, 2025

This NFC fixes a quirk observed in #162839

Copy link
Contributor

@NagyDonat NagyDonat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for creating this patch!

@steakhal
Copy link
Contributor

@kazutakahirata Do you know who should review this PR?

@necto necto enabled auto-merge (squash) October 21, 2025 12:29
@steakhal steakhal changed the title [NFC][support]Add operator- to TimeRecord [NFC][support] Add operator- to TimeRecord Oct 21, 2025
@necto necto merged commit 14af435 into llvm:main Oct 21, 2025
10 checks passed
@llvm-ci

This comment was marked as off-topic.

@necto necto deleted the az/duration branch October 21, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category llvm:support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants