1bf77ab7fSAdam Balogh // 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
2bf77ab7fSAdam Balogh // 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
3bf77ab7fSAdam Balogh 
4bf77ab7fSAdam Balogh #include "Inputs/system-header-simulator-cxx.h"
5bf77ab7fSAdam Balogh 
good_insert1(std::vector<int> & V,int n)623022b93SAdam Balogh void good_insert1(std::vector<int> &V, int n) {
723022b93SAdam Balogh   V.insert(V.cbegin(), n); // no-warning
8bf77ab7fSAdam Balogh }
9bf77ab7fSAdam Balogh 
good_insert2(std::vector<int> & V,int len,int n)1023022b93SAdam Balogh void good_insert2(std::vector<int> &V, int len, int n) {
1123022b93SAdam Balogh   V.insert(V.cbegin(), len, n); // no-warning
12bf77ab7fSAdam Balogh }
13bf77ab7fSAdam Balogh 
good_insert3(std::vector<int> & V1,std::vector<int> & V2)1423022b93SAdam Balogh void good_insert3(std::vector<int> &V1, std::vector<int> &V2) {
1523022b93SAdam Balogh   V1.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // no-warning
16bf77ab7fSAdam Balogh }
17bf77ab7fSAdam Balogh 
good_insert4(std::vector<int> & V,int len,int n)1823022b93SAdam Balogh void good_insert4(std::vector<int> &V, int len, int n) {
1923022b93SAdam Balogh   V.insert(V.cbegin(), {n-1, n, n+1}); // no-warning
20bf77ab7fSAdam Balogh }
21bf77ab7fSAdam Balogh 
good_insert_find(std::vector<int> & V,int n,int m)2223022b93SAdam Balogh void good_insert_find(std::vector<int> &V, int n, int m) {
2323022b93SAdam Balogh   auto i = std::find(V.cbegin(), V.cend(), n);
2423022b93SAdam Balogh   V.insert(i, m); // no-warning
25bf77ab7fSAdam Balogh }
26bf77ab7fSAdam Balogh 
good_erase1(std::vector<int> & V)2723022b93SAdam Balogh void good_erase1(std::vector<int> &V) {
2823022b93SAdam Balogh   V.erase(V.cbegin()); // no-warning
29bf77ab7fSAdam Balogh }
30bf77ab7fSAdam Balogh 
good_erase2(std::vector<int> & V)3123022b93SAdam Balogh void good_erase2(std::vector<int> &V) {
3223022b93SAdam Balogh   V.erase(V.cbegin(), V.cend()); // no-warning
33bf77ab7fSAdam Balogh }
34bf77ab7fSAdam Balogh 
good_emplace(std::vector<int> & V,int n)3523022b93SAdam Balogh void good_emplace(std::vector<int> &V, int n) {
3623022b93SAdam Balogh   V.emplace(V.cbegin(), n); // no-warning
37bf77ab7fSAdam Balogh }
38bf77ab7fSAdam Balogh 
good_ctor(std::vector<int> & V)3923022b93SAdam Balogh void good_ctor(std::vector<int> &V) {
4023022b93SAdam Balogh   std::vector<int> new_v(V.cbegin(), V.cend()); // no-warning
41bf77ab7fSAdam Balogh }
42bf77ab7fSAdam Balogh 
good_find(std::vector<int> & V,int n)4323022b93SAdam Balogh void good_find(std::vector<int> &V, int n) {
4423022b93SAdam Balogh   std::find(V.cbegin(), V.cend(), n); // no-warning
45bf77ab7fSAdam Balogh }
46bf77ab7fSAdam Balogh 
good_find_first_of(std::vector<int> & V1,std::vector<int> & V2)4723022b93SAdam Balogh void good_find_first_of(std::vector<int> &V1, std::vector<int> &V2) {
4823022b93SAdam Balogh   std::find_first_of(V1.cbegin(), V1.cend(), V2.cbegin(), V2.cend()); // no-warning
49bf77ab7fSAdam Balogh }
50bf77ab7fSAdam Balogh 
good_copy(std::vector<int> & V1,std::vector<int> & V2,int n)5123022b93SAdam Balogh void good_copy(std::vector<int> &V1, std::vector<int> &V2, int n) {
5223022b93SAdam Balogh   std::copy(V1.cbegin(), V1.cend(), V2.begin()); // no-warning
53bf77ab7fSAdam Balogh }
54bf77ab7fSAdam Balogh 
bad_insert1(std::vector<int> & V1,std::vector<int> & V2,int n)5523022b93SAdam Balogh void bad_insert1(std::vector<int> &V1, std::vector<int> &V2, int n) {
5623022b93SAdam Balogh   V2.insert(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}}
57bf77ab7fSAdam Balogh }
58bf77ab7fSAdam Balogh 
bad_insert2(std::vector<int> & V1,std::vector<int> & V2,int len,int n)5923022b93SAdam Balogh void bad_insert2(std::vector<int> &V1, std::vector<int> &V2, int len, int n) {
6023022b93SAdam Balogh   V2.insert(V1.cbegin(), len, n); // expected-warning{{Container accessed using foreign iterator argument}}
61bf77ab7fSAdam Balogh }
62bf77ab7fSAdam Balogh 
bad_insert3(std::vector<int> & V1,std::vector<int> & V2)6323022b93SAdam Balogh void bad_insert3(std::vector<int> &V1, std::vector<int> &V2) {
6423022b93SAdam Balogh   V2.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
6523022b93SAdam Balogh   V1.insert(V1.cbegin(), V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
6623022b93SAdam Balogh   V1.insert(V1.cbegin(), V2.cbegin(), V1.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
67bf77ab7fSAdam Balogh }
68bf77ab7fSAdam Balogh 
bad_insert4(std::vector<int> & V1,std::vector<int> & V2,int len,int n)6923022b93SAdam Balogh void bad_insert4(std::vector<int> &V1, std::vector<int> &V2, int len, int n) {
7023022b93SAdam Balogh   V2.insert(V1.cbegin(), {n-1, n, n+1}); // expected-warning{{Container accessed using foreign iterator argument}}
71bf77ab7fSAdam Balogh }
72bf77ab7fSAdam Balogh 
bad_erase1(std::vector<int> & V1,std::vector<int> & V2)7323022b93SAdam Balogh void bad_erase1(std::vector<int> &V1, std::vector<int> &V2) {
7423022b93SAdam Balogh   V2.erase(V1.cbegin()); // expected-warning{{Container accessed using foreign iterator argument}}
75bf77ab7fSAdam Balogh }
76bf77ab7fSAdam Balogh 
bad_erase2(std::vector<int> & V1,std::vector<int> & V2)7723022b93SAdam Balogh void bad_erase2(std::vector<int> &V1, std::vector<int> &V2) {
7823022b93SAdam Balogh   V2.erase(V2.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
7923022b93SAdam Balogh   V2.erase(V1.cbegin(), V2.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
8023022b93SAdam Balogh   V2.erase(V1.cbegin(), V1.cend()); // expected-warning{{Container accessed using foreign iterator argument}}
81bf77ab7fSAdam Balogh }
82bf77ab7fSAdam Balogh 
bad_emplace(std::vector<int> & V1,std::vector<int> & V2,int n)8323022b93SAdam Balogh void bad_emplace(std::vector<int> &V1, std::vector<int> &V2, int n) {
8423022b93SAdam Balogh   V2.emplace(V1.cbegin(), n); // expected-warning{{Container accessed using foreign iterator argument}}
85bf77ab7fSAdam Balogh }
86bf77ab7fSAdam Balogh 
good_comparison(std::vector<int> & V)8723022b93SAdam Balogh void good_comparison(std::vector<int> &V) {
8823022b93SAdam Balogh   if (V.cbegin() == V.cend()) {} // no-warning
89bf77ab7fSAdam Balogh }
90bf77ab7fSAdam Balogh 
bad_comparison(std::vector<int> & V1,std::vector<int> & V2)9123022b93SAdam Balogh void bad_comparison(std::vector<int> &V1, std::vector<int> &V2) {
9223022b93SAdam Balogh   if (V1.cbegin() != V2.cend()) {} // expected-warning{{Iterators of different containers used where the same container is expected}}
93bf77ab7fSAdam Balogh }
94bf77ab7fSAdam Balogh 
bad_ctor(std::vector<int> & V1,std::vector<int> & V2)9523022b93SAdam Balogh void bad_ctor(std::vector<int> &V1, std::vector<int> &V2) {
9623022b93SAdam Balogh   std::vector<int> new_v(V1.cbegin(), V2.cend()); // expected-warning{{Iterators of different containers used where the same container is expected}}
97bf77ab7fSAdam Balogh }
98bf77ab7fSAdam Balogh 
bad_find(std::vector<int> & V1,std::vector<int> & V2,int n)9923022b93SAdam Balogh void bad_find(std::vector<int> &V1, std::vector<int> &V2, int n) {
10023022b93SAdam Balogh   std::find(V1.cbegin(), V2.cend(), n); // expected-warning{{Iterators of different containers used where the same container is expected}}
101bf77ab7fSAdam Balogh }
102bf77ab7fSAdam Balogh 
bad_find_first_of(std::vector<int> & V1,std::vector<int> & V2)10323022b93SAdam Balogh void bad_find_first_of(std::vector<int> &V1, std::vector<int> &V2) {
10423022b93SAdam Balogh   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}}
10523022b93SAdam Balogh   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}}
106bf77ab7fSAdam Balogh }
107d703305eSAdam Balogh 
108d703305eSAdam Balogh std::vector<int> &return_vector_ref();
109d703305eSAdam Balogh 
ignore_conjured1()110d703305eSAdam Balogh void ignore_conjured1() {
11123022b93SAdam Balogh   std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();
112d703305eSAdam Balogh 
11323022b93SAdam Balogh   V2.erase(V1.cbegin()); // no-warning
114d703305eSAdam Balogh }
115d703305eSAdam Balogh 
ignore_conjured2()116d703305eSAdam Balogh void ignore_conjured2() {
11723022b93SAdam Balogh   std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();
118d703305eSAdam Balogh 
11923022b93SAdam Balogh   if (V1.cbegin() == V2.cbegin()) {} //no-warning
120d703305eSAdam Balogh }
121*9e63b190SAdam Balogh 
122*9e63b190SAdam Balogh template<typename T>
123*9e63b190SAdam Balogh struct cont_with_ptr_iterator {
124*9e63b190SAdam Balogh   T *begin() const;
125*9e63b190SAdam Balogh   T *end() const;
126*9e63b190SAdam Balogh };
127*9e63b190SAdam Balogh 
comparison_ptr_iterator(cont_with_ptr_iterator<int> & C1,cont_with_ptr_iterator<int> & C2)128*9e63b190SAdam Balogh void comparison_ptr_iterator(cont_with_ptr_iterator<int> &C1,
129*9e63b190SAdam Balogh                              cont_with_ptr_iterator<int> &C2) {
130*9e63b190SAdam Balogh   if (C1.begin() != C2.end()) {} // expected-warning{{Iterators of different containers used where the same container is expected}}
131*9e63b190SAdam Balogh }
132*9e63b190SAdam Balogh 
133