1 // RUN: %clang_analyze_cc1 %s -std=c++14 -analyzer-output=text -verify \
2 // RUN: -analyzer-checker=core,alpha.nondeterminism.PointerSorting
3 
4 #include "Inputs/system-header-simulator-cxx.h"
5 
f(int x)6 bool f(int x) { return true; }
g(int * x)7 bool g(int *x) { return true; }
8 
PointerSorting()9 void PointerSorting() {
10   int a = 1, b = 2;
11   std::vector<int> V1 = {a, b};
12   std::vector<int *> V2 = {&a, &b};
13 
14   std::is_sorted(V1.begin(), V1.end());                    // no-warning
15   std::nth_element(V1.begin(), V1.begin() + 1, V1.end());  // no-warning
16   std::partial_sort(V1.begin(), V1.begin() + 1, V1.end()); // no-warning
17   std::sort(V1.begin(), V1.end());                         // no-warning
18   std::stable_sort(V1.begin(), V1.end());                  // no-warning
19   std::partition(V1.begin(), V1.end(), f);                 // no-warning
20   std::stable_partition(V1.begin(), V1.end(), g);          // no-warning
21 
22   std::is_sorted(V2.begin(), V2.end()); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
23   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
24   std::nth_element(V2.begin(), V2.begin() + 1, V2.end()); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
25   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
26   std::partial_sort(V2.begin(), V2.begin() + 1, V2.end()); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
27   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
28   std::sort(V2.begin(), V2.end()); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
29   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
30   std::stable_sort(V2.begin(), V2.end()); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
31   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
32   std::partition(V2.begin(), V2.end(), f); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
33   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
34   std::stable_partition(V2.begin(), V2.end(), g); // expected-warning {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
35   // expected-note@-1 {{Sorting pointer-like elements can result in non-deterministic ordering}} [alpha.nondeterminism.PointerSorting]
36 }
37