File: duration-division.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (75 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// RUN: %check_clang_tidy %s abseil-duration-division %t

namespace absl {

class Duration {};

int operator/(Duration lhs, Duration rhs);

double FDivDuration(Duration num, Duration den);

}  // namespace absl

void TakesDouble(double);

#define MACRO_EQ(x, y) (x == y)
#define MACRO_DIVEQ(x,y,z) (x/y == z)
#define CHECK(x) (x)

void Positives() {
  absl::Duration d;

  const double num_double = d/d;
  // CHECK-MESSAGES: [[@LINE-1]]:30: warning: operator/ on absl::Duration objects performs integer division; did you mean to use FDivDuration()? [abseil-duration-division]
  // CHECK-FIXES: const double num_double = absl::FDivDuration(d, d);
  const float num_float = d/d;
  // CHECK-MESSAGES: [[@LINE-1]]:28: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: const float num_float = absl::FDivDuration(d, d);
  const auto SomeVal = 1.0 + d/d;
  // CHECK-MESSAGES: [[@LINE-1]]:31: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: const auto SomeVal = 1.0 + absl::FDivDuration(d, d);
  if (MACRO_EQ(d/d, 0.0)) {}
  // CHECK-MESSAGES: [[@LINE-1]]:17: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: if (MACRO_EQ(absl::FDivDuration(d, d), 0.0)) {}
  if (CHECK(MACRO_EQ(d/d, 0.0))) {}
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: if (CHECK(MACRO_EQ(absl::FDivDuration(d, d), 0.0))) {}

  // This one generates a message, but no fix.
  if (MACRO_DIVEQ(d, d, 0.0)) {}
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: if (MACRO_DIVEQ(d, d, 0.0)) {}
 
  TakesDouble(d/d);
  // CHECK-MESSAGES: [[@LINE-1]]:16: warning: operator/ on absl::Duration objects
  // CHECK-FIXES: TakesDouble(absl::FDivDuration(d, d));
}

void TakesInt(int);
template <class T>
void TakesGeneric(T);

void Negatives() {
  absl::Duration d;
  const int num_int = d/d;
  const long num_long = d/d;
  const short num_short = d/d;
  const char num_char = d/d;
  const auto num_auto = d/d;
  const auto SomeVal = 1 + d/d;

  TakesInt(d/d);
  TakesGeneric(d/d);
  // Explicit cast should disable the warning.
  const double num_cast1 = static_cast<double>(d/d);
  const double num_cast2 = (double)(d/d);
}

template <class T>
double DoubleDivision(T t1, T t2) {return t1/t2;}

//This also won't trigger a warning
void TemplateDivision() {
  absl::Duration d;
  DoubleDivision(d, d);
}