1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -triple x86_64-apple-darwin13 -Wno-shift-count-overflow -verify %s 2 3 void clang_analyzer_eval(int); 4 #define CHECK(expr) if (!(expr)) return; clang_analyzer_eval(expr) 5 6 void testPersistentConstraints(int x, int y) { 7 // Sanity check 8 CHECK(x); // expected-warning{{TRUE}} 9 CHECK(x & 1); // expected-warning{{TRUE}} 10 11 // False positives due to SValBuilder giving up on certain kinds of exprs. 12 CHECK(1 - x); // expected-warning{{UNKNOWN}} 13 CHECK(x & y); // expected-warning{{UNKNOWN}} 14 } 15 16 int testConstantShifts_PR18073(int which) { 17 // FIXME: We should have a checker that actually specifically checks bitwise 18 // shifts against the width of the LHS's /static/ type, rather than just 19 // having BasicValueFactory return "undefined" when dealing with two constant 20 // operands. 21 switch (which) { 22 case 1: 23 return 0ULL << 63; // no-warning 24 case 2: 25 return 0ULL << 64; // expected-warning{{The result of the left shift is undefined due to shifting by '64', which is greater or equal to the width of type 'unsigned long long'}} 26 case 3: 27 return 0ULL << 65; // expected-warning{{The result of the left shift is undefined due to shifting by '65', which is greater or equal to the width of type 'unsigned long long'}} 28 29 default: 30 return 0; 31 } 32 } 33 34 int testOverflowShift(int a) { 35 if (a == 323) { 36 return 1 << a; // expected-warning{{The result of the left shift is undefined due to shifting by '323', which is greater or equal to the width of type 'int'}} 37 } 38 return 0; 39 } 40 41 int testNegativeShift(int a) { 42 if (a == -5) { 43 return 1 << a; // expected-warning{{The result of the left shift is undefined because the right operand is negative}} 44 } 45 return 0; 46 } 47 48 int testNegativeLeftShift(int a) { 49 if (a == -3) { 50 return a << 1; // expected-warning{{The result of the left shift is undefined because the left operand is negative}} 51 } 52 return 0; 53 } 54 55 int testUnrepresentableLeftShift(int a) { 56 if (a == 8) 57 return a << 30; // expected-warning{{The result of the left shift is undefined due to shifting '8' by '30', which is unrepresentable in the unsigned version of the return type 'int'}} 58 return 0; 59 } 60