85 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-nondeterministic-pointer-iteration-order %t -- -- -I%S -std=c++!42 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 10template<class T>11void f(T x);12 13void 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-warning25 f(i);26 27 for (auto i : OrderedPtrSet) // no-warning28 f(i);29 30 for (auto i : UnorderedIntSet) // no-warning31 f(i);32 33 for (auto i : UnorderedPtrSet)34 f(i);35 // CHECK-MESSAGES: :[[@LINE-2]]:17: warning: iteration of pointers is nondeterministic36 37 for (auto &i : UnorderedPtrSet)38 f(i);39 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic40 41 for (auto &i : IntMap) // no-warning42 f(i);43 44 for (auto &i : PtrMap) // no-warning45 f(i);46 47 for (auto &i : IntUnorderedMap) // no-warning48 f(i);49 50 for (auto &i : PtrUnorderedMap)51 f(i);52 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic53}54 55bool g (int *x) { return true; }56bool h (int x) { return true; }57 58void 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-warning64 std::nth_element(V1.begin(), V1.begin() + 1, V1.end()); // no-warning65 std::partial_sort(V1.begin(), V1.begin() + 1, V1.end()); // no-warning66 std::sort(V1.begin(), V1.end()); // no-warning67 std::stable_sort(V1.begin(), V1.end()); // no-warning68 std::partition(V1.begin(), V1.end(), h); // no-warning69 std::stable_partition(V1.begin(), V1.end(), h); // no-warning70 std::is_sorted(V2.begin(), V2.end());71 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic72 std::nth_element(V2.begin(), V2.begin() + 1, V2.end());73 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic74 std::partial_sort(V2.begin(), V2.begin() + 1, V2.end());75 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic76 std::sort(V2.begin(), V2.end());77 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic78 std::stable_sort(V2.begin(), V2.end());79 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic80 std::partition(V2.begin(), V2.end(), g);81 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic82 std::stable_partition(V2.begin(), V2.end(), g);83 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic84}85