1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -Wno-null-dereference -Wno-tautological-undefined-compare -analyzer-config eagerly-assume=false %s
3 void clang_analyzer_eval(bool);
5 typedef typeof(sizeof(int)) size_t;
21 // These next two tests just shouldn't crash.
27 // just a basic correctness test, the same behavior as t1()
33 // Each of the tests below is repeated with pointers as well as references.
34 // This is mostly a basic correctness check, but then again, both should work!
37 r
= 'c'; // no-warning
39 return *(char*)0; // no-warning
44 *p
= 'c'; // no-warning
46 return *(char*)0; // no-warning
50 r
= 'c'; // no-warning
52 return *(char*)0; // no-warning
56 *p
= 'c'; // no-warning
58 return *(char*)0; // no-warning
63 // Test that the array-to-pointer decay works for array references as well.
64 // More generally, when we want an lvalue for a reference field, we still need
65 // to do one level of load.
71 int *m() { return x
; }
77 int *m() { return x
; }
79 void testArrayToPointerDecayWithNonTypedValueRegion() {
82 clang_analyzer_eval(p
[0] == q
[0]); // expected-warning{{TRUE}}
93 if (s2
.x
!= a
) return;
96 clang_analyzer_eval(s
.x
[0] == 42); // expected-warning{{TRUE}}
97 clang_analyzer_eval(s2
.x
[0] == 42); // expected-warning{{TRUE}}
101 void testNullReference() {
103 int &y
= *x
; // expected-warning{{Dereference of null pointer}}
107 void testRetroactiveNullReference(int *x
) {
108 // According to the C++ standard, there is no such thing as a
109 // "null reference". So the 'if' statement ought to be dead code.
110 // However, Clang (and other compilers) don't actually check that a pointer
111 // value is non-null in the implementation of references, so it is possible
112 // to produce a supposed "null reference" at runtime. The analyzer should
113 // still warn when it can prove such errors.
117 y
= 5; // expected-warning{{Dereference of null pointer}}
120 namespace TestReferenceAddress
{
121 struct S
{ int &x
; };
125 void testReferenceAddress(int &x
) {
126 // FIXME: Move non-zero reference assumption out of RangeConstraintManager.cpp:422
127 #ifdef ANALYZER_CM_Z3
128 clang_analyzer_eval(&x
!= 0); // expected-warning{{UNKNOWN}}
129 clang_analyzer_eval(&ref() != 0); // expected-warning{{UNKNOWN}}
131 clang_analyzer_eval(&x
!= 0); // expected-warning{{TRUE}}
132 clang_analyzer_eval(&ref() != 0); // expected-warning{{TRUE}}
135 #ifdef ANALYZER_CM_Z3
136 clang_analyzer_eval(&getS().x
!= 0); // expected-warning{{UNKNOWN}}
138 clang_analyzer_eval(&getS().x
!= 0); // expected-warning{{TRUE}}
141 #ifdef ANALYZER_CM_Z3
142 clang_analyzer_eval(&getSP()->x
!= 0); // expected-warning{{UNKNOWN}}
144 clang_analyzer_eval(&getSP()->x
!= 0); // expected-warning{{TRUE}}
149 void testFunctionPointerReturn(void *opaque
) {
150 typedef int &(*RefFn
)();
152 RefFn getRef
= (RefFn
)opaque
;
154 // Don't crash writing to or reading from this reference.
157 clang_analyzer_eval(x
== 42); // expected-warning{{TRUE}}
160 int &testReturnNullReference() {
162 return *x
; // expected-warning{{Returning null reference}}
165 char &refFromPointer() {
169 void testReturnReference() {
170 clang_analyzer_eval(ptr() == 0); // expected-warning{{UNKNOWN}}
171 clang_analyzer_eval(&refFromPointer() == 0); // expected-warning{{FALSE}}
174 void intRefParam(int &r
) {
178 void test(int *ptr
) {
179 clang_analyzer_eval(ptr
== 0); // expected-warning{{UNKNOWN}}
181 extern void use(int &ref
);
184 clang_analyzer_eval(ptr
== 0); // expected-warning{{FALSE}}
187 void testIntRefParam() {
189 intRefParam(i
); // no-warning
192 int refParam(int &byteIndex
) {
196 void testRefParam(int *p
) {
199 refParam(*p
); // expected-warning {{Forming reference to null pointer}}
202 int ptrRefParam(int *&byteIndex
) {
203 return *byteIndex
; // expected-warning {{Dereference of null pointer}}
205 void testRefParam2() {
214 return coin() ? &x
: 0;
221 void testSuppression() {
225 namespace rdar11212286
{
230 return *x
; // expected-warning {{Forming reference to null pointer}}
236 return *x
; // expected-warning {{Forming reference to null pointer}}
246 return *x
; // no-warning
253 template <class T
> void bar(const T
&obj
) {}
255 bar(bit
); // don't crash