• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %check_clang_tidy -std=c++11-or-later %s abseil-time-subtraction %t -- -- -I %S/Inputs
2 // FIXME: Fix the checker to work in C++17 mode.
3 
4 #include "absl/time/time.h"
5 
6 void g(absl::Duration d);
7 
f()8 void f() {
9   absl::Time t;
10   int x, y;
11   absl::Duration d;
12 
13   d = absl::Hours(absl::ToUnixHours(t) - x);
14   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
15   // CHECK-FIXES: d = (t - absl::FromUnixHours(x));
16   d = absl::Minutes(absl::ToUnixMinutes(t) - x);
17   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
18   // CHECK-FIXES: d = (t - absl::FromUnixMinutes(x));
19   d = absl::Seconds(absl::ToUnixSeconds(t) - x);
20   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
21   // CHECK-FIXES: d = (t - absl::FromUnixSeconds(x));
22   d = absl::Milliseconds(absl::ToUnixMillis(t) - x);
23   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
24   // CHECK-FIXES: d = (t - absl::FromUnixMillis(x));
25   d = absl::Microseconds(absl::ToUnixMicros(t) - x);
26   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
27   // CHECK-FIXES: d = (t - absl::FromUnixMicros(x));
28   d = absl::Nanoseconds(absl::ToUnixNanos(t) - x);
29   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
30   // CHECK-FIXES: d = (t - absl::FromUnixNanos(x));
31 
32   y = x - absl::ToUnixHours(t);
33   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
34   // CHECK-FIXES: y = absl::ToInt64Hours(absl::FromUnixHours(x) - t);
35   y = x - absl::ToUnixMinutes(t);
36   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
37   // CHECK-FIXES: y = absl::ToInt64Minutes(absl::FromUnixMinutes(x) - t);
38   y = x - absl::ToUnixSeconds(t);
39   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
40   // CHECK-FIXES: y = absl::ToInt64Seconds(absl::FromUnixSeconds(x) - t);
41   y = x - absl::ToUnixMillis(t);
42   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
43   // CHECK-FIXES: y = absl::ToInt64Milliseconds(absl::FromUnixMillis(x) - t);
44   y = x - absl::ToUnixMicros(t);
45   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
46   // CHECK-FIXES: y = absl::ToInt64Microseconds(absl::FromUnixMicros(x) - t);
47   y = x - absl::ToUnixNanos(t);
48   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
49   // CHECK-FIXES: y = absl::ToInt64Nanoseconds(absl::FromUnixNanos(x) - t);
50 
51   // Check parenthesis placement
52   d = 5 * absl::Seconds(absl::ToUnixSeconds(t) - x);
53   // CHECK-MESSAGES: [[@LINE-1]]:11: warning: perform subtraction in the time domain [abseil-time-subtraction]
54   // CHECK-FIXES: d = 5 * (t - absl::FromUnixSeconds(x));
55   d = absl::Seconds(absl::ToUnixSeconds(t) - x) / 5;
56   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
57   // CHECK-FIXES: d = (t - absl::FromUnixSeconds(x)) / 5;
58 
59   // No extra parens around arguments
60   g(absl::Seconds(absl::ToUnixSeconds(t) - x));
61   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: perform subtraction in the time domain [abseil-time-subtraction]
62   // CHECK-FIXES: g(t - absl::FromUnixSeconds(x));
63   g(absl::Seconds(x - absl::ToUnixSeconds(t)));
64   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: perform subtraction in the time domain [abseil-time-subtraction]
65   // CHECK-FIXES: g(absl::FromUnixSeconds(x) - t);
66 
67   // More complex subexpressions
68   d = absl::Hours(absl::ToUnixHours(t) - 5 * x);
69   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the time domain [abseil-time-subtraction]
70   // CHECK-FIXES: d = (t - absl::FromUnixHours(5 * x));
71 
72   // These should not trigger; they are likely bugs
73   d = absl::Milliseconds(absl::ToUnixSeconds(t) - x);
74   d = absl::Seconds(absl::ToUnixMicros(t) - x);
75 
76   // Various macro scenarios
77 #define SUB(z, t1) z - absl::ToUnixSeconds(t1)
78   y = SUB(x, t);
79 #undef SUB
80 
81 #define MILLIS(t1) absl::ToUnixMillis(t1)
82   y = x - MILLIS(t);
83 #undef MILLIS
84 
85 #define HOURS(z) absl::Hours(z)
86   d = HOURS(absl::ToUnixHours(t) - x);
87 #undef HOURS
88 
89   // This should match the expression inside the macro invocation.
90 #define SECONDS(z) absl::Seconds(z)
91   d = SECONDS(x - absl::ToUnixSeconds(t));
92   // CHECK-MESSAGES: [[@LINE-1]]:15: warning: perform subtraction in the time domain [abseil-time-subtraction]
93   // CHECK-FIXES: SECONDS(absl::ToInt64Seconds(absl::FromUnixSeconds(x) - t))
94 #undef SECONDS
95 }
96 
97 template<typename T>
func(absl::Time t,T x)98 void func(absl::Time t, T x) {
99   absl::Duration d = absl::Seconds(absl::ToUnixSeconds(t) - x);
100   // CHECK-MESSAGES: [[@LINE-1]]:22: warning: perform subtraction in the time domain [abseil-time-subtraction]
101   // CHECK-FIXES: absl::Duration d = t - absl::FromUnixSeconds(x);
102 }
103 
g()104 void g() {
105   func(absl::Now(), 5);
106 }
107 
parens_in_return()108 absl::Duration parens_in_return() {
109   absl::Time t;
110   int x;
111 
112   return absl::Seconds(absl::ToUnixSeconds(t) - x);
113   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: perform subtraction in the time domain [abseil-time-subtraction]
114   // CHECK-FIXES: return t - absl::FromUnixSeconds(x);
115   return absl::Seconds(x - absl::ToUnixSeconds(t));
116   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: perform subtraction in the time domain [abseil-time-subtraction]
117   // CHECK-FIXES: return absl::FromUnixSeconds(x) - t;
118 }
119