brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.3 KiB · 8f49e71 Raw
167 lines · cpp
1// RUN: %check_clang_tidy %s abseil-duration-comparison %t -- -- -I%S/Inputs2 3#include "absl/time/time.h"4 5void f() {6  double x;7  absl::Duration d1, d2;8  bool b;9  absl::Time t1, t2;10 11  // Check against the RHS12  b = x > absl::ToDoubleSeconds(d1);13  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]14  // CHECK-FIXES: b = absl::Seconds(x) > d1;15  b = x >= absl::ToDoubleSeconds(d1);16  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]17  // CHECK-FIXES: b = absl::Seconds(x) >= d1;18  b = x == absl::ToDoubleSeconds(d1);19  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]20  // CHECK-FIXES: b = absl::Seconds(x) == d1;21  b = x <= absl::ToDoubleSeconds(d1);22  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]23  // CHECK-FIXES: b = absl::Seconds(x) <= d1;24  b = x < absl::ToDoubleSeconds(d1);25  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]26  // CHECK-FIXES: b = absl::Seconds(x) < d1;27  b = x == absl::ToDoubleSeconds(t1 - t2);28  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]29  // CHECK-FIXES: b = absl::Seconds(x) == t1 - t2;30  b = absl::ToDoubleSeconds(d1) > absl::ToDoubleSeconds(d2);31  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]32  // CHECK-FIXES: b = d1 > d2;33 34  // Check against the LHS35  b = absl::ToDoubleSeconds(d1) < x;36  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]37  // CHECK-FIXES: b = d1 < absl::Seconds(x);38  b = absl::ToDoubleSeconds(d1) <= x;39  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]40  // CHECK-FIXES: b = d1 <= absl::Seconds(x);41  b = absl::ToDoubleSeconds(d1) == x;42  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]43  // CHECK-FIXES: b = d1 == absl::Seconds(x);44  b = absl::ToDoubleSeconds(d1) >= x;45  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]46  // CHECK-FIXES: b = d1 >= absl::Seconds(x);47  b = absl::ToDoubleSeconds(d1) > x;48  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]49  // CHECK-FIXES: b = d1 > absl::Seconds(x);50 51  // Comparison against zero52  b = absl::ToDoubleSeconds(d1) < 0.0;53  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]54  // CHECK-FIXES: b = d1 < absl::ZeroDuration();55  b = absl::ToDoubleSeconds(d1) < 0;56  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]57  // CHECK-FIXES: b = d1 < absl::ZeroDuration();58 59  // Scales other than Seconds60  b = x > absl::ToDoubleMicroseconds(d1);61  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]62  // CHECK-FIXES: b = absl::Microseconds(x) > d1;63  b = x >= absl::ToDoubleMilliseconds(d1);64  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]65  // CHECK-FIXES: b = absl::Milliseconds(x) >= d1;66  b = x == absl::ToDoubleNanoseconds(d1);67  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]68  // CHECK-FIXES: b = absl::Nanoseconds(x) == d1;69  b = x <= absl::ToDoubleMinutes(d1);70  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]71  // CHECK-FIXES: b = absl::Minutes(x) <= d1;72  b = x < absl::ToDoubleHours(d1);73  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]74  // CHECK-FIXES: b = absl::Hours(x) < d1;75 76  // Integer comparisons77  b = x > absl::ToInt64Microseconds(d1);78  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]79  // CHECK-FIXES: b = absl::Microseconds(x) > d1;80  b = x >= absl::ToInt64Milliseconds(d1);81  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]82  // CHECK-FIXES: b = absl::Milliseconds(x) >= d1;83  b = x == absl::ToInt64Nanoseconds(d1);84  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]85  // CHECK-FIXES: b = absl::Nanoseconds(x) == d1;86  b = x == absl::ToInt64Seconds(d1);87  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]88  // CHECK-FIXES: b = absl::Seconds(x) == d1;89  b = x <= absl::ToInt64Minutes(d1);90  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]91  // CHECK-FIXES: b = absl::Minutes(x) <= d1;92  b = x < absl::ToInt64Hours(d1);93  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]94  // CHECK-FIXES: b = absl::Hours(x) < d1;95 96  // Other abseil-duration checks folded into this one97  b = static_cast<double>(5) > absl::ToDoubleSeconds(d1);98  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]99  // CHECK-FIXES: b = absl::Seconds(5) > d1;100  b = double(5) > absl::ToDoubleSeconds(d1);101  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]102  // CHECK-FIXES: b = absl::Seconds(5) > d1;103  b = float(5) > absl::ToDoubleSeconds(d1);104  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]105  // CHECK-FIXES: b = absl::Seconds(5) > d1;106  b = ((double)5) > absl::ToDoubleSeconds(d1);107  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]108  // CHECK-FIXES: b = absl::Seconds(5) > d1;109  b = 5.0 > absl::ToDoubleSeconds(d1);110  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]111  // CHECK-FIXES: b = absl::Seconds(5) > d1;112 113  // A long expression114  bool some_condition;115  int very_very_very_very_long_variable_name;116  absl::Duration SomeDuration;117  if (some_condition && very_very_very_very_long_variable_name118     < absl::ToDoubleSeconds(SomeDuration)) {119  // CHECK-MESSAGES: [[@LINE-2]]:25: warning: perform comparison in the duration domain [abseil-duration-comparison]120  // CHECK-FIXES: if (some_condition && absl::Seconds(very_very_very_very_long_variable_name) < SomeDuration) {121    return;122  }123 124  // A complex expression125  int y;126  b = (y + 5) * 10 > absl::ToDoubleMilliseconds(d1);127  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]128  // CHECK-FIXES: b = absl::Milliseconds((y + 5) * 10) > d1;129 130  // We should still transform the expression inside this macro invocation131#define VALUE_IF(v, e) v ? (e) : 0132  int a = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1));133  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: perform comparison in the duration domain [abseil-duration-comparison]134  // CHECK-FIXES: int a = VALUE_IF(1, absl::Seconds(5) > d1);135#undef VALUE_IF136 137#define VALUE_IF_2(e) (e)138#define VALUE_IF(v, e) v ? VALUE_IF_2(e) : VALUE_IF_2(0)139  int a2 = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1));140  // CHECK-MESSAGES: [[@LINE-1]]:24: warning: perform comparison in the duration domain [abseil-duration-comparison]141  // CHECK-FIXES: int a2 = VALUE_IF(1, absl::Seconds(5) > d1);142#undef VALUE_IF143#undef VALUE_IF_2144 145#define VALUE_IF_2(e) (e)146#define VALUE_IF(v, e, type) (v ? VALUE_IF_2(absl::To##type##Seconds(e)) : 0)147  int a3 = VALUE_IF(1, d1, Double);148#undef VALUE_IF149#undef VALUE_IF_2150 151#define VALUE_IF_2(e) (e)152#define VALUE_IF(v, e, type) (v ? (5 > VALUE_IF_2(absl::To##type##Seconds(e))) : 0)153  int a4 = VALUE_IF(1, d1, Double);154#undef VALUE_IF155#undef VALUE_IF_2156 157  // These should not match158  b = 6 < 4;159 160#define TODOUBLE(x) absl::ToDoubleSeconds(x)161  b = 5.0 > TODOUBLE(d1);162#undef TODOUBLE163#define THIRTY 30.0164  b = THIRTY > absl::ToDoubleSeconds(d1);165#undef THIRTY166}167