1 // RUN: %check_clang_tidy %s bugprone-nondeterministic-pointer-iteration-order %t -- -- -I%S -std=c++!4 2 3 #include "Inputs/system-header-simulator/sim_set" 4 #include "Inputs/system-header-simulator/sim_unordered_set" 5 #include "Inputs/system-header-simulator/sim_map" 6 #include "Inputs/system-header-simulator/sim_unordered_map" 7 #include "Inputs/system-header-simulator/sim_vector" 8 #include "Inputs/system-header-simulator/sim_algorithm" 9 10 template<class T> 11 void f(T x); 12 13 void PointerIteration() { 14 int a = 1, b = 2; 15 std::set<int> OrderedIntSet = {a, b}; 16 std::set<int *> OrderedPtrSet = {&a, &b}; 17 std::unordered_set<int> UnorderedIntSet = {a, b}; 18 std::unordered_set<int *> UnorderedPtrSet = {&a, &b}; 19 std::map<int, int> IntMap = { std::make_pair(a,a), std::make_pair(b,b) }; 20 std::map<int*, int*> PtrMap = { std::make_pair(&a,&a), std::make_pair(&b,&b) }; 21 std::unordered_map<int, int> IntUnorderedMap = { std::make_pair(a,a), std::make_pair(b,b) }; 22 std::unordered_map<int*, int*> PtrUnorderedMap = { std::make_pair(&a,&a), std::make_pair(&b,&b) }; 23 24 for (auto i : OrderedIntSet) // no-warning 25 f(i); 26 27 for (auto i : OrderedPtrSet) // no-warning 28 f(i); 29 30 for (auto i : UnorderedIntSet) // no-warning 31 f(i); 32 33 for (auto i : UnorderedPtrSet) 34 f(i); 35 // CHECK-MESSAGES: :[[@LINE-2]]:17: warning: iteration of pointers is nondeterministic 36 37 for (auto &i : UnorderedPtrSet) 38 f(i); 39 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic 40 41 for (auto &i : IntMap) // no-warning 42 f(i); 43 44 for (auto &i : PtrMap) // no-warning 45 f(i); 46 47 for (auto &i : IntUnorderedMap) // no-warning 48 f(i); 49 50 for (auto &i : PtrUnorderedMap) 51 f(i); 52 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic 53 } 54 55 bool g (int *x) { return true; } 56 bool h (int x) { return true; } 57 58 void PointerSorting() { 59 int a = 1, b = 2, c = 3; 60 std::vector<int> V1 = {a, b}; 61 std::vector<int *> V2 = {&a, &b}; 62 63 std::is_sorted(V1.begin(), V1.end()); // no-warning 64 std::nth_element(V1.begin(), V1.begin() + 1, V1.end()); // no-warning 65 std::partial_sort(V1.begin(), V1.begin() + 1, V1.end()); // no-warning 66 std::sort(V1.begin(), V1.end()); // no-warning 67 std::stable_sort(V1.begin(), V1.end()); // no-warning 68 std::partition(V1.begin(), V1.end(), h); // no-warning 69 std::stable_partition(V1.begin(), V1.end(), h); // no-warning 70 std::is_sorted(V2.begin(), V2.end()); 71 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 72 std::nth_element(V2.begin(), V2.begin() + 1, V2.end()); 73 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 74 std::partial_sort(V2.begin(), V2.begin() + 1, V2.end()); 75 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 76 std::sort(V2.begin(), V2.end()); 77 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 78 std::stable_sort(V2.begin(), V2.end()); 79 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 80 std::partition(V2.begin(), V2.end(), g); 81 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 82 std::stable_partition(V2.begin(), V2.end(), g); 83 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic 84 } 85