summaryrefslogtreecommitdiff
path: root/clang/test/Analysis/dead-stores.cpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-10-15 17:10:06 +1100
committerCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-10-15 17:10:06 +1100
commitbe1de4be954c80875ad4108e0a33e8e131b2f2c0 (patch)
tree1fbbecf276bf7c7bdcbb4dd446099d6d90eaa516 /clang/test/Analysis/dead-stores.cpp
parentc4626a62754862d20b41e8a46a3574264ea80e6d (diff)
parentf1bd2e48c5324d3f7cda4090c87f8a5b6f463ce2 (diff)
Merge branch 'master' of ssh://bitbucket.org/czan/honours
Diffstat (limited to 'clang/test/Analysis/dead-stores.cpp')
-rw-r--r--clang/test/Analysis/dead-stores.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/clang/test/Analysis/dead-stores.cpp b/clang/test/Analysis/dead-stores.cpp
new file mode 100644
index 0000000..43d8796
--- /dev/null
+++ b/clang/test/Analysis/dead-stores.cpp
@@ -0,0 +1,112 @@
+// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
+// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-store=region -analyzer-constraints=basic -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
+// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-store=region -analyzer-constraints=range -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
+
+//===----------------------------------------------------------------------===//
+// Basic dead store checking (but in C++ mode).
+//===----------------------------------------------------------------------===//
+
+int j;
+void test1() {
+ int x = 4;
+
+ x = x + 1; // expected-warning{{never read}}
+
+ switch (j) {
+ case 1:
+ throw 1;
+ (void)x;
+ break;
+ }
+}
+
+//===----------------------------------------------------------------------===//
+// Dead store checking involving constructors.
+//===----------------------------------------------------------------------===//
+
+class Test2 {
+ int &x;
+public:
+ Test2(int &y) : x(y) {}
+ ~Test2() { ++x; }
+};
+
+int test2(int x) {
+ { Test2 a(x); } // no-warning
+ return x;
+}
+
+//===----------------------------------------------------------------------===//
+// Dead store checking involving CXXTemporaryExprs
+//===----------------------------------------------------------------------===//
+
+namespace TestTemp {
+ template<typename _Tp>
+ class pencil {
+ public:
+ ~pencil() throw() {}
+ };
+ template<typename _Tp, typename _Number2> struct _Row_base {
+ _Row_base(const pencil<_Tp>& x) {}
+ };
+ template<typename _Tp, typename _Number2 = TestTemp::pencil<_Tp> >
+ class row : protected _Row_base<_Tp, _Number2> {
+ typedef _Row_base<_Tp, _Number2> _Base;
+ typedef _Number2 pencil_type;
+ public:
+ explicit row(const pencil_type& __a = pencil_type()) : _Base(__a) {}
+ };
+}
+
+void test2_b() {
+ TestTemp::row<const char*> x; // no-warning
+}
+
+//===----------------------------------------------------------------------===//
+// Test references.
+//===----------------------------------------------------------------------===//
+
+void test3_a(int x) {
+ x = x + 1; // expected-warning{{never read}}
+}
+
+void test3_b(int &x) {
+ x = x + 1; // no-warninge
+}
+
+void test3_c(int x) {
+ int &y = x;
+ // Shows the limitation of dead stores tracking. The write is really
+ // dead since the value cannot escape the function.
+ ++y; // no-warning
+}
+
+void test3_d(int &x) {
+ int &y = x;
+ ++y; // no-warning
+}
+
+void test3_e(int &x) {
+ int &y = x;
+}
+
+//===----------------------------------------------------------------------===//
+// Dead stores involving 'new'
+//===----------------------------------------------------------------------===//
+
+static void test_new(unsigned n) {
+ char **p = new char* [n]; // expected-warning{{never read}}
+}
+
+//===----------------------------------------------------------------------===//
+// Dead stores in namespaces.
+//===----------------------------------------------------------------------===//
+
+namespace foo {
+ int test_4(int x) {
+ x = 2; // expected-warning{{Value stored to 'x' is never read}}
+ x = 2;
+ return x;
+ }
+}
+