1 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -fblocks %s -fexceptions -fcxx-exceptions -Wno-tautological-undefined-compare
2 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -fblocks %s -fexceptions -fcxx-exceptions -Wno-tautological-undefined-compare
4 void clang_analyzer_warnIfReached();
6 // Test basic handling of references.
12 // Test test1_aux() evaluates to char &.
13 char test1_as_rvalue() {
17 // Test passing a value as a reference. The 'const' in test2_aux() adds
18 // an ImplicitCastExpr, which is evaluated as an lvalue.
19 int test2_aux(const int &n
);
24 int test2_b_aux(const short &n
);
26 return test2_b_aux(n
);
29 // Test getting the lvalue of a derived and converting it to a base. This
30 // previously crashed.
32 class Test3_Derived
: public Test3_Base
{};
34 int test3_aux(Test3_Base
&x
);
35 int test3(Test3_Derived x
) {
39 //===---------------------------------------------------------------------===//
40 // Test CFG support for C++ condition variables.
41 //===---------------------------------------------------------------------===//
43 int test_init_in_condition_aux();
44 int test_init_in_condition() {
45 if (int x
= test_init_in_condition_aux()) { // no-warning
51 int test_init_in_condition_switch() {
52 switch (int x
= test_init_in_condition_aux()) { // no-warning
59 clang_analyzer_warnIfReached(); // unreachable
67 int test_init_in_condition_while() {
69 while (int x
= ++z
) { // no-warning
77 clang_analyzer_warnIfReached(); // unreachable
82 int test_init_in_condition_for() {
84 for (int x
= 0; int y
= ++z
; ++x
) {
85 if (x
== y
) // no-warning
91 clang_analyzer_warnIfReached(); // unreachable
95 //===---------------------------------------------------------------------===//
96 // Test handling of 'this' pointer.
97 //===---------------------------------------------------------------------===//
99 class TestHandleThis
{
104 int null_deref_negative();
105 int null_deref_positive();
108 int TestHandleThis::foo() {
109 // Assume that 'x' is initialized.
110 return x
+ 1; // no-warning
113 int TestHandleThis::null_deref_negative() {
118 clang_analyzer_warnIfReached(); // unreachable
122 int TestHandleThis::null_deref_positive() {
127 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
131 // PR 7675 - passing literals by-reference
132 void pr7675(const double &a
);
133 void pr7675(const int &a
);
134 void pr7675(const char &a
);
135 void pr7675_i(const _Complex
double &a
);
143 // Add check to ensure we are analyzing the code up to this point.
144 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
147 // <rdar://problem/8375510> - CFGBuilder should handle temporaries.
151 R8375510
operator++(int);
154 int r8375510(R8375510 x
, R8375510 y
) {
158 // PR8419 -- this used to crash.
163 char& operator[](int n
);
171 get8419()--; // used to crash
172 --s
[0]; // used to crash
173 s
[0] &= 1; // used to crash
174 s
[0]++; // used to crash
177 // PR8426 -- this used to crash.
181 template <class T
> class Foo
{
187 template <class T
> Foo
<T
>::~Foo() {
193 // PR8427 -- this used to crash.
197 bool operator==(Dummy
, int);
199 template <typename T
>
204 template <typename T
>
205 bool Foo2
<T
>::Bar() {
209 // PR8433 -- this used to crash.
211 template <typename T
>
219 template <typename T
>
220 void Foo3
<T
>::Bar() {
225 //===---------------------------------------------------------------------===//
226 // Handle misc. C++ constructs.
227 //===---------------------------------------------------------------------===//
233 void test_namespace() {
234 // Previously triggered a crash.
239 // Test handling methods that accept references as parameters, and that
240 // variables are properly invalidated.
242 bool foo(unsigned valA
, long long &result
) const;
243 bool foo(unsigned valA
, int &result
) const;
245 bool RDar9203355::foo(unsigned valA
, int &result
) const {
247 if (foo(valA
, val
) ||
248 (int)val
!= val
) // no-warning
250 result
= val
; // no-warning
254 // Test handling of new[].
256 int *x
= new int[10];
257 for (unsigned i
= 0 ; i
< 2 ; ++i
) {
258 // This previously triggered an uninitialized values warning.
259 x
[i
] = 1; // no-warning
263 // Test basic support for dynamic_cast<>.
264 struct Rdar9212495_C
{ virtual void bar() const; };
265 class Rdar9212495_B
: public Rdar9212495_C
{};
266 class Rdar9212495_A
: public Rdar9212495_B
{};
267 const Rdar9212495_A
& rdar9212495(const Rdar9212495_C
* ptr
) {
268 const Rdar9212495_A
& val
= dynamic_cast<const Rdar9212495_A
&>(*ptr
);
270 // This is not valid C++; dynamic_cast with a reference type will throw an
271 // exception if the pointer does not match the expected type. However, our
272 // implementation of dynamic_cast will pass through a null pointer...or a
273 // "null reference"! So this branch is actually possible.
275 val
.bar(); // expected-warning{{Called C++ object pointer is null}}
281 const Rdar9212495_A
* rdar9212495_ptr(const Rdar9212495_C
* ptr
) {
282 const Rdar9212495_A
* val
= dynamic_cast<const Rdar9212495_A
*>(ptr
);
285 val
->bar(); // expected-warning{{Called C++ object pointer is null}}
291 // Test constructors invalidating arguments. Previously this raised
292 // an uninitialized value warning.
293 extern "C" void __attribute__((noreturn
)) PR9645_exit(int i
);
295 class PR9645_SideEffect
298 PR9645_SideEffect(int *pi
); // caches pi in i_
299 void Read(int *pi
); // copies *pi into *i_
307 PR9645_SideEffect
se(&i
);
309 se
.Read(&j
); // this has a side-effect of initializing i.
311 PR9645_exit(i
); // no-warning
314 PR9645_SideEffect::PR9645_SideEffect(int *pi
) : i_(pi
) {}
315 void PR9645_SideEffect::Read(int *pi
) { *i_
= *pi
; }
317 // Invalidate fields during C++ method calls.
326 void RDar9267815::test_pos() {
329 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
331 void RDar9267815::test() {
335 clang_analyzer_warnIfReached(); // no-warning
338 void RDar9267815::test2() {
343 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
346 // Test reference parameters.
347 void test_ref_double_aux(double &Value
);
348 float test_ref_double() {
350 test_ref_double_aux(dVal
);
351 // This previously warned because 'dVal' was thought to be uninitialized.
352 float Val
= (float)dVal
; // no-warning
356 // Test invalidation of class fields.
357 class TestInvalidateClass
{
362 void test_invalidate_class_aux(TestInvalidateClass
&x
);
364 int test_invalidate_class() {
365 TestInvalidateClass y
;
366 test_invalidate_class_aux(y
);
367 return y
.x
; // no-warning
370 // Test correct pointer arithmetic using 'p--'. This is to warn that we
371 // were loading beyond the written characters in buf.
372 char *RDar9269695(char *dst
, unsigned int n
)
382 *dst
++ = *--p
; // no-warning
388 // Test that we invalidate byref arguments passed to constructors.
389 class TestInvalidateInCtor
{
391 TestInvalidateInCtor(unsigned &x
);
394 unsigned test_invalidate_in_ctor() {
396 TestInvalidateInCtor
foo(x
);
397 return x
; // no-warning
399 unsigned test_invalidate_in_ctor_new() {
401 delete (new TestInvalidateInCtor(x
));
402 return x
; // no-warning
405 // Test assigning into a symbolic offset.
406 struct TestAssignIntoSymbolicOffset
{
408 void test(int x
, int y
);
411 void TestAssignIntoSymbolicOffset::test(int x
, int y
) {
418 stuff
[x
] = new int*[y
+1];
419 // Previously triggered a null dereference.
420 stuff
[x
][y
] = 0; // no-warning
424 // Test loads from static fields. This previously triggered an uninitialized
426 class ClassWithStatic
{
428 static const unsigned value
= 1;
431 int rdar9948787_negative() {
432 ClassWithStatic classWithStatic
;
433 unsigned value
= classWithStatic
.value
;
436 clang_analyzer_warnIfReached(); // no-warning
440 int rdar9948787_positive() {
441 ClassWithStatic classWithStatic
;
442 unsigned value
= classWithStatic
.value
;
445 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
449 // Regression test against global constants and switches.
450 enum rdar10202899_ValT
{ rdar10202899_ValTA
, rdar10202899_ValTB
, rdar10202899_ValTC
};
451 const rdar10202899_ValT val
= rdar10202899_ValTA
;
452 void rdar10202899_test1() {
454 case rdar10202899_ValTA
: {}
458 void rdar10202899_test2() {
459 if (val
== rdar10202899_ValTA
)
461 clang_analyzer_warnIfReached(); // no-warning
464 void rdar10202899_test3() {
466 case rdar10202899_ValTA
: return;
469 clang_analyzer_warnIfReached(); // no-warning
472 // This used to crash the analyzer because of the unnamed bitfield.
482 clang_analyzer_warnIfReached(); // no-warning
484 clang_analyzer_warnIfReached(); // no-warning
486 clang_analyzer_warnIfReached(); // no-warning
489 // Handle doing a load from the memory associated with the code for
491 extern double nan( const char * );
493 double NaN
= *(double*) nan
;
497 // Test that 'this' is assumed non-null upon analyzing the entry to a "top-level"
498 // function (i.e., when not analyzing from a specific caller).
499 struct TestNullThis
{
504 void TestNullThis::test() {
508 field
= 2; // no-warning
511 // Test handling of 'catch' exception variables, and not warning
512 // about uninitialized values.
513 enum MyEnum
{ MyEnumValue
};
514 MyEnum
rdar10892489() {
518 return e
; // no-warning
523 MyEnum
rdar10892489_positive() {
529 *p
= 0xDEADBEEF; // {{null}}
535 // Test handling of catch with no condition variable.
546 void PR11545_positive() {
555 *p
= 0xDEADBEEF; // {{null}}
559 // Test handling taking the address of a field. While the analyzer
560 // currently doesn't do anything intelligent here, this previously
561 // resulted in a crash.
568 struct PR11146::Entry
{
572 void PR11146::baz() {
576 // Test symbolicating a reference. In this example, the
577 // analyzer (originally) didn't know how to handle x[index - index2],
578 // returning an UnknownVal. The conjured symbol wasn't a location,
579 // and would result in a crash.
580 void rdar10924675(unsigned short x
[], int index
, int index2
) {
581 unsigned short &y
= x
[index
- index2
];
586 // Test handling CXXScalarValueInitExprs.
587 void rdar11401827() {
590 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
591 ; // Suppress warning that both branches are identical
594 clang_analyzer_warnIfReached(); // no-warning
598 //===---------------------------------------------------------------------===//
599 // Handle inlining of C++ method calls.
600 //===---------------------------------------------------------------------===//
608 *p
= 0; // expected-warning {{null pointer}}
618 void test_alloca_in_a_recursive_function(int p1
) {
619 __builtin_alloca (p1
);
620 test_alloca_in_a_recursive_function(1);
621 test_alloca_in_a_recursive_function(2);
624 //===---------------------------------------------------------------------===//
626 //===---------------------------------------------------------------------===//
628 // Tests assigning using a C-style initializer to a struct
629 // variable whose sub-field is also a struct. This currently
630 // results in a CXXTempObjectRegion being created, but not
631 // properly handled. For now, we just ignore that value
632 // to avoid a crash (<rdar://problem/12753384>).
633 struct RDar12753384_ClassA
{
636 struct RDar12753384_ClassB
{
638 RDar12753384_ClassA y
[ 8 ] ;
640 unsigned RDar12753384() {
641 RDar12753384_ClassB w
= { 0x00 };
642 RDar12753384_ClassA y
[8];
646 // This testcase tests whether we treat the anonymous union and union
647 // the same way. This previously resulted in a "return of stack address"
648 // warning because the anonymous union resulting in a temporary object
649 // getting put into the initializer. We still aren't handling this correctly,
650 // but now if a temporary object appears in an initializer we just ignore it.
651 // Fixes <rdar://problem/12755044>.
653 struct Rdar12755044_foo
655 struct Rdar12755044_bar
664 struct Rdar12755044_foo_anon
666 struct Rdar12755044_bar
675 const Rdar12755044_foo_anon
*radar12755044_anon() {
676 static const Rdar12755044_foo_anon Rdar12755044_foo_list
[] = { { { } } };
677 return Rdar12755044_foo_list
; // no-warning
680 const Rdar12755044_foo
*radar12755044() {
681 static const Rdar12755044_foo Rdar12755044_foo_list
[] = { { { } } };
682 return Rdar12755044_foo_list
; // no-warning
685 // Test the correct handling of integer to bool conversions. Previously
686 // this resulted in a false positive because integers were being truncated
687 // and not tested for non-zero.
688 void rdar12759044() {
691 clang_analyzer_warnIfReached(); // no-warning
695 // The analyzer currently does not model complex types. Test that the load
696 // from 'x' is not flagged as being uninitialized.
697 typedef __complex__
float _ComplexT
;
698 void rdar12964481(_ComplexT
*y
) {
702 *y
*= x
; // no-warning
704 void rdar12964481_b(_ComplexT
*y
) {
706 // Eventually this should be a warning.
707 *y
*= x
; // no-warning
710 // Test case for PR 12921. This previously produced
712 static const int pr12921_arr
[] = { 0, 1 };
713 static const int pr12921_arrcount
= sizeof(pr12921_arr
)/sizeof(int);
715 int pr12921(int argc
, char **argv
) {
717 for (i
= 0; i
< pr12921_arrcount
; i
++) {
725 if (i
== pr12921_arrcount
) return 66;
726 return pr12921_arr
[retval
];