2018-09-10 17:19:54 +08:00
// 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"
2019-11-08 19:52:09 +08:00
void good_insert1 ( std : : vector < int > & V , int n ) {
V . insert ( V . cbegin ( ) , n ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_insert2 ( std : : vector < int > & V , int len , int n ) {
V . insert ( V . cbegin ( ) , len , n ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_insert3 ( std : : vector < int > & V1 , std : : vector < int > & V2 ) {
V1 . insert ( V1 . cbegin ( ) , V2 . cbegin ( ) , V2 . cend ( ) ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_insert4 ( std : : vector < int > & V , int len , int n ) {
V . insert ( V . cbegin ( ) , { n - 1 , n , n + 1 } ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_erase1 ( std : : vector < int > & V ) {
V . erase ( V . cbegin ( ) ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_erase2 ( std : : vector < int > & V ) {
V . erase ( V . cbegin ( ) , V . cend ( ) ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_emplace ( std : : vector < int > & V , int n ) {
V . emplace ( V . cbegin ( ) , n ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_ctor ( std : : vector < int > & V ) {
std : : vector < int > new_v ( V . cbegin ( ) , V . cend ( ) ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_find ( std : : vector < int > & V , int n ) {
std : : find ( V . cbegin ( ) , V . cend ( ) , n ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void good_copy ( std : : vector < int > & V1 , std : : vector < int > & V2 , int n ) {
std : : copy ( V1 . cbegin ( ) , V1 . cend ( ) , V2 . begin ( ) ) ; // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
void bad_erase1 ( std : : vector < int > & V1 , std : : vector < int > & V2 ) {
V2 . erase ( V1 . cbegin ( ) ) ; // expected-warning{{Container accessed using foreign iterator argument}}
2018-10-13 18:24:48 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-10-13 18:24:48 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-10-13 18:24:48 +08:00
}
2019-11-08 19:52:09 +08:00
void good_comparison ( std : : vector < int > & V ) {
if ( V . cbegin ( ) = = V . cend ( ) ) { } // no-warning
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-11-08 19:52:09 +08:00
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}}
2018-09-10 17:19:54 +08:00
}
2019-03-13 21:55:11 +08:00
std : : vector < int > & return_vector_ref ( ) ;
void ignore_conjured1 ( ) {
2019-11-08 19:52:09 +08:00
std : : vector < int > & V1 = return_vector_ref ( ) , & V2 = return_vector_ref ( ) ;
2019-03-13 21:55:11 +08:00
2019-11-08 19:52:09 +08:00
V2 . erase ( V1 . cbegin ( ) ) ; // no-warning
2019-03-13 21:55:11 +08:00
}
void ignore_conjured2 ( ) {
2019-11-08 19:52:09 +08:00
std : : vector < int > & V1 = return_vector_ref ( ) , & V2 = return_vector_ref ( ) ;
2019-03-13 21:55:11 +08:00
2019-11-08 19:52:09 +08:00
if ( V1 . cbegin ( ) = = V2 . cbegin ( ) ) { } //no-warning
2019-03-13 21:55:11 +08:00
}
2020-06-17 02:00:47 +08:00
template < typename T >
struct cont_with_ptr_iterator {
T * begin ( ) const ;
T * end ( ) const ;
} ;
void comparison_ptr_iterator ( cont_with_ptr_iterator < int > & C1 ,
cont_with_ptr_iterator < int > & C2 ) {
if ( C1 . begin ( ) ! = C2 . end ( ) ) { } // expected-warning{{Iterators of different containers used where the same container is expected}}
}