File: performance-type-promotion-in-math-fn.cpp

package info (click to toggle)
llvm-toolchain-6.0 1%3A6.0.1-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 598,080 kB
  • sloc: cpp: 3,046,253; ansic: 595,057; asm: 271,965; python: 128,926; objc: 106,554; sh: 21,906; lisp: 10,191; pascal: 6,094; ml: 5,544; perl: 5,265; makefile: 2,227; cs: 2,027; xml: 686; php: 212; csh: 117
file content (316 lines) | stat: -rw-r--r-- 11,201 bytes parent folder | download | duplicates (26)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// RUN: %check_clang_tidy %s performance-type-promotion-in-math-fn %t

// CHECK-FIXES: #include <cmath>

double acos(double);
double acosh(double);
double asin(double);
double asinh(double);
double atan2(double, double);
double atan(double);
double atanh(double);
double cbrt(double);
double ceil(double);
double copysign(double, double);
double cos(double);
double cosh(double);
double erfc(double);
double erf(double);
double exp2(double);
double exp(double);
double expm1(double);
double fabs(double);
double fdim(double, double);
double floor(double);
double fma(double, double, double);
double fmax(double, double);
double fmin(double, double);
double fmod(double, double);
double frexp(double, int *);
double hypot(double, double);
double ilogb(double);
double ldexp(double, double);
double lgamma(double);
long long llrint(double);
double log10(double);
double log1p(double);
double log2(double);
double logb(double);
double log(double);
long lrint(double);
double modf(double);
double nearbyint(double);
double nextafter(double, double);
double nexttoward(double, long double);
double pow(double, double);
double remainder(double, double);
double remquo(double, double, int *);
double rint(double);
double round(double);
double scalbln(double, long);
double scalbn(double, int);
double sin(double);
double sinh(double);
double sqrt(double);
double tan(double);
double tanh(double);
double tgamma(double);
double trunc(double);
long long llround(double);
long lround(double);

void check_all_fns() {
  float a, b, c;
  int i;
  long l;
  int *int_ptr;

  acos(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'acos' promotes float to double [performance-type-promotion-in-math-fn]
  // CHECK-FIXES: {{^}}  std::acos(a);{{$}}
  acosh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'acosh'
  // CHECK-FIXES: {{^}}  std::acosh(a);{{$}}
  asin(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'asin'
  // CHECK-FIXES: {{^}}  std::asin(a);{{$}}
  asinh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'asinh'
  // CHECK-FIXES: {{^}}  std::asinh(a);{{$}}
  atan2(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'atan2'
  // CHECK-FIXES: {{^}}  std::atan2(a, b);{{$}}
  atan(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'atan'
  // CHECK-FIXES: {{^}}  std::atan(a);{{$}}
  atanh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'atanh'
  // CHECK-FIXES: {{^}}  std::atanh(a);{{$}}
  cbrt(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'cbrt'
  // CHECK-FIXES: {{^}}  std::cbrt(a);{{$}}
  ceil(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'ceil'
  // CHECK-FIXES: {{^}}  std::ceil(a);{{$}}
  copysign(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'copysign'
  // CHECK-FIXES: {{^}}  std::copysign(a, b);{{$}}
  cos(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'cos'
  // CHECK-FIXES: {{^}}  std::cos(a);{{$}}
  cosh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'cosh'
  // CHECK-FIXES: {{^}}  std::cosh(a);{{$}}
  erf(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'erf'
  // CHECK-FIXES: {{^}}  std::erf(a);{{$}}
  erfc(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'erfc'
  // CHECK-FIXES: {{^}}  std::erfc(a);{{$}}
  exp2(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'exp2'
  // CHECK-FIXES: {{^}}  std::exp2(a);{{$}}
  exp(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'exp'
  // CHECK-FIXES: {{^}}  std::exp(a);{{$}}
  expm1(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'expm1'
  // CHECK-FIXES: {{^}}  std::expm1(a);{{$}}
  fabs(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fabs'
  // CHECK-FIXES: {{^}}  std::fabs(a);{{$}}
  fdim(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fdim'
  // CHECK-FIXES: {{^}}  std::fdim(a, b);{{$}}
  floor(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'floor'
  // CHECK-FIXES: {{^}}  std::floor(a);{{$}}
  fma(a, b, c);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fma'
  // CHECK-FIXES: {{^}}  std::fma(a, b, c);{{$}}
  fmax(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fmax'
  // CHECK-FIXES: {{^}}  std::fmax(a, b);{{$}}
  fmin(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fmin'
  // CHECK-FIXES: {{^}}  std::fmin(a, b);{{$}}
  fmod(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'fmod'
  // CHECK-FIXES: {{^}}  std::fmod(a, b);{{$}}
  frexp(a, int_ptr);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'frexp'
  // CHECK-FIXES: {{^}}  std::frexp(a, int_ptr);{{$}}
  hypot(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'hypot'
  // CHECK-FIXES: {{^}}  std::hypot(a, b);{{$}}
  ilogb(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'ilogb'
  // CHECK-FIXES: {{^}}  std::ilogb(a);{{$}}
  ldexp(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'ldexp'
  // CHECK-FIXES: {{^}}  std::ldexp(a, b);{{$}}
  lgamma(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'lgamma'
  // CHECK-FIXES: {{^}}  std::lgamma(a);{{$}}
  llrint(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'llrint'
  // CHECK-FIXES: {{^}}  std::llrint(a);{{$}}
  llround(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'llround'
  // CHECK-FIXES: {{^}}  std::llround(a);{{$}}
  log10(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'log10'
  // CHECK-FIXES: {{^}}  std::log10(a);{{$}}
  log1p(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'log1p'
  // CHECK-FIXES: {{^}}  std::log1p(a);{{$}}
  log2(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'log2'
  // CHECK-FIXES: {{^}}  std::log2(a);{{$}}
  log(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'log'
  // CHECK-FIXES: {{^}}  std::log(a);{{$}}
  logb(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'logb'
  // CHECK-FIXES: {{^}}  std::logb(a);{{$}}
  lrint(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'lrint'
  // CHECK-FIXES: {{^}}  std::lrint(a);{{$}}
  lround(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'lround'
  // CHECK-FIXES: {{^}}  std::lround(a);{{$}}
  nearbyint(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nearbyint'
  // CHECK-FIXES: {{^}}  std::nearbyint(a);{{$}}
  nextafter(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nextafter'
  // CHECK-FIXES: {{^}}  std::nextafter(a, b);{{$}}
  nexttoward(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nexttoward'
  // CHECK-FIXES: {{^}}  std::nexttoward(a, b);{{$}}
  pow(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'pow'
  // CHECK-FIXES: {{^}}  std::pow(a, b);{{$}}
  remainder(a, b);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'remainder'
  // CHECK-FIXES: {{^}}  std::remainder(a, b);{{$}}
  remquo(a, b, int_ptr);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'remquo'
  // CHECK-FIXES: {{^}}  std::remquo(a, b, int_ptr);{{$}}
  rint(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'rint'
  // CHECK-FIXES: {{^}}  std::rint(a);{{$}}
  round(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'round'
  // CHECK-FIXES: {{^}}  std::round(a);{{$}}
  scalbln(a, l);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbln'
  // CHECK-FIXES: {{^}}  std::scalbln(a, l);{{$}}
  scalbn(a, i);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbn'
  // CHECK-FIXES: {{^}}  std::scalbn(a, i);{{$}}
  sin(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'sin'
  // CHECK-FIXES: {{^}}  std::sin(a);{{$}}
  sinh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'sinh'
  // CHECK-FIXES: {{^}}  std::sinh(a);{{$}}
  sqrt(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'sqrt'
  // CHECK-FIXES: {{^}}  std::sqrt(a);{{$}}
  tan(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'tan'
  // CHECK-FIXES: {{^}}  std::tan(a);{{$}}
  tanh(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'tanh'
  // CHECK-FIXES: {{^}}  std::tanh(a);{{$}}
  tgamma(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'tgamma'
  // CHECK-FIXES: {{^}}  std::tgamma(a);{{$}}
  trunc(a);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'trunc'
  // CHECK-FIXES: {{^}}  std::trunc(a);{{$}}
}

// nexttoward/nexttowardf are weird -- the second param is always long double.
// So we warn if the first arg is a float, regardless of what the second arg is.
void check_nexttoward() {
  nexttoward(0.f, 0);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nexttoward'
  // CHECK-FIXES: {{^}}  std::nexttoward(0.f, 0);{{$}}
  nexttoward(0.f, 0l);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nexttoward'
  // CHECK-FIXES: {{^}}  std::nexttoward(0.f, 0l);{{$}}
  nexttoward(0.f, 0.f);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nexttoward'
  // CHECK-FIXES: {{^}}  std::nexttoward(0.f, 0.f);{{$}}
  nexttoward(0.f, 0.);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'nexttoward'
  // CHECK-FIXES: {{^}}  std::nexttoward(0.f, 0.);{{$}}

  // No warnings for these.
  nexttoward(0., 0);
  nexttoward(0., 0.f);
  nexttoward(0., 0.);
}

// The second parameter to scalbn and scalbnf is an int, so we don't care what
// type you pass as that argument; we warn iff the first argument is a float.
void check_scalbn() {
  scalbn(0.f, 0);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbn'
  // CHECK-FIXES: {{^}}  std::scalbn(0.f, 0);{{$}}
  scalbn(0.f, static_cast<char>(0));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbn'
  // CHECK-FIXES: {{^}}  std::scalbn(0.f, static_cast<char>(0));{{$}}

  // No warnings for these.
  scalbn(0., 0);
  scalbn(0., static_cast<char>(0));
}

// scalbln/scalblnf are like scalbn/scalbnf except their second arg is a long.
// Again, doesn't matter what we pass for the second arg; we warn iff the first
// arg is a float.
void check_scalbln() {
  scalbln(0.f, 0);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbln'
  // CHECK-FIXES: {{^}}  std::scalbln(0.f, 0);{{$}}
  scalbln(0.f, 0l);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'scalbln'
  // CHECK-FIXES: {{^}}  std::scalbln(0.f, 0l);{{$}}

  // No warnings for these.
  scalbln(0., 0);
  scalbln(0., 0l);
}

float cosf(float);
double foo(double);         // not a math.h function
float cos(float);           // not a math.h function (wrong signature)
double cos(double, double); // not a math.h function (wrong signature)

namespace std {
void cos(float);
} // namespace std

void check_no_warnings() {
  foo(0.); // no warning because not a math.h function.

  sin(0);        // no warning because arg is an int
  cos(0.);       // no warning because arg is a double
  std::cos(0.f); // no warning because not ::cos.
  cosf(0.f);     // no warning; we expect this to take a float
  cos(0.f);      // does not match the expected signature of ::cos
  cos(0.f, 0.f); // does not match the expected signature of ::cos

  // No warnings because all args are not floats.
  remainder(0., 0.f);
  remainder(0.f, 0.);
  remainder(0, 0.f);
  remainder(0.f, 0);
  fma(0.f, 0.f, 0);
  fma(0.f, 0.f, 0.);
  fma(0.f, 0., 0.f);
  fma(0., 0.f, 0.f);
}