brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.3 KiB · 652d70b Raw
238 lines · cpp
1// RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11    \2// RUN:  -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code            \3// RUN:  -analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false\4// RUN:  -verify=non-nested %s5//6// RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11    \7// RUN:  -analyzer-checker=deadcode.DeadStores           \8// RUN:  -analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false\9// RUN:  -Wno-unreachable-code -verify=non-nested %s10//11// RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11    \12// RUN:  -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code            \13// RUN:  -verify=non-nested,nested %s14//15// RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++17    \16// RUN:  -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code            \17// RUN:  -verify=non-nested,nested %s18 19//===----------------------------------------------------------------------===//20// Basic dead store checking (but in C++ mode).21//===----------------------------------------------------------------------===//22 23int j;24int make_int();25void test1() {26  int x = 4;27  x = x + 1; // non-nested-warning {{never read}}28 29  switch (j) {30  case 1:31    throw 1;32    (void)x;33    break;34  }35 36  int y;37  (void)y;38  if ((y = make_int())) // nested-warning {{Although the value stored}}39    return;40 41  auto z = "text"; // non-nested-warning {{never read}}42}43 44//===----------------------------------------------------------------------===//45// Dead store checking involving constructors.46//===----------------------------------------------------------------------===//47 48class Test2 {49  int &x;50 51public:52  Test2(int &y) : x(y) {}53  ~Test2() { ++x; }54};55 56int test2(int x) {57  { Test2 a(x); } // no-warning58  return x;59}60 61class TestConstructor {62public:63  TestConstructor(int &y);64};65void copy(int x) {66  // All these calls might have side effects in the opaque constructor67  TestConstructor tc1 = x;                    // no-warning potential side effects68  TestConstructor tc2 = TestConstructor(x);   // no-warning potential side effects69  TestConstructor tc3 = (TestConstructor(x)); // no-warning potential side effects70  TestConstructor tc4 = (TestConstructor)(x); // no-warning potential side effects71}72 73//===----------------------------------------------------------------------===//74// Dead store checking involving CXXTemporaryExprs75//===----------------------------------------------------------------------===//76 77namespace TestTemp {78  template<typename _Tp>79  class pencil {80  public:81    ~pencil() throw() {}82  };83  template<typename _Tp, typename _Number2> struct _Row_base {84    _Row_base(const pencil<_Tp>& x) {}85  };86  template<typename _Tp, typename _Number2 = TestTemp::pencil<_Tp> >87  class row : protected _Row_base<_Tp, _Number2>     {88    typedef _Row_base<_Tp, _Number2> _Base;89    typedef _Number2 pencil_type;90  public:91    explicit row(const pencil_type& __a = pencil_type()) : _Base(__a) {}92  };93}94 95void test2_b() {96  TestTemp::row<const char*> x; // no-warning97}98 99//===----------------------------------------------------------------------===//100// Test references.101//===----------------------------------------------------------------------===//102 103void test3_a(int x) {104  x = x + 1; // non-nested-warning {{never read}}105}106 107void test3_b(int &x) {108  x = x + 1; // no-warning109}110 111void test3_c(int x) {112  int &y = x;113  // Shows the limitation of dead stores tracking. The write is really dead114  // since the value cannot escape the function.115  ++y; // no-warning116}117 118void test3_d(int &x) {119  int &y = x;120  ++y; // no-warning121}122 123void test3_e(int &x) {124  int &y = x;125}126 127//===----------------------------------------------------------------------===//128// Dead stores involving 'new'129//===----------------------------------------------------------------------===//130 131static void test_new(unsigned n) {132  char **p = new char *[n]; // non-nested-warning {{never read}}133}134 135//===----------------------------------------------------------------------===//136// Dead stores in namespaces.137//===----------------------------------------------------------------------===//138 139namespace foo {140int test_4(int x) {141  x = 2; // non-nested-warning {{Value stored to 'x' is never read}}142  x = 2;143  return x;144}145}146 147//===----------------------------------------------------------------------===//148// Dead stores in with EH code.149//===----------------------------------------------------------------------===//150 151void test_5_Aux();152int test_5() {153  int x = 0;154  try {155    x = 2; // no-warning156    test_5_Aux();157  } catch (int z) {158    return x + z;159  }160  return 1;161}162 163int test_6_aux(unsigned x);164void test_6() {165  unsigned currDestLen = 0; // no-warning166  try {167    while (test_6_aux(currDestLen)) {168      currDestLen += 2; // no-warning169    }170  } catch (void *) {171  }172}173 174void test_6b() {175  unsigned currDestLen = 0; // no-warning176  try {177    while (test_6_aux(currDestLen)) {178      currDestLen += 2;179      // non-nested-warning@-1 {{Value stored to 'currDestLen' is never read}}180      break;181    }182  } catch (void *) {183  }184}185 186void testCXX11Using() {187  using Int = int;188  Int value;189  value = 1; // non-nested-warning {{never read}}190}191 192//===----------------------------------------------------------------------===//193// Dead stores in template instantiations (do not warn).194//===----------------------------------------------------------------------===//195 196template <bool f> int radar13213575_testit(int i) {197  int x = 5+i; // warning: Value stored to 'x' during its initialization is never read198  int y = 7;199  if (f)200    return x;201  else202    return y;203}204 205int radar_13213575() {206  return radar13213575_testit<true>(5) + radar13213575_testit<false>(3);207}208 209template <class T>210void test_block_in_dependent_context(typename T::some_t someArray) {211  ^{212    int i = someArray[0]; // no-warning213  }();214}215 216void test_block_in_non_dependent_context(int *someArray) {217  ^{218    int i = someArray[0];219    // non-nested-warning@-1 {{Value stored to 'i' during its initialization is never read}}220  }();221}222 223 224//===----------------------------------------------------------------------===//225// Dead store checking involving lambdas.226//===----------------------------------------------------------------------===//227 228int basicLambda(int i, int j) {229  i = 5; // no warning230  j = 6; // no warning231  [i] { (void)i; }();232  [&j] { (void)j; }();233  i = 2;234  j = 3;235  return i + j;236}237 238