1 // RUN: %clang_analyze_cc1 -Wno-conversion -Wno-tautological-constant-compare -analyzer-checker=core,apiModeling,alpha.core.Conversion -verify %s
6 void assign(unsigned U, signed S) {
8 U8 = S; // expected-warning {{Loss of sign in implicit conversion}}
10 S8 = U; // expected-warning {{Loss of precision in implicit conversion}}
18 unsigned long L = 1000;
20 U8 += L; // expected-warning {{Loss of precision in implicit conversion}}
25 unsigned long L = 1000;
27 U8 -= L; // expected-warning {{Loss of precision in implicit conversion}}
32 unsigned long L = 1000;
34 U8 *= L; // expected-warning {{Loss of precision in implicit conversion}}
35 L *= I; // expected-warning {{Loss of sign in implicit conversion}}
41 unsigned long L = 1000;
43 U8 /= L; // no-warning
44 L /= I; // expected-warning {{Loss of sign in implicit conversion}}
48 unsigned long L = 1000;
50 U8 %= L; // no-warning
51 L %= I; // expected-warning {{Loss of sign in implicit conversion}}
55 unsigned long L = 1000;
57 U8 &= L; // no-warning
58 L &= I; // expected-warning {{Loss of sign in implicit conversion}}
62 unsigned long L = 1000;
64 U8 |= L; // expected-warning {{Loss of precision in implicit conversion}}
65 L |= I; // expected-warning {{Loss of sign in implicit conversion}}
69 unsigned long L = 1000;
71 U8 ^= L; // expected-warning {{Loss of precision in implicit conversion}}
72 L ^= I; // expected-warning {{Loss of sign in implicit conversion}}
76 long long A = 1LL << 60;
77 short X = A; // expected-warning {{Loss of precision in implicit conversion}}
80 void relational(unsigned U, signed S) {
82 if (U < S) { // no-warning
86 if (U < S) { // expected-warning {{Loss of sign in implicit conversion}}
91 void multiplication(unsigned U, signed S) {
93 S = U * S; // no-warning
95 S = U * S; // expected-warning {{Loss of sign}}
98 void division(unsigned U, signed S) {
100 S = U / S; // no-warning
102 S = U / S; // expected-warning {{Loss of sign}}
105 void dontwarn1(unsigned U, signed S) {
106 U8 = S; // It might be known that S is always 0x00-0xff.
107 S8 = U; // It might be known that U is always 0x00-0xff.
109 U8 = -1; // Explicit conversion.
110 S8 = ~0U; // Explicit conversion.
112 U8 &= U; // No loss of precision since there is &=.
115 void dontwarn2(unsigned int U) {
116 if (U <= 4294967295) {
118 if (U <= (2147483647 * 2U + 1U)) {
122 void dontwarn3(int X) {
126 // don't warn for macros
127 #define DOSTUFF ({ unsigned X = 1000; U8 = X; })
132 // don't warn for calculations
133 // seen some fp. For instance: c2 = (c2 >= 'A' && c2 <= 'Z') ? c2 - 'A' + 'a' : c2;
134 // there is a todo in the checker to handle calculations
140 char dontwarn6(long long x) {
147 // C library functions, handled via apiModeling.StdCLibraryFunctions
150 void libraryFunction1() {
154 kb2[0] = X; // no-warning
159 typedef struct FILE {} FILE; int getc(FILE *stream);
161 char reply_string[8192];
163 extern int dostuff(void);
164 int libraryFunction2() {
167 char *cp = reply_string;
173 while ((c = getc(cin)) != '\n') {
174 if (dig < 4 && dostuff())
175 code = code * 10 + (c - '0');
176 if (!pflag && code == 227)
182 if (cp < &reply_string[sizeof(reply_string) - 1])
183 *cp++ = c; // no-warning
188 double floating_point(long long a, int b) {
190 double r = a; // expected-warning {{Loss of precision}}
192 } else if (b > 1 << 25) {
193 float f = b; // expected-warning {{Loss of precision}}
199 double floating_point2() {
201 long long b = 1LL << 53;
202 float f = a; // no-warning
203 double d = b; // no-warning
207 int floating_point_3(unsigned long long a) {
208 double b = a; // no-warning