brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.7 KiB · 325e776 Raw
143 lines · cpp
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 -verify2// 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 -verify3 4#include "Inputs/system-header-simulator-cxx.h"5 6void good_insert1(std::vector<int> &V, int n) {7  V.insert(V.cbegin(), n); // no-warning8}9 10void good_insert2(std::vector<int> &V, int len, int n) {11  V.insert(V.cbegin(), len, n); // no-warning12}13 14void good_insert3(std::vector<int> &V1, std::vector<int> &V2) {15  V1.insert(V1.cbegin(), V2.cbegin(), V2.cend()); // no-warning16}17 18void good_insert4(std::vector<int> &V, int len, int n) {19  V.insert(V.cbegin(), {n-1, n, n+1}); // no-warning20}21 22void good_insert5(std::vector<int> &V, int len, int n) {23  std::unordered_set<int> us;24  us.insert(V.cbegin(), V.cend()); // no-warning25}26 27void 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-warning30}31 32void good_erase1(std::vector<int> &V) {33  V.erase(V.cbegin()); // no-warning34}35 36void good_erase2(std::vector<int> &V) {37  V.erase(V.cbegin(), V.cend()); // no-warning38}39 40void good_emplace(std::vector<int> &V, int n) {41  V.emplace(V.cbegin(), n); // no-warning42}43 44void good_ctor(std::vector<int> &V) {45  std::vector<int> new_v(V.cbegin(), V.cend()); // no-warning46}47 48void good_find(std::vector<int> &V, int n) {49  std::find(V.cbegin(), V.cend(), n); // no-warning50}51 52void 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-warning54}55 56void good_copy(std::vector<int> &V1, std::vector<int> &V2, int n) {57  std::copy(V1.cbegin(), V1.cend(), V2.begin()); // no-warning58}59 60void 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 64void 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 68void 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 74void 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 78void 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 83void 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 87void 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 93void 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 97void good_comparison(std::vector<int> &V) {98  if (V.cbegin() == V.cend()) {} // no-warning99}100 101void 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 105void 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 109void 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 113void 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 118std::vector<int> &return_vector_ref();119 120void ignore_conjured1() {121  std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();122 123  V2.erase(V1.cbegin()); // no-warning124}125 126void ignore_conjured2() {127  std::vector<int> &V1 = return_vector_ref(), &V2 = return_vector_ref();128 129  if (V1.cbegin() == V2.cbegin()) {} //no-warning130}131 132template<typename T>133struct cont_with_ptr_iterator {134  T *begin() const;135  T *end() const;136};137 138void 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 143