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
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-warning
25 f(x: i);
26
27 for (auto i : OrderedPtrSet) // no-warning
28 f(x: i);
29
30 for (auto i : UnorderedIntSet) // no-warning
31 f(x: i);
32
33 for (auto i : UnorderedPtrSet)
34 f(x: i);
35 // CHECK-MESSAGES: :[[@LINE-2]]:17: warning: iteration of pointers is nondeterministic
36
37 for (auto &i : UnorderedPtrSet)
38 f(x: i);
39 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic
40
41 for (auto &i : IntMap) // no-warning
42 f(x: i);
43
44 for (auto &i : PtrMap) // no-warning
45 f(x: i);
46
47 for (auto &i : IntUnorderedMap) // no-warning
48 f(x: i);
49
50 for (auto &i : PtrUnorderedMap)
51 f(x: i);
52 // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: iteration of pointers is nondeterministic
53}
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(first: V1.begin(), last: V1.end()); // no-warning
64 std::nth_element(first: V1.begin(), nth: V1.begin() + 1, last: V1.end()); // no-warning
65 std::partial_sort(first: V1.begin(), middle: V1.begin() + 1, last: V1.end()); // no-warning
66 std::sort(first: V1.begin(), last: V1.end()); // no-warning
67 std::stable_sort(first: V1.begin(), last: V1.end()); // no-warning
68 std::partition(first: V1.begin(), last: V1.end(), p: h); // no-warning
69 std::stable_partition(first: V1.begin(), last: V1.end(), p: h); // no-warning
70 std::is_sorted(first: V2.begin(), last: V2.end());
71 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
72 std::nth_element(first: V2.begin(), nth: V2.begin() + 1, last: V2.end());
73 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
74 std::partial_sort(first: V2.begin(), middle: V2.begin() + 1, last: V2.end());
75 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
76 std::sort(first: V2.begin(), last: V2.end());
77 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
78 std::stable_sort(first: V2.begin(), last: V2.end());
79 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
80 std::partition(first: V2.begin(), last: V2.end(), p: g);
81 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
82 std::stable_partition(first: V2.begin(), last: V2.end(), p: g);
83 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: sorting pointers is nondeterministic
84}
85

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/nondeterministic-pointer-iteration-order.cpp