blob: f5bc51843aaf78cb98a7cac4f9dae25ba4acff5a [file] [log] [blame]
Argyrios Kyrtzidis3e7ab192011-02-28 01:27:12 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-checker=DeadStores -verify -Wno-unreachable-code %s
2// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-checker=DeadStores -verify -Wno-unreachable-code %s
3// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-checker=DeadStores -verify -Wno-unreachable-code %s
4// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-checker=DeadStores -verify -Wno-unreachable-code %s
5// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-checker=DeadStores -verify -Wno-unreachable-code %s
Mike Stump8dd1b6b2009-07-22 22:56:04 +00006
Ted Kremenek29f38082009-12-15 04:12:12 +00007//===----------------------------------------------------------------------===//
8// Basic dead store checking (but in C++ mode).
9//===----------------------------------------------------------------------===//
10
Mike Stump8dd1b6b2009-07-22 22:56:04 +000011int j;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000012void test1() {
Mike Stump8dd1b6b2009-07-22 22:56:04 +000013 int x = 4;
14
Ted Kremenekb1c392a2011-02-12 00:17:19 +000015 x = x + 1; // expected-warning{{never read}}
Mike Stump8dd1b6b2009-07-22 22:56:04 +000016
17 switch (j) {
18 case 1:
19 throw 1;
20 (void)x;
21 break;
22 }
23}
Ted Kremenek29f38082009-12-15 04:12:12 +000024
25//===----------------------------------------------------------------------===//
26// Dead store checking involving constructors.
27//===----------------------------------------------------------------------===//
28
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000029class Test2 {
Ted Kremenek29f38082009-12-15 04:12:12 +000030 int &x;
31public:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000032 Test2(int &y) : x(y) {}
33 ~Test2() { ++x; }
Ted Kremenek29f38082009-12-15 04:12:12 +000034};
35
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000036int test2(int x) {
37 { Test2 a(x); } // no-warning
Ted Kremenek29f38082009-12-15 04:12:12 +000038 return x;
39}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000040
41//===----------------------------------------------------------------------===//
Ted Kremenek857f41c2009-12-23 04:11:44 +000042// Dead store checking involving CXXTemporaryExprs
43//===----------------------------------------------------------------------===//
44
45namespace TestTemp {
46 template<typename _Tp>
47 class pencil {
48 public:
49 ~pencil() throw() {}
50 };
51 template<typename _Tp, typename _Number2> struct _Row_base {
52 _Row_base(const pencil<_Tp>& x) {}
53 };
54 template<typename _Tp, typename _Number2 = TestTemp::pencil<_Tp> >
55 class row : protected _Row_base<_Tp, _Number2> {
56 typedef _Row_base<_Tp, _Number2> _Base;
57 typedef _Number2 pencil_type;
58 public:
59 explicit row(const pencil_type& __a = pencil_type()) : _Base(__a) {}
60 };
61}
62
63void test2_b() {
64 TestTemp::row<const char*> x; // no-warning
65}
66
67//===----------------------------------------------------------------------===//
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000068// Test references.
69//===----------------------------------------------------------------------===//
70
71void test3_a(int x) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +000072 x = x + 1; // expected-warning{{never read}}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000073}
74
75void test3_b(int &x) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +000076 x = x + 1; // no-warninge
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000077}
78
79void test3_c(int x) {
80 int &y = x;
81 // Shows the limitation of dead stores tracking. The write is really
82 // dead since the value cannot escape the function.
83 ++y; // no-warning
84}
85
86void test3_d(int &x) {
87 int &y = x;
88 ++y; // no-warning
89}
90
91void test3_e(int &x) {
92 int &y = x;
93}
94
Ted Kremenekda42d522010-06-25 22:48:52 +000095//===----------------------------------------------------------------------===//
96// Dead stores involving 'new'
97//===----------------------------------------------------------------------===//
98
99static void test_new(unsigned n) {
100 char **p = new char* [n]; // expected-warning{{never read}}
101}
102
Ted Kremeneka1ec4f32011-01-20 17:09:48 +0000103//===----------------------------------------------------------------------===//
104// Dead stores in namespaces.
105//===----------------------------------------------------------------------===//
106
107namespace foo {
108 int test_4(int x) {
109 x = 2; // expected-warning{{Value stored to 'x' is never read}}
110 x = 2;
111 return x;
112 }
113}
114