xref: /aosp_15_r20/external/clang/test/Sema/warn-absolute-value-header.c (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 int abs(int);
5*67e74705SXin Li 
6*67e74705SXin Li // Wrong signature
7*67e74705SXin Li int fabsf(int);
8*67e74705SXin Li // expected-warning@-1{{incompatible redeclaration of library function 'fabsf'}}
9*67e74705SXin Li // expected-note@-2{{'fabsf' is a builtin with type 'float (float)'}}
10*67e74705SXin Li 
test_int(int i,unsigned u,long long ll,float f,double d)11*67e74705SXin Li void test_int(int i, unsigned u, long long ll, float f, double d) {
12*67e74705SXin Li   (void)abs(i);
13*67e74705SXin Li 
14*67e74705SXin Li   // Remove abs call
15*67e74705SXin Li   (void)abs(u);
16*67e74705SXin Li   // expected-warning@-1{{taking the absolute value of unsigned type 'unsigned int' has no effect}}
17*67e74705SXin Li   // expected-note@-2{{remove the call to 'abs' since unsigned values cannot be negative}}
18*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:9-[[@LINE-3]]:12}:""
19*67e74705SXin Li 
20*67e74705SXin Li   int llabs;
21*67e74705SXin Li   (void)llabs;
22*67e74705SXin Li   // Conflict in names, no notes
23*67e74705SXin Li   (void)abs(ll);
24*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}}
25*67e74705SXin Li 
26*67e74705SXin Li   // Conflict in names, no notes
27*67e74705SXin Li   (void)abs(f);
28*67e74705SXin Li   // expected-warning@-1{{using integer absolute value function 'abs' when argument is of floating point type}}
29*67e74705SXin Li 
30*67e74705SXin Li   // Suggest header.
31*67e74705SXin Li   (void)abs(d);
32*67e74705SXin Li   // expected-warning@-1{{using integer absolute value function 'abs' when argument is of floating point type}}
33*67e74705SXin Li   // expected-note@-2{{use function 'fabs' instead}}
34*67e74705SXin Li   // expected-note@-3{{include the header <math.h> or explicitly provide a declaration for 'fabs'}}
35*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:9-[[@LINE-4]]:12}:"fabs"
36*67e74705SXin Li }
37