1 // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.MismatchedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=false %s -verify 2 // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.MismatchedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 %s -verify 3 4 #include "Inputs/system-header-simulator-cxx.h" 5 6 void good_insert1(std::vector<int> &V, int n) { 7 V.insert(V.cbegin(), n); // no-warning 8 } 9 10 void good_insert2(std::vector<int> &V, int len, int n) { 11 V.insert(V.cbegin(), len, n); // no-warning 12 } 13 14 void good_insert3(std::vector<int> &V1, std::vector<int> &V2) { 15 V1.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // no-warning 16 } 17 18 void good_insert4(std::vector<int> &V, int len, int n) { 19 V.insert(V.cbegin(), {n-1, n, n+1}); // no-warning 20 } 21 22 void good_insert_find(std::vector<int> &V, int n, int m) { 23 auto i = std::find(V.cbegin(), V.cend(), n); 24 V.insert(i, m); // no-warning 25 } 26 27 void good_erase1(std::vector<int> &V) { 28 V.erase(V.cbegin()); // no-warning 29 } 30 31 void good_erase2(std::vector<int> &V) { 32 V.erase(V.cbegin(), V.cend()); // no-warning 33 } 34 35 void good_emplace(std::vector<int> &V, int n) { 36 V.emplace(V.cbegin(), n); // no-warning 37 } 38 39 void good_ctor(std::vector<int> &V) { 40 std::vector<int> new_v(V.cbegin(), V.cend()); // no-warning 41 } 42 43 void good_find(std::vector<int> &V, int n) { 44 std::find(V.cbegin(), V.cend(), n); // no-warning 45 } 46 47 void good_find_first_of(std::vector<int> &V1, std::vector<int> &V2) { 48 std::find_first_of(V1.cbegin(), V1.cend(), V2.cbegin(), V2.cend()); // no-warning 49 } 50 51 void good_copy(std::vector<int> &V1, std::vector<int> &V2, int n) { 52 std::copy(V1.cbegin(), V1.cend(), V2.begin()); // no-warning 53 } 54 55 void bad_insert1(std::vector<int> &V1, std::vector<int> &V2, int n) { 56 V2.insert(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}} 57 } 58 59 void bad_insert2(std::vector<int> &V1, std::vector<int> &V2, int len, int n) { 60 V2.insert(V1.cbegin(), len, n); // expected-warning{{Container accessed using foreign iterator argument}} 61 } 62 63 void bad_insert3(std::vector<int> &V1, std::vector<int> &V2) { 64 V2.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}} 65 V1.insert(V1.cbegin(), V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}} 66 V1.insert(V1.cbegin(), V2.cbegin(), V1.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}} 67 } 68 69 void bad_insert4(std::vector<int> &V1, std::vector<int> &V2, int len, int n) { 70 V2.insert(V1.cbegin(), {n-1, n, n+1}); // expected-warning{{Container accessed using foreign iterator argument}} 71 } 72 73 void bad_erase1(std::vector<int> &V1, std::vector<int> &V2) { 74 V2.erase(V1.cbegin()); // expected-warning{{Container accessed using foreign iterator argument}} 75 } 76 77 void bad_erase2(std::vector<int> &V1, std::vector<int> &V2) { 78 V2.erase(V2.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}} 79 V2.erase(V1.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}} 80 V2.erase(V1.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}} 81 } 82 83 void bad_emplace(std::vector<int> &V1, std::vector<int> &V2, int n) { 84 V2.emplace(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}} 85 } 86 87 void good_comparison(std::vector<int> &V) { 88 if (V.cbegin() == V.cend()) {} // no-warning 89 } 90 91 void bad_comparison(std::vector<int> &V1, std::vector<int> &V2) { 92 if (V1.cbegin() != V2.cend()) {} // expected-warning{{Iterators of different containers used where the same container is expected}} 93 } 94 95 void bad_ctor(std::vector<int> &V1, std::vector<int> &V2) { 96 std::vector<int> new_v(V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}} 97 } 98 99 void bad_find(std::vector<int> &V1, std::vector<int> &V2, int n) { 100 std::find(V1.cbegin(), V2.cend(), n); // expected-warning{{Iterators of different containers used where the same container is expected}} 101 } 102 103 void bad_find_first_of(std::vector<int> &V1, std::vector<int> &V2) { 104 std::find_first_of(V1.cbegin(), V2.cend(), V2.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}} 105 std::find_first_of(V1.cbegin(), V1.cend(), V2.cbegin(), V1.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}} 106 } 107 108 std::vector<int> &return_vector_ref(); 109 110 void ignore_conjured1() { 111 std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref(); 112 113 V2.erase(V1.cbegin()); // no-warning 114 } 115 116 void ignore_conjured2() { 117 std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref(); 118 119 if (V1.cbegin() == V2.cbegin()) {} //no-warning 120 } 121 122 template<typename T> 123 struct cont_with_ptr_iterator { 124 T *begin() const; 125 T *end() const; 126 }; 127 128 void comparison_ptr_iterator(cont_with_ptr_iterator<int> &C1, 129 cont_with_ptr_iterator<int> &C2) { 130 if (C1.begin() != C2.end()) {} // expected-warning{{Iterators of different containers used where the same container is expected}} 131 } 132 133