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