xref: /aosp_15_r20/external/clang/test/SemaCXX/warn-absolute-value-header.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -verify %s -Wabsolute-value
2*67e74705SXin Li // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only %s -Wabsolute-value -fdiagnostics-parseable-fixits 2>&1 | FileCheck %s
3*67e74705SXin Li 
4*67e74705SXin Li extern "C" {
5*67e74705SXin Li   int abs(int);
6*67e74705SXin Li   float fabsf(float);
7*67e74705SXin Li }
8*67e74705SXin Li 
9*67e74705SXin Li namespace std {
10*67e74705SXin Li   int abs(int);
11*67e74705SXin Li   float abs(float);
12*67e74705SXin Li }
13*67e74705SXin Li 
test(long long ll,double d,int i,float f)14*67e74705SXin Li void test(long long ll, double d, int i, float f) {
15*67e74705SXin Li   // Suggest including cmath
16*67e74705SXin Li   (void)abs(d);
17*67e74705SXin Li   // expected-warning@-1{{using integer absolute value function 'abs' when argument is of floating point type}}
18*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
19*67e74705SXin Li   // expected-note@-3{{include the header <cmath> or explicitly provide a declaration for 'std::abs'}}
20*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:9-[[@LINE-4]]:12}:"std::abs"
21*67e74705SXin Li 
22*67e74705SXin Li   (void)fabsf(d);
23*67e74705SXin Li   // expected-warning@-1{{absolute value function 'fabsf' given an argument of type 'double' but has parameter of type 'float' which may cause truncation of value}}
24*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
25*67e74705SXin Li   // expected-note@-3{{include the header <cmath> or explicitly provide a declaration for 'std::abs'}}
26*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:9-[[@LINE-4]]:14}:"std::abs"
27*67e74705SXin Li 
28*67e74705SXin Li   // Suggest including cstdlib
29*67e74705SXin Li   (void)abs(ll);
30*67e74705SXin Li   // expected-warning@-1{{absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value}}
31*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
32*67e74705SXin Li   // expected-note@-3{{include the header <cstdlib> or explicitly provide a declaration for 'std::abs'}}
33*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:9-[[@LINE-4]]:12}:"std::abs"
34*67e74705SXin Li   (void)fabsf(ll);
35*67e74705SXin Li   // expected-warning@-1{{using floating point absolute value function 'fabsf' when argument is of integer type}}
36*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
37*67e74705SXin Li   // expected-note@-3{{include the header <cstdlib> or explicitly provide a declaration for 'std::abs'}}
38*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:9-[[@LINE-4]]:14}:"std::abs"
39*67e74705SXin Li 
40*67e74705SXin Li   // Proper function already called, no warnings.
41*67e74705SXin Li   (void)abs(i);
42*67e74705SXin Li   (void)fabsf(f);
43*67e74705SXin Li 
44*67e74705SXin Li   // Declarations found, suggest name change.
45*67e74705SXin Li   (void)fabsf(i);
46*67e74705SXin Li   // expected-warning@-1{{using floating point absolute value function 'fabsf' when argument is of integer type}}
47*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
48*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:9-[[@LINE-3]]:14}:"std::abs"
49*67e74705SXin Li   (void)abs(f);
50*67e74705SXin Li   // expected-warning@-1{{using integer absolute value function 'abs' when argument is of floating point type}}
51*67e74705SXin Li   // expected-note@-2{{use function 'std::abs' instead}}
52*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:9-[[@LINE-3]]:12}:"std::abs"
53*67e74705SXin Li }
54