1 // RUN: %check_clang_tidy %s abseil-time-comparison %t -- -- -I%S/Inputs
2 
3 #include "absl/time/time.h"
4 
f()5 void f() {
6   double x;
7   absl::Duration d1, d2;
8   bool b;
9   absl::Time t1, t2;
10 
11   // Check against the RHS
12   b = x > absl::ToUnixSeconds(t1);
13   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
14   // CHECK-FIXES: absl::FromUnixSeconds(x) > t1;
15   b = x >= absl::ToUnixSeconds(t1);
16   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
17   // CHECK-FIXES: absl::FromUnixSeconds(x) >= t1;
18   b = x == absl::ToUnixSeconds(t1);
19   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
20   // CHECK-FIXES: absl::FromUnixSeconds(x) == t1;
21   b = x <= absl::ToUnixSeconds(t1);
22   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
23   // CHECK-FIXES: absl::FromUnixSeconds(x) <= t1;
24   b = x < absl::ToUnixSeconds(t1);
25   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
26   // CHECK-FIXES: absl::FromUnixSeconds(x) < t1;
27   b = x == absl::ToUnixSeconds(t1 - d2);
28   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
29   // CHECK-FIXES: absl::FromUnixSeconds(x) == t1 - d2;
30   b = absl::ToUnixSeconds(t1) > absl::ToUnixSeconds(t2);
31   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
32   // CHECK-FIXES: t1 > t2;
33 
34   // Check against the LHS
35   b = absl::ToUnixSeconds(t1) < x;
36   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
37   // CHECK-FIXES: t1 < absl::FromUnixSeconds(x);
38   b = absl::ToUnixSeconds(t1) <= x;
39   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
40   // CHECK-FIXES: t1 <= absl::FromUnixSeconds(x);
41   b = absl::ToUnixSeconds(t1) == x;
42   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
43   // CHECK-FIXES: t1 == absl::FromUnixSeconds(x);
44   b = absl::ToUnixSeconds(t1) >= x;
45   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
46   // CHECK-FIXES: t1 >= absl::FromUnixSeconds(x);
47   b = absl::ToUnixSeconds(t1) > x;
48   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
49   // CHECK-FIXES: t1 > absl::FromUnixSeconds(x);
50 
51   // Comparison against zero
52   b = absl::ToUnixSeconds(t1) < 0.0;
53   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
54   // CHECK-FIXES: t1 < absl::UnixEpoch();
55   b = absl::ToUnixSeconds(t1) < 0;
56   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
57   // CHECK-FIXES: t1 < absl::UnixEpoch();
58 
59   // Scales other than Seconds
60   b = x > absl::ToUnixMicros(t1);
61   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
62   // CHECK-FIXES: absl::FromUnixMicros(x) > t1;
63   b = x >= absl::ToUnixMillis(t1);
64   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
65   // CHECK-FIXES: absl::FromUnixMillis(x) >= t1;
66   b = x == absl::ToUnixNanos(t1);
67   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
68   // CHECK-FIXES: absl::FromUnixNanos(x) == t1;
69   b = x <= absl::ToUnixMinutes(t1);
70   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
71   // CHECK-FIXES: absl::FromUnixMinutes(x) <= t1;
72   b = x < absl::ToUnixHours(t1);
73   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
74   // CHECK-FIXES: absl::FromUnixHours(x) < t1;
75 
76   // A long expression
77   bool some_condition;
78   int very_very_very_very_long_variable_name;
79   absl::Time SomeTime;
80   if (some_condition && very_very_very_very_long_variable_name
81      < absl::ToUnixSeconds(SomeTime)) {
82   // CHECK-MESSAGES: [[@LINE-2]]:25: warning: perform comparison in the time domain [abseil-time-comparison]
83   // CHECK-FIXES: if (some_condition && absl::FromUnixSeconds(very_very_very_very_long_variable_name) < SomeTime) {
84     return;
85   }
86 
87   // A complex expression
88   int y;
89   b = (y + 5) * 10 > absl::ToUnixMillis(t1);
90   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the time domain [abseil-time-comparison]
91   // CHECK-FIXES: absl::FromUnixMillis((y + 5) * 10) > t1;
92 
93   // We should still transform the expression inside this macro invocation
94 #define VALUE_IF(v, e) v ? (e) : 0
95   int a = VALUE_IF(1, 5 > absl::ToUnixSeconds(t1));
96   // CHECK-MESSAGES: [[@LINE-1]]:23: warning: perform comparison in the time domain [abseil-time-comparison]
97   // CHECK-FIXES: VALUE_IF(1, absl::FromUnixSeconds(5) > t1);
98 #undef VALUE_IF
99 
100 #define VALUE_IF_2(e) (e)
101 #define VALUE_IF(v, e) v ? VALUE_IF_2(e) : VALUE_IF_2(0)
102   int a2 = VALUE_IF(1, 5 > absl::ToUnixSeconds(t1));
103   // CHECK-MESSAGES: [[@LINE-1]]:24: warning: perform comparison in the time domain [abseil-time-comparison]
104   // CHECK-FIXES: VALUE_IF(1, absl::FromUnixSeconds(5) > t1);
105 #undef VALUE_IF
106 #undef VALUE_IF_2
107 
108 #define VALUE_IF_2(e) (e)
109 #define VALUE_IF(v, e, type) (v ? VALUE_IF_2(absl::To##type##Seconds(e)) : 0)
110   int a3 = VALUE_IF(1, t1, Unix);
111 #undef VALUE_IF
112 #undef VALUE_IF_2
113 
114 #define VALUE_IF_2(e) (e)
115 #define VALUE_IF(v, e, type) (v ? (5 > VALUE_IF_2(absl::To##type##Seconds(e))) : 0)
116   int a4 = VALUE_IF(1, t1, Unix);
117 #undef VALUE_IF
118 #undef VALUE_IF_2
119 
120   // These should not match
121   b = 6 < 4;
122 
123 #define TODOUBLE(x) absl::ToUnixSeconds(x)
124   b = 5.0 > TODOUBLE(t1);
125 #undef TODOUBLE
126 #define THIRTY 30.0
127   b = THIRTY > absl::ToUnixSeconds(t1);
128 #undef THIRTY
129 }
130