dead-stores.cpp revision e41721e7dfabcc15cb50be9075a4153f1ad648ea
1// RUN: %clang_cc1 -fexceptions -analyze -analyzer-experimental-internal-checks -analyzer-checker=core.DeadStores -verify -Wno-unreachable-code %s
2// RUN: %clang_cc1 -fexceptions -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-checker=core.DeadStores -verify -Wno-unreachable-code %s
3// RUN: %clang_cc1 -fexceptions -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-checker=core.DeadStores -verify -Wno-unreachable-code %s
4// RUN: %clang_cc1 -fexceptions -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-checker=core.DeadStores -verify -Wno-unreachable-code %s
5// RUN: %clang_cc1 -fexceptions -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-checker=core.DeadStores -verify -Wno-unreachable-code %s
6
7//===----------------------------------------------------------------------===//
8// Basic dead store checking (but in C++ mode).
9//===----------------------------------------------------------------------===//
10
11int j;
12void test1() {
13  int x = 4;
14
15  x = x + 1; // expected-warning{{never read}}
16
17  switch (j) {
18  case 1:
19    throw 1;
20    (void)x;
21    break;
22  }
23}
24
25//===----------------------------------------------------------------------===//
26// Dead store checking involving constructors.
27//===----------------------------------------------------------------------===//
28
29class Test2 {
30  int &x;
31public:
32  Test2(int &y) : x(y) {}
33  ~Test2() { ++x; }
34};
35
36int test2(int x) {
37  { Test2 a(x); } // no-warning
38  return x;
39}
40
41//===----------------------------------------------------------------------===//
42// 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//===----------------------------------------------------------------------===//
68// Test references.
69//===----------------------------------------------------------------------===//
70
71void test3_a(int x) {
72   x = x + 1; // expected-warning{{never read}}
73}
74
75void test3_b(int &x) {
76  x = x + 1; // 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
95//===----------------------------------------------------------------------===//
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
103//===----------------------------------------------------------------------===//
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
115