1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc void clang_analyzer_eval(bool); 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc void usePointer(int * const *); 6*f4a2713aSLionel Sambuc void useReference(int * const &); 7*f4a2713aSLionel Sambuc 8*f4a2713aSLionel Sambuc void testPointer() { 9*f4a2713aSLionel Sambuc int x; 10*f4a2713aSLionel Sambuc int *p; 11*f4a2713aSLionel Sambuc 12*f4a2713aSLionel Sambuc p = &x; 13*f4a2713aSLionel Sambuc x = 42; 14*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 15*f4a2713aSLionel Sambuc usePointer(&p); 16*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 17*f4a2713aSLionel Sambuc 18*f4a2713aSLionel Sambuc p = &x; 19*f4a2713aSLionel Sambuc x = 42; 20*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 21*f4a2713aSLionel Sambuc useReference(p); 22*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc int * const cp1 = &x; 25*f4a2713aSLionel Sambuc x = 42; 26*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 27*f4a2713aSLionel Sambuc usePointer(&cp1); 28*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 29*f4a2713aSLionel Sambuc 30*f4a2713aSLionel Sambuc int * const cp2 = &x; 31*f4a2713aSLionel Sambuc x = 42; 32*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 33*f4a2713aSLionel Sambuc useReference(cp2); 34*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 35*f4a2713aSLionel Sambuc } 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc 38*f4a2713aSLionel Sambuc struct Wrapper { 39*f4a2713aSLionel Sambuc int *ptr; 40*f4a2713aSLionel Sambuc }; 41*f4a2713aSLionel Sambuc 42*f4a2713aSLionel Sambuc void useStruct(Wrapper &w); 43*f4a2713aSLionel Sambuc void useConstStruct(const Wrapper &w); 44*f4a2713aSLionel Sambuc 45*f4a2713aSLionel Sambuc void testPointerStruct() { 46*f4a2713aSLionel Sambuc int x; 47*f4a2713aSLionel Sambuc Wrapper w; 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc w.ptr = &x; 50*f4a2713aSLionel Sambuc x = 42; 51*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 52*f4a2713aSLionel Sambuc useStruct(w); 53*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 54*f4a2713aSLionel Sambuc 55*f4a2713aSLionel Sambuc w.ptr = &x; 56*f4a2713aSLionel Sambuc x = 42; 57*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 58*f4a2713aSLionel Sambuc useConstStruct(w); 59*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 60*f4a2713aSLionel Sambuc } 61*f4a2713aSLionel Sambuc 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc struct RefWrapper { 64*f4a2713aSLionel Sambuc int &ref; 65*f4a2713aSLionel Sambuc }; 66*f4a2713aSLionel Sambuc 67*f4a2713aSLionel Sambuc void useStruct(RefWrapper &w); 68*f4a2713aSLionel Sambuc void useConstStruct(const RefWrapper &w); 69*f4a2713aSLionel Sambuc 70*f4a2713aSLionel Sambuc void testReferenceStruct() { 71*f4a2713aSLionel Sambuc int x; 72*f4a2713aSLionel Sambuc RefWrapper w = { x }; 73*f4a2713aSLionel Sambuc 74*f4a2713aSLionel Sambuc x = 42; 75*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 76*f4a2713aSLionel Sambuc useStruct(w); 77*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 78*f4a2713aSLionel Sambuc } 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel Sambuc // FIXME: This test is split into two functions because region invalidation 81*f4a2713aSLionel Sambuc // does not preserve reference bindings. <rdar://problem/13320347> 82*f4a2713aSLionel Sambuc void testConstReferenceStruct() { 83*f4a2713aSLionel Sambuc int x; 84*f4a2713aSLionel Sambuc RefWrapper w = { x }; 85*f4a2713aSLionel Sambuc 86*f4a2713aSLionel Sambuc x = 42; 87*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{TRUE}} 88*f4a2713aSLionel Sambuc useConstStruct(w); 89*f4a2713aSLionel Sambuc clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}} 90*f4a2713aSLionel Sambuc } 91*f4a2713aSLionel Sambuc 92