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
4 #include "Inputs/system-header-simulator-cxx.h"
6 void good_insert1(std::vector
<int> &V
, int n
) {
7 V
.insert(V
.cbegin(), n
); // no-warning
10 void good_insert2(std::vector
<int> &V
, int len
, int n
) {
11 V
.insert(V
.cbegin(), len
, n
); // no-warning
14 void good_insert3(std::vector
<int> &V1
, std::vector
<int> &V2
) {
15 V1
.insert(V1
.cbegin(), V2
.cbegin(), V2
.cend()); // no-warning
18 void good_insert4(std::vector
<int> &V
, int len
, int n
) {
19 V
.insert(V
.cbegin(), {n
-1, n
, n
+1}); // no-warning
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
27 void good_erase1(std::vector
<int> &V
) {
28 V
.erase(V
.cbegin()); // no-warning
31 void good_erase2(std::vector
<int> &V
) {
32 V
.erase(V
.cbegin(), V
.cend()); // no-warning
35 void good_emplace(std::vector
<int> &V
, int n
) {
36 V
.emplace(V
.cbegin(), n
); // no-warning
39 void good_ctor(std::vector
<int> &V
) {
40 std::vector
<int> new_v(V
.cbegin(), V
.cend()); // no-warning
43 void good_find(std::vector
<int> &V
, int n
) {
44 std::find(V
.cbegin(), V
.cend(), n
); // no-warning
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
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
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}}
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}}
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}}
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}}
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}}
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}}
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}}
87 void good_comparison(std::vector
<int> &V
) {
88 if (V
.cbegin() == V
.cend()) {} // no-warning
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}}
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}}
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}}
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}}
108 std::vector
<int> &return_vector_ref();
110 void ignore_conjured1() {
111 std::vector
<int> &V1
= return_vector_ref(), &V2
= return_vector_ref();
113 V2
.erase(V1
.cbegin()); // no-warning
116 void ignore_conjured2() {
117 std::vector
<int> &V1
= return_vector_ref(), &V2
= return_vector_ref();
119 if (V1
.cbegin() == V2
.cbegin()) {} //no-warning
123 struct cont_with_ptr_iterator
{
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}}