|
| 1 | +// RUN: %check_clang_tidy %s abseil-duration-comparison %t |
| 2 | + |
| 3 | +// Mimic the implementation of absl::Duration |
| 4 | +namespace absl { |
| 5 | + |
| 6 | +class Duration {}; |
| 7 | +class Time{}; |
| 8 | + |
| 9 | +Duration Nanoseconds(long long); |
| 10 | +Duration Microseconds(long long); |
| 11 | +Duration Milliseconds(long long); |
| 12 | +Duration Seconds(long long); |
| 13 | +Duration Minutes(long long); |
| 14 | +Duration Hours(long long); |
| 15 | + |
| 16 | +#define GENERATE_DURATION_FACTORY_OVERLOADS(NAME) \ |
| 17 | + Duration NAME(float n); \ |
| 18 | + Duration NAME(double n); \ |
| 19 | + template <typename T> \ |
| 20 | + Duration NAME(T n); |
| 21 | + |
| 22 | +GENERATE_DURATION_FACTORY_OVERLOADS(Nanoseconds); |
| 23 | +GENERATE_DURATION_FACTORY_OVERLOADS(Microseconds); |
| 24 | +GENERATE_DURATION_FACTORY_OVERLOADS(Milliseconds); |
| 25 | +GENERATE_DURATION_FACTORY_OVERLOADS(Seconds); |
| 26 | +GENERATE_DURATION_FACTORY_OVERLOADS(Minutes); |
| 27 | +GENERATE_DURATION_FACTORY_OVERLOADS(Hours); |
| 28 | +#undef GENERATE_DURATION_FACTORY_OVERLOADS |
| 29 | + |
| 30 | +using int64_t = long long int; |
| 31 | + |
| 32 | +double ToDoubleHours(Duration d); |
| 33 | +double ToDoubleMinutes(Duration d); |
| 34 | +double ToDoubleSeconds(Duration d); |
| 35 | +double ToDoubleMilliseconds(Duration d); |
| 36 | +double ToDoubleMicroseconds(Duration d); |
| 37 | +double ToDoubleNanoseconds(Duration d); |
| 38 | +int64_t ToInt64Hours(Duration d); |
| 39 | +int64_t ToInt64Minutes(Duration d); |
| 40 | +int64_t ToInt64Seconds(Duration d); |
| 41 | +int64_t ToInt64Milliseconds(Duration d); |
| 42 | +int64_t ToInt64Microseconds(Duration d); |
| 43 | +int64_t ToInt64Nanoseconds(Duration d); |
| 44 | + |
| 45 | +// Relational Operators |
| 46 | +constexpr bool operator<(Duration lhs, Duration rhs); |
| 47 | +constexpr bool operator>(Duration lhs, Duration rhs); |
| 48 | +constexpr bool operator>=(Duration lhs, Duration rhs); |
| 49 | +constexpr bool operator<=(Duration lhs, Duration rhs); |
| 50 | +constexpr bool operator==(Duration lhs, Duration rhs); |
| 51 | +constexpr bool operator!=(Duration lhs, Duration rhs); |
| 52 | + |
| 53 | +// Additive Operators |
| 54 | +inline Time operator+(Time lhs, Duration rhs); |
| 55 | +inline Time operator+(Duration lhs, Time rhs); |
| 56 | +inline Time operator-(Time lhs, Duration rhs); |
| 57 | +inline Duration operator-(Time lhs, Time rhs); |
| 58 | + |
| 59 | +} // namespace absl |
| 60 | + |
| 61 | +void f() { |
| 62 | + double x; |
| 63 | + absl::Duration d1, d2; |
| 64 | + bool b; |
| 65 | + absl::Time t1, t2; |
| 66 | + |
| 67 | + // Check against the RHS |
| 68 | + b = x > absl::ToDoubleSeconds(d1); |
| 69 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 70 | + // CHECK-FIXES: absl::Seconds(x) > d1; |
| 71 | + b = x >= absl::ToDoubleSeconds(d1); |
| 72 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 73 | + // CHECK-FIXES: absl::Seconds(x) >= d1; |
| 74 | + b = x == absl::ToDoubleSeconds(d1); |
| 75 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 76 | + // CHECK-FIXES: absl::Seconds(x) == d1; |
| 77 | + b = x <= absl::ToDoubleSeconds(d1); |
| 78 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 79 | + // CHECK-FIXES: absl::Seconds(x) <= d1; |
| 80 | + b = x < absl::ToDoubleSeconds(d1); |
| 81 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 82 | + // CHECK-FIXES: absl::Seconds(x) < d1; |
| 83 | + b = x == absl::ToDoubleSeconds(t1 - t2); |
| 84 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 85 | + // CHECK-FIXES: absl::Seconds(x) == t1 - t2; |
| 86 | + b = absl::ToDoubleSeconds(d1) > absl::ToDoubleSeconds(d2); |
| 87 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 88 | + // CHECK-FIXES: d1 > d2; |
| 89 | + |
| 90 | + // Check against the LHS |
| 91 | + b = absl::ToDoubleSeconds(d1) < x; |
| 92 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 93 | + // CHECK-FIXES: d1 < absl::Seconds(x); |
| 94 | + b = absl::ToDoubleSeconds(d1) <= x; |
| 95 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 96 | + // CHECK-FIXES: d1 <= absl::Seconds(x); |
| 97 | + b = absl::ToDoubleSeconds(d1) == x; |
| 98 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 99 | + // CHECK-FIXES: d1 == absl::Seconds(x); |
| 100 | + b = absl::ToDoubleSeconds(d1) >= x; |
| 101 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 102 | + // CHECK-FIXES: d1 >= absl::Seconds(x); |
| 103 | + b = absl::ToDoubleSeconds(d1) > x; |
| 104 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 105 | + // CHECK-FIXES: d1 > absl::Seconds(x); |
| 106 | + |
| 107 | + // Comparison against zero |
| 108 | + b = absl::ToDoubleSeconds(d1) < 0.0; |
| 109 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 110 | + // CHECK-FIXES: d1 < absl::ZeroDuration(); |
| 111 | + b = absl::ToDoubleSeconds(d1) < 0; |
| 112 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 113 | + // CHECK-FIXES: d1 < absl::ZeroDuration(); |
| 114 | + |
| 115 | + // Scales other than Seconds |
| 116 | + b = x > absl::ToDoubleMicroseconds(d1); |
| 117 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 118 | + // CHECK-FIXES: absl::Microseconds(x) > d1; |
| 119 | + b = x >= absl::ToDoubleMilliseconds(d1); |
| 120 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 121 | + // CHECK-FIXES: absl::Milliseconds(x) >= d1; |
| 122 | + b = x == absl::ToDoubleNanoseconds(d1); |
| 123 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 124 | + // CHECK-FIXES: absl::Nanoseconds(x) == d1; |
| 125 | + b = x <= absl::ToDoubleMinutes(d1); |
| 126 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 127 | + // CHECK-FIXES: absl::Minutes(x) <= d1; |
| 128 | + b = x < absl::ToDoubleHours(d1); |
| 129 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 130 | + // CHECK-FIXES: absl::Hours(x) < d1; |
| 131 | + |
| 132 | + // Integer comparisons |
| 133 | + b = x > absl::ToInt64Microseconds(d1); |
| 134 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 135 | + // CHECK-FIXES: absl::Microseconds(x) > d1; |
| 136 | + b = x >= absl::ToInt64Milliseconds(d1); |
| 137 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 138 | + // CHECK-FIXES: absl::Milliseconds(x) >= d1; |
| 139 | + b = x == absl::ToInt64Nanoseconds(d1); |
| 140 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 141 | + // CHECK-FIXES: absl::Nanoseconds(x) == d1; |
| 142 | + b = x == absl::ToInt64Seconds(d1); |
| 143 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 144 | + // CHECK-FIXES: absl::Seconds(x) == d1; |
| 145 | + b = x <= absl::ToInt64Minutes(d1); |
| 146 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 147 | + // CHECK-FIXES: absl::Minutes(x) <= d1; |
| 148 | + b = x < absl::ToInt64Hours(d1); |
| 149 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 150 | + // CHECK-FIXES: absl::Hours(x) < d1; |
| 151 | + |
| 152 | + // Other abseil-duration checks folded into this one |
| 153 | + b = static_cast<double>(5) > absl::ToDoubleSeconds(d1); |
| 154 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 155 | + // CHECK-FIXES: absl::Seconds(5) > d1; |
| 156 | + b = double(5) > absl::ToDoubleSeconds(d1); |
| 157 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 158 | + // CHECK-FIXES: absl::Seconds(5) > d1; |
| 159 | + b = float(5) > absl::ToDoubleSeconds(d1); |
| 160 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 161 | + // CHECK-FIXES: absl::Seconds(5) > d1; |
| 162 | + b = ((double)5) > absl::ToDoubleSeconds(d1); |
| 163 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 164 | + // CHECK-FIXES: absl::Seconds(5) > d1; |
| 165 | + b = 5.0 > absl::ToDoubleSeconds(d1); |
| 166 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 167 | + // CHECK-FIXES: absl::Seconds(5) > d1; |
| 168 | + |
| 169 | + // A long expression |
| 170 | + bool some_condition; |
| 171 | + int very_very_very_very_long_variable_name; |
| 172 | + absl::Duration SomeDuration; |
| 173 | + if (some_condition && very_very_very_very_long_variable_name |
| 174 | + < absl::ToDoubleSeconds(SomeDuration)) { |
| 175 | + // CHECK-MESSAGES: [[@LINE-2]]:25: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 176 | + // CHECK-FIXES: if (some_condition && absl::Seconds(very_very_very_very_long_variable_name) < SomeDuration) { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + // A complex expression |
| 181 | + int y; |
| 182 | + b = (y + 5) * 10 > absl::ToDoubleMilliseconds(d1); |
| 183 | + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 184 | + // CHECK-FIXES: absl::Milliseconds((y + 5) * 10) > d1; |
| 185 | + |
| 186 | + // These should not match |
| 187 | + b = 6 < 4; |
| 188 | + |
| 189 | +#define TODOUBLE(x) absl::ToDoubleSeconds(x) |
| 190 | + b = 5.0 > TODOUBLE(d1); |
| 191 | +#undef TODOUBLE |
| 192 | +#define THIRTY 30.0 |
| 193 | + b = THIRTY > absl::ToDoubleSeconds(d1); |
| 194 | +#undef THIRTY |
| 195 | +} |
0 commit comments