Debian Codegraph @

clang/test/Analysis/mismatched-iterator.cpp

raw
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_insert5(std::vector<int> &V, int len, int n) {
23 std::unordered_set<int> us;
24 us.insert(V.cbegin(), V.cend()); // no-warning
25 }
26
27 void good_insert_find(std::vector<int> &V, int n, int m) {
28 auto i = std::find(V.cbegin(), V.cend(), n);
29 V.insert(i, m); // no-warning
30 }
31
32 void good_erase1(std::vector<int> &V) {
33 V.erase(V.cbegin()); // no-warning
34 }
35
36 void good_erase2(std::vector<int> &V) {
37 V.erase(V.cbegin(), V.cend()); // no-warning
38 }
39
40 void good_emplace(std::vector<int> &V, int n) {
41 V.emplace(V.cbegin(), n); // no-warning
42 }
43
44 void good_ctor(std::vector<int> &V) {
45 std::vector<int> new_v(V.cbegin(), V.cend()); // no-warning
46 }
47
48 void good_find(std::vector<int> &V, int n) {
49 std::find(V.cbegin(), V.cend(), n); // no-warning
50 }
51
52 void good_find_first_of(std::vector<int> &V1, std::vector<int> &V2) {
53 std::find_first_of(V1.cbegin(), V1.cend(), V2.cbegin(), V2.cend()); // no-warning
54 }
55
56 void good_copy(std::vector<int> &V1, std::vector<int> &V2, int n) {
57 std::copy(V1.cbegin(), V1.cend(), V2.begin()); // no-warning
58 }
59
60 void bad_insert1(std::vector<int> &V1, std::vector<int> &V2, int n) {
61 V2.insert(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}}
62 }
63
64 void bad_insert2(std::vector<int> &V1, std::vector<int> &V2, int len, int n) {
65 V2.insert(V1.cbegin(), len, n); // expected-warning{{Container accessed using foreign iterator argument}}
66 }
67
68 void bad_insert3(std::vector<int> &V1, std::vector<int> &V2) {
69 V2.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
70 V1.insert(V1.cbegin(), V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
71 V1.insert(V1.cbegin(), V2.cbegin(), V1.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
72 }
73
74 void bad_insert4(std::vector<int> &V1, std::vector<int> &V2, int len, int n) {
75 V2.insert(V1.cbegin(), {n-1, n, n+1}); // expected-warning{{Container accessed using foreign iterator argument}}
76 }
77
78 void bad_insert5(std::vector<int> &V1, std::vector<int> &V2, int len, int n) {
79 std::unordered_set<int> us;
80 us.insert(V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
81 }
82
83 void bad_erase1(std::vector<int> &V1, std::vector<int> &V2) {
84 V2.erase(V1.cbegin()); // expected-warning{{Container accessed using foreign iterator argument}}
85 }
86
87 void bad_erase2(std::vector<int> &V1, std::vector<int> &V2) {
88 V2.erase(V2.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
89 V2.erase(V1.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
90 V2.erase(V1.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
91 }
92
93 void bad_emplace(std::vector<int> &V1, std::vector<int> &V2, int n) {
94 V2.emplace(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}}
95 }
96
97 void good_comparison(std::vector<int> &V) {
98 if (V.cbegin() == V.cend()) {} // no-warning
99 }
100
101 void bad_comparison(std::vector<int> &V1, std::vector<int> &V2) {
102 if (V1.cbegin() != V2.cend()) {} // expected-warning{{Iterators of different containers used where the same container is expected}}
103 }
104
105 void bad_ctor(std::vector<int> &V1, std::vector<int> &V2) {
106 std::vector<int> new_v(V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
107 }
108
109 void bad_find(std::vector<int> &V1, std::vector<int> &V2, int n) {
110 std::find(V1.cbegin(), V2.cend(), n); // expected-warning{{Iterators of different containers used where the same container is expected}}
111 }
112
113 void bad_find_first_of(std::vector<int> &V1, std::vector<int> &V2) {
114 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}}
115 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}}
116 }
117
118 std::vector<int> &return_vector_ref();
119
120 void ignore_conjured1() {
121 std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();
122
123 V2.erase(V1.cbegin()); // no-warning
124 }
125
126 void ignore_conjured2() {
127 std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();
128
129 if (V1.cbegin() == V2.cbegin()) {} //no-warning
130 }
131
132 template<typename T>
133 struct cont_with_ptr_iterator {
134 T *begin() const;
135 T *end() const;
136 };
137
138 void comparison_ptr_iterator(cont_with_ptr_iterator<int> &C1,
139 cont_with_ptr_iterator<int> &C2) {
140 if (C1.begin() != C2.end()) {} // expected-warning{{Iterators of different containers used where the same container is expected}}
141 }
142