blob: b21ffad6c5f073c110cf011d9bf815e983834800 [file] [log] [blame]
Ted Kremenekcd9902b2010-02-05 01:52:40 +00001// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -verify -Wno-unreachable-code %s
Ted Kremenek565e4652010-02-05 02:06:54 +00002// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -verify -Wno-unreachable-code %s
3// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -verify -Wno-unreachable-code %s
4// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -verify -Wno-unreachable-code %s
5// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -verify -Wno-unreachable-code %s
Mike Stump0979d802009-07-22 22:56:04 +00006
Ted Kremenek43f19e32009-12-15 04:12:12 +00007//===----------------------------------------------------------------------===//
8// Basic dead store checking (but in C++ mode).
9//===----------------------------------------------------------------------===//
10
Mike Stump0979d802009-07-22 22:56:04 +000011int j;
Ted Kremenek852274d2009-12-16 03:18:58 +000012void test1() {
Mike Stump0979d802009-07-22 22:56:04 +000013 int x = 4;
14
15 ++x; // expected-warning{{never read}}
16
17 switch (j) {
18 case 1:
19 throw 1;
20 (void)x;
21 break;
22 }
23}
Ted Kremenek43f19e32009-12-15 04:12:12 +000024
25//===----------------------------------------------------------------------===//
26// Dead store checking involving constructors.
27//===----------------------------------------------------------------------===//
28
Ted Kremenek852274d2009-12-16 03:18:58 +000029class Test2 {
Ted Kremenek43f19e32009-12-15 04:12:12 +000030 int &x;
31public:
Ted Kremenek852274d2009-12-16 03:18:58 +000032 Test2(int &y) : x(y) {}
33 ~Test2() { ++x; }
Ted Kremenek43f19e32009-12-15 04:12:12 +000034};
35
Ted Kremenek852274d2009-12-16 03:18:58 +000036int test2(int x) {
37 { Test2 a(x); } // no-warning
Ted Kremenek43f19e32009-12-15 04:12:12 +000038 return x;
39}
Ted Kremenek852274d2009-12-16 03:18:58 +000040
41//===----------------------------------------------------------------------===//
Ted Kremenek604d9392009-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 Kremenek852274d2009-12-16 03:18:58 +000068// Test references.
69//===----------------------------------------------------------------------===//
70
71void test3_a(int x) {
72 ++x; // expected-warning{{never read}}
73}
74
75void test3_b(int &x) {
76 ++x; // no-warninge
77}
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 Kremeneka0063422010-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