1a3f4d17aSAdam Balogh // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.IteratorRange -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=false -analyzer-output=text %s -verify
2a3f4d17aSAdam Balogh // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.IteratorRange -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 -analyzer-output=text %s -verify
38fa639ecSArtem Dergachev
48fa639ecSArtem Dergachev #include "Inputs/system-header-simulator-cxx.h"
58fa639ecSArtem Dergachev
68fa639ecSArtem Dergachev void clang_analyzer_warnIfReached();
78fa639ecSArtem Dergachev
823022b93SAdam Balogh // Dereference - operator*()
923022b93SAdam Balogh
deref_begin(const std::vector<int> & V)1023022b93SAdam Balogh void deref_begin(const std::vector<int> &V) {
1123022b93SAdam Balogh auto i = V.begin();
128fa639ecSArtem Dergachev *i; // no-warning
138fa639ecSArtem Dergachev }
148fa639ecSArtem Dergachev
deref_begind_begin(const std::vector<int> & V)1523022b93SAdam Balogh void deref_begind_begin(const std::vector<int> &V) {
1623022b93SAdam Balogh auto i = ++V.begin();
17b03ed5e4SAdam Balogh *i; // no-warning
18b03ed5e4SAdam Balogh }
1923022b93SAdam Balogh
2023022b93SAdam Balogh template <typename Iter> Iter return_any_iterator(const Iter &It);
2123022b93SAdam Balogh
deref_unknown(const std::vector<int> & V)2223022b93SAdam Balogh void deref_unknown(const std::vector<int> &V) {
2323022b93SAdam Balogh auto i = return_any_iterator(V.begin());
2423022b93SAdam Balogh *i; // no-warning
25b03ed5e4SAdam Balogh }
26b03ed5e4SAdam Balogh
deref_ahead_of_end(const std::vector<int> & V)2723022b93SAdam Balogh void deref_ahead_of_end(const std::vector<int> &V) {
2823022b93SAdam Balogh auto i = --V.end();
2923022b93SAdam Balogh *i; // no-warning
3023022b93SAdam Balogh }
3123022b93SAdam Balogh
deref_end(const std::vector<int> & V)3223022b93SAdam Balogh void deref_end(const std::vector<int> &V) {
3323022b93SAdam Balogh auto i = V.end();
34d5bd3f63SAdam Balogh *i; // expected-warning{{Past-the-end iterator dereferenced}}
35a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
36b03ed5e4SAdam Balogh }
37b03ed5e4SAdam Balogh
3823022b93SAdam Balogh // Prefix increment - operator++()
3923022b93SAdam Balogh
incr_begin(const std::vector<int> & V)4023022b93SAdam Balogh void incr_begin(const std::vector<int> &V) {
4123022b93SAdam Balogh auto i = V.begin();
4223022b93SAdam Balogh ++i; // no-warning
43b03ed5e4SAdam Balogh }
44b03ed5e4SAdam Balogh
incr_behind_begin(const std::vector<int> & V)4523022b93SAdam Balogh void incr_behind_begin(const std::vector<int> &V) {
4623022b93SAdam Balogh auto i = ++V.begin();
4723022b93SAdam Balogh ++i; // no-warning
4823022b93SAdam Balogh }
4923022b93SAdam Balogh
incr_unknown(const std::vector<int> & V)5023022b93SAdam Balogh void incr_unknown(const std::vector<int> &V) {
5123022b93SAdam Balogh auto i = return_any_iterator(V.begin());
5223022b93SAdam Balogh ++i; // no-warning
5323022b93SAdam Balogh }
5423022b93SAdam Balogh
incr_ahead_of_end(const std::vector<int> & V)5523022b93SAdam Balogh void incr_ahead_of_end(const std::vector<int> &V) {
5623022b93SAdam Balogh auto i = --V.end();
5723022b93SAdam Balogh ++i; // no-warning
5823022b93SAdam Balogh }
5923022b93SAdam Balogh
incr_end(const std::vector<int> & V)6023022b93SAdam Balogh void incr_end(const std::vector<int> &V) {
6123022b93SAdam Balogh auto i = V.end();
6223022b93SAdam Balogh ++i; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
63a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
6423022b93SAdam Balogh }
6523022b93SAdam Balogh
6623022b93SAdam Balogh // Postfix increment - operator++(int)
6723022b93SAdam Balogh
begin_incr(const std::vector<int> & V)6823022b93SAdam Balogh void begin_incr(const std::vector<int> &V) {
6923022b93SAdam Balogh auto i = V.begin();
7023022b93SAdam Balogh i++; // no-warning
7123022b93SAdam Balogh }
7223022b93SAdam Balogh
behind_begin_incr(const std::vector<int> & V)7323022b93SAdam Balogh void behind_begin_incr(const std::vector<int> &V) {
7423022b93SAdam Balogh auto i = ++V.begin();
7523022b93SAdam Balogh i++; // no-warning
7623022b93SAdam Balogh }
7723022b93SAdam Balogh
unknown_incr(const std::vector<int> & V)7823022b93SAdam Balogh void unknown_incr(const std::vector<int> &V) {
7923022b93SAdam Balogh auto i = return_any_iterator(V.begin());
8023022b93SAdam Balogh i++; // no-warning
8123022b93SAdam Balogh }
8223022b93SAdam Balogh
ahead_of_end_incr(const std::vector<int> & V)8323022b93SAdam Balogh void ahead_of_end_incr(const std::vector<int> &V) {
8423022b93SAdam Balogh auto i = --V.end();
8523022b93SAdam Balogh i++; // no-warning
8623022b93SAdam Balogh }
8723022b93SAdam Balogh
end_incr(const std::vector<int> & V)8823022b93SAdam Balogh void end_incr(const std::vector<int> &V) {
8923022b93SAdam Balogh auto i = V.end();
9023022b93SAdam Balogh i++; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
91a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
9223022b93SAdam Balogh }
9323022b93SAdam Balogh
9423022b93SAdam Balogh // Prefix decrement - operator--()
9523022b93SAdam Balogh
decr_begin(const std::vector<int> & V)9623022b93SAdam Balogh void decr_begin(const std::vector<int> &V) {
9723022b93SAdam Balogh auto i = V.begin();
9823022b93SAdam Balogh --i; // expected-warning{{Iterator decremented ahead of its valid range}}
99a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
10023022b93SAdam Balogh }
10123022b93SAdam Balogh
decr_behind_begin(const std::vector<int> & V)10223022b93SAdam Balogh void decr_behind_begin(const std::vector<int> &V) {
10323022b93SAdam Balogh auto i = ++V.begin();
10423022b93SAdam Balogh --i; // no-warning
10523022b93SAdam Balogh }
10623022b93SAdam Balogh
decr_unknown(const std::vector<int> & V)10723022b93SAdam Balogh void decr_unknown(const std::vector<int> &V) {
10823022b93SAdam Balogh auto i = return_any_iterator(V.begin());
10923022b93SAdam Balogh --i; // no-warning
11023022b93SAdam Balogh }
11123022b93SAdam Balogh
decr_ahead_of_end(const std::vector<int> & V)11223022b93SAdam Balogh void decr_ahead_of_end(const std::vector<int> &V) {
11323022b93SAdam Balogh auto i = --V.end();
11423022b93SAdam Balogh --i; // no-warning
11523022b93SAdam Balogh }
11623022b93SAdam Balogh
decr_end(const std::vector<int> & V)11723022b93SAdam Balogh void decr_end(const std::vector<int> &V) {
11823022b93SAdam Balogh auto i = V.end();
11923022b93SAdam Balogh --i; // no-warning
12023022b93SAdam Balogh }
12123022b93SAdam Balogh
12223022b93SAdam Balogh // Postfix decrement - operator--(int)
12323022b93SAdam Balogh
begin_decr(const std::vector<int> & V)12423022b93SAdam Balogh void begin_decr(const std::vector<int> &V) {
12523022b93SAdam Balogh auto i = V.begin();
12623022b93SAdam Balogh i--; // expected-warning{{Iterator decremented ahead of its valid range}}
127a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
12823022b93SAdam Balogh }
12923022b93SAdam Balogh
behind_begin_decr(const std::vector<int> & V)13023022b93SAdam Balogh void behind_begin_decr(const std::vector<int> &V) {
13123022b93SAdam Balogh auto i = ++V.begin();
13223022b93SAdam Balogh i--; // no-warning
13323022b93SAdam Balogh }
13423022b93SAdam Balogh
unknown_decr(const std::vector<int> & V)13523022b93SAdam Balogh void unknown_decr(const std::vector<int> &V) {
13623022b93SAdam Balogh auto i = return_any_iterator(V.begin());
13723022b93SAdam Balogh i--; // no-warning
13823022b93SAdam Balogh }
13923022b93SAdam Balogh
ahead_of_end_decr(const std::vector<int> & V)14023022b93SAdam Balogh void ahead_of_end_decr(const std::vector<int> &V) {
14123022b93SAdam Balogh auto i = --V.end();
14223022b93SAdam Balogh i--; // no-warning
14323022b93SAdam Balogh }
14423022b93SAdam Balogh
end_decr(const std::vector<int> & V)14523022b93SAdam Balogh void end_decr(const std::vector<int> &V) {
14623022b93SAdam Balogh auto i = V.end();
14723022b93SAdam Balogh i--; // no-warning
14823022b93SAdam Balogh }
14923022b93SAdam Balogh
15023022b93SAdam Balogh // Addition assignment - operator+=(int)
15123022b93SAdam Balogh
incr_by_2_begin(const std::vector<int> & V)15223022b93SAdam Balogh void incr_by_2_begin(const std::vector<int> &V) {
15323022b93SAdam Balogh auto i = V.begin();
15423022b93SAdam Balogh i += 2; // no-warning
15523022b93SAdam Balogh }
15623022b93SAdam Balogh
incr_by_2_behind_begin(const std::vector<int> & V)15723022b93SAdam Balogh void incr_by_2_behind_begin(const std::vector<int> &V) {
15823022b93SAdam Balogh auto i = ++V.begin();
15923022b93SAdam Balogh i += 2; // no-warning
16023022b93SAdam Balogh }
16123022b93SAdam Balogh
incr_by_2_unknown(const std::vector<int> & V)16223022b93SAdam Balogh void incr_by_2_unknown(const std::vector<int> &V) {
16323022b93SAdam Balogh auto i = return_any_iterator(V.begin());
16423022b93SAdam Balogh i += 2; // no-warning
16523022b93SAdam Balogh }
16623022b93SAdam Balogh
incr_by_2_ahead_by_2_of_end(const std::vector<int> & V)16723022b93SAdam Balogh void incr_by_2_ahead_by_2_of_end(const std::vector<int> &V) {
16823022b93SAdam Balogh auto i = --V.end();
169b03ed5e4SAdam Balogh --i;
17023022b93SAdam Balogh i += 2; // no-warning
171b03ed5e4SAdam Balogh }
172b03ed5e4SAdam Balogh
incr_by_2_ahead_of_end(const std::vector<int> & V)17323022b93SAdam Balogh void incr_by_2_ahead_of_end(const std::vector<int> &V) {
17423022b93SAdam Balogh auto i = --V.end();
17523022b93SAdam Balogh i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
176a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
177b03ed5e4SAdam Balogh }
178b03ed5e4SAdam Balogh
incr_by_2_end(const std::vector<int> & V)17923022b93SAdam Balogh void incr_by_2_end(const std::vector<int> &V) {
18023022b93SAdam Balogh auto i = V.end();
18123022b93SAdam Balogh i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
182a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
183b03ed5e4SAdam Balogh }
184b03ed5e4SAdam Balogh
18523022b93SAdam Balogh // Addition - operator+(int)
18623022b93SAdam Balogh
incr_by_2_copy_begin(const std::vector<int> & V)18723022b93SAdam Balogh void incr_by_2_copy_begin(const std::vector<int> &V) {
18823022b93SAdam Balogh auto i = V.begin();
18923022b93SAdam Balogh auto j = i + 2; // no-warning
190b03ed5e4SAdam Balogh }
191b03ed5e4SAdam Balogh
incr_by_2_copy_behind_begin(const std::vector<int> & V)19223022b93SAdam Balogh void incr_by_2_copy_behind_begin(const std::vector<int> &V) {
19323022b93SAdam Balogh auto i = ++V.begin();
19423022b93SAdam Balogh auto j = i + 2; // no-warning
195b03ed5e4SAdam Balogh }
196b03ed5e4SAdam Balogh
incr_by_2_copy_unknown(const std::vector<int> & V)19723022b93SAdam Balogh void incr_by_2_copy_unknown(const std::vector<int> &V) {
19823022b93SAdam Balogh auto i = return_any_iterator(V.begin());
19923022b93SAdam Balogh auto j = i + 2; // no-warning
200b03ed5e4SAdam Balogh }
201b03ed5e4SAdam Balogh
incr_by_2_copy_ahead_by_2_of_end(const std::vector<int> & V)20223022b93SAdam Balogh void incr_by_2_copy_ahead_by_2_of_end(const std::vector<int> &V) {
20323022b93SAdam Balogh auto i = --V.end();
20423022b93SAdam Balogh --i;
20523022b93SAdam Balogh auto j = i + 2; // no-warning
20645ca9b70SAdam Balogh }
20745ca9b70SAdam Balogh
incr_by_2_copy_ahead_of_end(const std::vector<int> & V)20823022b93SAdam Balogh void incr_by_2_copy_ahead_of_end(const std::vector<int> &V) {
20923022b93SAdam Balogh auto i = --V.end();
21023022b93SAdam Balogh auto j = i + 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
211a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
21245ca9b70SAdam Balogh }
21345ca9b70SAdam Balogh
incr_by_2_copy_end(const std::vector<int> & V)21423022b93SAdam Balogh void incr_by_2_copy_end(const std::vector<int> &V) {
21523022b93SAdam Balogh auto i = V.end();
21623022b93SAdam Balogh auto j = i + 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
217a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
21845ca9b70SAdam Balogh }
21945ca9b70SAdam Balogh
22023022b93SAdam Balogh // Subtraction assignment - operator-=(int)
22123022b93SAdam Balogh
decr_by_2_begin(const std::vector<int> & V)22223022b93SAdam Balogh void decr_by_2_begin(const std::vector<int> &V) {
22323022b93SAdam Balogh auto i = V.begin();
22423022b93SAdam Balogh i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}
225a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
226b03ed5e4SAdam Balogh }
227b03ed5e4SAdam Balogh
decr_by_2_behind_begin(const std::vector<int> & V)22823022b93SAdam Balogh void decr_by_2_behind_begin(const std::vector<int> &V) {
22923022b93SAdam Balogh auto i = ++V.begin();
23023022b93SAdam Balogh i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}
231a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
232b03ed5e4SAdam Balogh }
233b03ed5e4SAdam Balogh
decr_by_2_behind_begin_by_2(const std::vector<int> & V)23423022b93SAdam Balogh void decr_by_2_behind_begin_by_2(const std::vector<int> &V) {
23523022b93SAdam Balogh auto i = ++V.begin();
23623022b93SAdam Balogh ++i;
23723022b93SAdam Balogh i -= 2; // no-warning
2389a48ba6bSAdam Balogh }
2399a48ba6bSAdam Balogh
decr_by_2_unknown(const std::vector<int> & V)24023022b93SAdam Balogh void decr_by_2_unknown(const std::vector<int> &V) {
24123022b93SAdam Balogh auto i = return_any_iterator(V.begin());
24223022b93SAdam Balogh i -= 2; // no-warning
2439a48ba6bSAdam Balogh }
2449a48ba6bSAdam Balogh
decr_by_2_ahead_of_end(const std::vector<int> & V)24523022b93SAdam Balogh void decr_by_2_ahead_of_end(const std::vector<int> &V) {
24623022b93SAdam Balogh auto i = --V.end();
24723022b93SAdam Balogh i -= 2; // no-warning
2489a48ba6bSAdam Balogh }
2499a48ba6bSAdam Balogh
decr_by_2_end(const std::vector<int> & V)25023022b93SAdam Balogh void decr_by_2_end(const std::vector<int> &V) {
25123022b93SAdam Balogh auto i = V.end();
25223022b93SAdam Balogh i -= 2; // no-warning
2539a48ba6bSAdam Balogh }
2549a48ba6bSAdam Balogh
25523022b93SAdam Balogh // Subtraction - operator-(int)
25623022b93SAdam Balogh
decr_by_2_copy_begin(const std::vector<int> & V)25723022b93SAdam Balogh void decr_by_2_copy_begin(const std::vector<int> &V) {
25823022b93SAdam Balogh auto i = V.begin();
25923022b93SAdam Balogh auto j = i - 2; // expected-warning{{Iterator decremented ahead of its valid range}}
260a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
2619a48ba6bSAdam Balogh }
2629a48ba6bSAdam Balogh
decr_by_2_copy_behind_begin(const std::vector<int> & V)26323022b93SAdam Balogh void decr_by_2_copy_behind_begin(const std::vector<int> &V) {
26423022b93SAdam Balogh auto i = ++V.begin();
26523022b93SAdam Balogh auto j = i - 2; // expected-warning{{Iterator decremented ahead of its valid range}}
266a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
2679a48ba6bSAdam Balogh }
2689a48ba6bSAdam Balogh
decr_by_2_copy_behind_begin_by_2(const std::vector<int> & V)26923022b93SAdam Balogh void decr_by_2_copy_behind_begin_by_2(const std::vector<int> &V) {
27023022b93SAdam Balogh auto i = ++V.begin();
27123022b93SAdam Balogh ++i;
27223022b93SAdam Balogh auto j = i - 2; // no-warning
2739a48ba6bSAdam Balogh }
2749a48ba6bSAdam Balogh
decr_by_2_copy_unknown(const std::vector<int> & V)27523022b93SAdam Balogh void decr_by_2_copy_unknown(const std::vector<int> &V) {
27623022b93SAdam Balogh auto i = return_any_iterator(V.begin());
27723022b93SAdam Balogh auto j = i - 2; // no-warning
2789a48ba6bSAdam Balogh }
2799a48ba6bSAdam Balogh
decr_by_2_copy_ahead_of_end(const std::vector<int> & V)28023022b93SAdam Balogh void decr_by_2_copy_ahead_of_end(const std::vector<int> &V) {
28123022b93SAdam Balogh auto i = --V.end();
28223022b93SAdam Balogh auto j = i - 2; // no-warning
283b03ed5e4SAdam Balogh }
2849a48ba6bSAdam Balogh
decr_by_2_copy_end(const std::vector<int> & V)28523022b93SAdam Balogh void decr_by_2_copy_end(const std::vector<int> &V) {
28623022b93SAdam Balogh auto i = V.end();
28723022b93SAdam Balogh auto j = i - 2; // no-warning
288d5bd3f63SAdam Balogh }
289d5bd3f63SAdam Balogh
29023022b93SAdam Balogh //
29123022b93SAdam Balogh // Subscript - operator[](int)
29223022b93SAdam Balogh //
29323022b93SAdam Balogh
29423022b93SAdam Balogh // By zero
29523022b93SAdam Balogh
subscript_zero_begin(const std::vector<int> & V)29623022b93SAdam Balogh void subscript_zero_begin(const std::vector<int> &V) {
29723022b93SAdam Balogh auto i = V.begin();
29823022b93SAdam Balogh auto j = i[0]; // no-warning
299d5bd3f63SAdam Balogh }
300d5bd3f63SAdam Balogh
subscript_zero_behind_begin(const std::vector<int> & V)30123022b93SAdam Balogh void subscript_zero_behind_begin(const std::vector<int> &V) {
30223022b93SAdam Balogh auto i = ++V.begin();
30323022b93SAdam Balogh auto j = i[0]; // no-warning
304d5bd3f63SAdam Balogh }
305d5bd3f63SAdam Balogh
subscript_zero_unknown(const std::vector<int> & V)30623022b93SAdam Balogh void subscript_zero_unknown(const std::vector<int> &V) {
30723022b93SAdam Balogh auto i = return_any_iterator(V.begin());
30823022b93SAdam Balogh auto j = i[0]; // no-warning
309d5bd3f63SAdam Balogh }
310d5bd3f63SAdam Balogh
subscript_zero_ahead_of_end(const std::vector<int> & V)31123022b93SAdam Balogh void subscript_zero_ahead_of_end(const std::vector<int> &V) {
31223022b93SAdam Balogh auto i = --V.end();
31323022b93SAdam Balogh auto j = i[0]; // no-warning
3149a48ba6bSAdam Balogh }
31542d241fcSAdam Balogh
subscript_zero_end(const std::vector<int> & V)31623022b93SAdam Balogh void subscript_zero_end(const std::vector<int> &V) {
31723022b93SAdam Balogh auto i = V.end();
31823022b93SAdam Balogh auto j = i[0]; // expected-warning{{Past-the-end iterator dereferenced}}
319a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
32023022b93SAdam Balogh }
32123022b93SAdam Balogh
32223022b93SAdam Balogh // By negative number
32323022b93SAdam Balogh
subscript_negative_begin(const std::vector<int> & V)32423022b93SAdam Balogh void subscript_negative_begin(const std::vector<int> &V) {
32523022b93SAdam Balogh auto i = V.begin();
32623022b93SAdam Balogh auto j = i[-1]; // no-warning FIXME: expect warning Iterator decremented ahead of its valid range
32723022b93SAdam Balogh }
32823022b93SAdam Balogh
subscript_negative_behind_begin(const std::vector<int> & V)32923022b93SAdam Balogh void subscript_negative_behind_begin(const std::vector<int> &V) {
33023022b93SAdam Balogh auto i = ++V.begin();
33123022b93SAdam Balogh auto j = i[-1]; // no-warning
33223022b93SAdam Balogh }
33323022b93SAdam Balogh
subscript_negative_unknown(const std::vector<int> & V)33423022b93SAdam Balogh void subscript_negative_unknown(const std::vector<int> &V) {
33523022b93SAdam Balogh auto i = return_any_iterator(V.begin());
33623022b93SAdam Balogh auto j = i[-1]; // no-warning
33723022b93SAdam Balogh }
33823022b93SAdam Balogh
subscript_negative_ahead_of_end(const std::vector<int> & V)33923022b93SAdam Balogh void subscript_negative_ahead_of_end(const std::vector<int> &V) {
34023022b93SAdam Balogh auto i = --V.end();
34123022b93SAdam Balogh auto j = i[-1]; // no-warning
34223022b93SAdam Balogh }
34323022b93SAdam Balogh
subscript_negative_end(const std::vector<int> & V)34423022b93SAdam Balogh void subscript_negative_end(const std::vector<int> &V) {
34523022b93SAdam Balogh auto i = V.end();
346a3f4d17aSAdam Balogh auto j = i[-1]; // expected-warning{{Past-the-end iterator dereferenced}} FIXME: expect no warning
347a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
34823022b93SAdam Balogh }
34923022b93SAdam Balogh
35023022b93SAdam Balogh // By positive number
35123022b93SAdam Balogh
subscript_positive_begin(const std::vector<int> & V)35223022b93SAdam Balogh void subscript_positive_begin(const std::vector<int> &V) {
35323022b93SAdam Balogh auto i = V.begin();
35423022b93SAdam Balogh auto j = i[1]; // no-warning
35523022b93SAdam Balogh }
35623022b93SAdam Balogh
subscript_positive_behind_begin(const std::vector<int> & V)35723022b93SAdam Balogh void subscript_positive_behind_begin(const std::vector<int> &V) {
35823022b93SAdam Balogh auto i = ++V.begin();
35923022b93SAdam Balogh auto j = i[1]; // no-warning
36023022b93SAdam Balogh }
36123022b93SAdam Balogh
subscript_positive_unknown(const std::vector<int> & V)36223022b93SAdam Balogh void subscript_positive_unknown(const std::vector<int> &V) {
36323022b93SAdam Balogh auto i = return_any_iterator(V.begin());
36423022b93SAdam Balogh auto j = i[1]; // no-warning
36523022b93SAdam Balogh }
36623022b93SAdam Balogh
subscript_positive_ahead_of_end(const std::vector<int> & V)36723022b93SAdam Balogh void subscript_positive_ahead_of_end(const std::vector<int> &V) {
36823022b93SAdam Balogh auto i = --V.end();
36923022b93SAdam Balogh auto j = i[1]; // no-warning FIXME: expected warning Past-the-end iterator dereferenced
37023022b93SAdam Balogh }
37123022b93SAdam Balogh
subscript_positive_end(const std::vector<int> & V)37223022b93SAdam Balogh void subscript_positive_end(const std::vector<int> &V) {
37323022b93SAdam Balogh auto i = V.end();
37423022b93SAdam Balogh auto j = i[1]; // expected-warning{{Past-the-end iterator dereferenced}} FIXME: expect warning Iterator incremented behind the past-the-end iterator
375a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}} FIXME: expect note@-1 Iterator incremented behind the past-the-end iterator
37623022b93SAdam Balogh }
37723022b93SAdam Balogh
37823022b93SAdam Balogh //
379ccc0d351SAdam Balogh // std::advance()
380ccc0d351SAdam Balogh //
381ccc0d351SAdam Balogh
382ccc0d351SAdam Balogh // std::advance() by +1
383ccc0d351SAdam Balogh
advance_plus_1_begin(const std::vector<int> & V)384ccc0d351SAdam Balogh void advance_plus_1_begin(const std::vector<int> &V) {
385ccc0d351SAdam Balogh auto i = V.begin();
386ccc0d351SAdam Balogh std::advance(i, 1); // no-warning
387ccc0d351SAdam Balogh }
388ccc0d351SAdam Balogh
advance_plus_1_behind_begin(const std::vector<int> & V)389ccc0d351SAdam Balogh void advance_plus_1_behind_begin(const std::vector<int> &V) {
390ccc0d351SAdam Balogh auto i = ++V.begin();
391ccc0d351SAdam Balogh std::advance(i, 1); // no-warning
392ccc0d351SAdam Balogh }
393ccc0d351SAdam Balogh
advance_plus_1_unknown(const std::vector<int> & V)394ccc0d351SAdam Balogh void advance_plus_1_unknown(const std::vector<int> &V) {
395ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
396ccc0d351SAdam Balogh std::advance(i, 1); // no-warning
397ccc0d351SAdam Balogh }
398ccc0d351SAdam Balogh
advance_plus_1_ahead_of_end(const std::vector<int> & V)399ccc0d351SAdam Balogh void advance_plus_1_ahead_of_end(const std::vector<int> &V) {
400ccc0d351SAdam Balogh auto i = --V.end();
401ccc0d351SAdam Balogh std::advance(i, 1); // no-warning
402ccc0d351SAdam Balogh }
403ccc0d351SAdam Balogh
advance_plus_1_end(const std::vector<int> & V)404ccc0d351SAdam Balogh void advance_plus_1_end(const std::vector<int> &V) {
405ccc0d351SAdam Balogh auto i = V.end();
406ccc0d351SAdam Balogh std::advance(i, 1); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
407a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
408ccc0d351SAdam Balogh }
409ccc0d351SAdam Balogh
410ccc0d351SAdam Balogh // std::advance() by -1
411ccc0d351SAdam Balogh
advance_minus_1_begin(const std::vector<int> & V)412ccc0d351SAdam Balogh void advance_minus_1_begin(const std::vector<int> &V) {
413ccc0d351SAdam Balogh auto i = V.begin();
414ccc0d351SAdam Balogh std::advance(i, -1); // expected-warning{{Iterator decremented ahead of its valid range}}
415a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
416ccc0d351SAdam Balogh }
417ccc0d351SAdam Balogh
advance_minus_1_behind_begin(const std::vector<int> & V)418ccc0d351SAdam Balogh void advance_minus_1_behind_begin(const std::vector<int> &V) {
419ccc0d351SAdam Balogh auto i = ++V.begin();
420ccc0d351SAdam Balogh std::advance(i, -1); // no-warning
421ccc0d351SAdam Balogh }
422ccc0d351SAdam Balogh
advance_minus_1_unknown(const std::vector<int> & V)423ccc0d351SAdam Balogh void advance_minus_1_unknown(const std::vector<int> &V) {
424ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
425ccc0d351SAdam Balogh std::advance(i, -1); // no-warning
426ccc0d351SAdam Balogh }
427ccc0d351SAdam Balogh
advance_minus_1_ahead_of_end(const std::vector<int> & V)428ccc0d351SAdam Balogh void advance_minus_1_ahead_of_end(const std::vector<int> &V) {
429ccc0d351SAdam Balogh auto i = --V.end();
430ccc0d351SAdam Balogh std::advance(i, -1); // no-warning
431ccc0d351SAdam Balogh }
432ccc0d351SAdam Balogh
advance_minus_1_end(const std::vector<int> & V)433ccc0d351SAdam Balogh void advance_minus_1_end(const std::vector<int> &V) {
434ccc0d351SAdam Balogh auto i = V.end();
435ccc0d351SAdam Balogh std::advance(i, -1); // no-warning
436ccc0d351SAdam Balogh }
437ccc0d351SAdam Balogh
438ccc0d351SAdam Balogh // std::advance() by +2
439ccc0d351SAdam Balogh
advance_plus_2_begin(const std::vector<int> & V)440ccc0d351SAdam Balogh void advance_plus_2_begin(const std::vector<int> &V) {
441ccc0d351SAdam Balogh auto i = V.begin();
442ccc0d351SAdam Balogh std::advance(i, 2); // no-warning
443ccc0d351SAdam Balogh }
444ccc0d351SAdam Balogh
advance_plus_2_behind_begin(const std::vector<int> & V)445ccc0d351SAdam Balogh void advance_plus_2_behind_begin(const std::vector<int> &V) {
446ccc0d351SAdam Balogh auto i = ++V.begin();
447ccc0d351SAdam Balogh std::advance(i, 2); // no-warning
448ccc0d351SAdam Balogh }
449ccc0d351SAdam Balogh
advance_plus_2_unknown(const std::vector<int> & V)450ccc0d351SAdam Balogh void advance_plus_2_unknown(const std::vector<int> &V) {
451ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
452ccc0d351SAdam Balogh std::advance(i, 2); // no-warning
453ccc0d351SAdam Balogh }
454ccc0d351SAdam Balogh
advance_plus_2_ahead_of_end(const std::vector<int> & V)455ccc0d351SAdam Balogh void advance_plus_2_ahead_of_end(const std::vector<int> &V) {
456ccc0d351SAdam Balogh auto i = --V.end();
457ccc0d351SAdam Balogh std::advance(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
458a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
459ccc0d351SAdam Balogh }
460ccc0d351SAdam Balogh
advance_plus_2_end(const std::vector<int> & V)461ccc0d351SAdam Balogh void advance_plus_2_end(const std::vector<int> &V) {
462ccc0d351SAdam Balogh auto i = V.end();
463ccc0d351SAdam Balogh std::advance(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
464a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
465ccc0d351SAdam Balogh }
466ccc0d351SAdam Balogh
467ccc0d351SAdam Balogh // std::advance() by -2
468ccc0d351SAdam Balogh
advance_minus_2_begin(const std::vector<int> & V)469ccc0d351SAdam Balogh void advance_minus_2_begin(const std::vector<int> &V) {
470ccc0d351SAdam Balogh auto i = V.begin();
471ccc0d351SAdam Balogh std::advance(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}
472a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
473ccc0d351SAdam Balogh }
474ccc0d351SAdam Balogh
advance_minus_2_behind_begin(const std::vector<int> & V)475ccc0d351SAdam Balogh void advance_minus_2_behind_begin(const std::vector<int> &V) {
476ccc0d351SAdam Balogh auto i = ++V.begin();
477ccc0d351SAdam Balogh std::advance(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}
478a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
479ccc0d351SAdam Balogh }
480ccc0d351SAdam Balogh
advance_minus_2_unknown(const std::vector<int> & V)481ccc0d351SAdam Balogh void advance_minus_2_unknown(const std::vector<int> &V) {
482ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
483ccc0d351SAdam Balogh std::advance(i, -2); // no-warning
484ccc0d351SAdam Balogh }
485ccc0d351SAdam Balogh
advance_minus_2_ahead_of_end(const std::vector<int> & V)486ccc0d351SAdam Balogh void advance_minus_2_ahead_of_end(const std::vector<int> &V) {
487ccc0d351SAdam Balogh auto i = --V.end();
488ccc0d351SAdam Balogh std::advance(i, -2); // no-warning
489ccc0d351SAdam Balogh }
490ccc0d351SAdam Balogh
advance_minus_2_end(const std::vector<int> & V)491ccc0d351SAdam Balogh void advance_minus_2_end(const std::vector<int> &V) {
492ccc0d351SAdam Balogh auto i = V.end();
493ccc0d351SAdam Balogh std::advance(i, -2); // no-warning
494ccc0d351SAdam Balogh }
495ccc0d351SAdam Balogh
496ccc0d351SAdam Balogh // std::advance() by 0
497ccc0d351SAdam Balogh
advance_0_begin(const std::vector<int> & V)498ccc0d351SAdam Balogh void advance_0_begin(const std::vector<int> &V) {
499ccc0d351SAdam Balogh auto i = V.begin();
500ccc0d351SAdam Balogh std::advance(i, 0); // no-warning
501ccc0d351SAdam Balogh }
502ccc0d351SAdam Balogh
advance_0_behind_begin(const std::vector<int> & V)503ccc0d351SAdam Balogh void advance_0_behind_begin(const std::vector<int> &V) {
504ccc0d351SAdam Balogh auto i = ++V.begin();
505ccc0d351SAdam Balogh std::advance(i, 0); // no-warning
506ccc0d351SAdam Balogh }
507ccc0d351SAdam Balogh
advance_0_unknown(const std::vector<int> & V)508ccc0d351SAdam Balogh void advance_0_unknown(const std::vector<int> &V) {
509ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
510ccc0d351SAdam Balogh std::advance(i, 0); // no-warning
511ccc0d351SAdam Balogh }
512ccc0d351SAdam Balogh
advance_0_ahead_of_end(const std::vector<int> & V)513ccc0d351SAdam Balogh void advance_0_ahead_of_end(const std::vector<int> &V) {
514ccc0d351SAdam Balogh auto i = --V.end();
515ccc0d351SAdam Balogh std::advance(i, 0); // no-warning
516ccc0d351SAdam Balogh }
517ccc0d351SAdam Balogh
advance_0_end(const std::vector<int> & V)518ccc0d351SAdam Balogh void advance_0_end(const std::vector<int> &V) {
519ccc0d351SAdam Balogh auto i = V.end();
520ccc0d351SAdam Balogh std::advance(i, 0); // no-warning
521ccc0d351SAdam Balogh }
522ccc0d351SAdam Balogh
523ccc0d351SAdam Balogh //
524ccc0d351SAdam Balogh // std::next()
525ccc0d351SAdam Balogh //
526ccc0d351SAdam Balogh
527ccc0d351SAdam Balogh // std::next() by +1 (default)
528ccc0d351SAdam Balogh
next_plus_1_begin(const std::vector<int> & V)529ccc0d351SAdam Balogh void next_plus_1_begin(const std::vector<int> &V) {
530ccc0d351SAdam Balogh auto i = V.begin();
531ccc0d351SAdam Balogh auto j = std::next(i); // no-warning
532ccc0d351SAdam Balogh }
533ccc0d351SAdam Balogh
next_plus_1_behind_begin(const std::vector<int> & V)534ccc0d351SAdam Balogh void next_plus_1_behind_begin(const std::vector<int> &V) {
535ccc0d351SAdam Balogh auto i = ++V.begin();
536ccc0d351SAdam Balogh auto j = std::next(i); // no-warning
537ccc0d351SAdam Balogh }
538ccc0d351SAdam Balogh
next_plus_1_unknown(const std::vector<int> & V)539ccc0d351SAdam Balogh void next_plus_1_unknown(const std::vector<int> &V) {
540ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
541ccc0d351SAdam Balogh auto j = std::next(i); // no-warning
542ccc0d351SAdam Balogh }
543ccc0d351SAdam Balogh
next_plus_1_ahead_of_end(const std::vector<int> & V)544ccc0d351SAdam Balogh void next_plus_1_ahead_of_end(const std::vector<int> &V) {
545ccc0d351SAdam Balogh auto i = --V.end();
546ccc0d351SAdam Balogh auto j = std::next(i); // no-warning
547ccc0d351SAdam Balogh }
548ccc0d351SAdam Balogh
next_plus_1_end(const std::vector<int> & V)549ccc0d351SAdam Balogh void next_plus_1_end(const std::vector<int> &V) {
550ccc0d351SAdam Balogh auto i = V.end();
551ccc0d351SAdam Balogh auto j = std::next(i); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
552a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
553ccc0d351SAdam Balogh }
554ccc0d351SAdam Balogh
555ccc0d351SAdam Balogh // std::next() by -1
556ccc0d351SAdam Balogh
next_minus_1_begin(const std::vector<int> & V)557ccc0d351SAdam Balogh void next_minus_1_begin(const std::vector<int> &V) {
558ccc0d351SAdam Balogh auto i = V.begin();
559ccc0d351SAdam Balogh auto j = std::next(i, -1); // expected-warning{{Iterator decremented ahead of its valid range}}
560a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
561ccc0d351SAdam Balogh }
562ccc0d351SAdam Balogh
next_minus_1_behind_begin(const std::vector<int> & V)563ccc0d351SAdam Balogh void next_minus_1_behind_begin(const std::vector<int> &V) {
564ccc0d351SAdam Balogh auto i = ++V.begin();
565ccc0d351SAdam Balogh auto j = std::next(i, -1); // no-warning
566ccc0d351SAdam Balogh }
567ccc0d351SAdam Balogh
next_minus_1_unknown(const std::vector<int> & V)568ccc0d351SAdam Balogh void next_minus_1_unknown(const std::vector<int> &V) {
569ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
570ccc0d351SAdam Balogh auto j = std::next(i, -1); // no-warning
571ccc0d351SAdam Balogh }
572ccc0d351SAdam Balogh
next_minus_1_ahead_of_end(const std::vector<int> & V)573ccc0d351SAdam Balogh void next_minus_1_ahead_of_end(const std::vector<int> &V) {
574ccc0d351SAdam Balogh auto i = --V.end();
575ccc0d351SAdam Balogh auto j = std::next(i, -1); // no-warning
576ccc0d351SAdam Balogh }
577ccc0d351SAdam Balogh
next_minus_1_end(const std::vector<int> & V)578ccc0d351SAdam Balogh void next_minus_1_end(const std::vector<int> &V) {
579ccc0d351SAdam Balogh auto i = V.end();
580ccc0d351SAdam Balogh auto j = std::next(i, -1); // no-warning
581ccc0d351SAdam Balogh }
582ccc0d351SAdam Balogh
583ccc0d351SAdam Balogh // std::next() by +2
584ccc0d351SAdam Balogh
next_plus_2_begin(const std::vector<int> & V)585ccc0d351SAdam Balogh void next_plus_2_begin(const std::vector<int> &V) {
586ccc0d351SAdam Balogh auto i = V.begin();
587ccc0d351SAdam Balogh auto j = std::next(i, 2); // no-warning
588ccc0d351SAdam Balogh }
589ccc0d351SAdam Balogh
next_plus_2_behind_begin(const std::vector<int> & V)590ccc0d351SAdam Balogh void next_plus_2_behind_begin(const std::vector<int> &V) {
591ccc0d351SAdam Balogh auto i = ++V.begin();
592ccc0d351SAdam Balogh auto j = std::next(i, 2); // no-warning
593ccc0d351SAdam Balogh }
594ccc0d351SAdam Balogh
next_plus_2_unknown(const std::vector<int> & V)595ccc0d351SAdam Balogh void next_plus_2_unknown(const std::vector<int> &V) {
596ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
597ccc0d351SAdam Balogh auto j = std::next(i, 2); // no-warning
598ccc0d351SAdam Balogh }
599ccc0d351SAdam Balogh
next_plus_2_ahead_of_end(const std::vector<int> & V)600ccc0d351SAdam Balogh void next_plus_2_ahead_of_end(const std::vector<int> &V) {
601ccc0d351SAdam Balogh auto i = --V.end();
602ccc0d351SAdam Balogh auto j = std::next(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
603a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
604ccc0d351SAdam Balogh }
605ccc0d351SAdam Balogh
next_plus_2_end(const std::vector<int> & V)606ccc0d351SAdam Balogh void next_plus_2_end(const std::vector<int> &V) {
607ccc0d351SAdam Balogh auto i = V.end();
608ccc0d351SAdam Balogh auto j = std::next(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
609a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
610ccc0d351SAdam Balogh }
611ccc0d351SAdam Balogh
612ccc0d351SAdam Balogh // std::next() by -2
613ccc0d351SAdam Balogh
next_minus_2_begin(const std::vector<int> & V)614ccc0d351SAdam Balogh void next_minus_2_begin(const std::vector<int> &V) {
615ccc0d351SAdam Balogh auto i = V.begin();
616ccc0d351SAdam Balogh auto j = std::next(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}
617a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
618ccc0d351SAdam Balogh }
619ccc0d351SAdam Balogh
next_minus_2_behind_begin(const std::vector<int> & V)620ccc0d351SAdam Balogh void next_minus_2_behind_begin(const std::vector<int> &V) {
621ccc0d351SAdam Balogh auto i = ++V.begin();
622ccc0d351SAdam Balogh auto j = std::next(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}
623a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
624ccc0d351SAdam Balogh }
625ccc0d351SAdam Balogh
next_minus_2_unknown(const std::vector<int> & V)626ccc0d351SAdam Balogh void next_minus_2_unknown(const std::vector<int> &V) {
627ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
628ccc0d351SAdam Balogh auto j = std::next(i, -2); // no-warning
629ccc0d351SAdam Balogh }
630ccc0d351SAdam Balogh
next_minus_2_ahead_of_end(const std::vector<int> & V)631ccc0d351SAdam Balogh void next_minus_2_ahead_of_end(const std::vector<int> &V) {
632ccc0d351SAdam Balogh auto i = --V.end();
633ccc0d351SAdam Balogh auto j = std::next(i, -2); // no-warning
634ccc0d351SAdam Balogh }
635ccc0d351SAdam Balogh
next_minus_2_end(const std::vector<int> & V)636ccc0d351SAdam Balogh void next_minus_2_end(const std::vector<int> &V) {
637ccc0d351SAdam Balogh auto i = V.end();
638ccc0d351SAdam Balogh auto j = std::next(i, -2); // no-warning
639ccc0d351SAdam Balogh }
640ccc0d351SAdam Balogh
641ccc0d351SAdam Balogh // std::next() by 0
642ccc0d351SAdam Balogh
next_0_begin(const std::vector<int> & V)643ccc0d351SAdam Balogh void next_0_begin(const std::vector<int> &V) {
644ccc0d351SAdam Balogh auto i = V.begin();
645ccc0d351SAdam Balogh auto j = std::next(i, 0); // no-warning
646ccc0d351SAdam Balogh }
647ccc0d351SAdam Balogh
next_0_behind_begin(const std::vector<int> & V)648ccc0d351SAdam Balogh void next_0_behind_begin(const std::vector<int> &V) {
649ccc0d351SAdam Balogh auto i = ++V.begin();
650ccc0d351SAdam Balogh auto j = std::next(i, 0); // no-warning
651ccc0d351SAdam Balogh }
652ccc0d351SAdam Balogh
next_0_unknown(const std::vector<int> & V)653ccc0d351SAdam Balogh void next_0_unknown(const std::vector<int> &V) {
654ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
655ccc0d351SAdam Balogh auto j = std::next(i, 0); // no-warning
656ccc0d351SAdam Balogh }
657ccc0d351SAdam Balogh
next_0_ahead_of_end(const std::vector<int> & V)658ccc0d351SAdam Balogh void next_0_ahead_of_end(const std::vector<int> &V) {
659ccc0d351SAdam Balogh auto i = --V.end();
660ccc0d351SAdam Balogh auto j = std::next(i, 0); // no-warning
661ccc0d351SAdam Balogh }
662ccc0d351SAdam Balogh
next_0_end(const std::vector<int> & V)663ccc0d351SAdam Balogh void next_0_end(const std::vector<int> &V) {
664ccc0d351SAdam Balogh auto i = V.end();
665ccc0d351SAdam Balogh auto j = std::next(i, 0); // no-warning
666ccc0d351SAdam Balogh }
667ccc0d351SAdam Balogh
668ccc0d351SAdam Balogh //
669ccc0d351SAdam Balogh // std::prev()
670ccc0d351SAdam Balogh //
671ccc0d351SAdam Balogh
672ccc0d351SAdam Balogh // std::prev() by +1 (default)
673ccc0d351SAdam Balogh
prev_plus_1_begin(const std::vector<int> & V)674ccc0d351SAdam Balogh void prev_plus_1_begin(const std::vector<int> &V) {
675ccc0d351SAdam Balogh auto i = V.begin();
676ccc0d351SAdam Balogh auto j = std::prev(i); // expected-warning{{Iterator decremented ahead of its valid range}}
677a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
678ccc0d351SAdam Balogh }
679ccc0d351SAdam Balogh
prev_plus_1_behind_begin(const std::vector<int> & V)680ccc0d351SAdam Balogh void prev_plus_1_behind_begin(const std::vector<int> &V) {
681ccc0d351SAdam Balogh auto i = ++V.begin();
682ccc0d351SAdam Balogh auto j = std::prev(i); // no-warning
683ccc0d351SAdam Balogh }
684ccc0d351SAdam Balogh
prev_plus_1_unknown(const std::vector<int> & V)685ccc0d351SAdam Balogh void prev_plus_1_unknown(const std::vector<int> &V) {
686ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
687ccc0d351SAdam Balogh auto j = std::prev(i); // no-warning
688ccc0d351SAdam Balogh }
689ccc0d351SAdam Balogh
prev_plus_1_ahead_of_end(const std::vector<int> & V)690ccc0d351SAdam Balogh void prev_plus_1_ahead_of_end(const std::vector<int> &V) {
691ccc0d351SAdam Balogh auto i = --V.end();
692ccc0d351SAdam Balogh auto j = std::prev(i); // no-warning
693ccc0d351SAdam Balogh }
694ccc0d351SAdam Balogh
prev_plus_1_end(const std::vector<int> & V)695ccc0d351SAdam Balogh void prev_plus_1_end(const std::vector<int> &V) {
696ccc0d351SAdam Balogh auto i = V.end();
697ccc0d351SAdam Balogh auto j = std::prev(i); // no-warning
698ccc0d351SAdam Balogh }
699ccc0d351SAdam Balogh
700ccc0d351SAdam Balogh // std::prev() by -1
701ccc0d351SAdam Balogh
prev_minus_1_begin(const std::vector<int> & V)702ccc0d351SAdam Balogh void prev_minus_1_begin(const std::vector<int> &V) {
703ccc0d351SAdam Balogh auto i = V.begin();
704ccc0d351SAdam Balogh auto j = std::prev(i, -1); // no-warning
705ccc0d351SAdam Balogh }
706ccc0d351SAdam Balogh
prev_minus_1_behind_begin(const std::vector<int> & V)707ccc0d351SAdam Balogh void prev_minus_1_behind_begin(const std::vector<int> &V) {
708ccc0d351SAdam Balogh auto i = ++V.begin();
709ccc0d351SAdam Balogh auto j = std::prev(i, -1); // no-warning
710ccc0d351SAdam Balogh }
711ccc0d351SAdam Balogh
prev_minus_1_unknown(const std::vector<int> & V)712ccc0d351SAdam Balogh void prev_minus_1_unknown(const std::vector<int> &V) {
713ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
714ccc0d351SAdam Balogh auto j = std::prev(i, -1); // no-warning
715ccc0d351SAdam Balogh }
716ccc0d351SAdam Balogh
prev_minus_1_ahead_of_end(const std::vector<int> & V)717ccc0d351SAdam Balogh void prev_minus_1_ahead_of_end(const std::vector<int> &V) {
718ccc0d351SAdam Balogh auto i = --V.end();
719ccc0d351SAdam Balogh auto j = std::prev(i, -1); // no-warning
720ccc0d351SAdam Balogh }
721ccc0d351SAdam Balogh
prev_minus_1_end(const std::vector<int> & V)722ccc0d351SAdam Balogh void prev_minus_1_end(const std::vector<int> &V) {
723ccc0d351SAdam Balogh auto i = V.end();
724ccc0d351SAdam Balogh auto j = std::prev(i, -1); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
725a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
726ccc0d351SAdam Balogh }
727ccc0d351SAdam Balogh
728ccc0d351SAdam Balogh // std::prev() by +2
729ccc0d351SAdam Balogh
prev_plus_2_begin(const std::vector<int> & V)730ccc0d351SAdam Balogh void prev_plus_2_begin(const std::vector<int> &V) {
731ccc0d351SAdam Balogh auto i = V.begin();
732ccc0d351SAdam Balogh auto j = std::prev(i, 2); // expected-warning{{Iterator decremented ahead of its valid range}}
733a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
734ccc0d351SAdam Balogh }
735ccc0d351SAdam Balogh
prev_plus_2_behind_begin(const std::vector<int> & V)736ccc0d351SAdam Balogh void prev_plus_2_behind_begin(const std::vector<int> &V) {
737ccc0d351SAdam Balogh auto i = ++V.begin();
738ccc0d351SAdam Balogh auto j = std::prev(i, 2); // expected-warning{{Iterator decremented ahead of its valid range}}
739a3f4d17aSAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
740ccc0d351SAdam Balogh }
741ccc0d351SAdam Balogh
prev_plus_2_unknown(const std::vector<int> & V)742ccc0d351SAdam Balogh void prev_plus_2_unknown(const std::vector<int> &V) {
743ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
744ccc0d351SAdam Balogh auto j = std::prev(i, 2); // no-warning
745ccc0d351SAdam Balogh }
746ccc0d351SAdam Balogh
prev_plus_2_ahead_of_end(const std::vector<int> & V)747ccc0d351SAdam Balogh void prev_plus_2_ahead_of_end(const std::vector<int> &V) {
748ccc0d351SAdam Balogh auto i = --V.end();
749ccc0d351SAdam Balogh auto j = std::prev(i, 2); // no-warning
750ccc0d351SAdam Balogh }
751ccc0d351SAdam Balogh
prev_plus_2_end(const std::vector<int> & V)752ccc0d351SAdam Balogh void prev_plus_2_end(const std::vector<int> &V) {
753ccc0d351SAdam Balogh auto i = V.end();
754ccc0d351SAdam Balogh auto j = std::prev(i, 2); // no-warning
755ccc0d351SAdam Balogh }
756ccc0d351SAdam Balogh
757ccc0d351SAdam Balogh // std::prev() by -2
758ccc0d351SAdam Balogh
prev_minus_2_begin(const std::vector<int> & V)759ccc0d351SAdam Balogh void prev_minus_2_begin(const std::vector<int> &V) {
760ccc0d351SAdam Balogh auto i = V.begin();
761ccc0d351SAdam Balogh auto j = std::prev(i, -2); // no-warning
762ccc0d351SAdam Balogh }
763ccc0d351SAdam Balogh
prev_minus_2_behind_begin(const std::vector<int> & V)764ccc0d351SAdam Balogh void prev_minus_2_behind_begin(const std::vector<int> &V) {
765ccc0d351SAdam Balogh auto i = ++V.begin();
766ccc0d351SAdam Balogh auto j = std::prev(i, -2); // no-warning
767ccc0d351SAdam Balogh }
768ccc0d351SAdam Balogh
prev_minus_2_unknown(const std::vector<int> & V)769ccc0d351SAdam Balogh void prev_minus_2_unknown(const std::vector<int> &V) {
770ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
771ccc0d351SAdam Balogh auto j = std::prev(i, -2); // no-warning
772ccc0d351SAdam Balogh }
773ccc0d351SAdam Balogh
prev_minus_2_ahead_of_end(const std::vector<int> & V)774ccc0d351SAdam Balogh void prev_minus_2_ahead_of_end(const std::vector<int> &V) {
775ccc0d351SAdam Balogh auto i = --V.end();
776ccc0d351SAdam Balogh auto j = std::prev(i, -2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
777a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
778ccc0d351SAdam Balogh }
779ccc0d351SAdam Balogh
prev_minus_2_end(const std::vector<int> & V)780ccc0d351SAdam Balogh void prev_minus_2_end(const std::vector<int> &V) {
781ccc0d351SAdam Balogh auto i = V.end();
782ccc0d351SAdam Balogh auto j = std::prev(i, -2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
783a3f4d17aSAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
784ccc0d351SAdam Balogh }
785ccc0d351SAdam Balogh
786ccc0d351SAdam Balogh // std::prev() by 0
787ccc0d351SAdam Balogh
prev_0_begin(const std::vector<int> & V)788ccc0d351SAdam Balogh void prev_0_begin(const std::vector<int> &V) {
789ccc0d351SAdam Balogh auto i = V.begin();
790ccc0d351SAdam Balogh auto j = std::prev(i, 0); // no-warning
791ccc0d351SAdam Balogh }
792ccc0d351SAdam Balogh
prev_0_behind_begin(const std::vector<int> & V)793ccc0d351SAdam Balogh void prev_0_behind_begin(const std::vector<int> &V) {
794ccc0d351SAdam Balogh auto i = ++V.begin();
795ccc0d351SAdam Balogh auto j = std::prev(i, 0); // no-warning
796ccc0d351SAdam Balogh }
797ccc0d351SAdam Balogh
prev_0_unknown(const std::vector<int> & V)798ccc0d351SAdam Balogh void prev_0_unknown(const std::vector<int> &V) {
799ccc0d351SAdam Balogh auto i = return_any_iterator(V.begin());
800ccc0d351SAdam Balogh auto j = std::prev(i, 0); // no-warning
801ccc0d351SAdam Balogh }
802ccc0d351SAdam Balogh
prev_0_ahead_of_end(const std::vector<int> & V)803ccc0d351SAdam Balogh void prev_0_ahead_of_end(const std::vector<int> &V) {
804ccc0d351SAdam Balogh auto i = --V.end();
805ccc0d351SAdam Balogh auto j = std::prev(i, 0); // no-warning
806ccc0d351SAdam Balogh }
807ccc0d351SAdam Balogh
prev_0_end(const std::vector<int> & V)808ccc0d351SAdam Balogh void prev_0_end(const std::vector<int> &V) {
809ccc0d351SAdam Balogh auto i = V.end();
810ccc0d351SAdam Balogh auto j = std::prev(i, 0); // no-warning
811ccc0d351SAdam Balogh }
812ccc0d351SAdam Balogh
813ba8cda98SDenys Petrov // std::prev() with int* for checking Loc value argument
814ba8cda98SDenys Petrov namespace std {
815ba8cda98SDenys Petrov template <typename T>
816ba8cda98SDenys Petrov T prev(T, int *);
817ba8cda98SDenys Petrov }
818ba8cda98SDenys Petrov
prev_loc_value(const std::vector<int> & V,int o)819ba8cda98SDenys Petrov void prev_loc_value(const std::vector<int> &V, int o) {
820ba8cda98SDenys Petrov
821ba8cda98SDenys Petrov auto i = return_any_iterator(V.begin());
822ba8cda98SDenys Petrov int *offset = &o;
823ba8cda98SDenys Petrov auto j = std::prev(i, offset); // no-warning
824ba8cda98SDenys Petrov }
825ba8cda98SDenys Petrov
826ccc0d351SAdam Balogh //
82723022b93SAdam Balogh // Structure member dereference operators
82823022b93SAdam Balogh //
82923022b93SAdam Balogh
83023022b93SAdam Balogh struct S {
83123022b93SAdam Balogh int n;
83242d241fcSAdam Balogh };
83342d241fcSAdam Balogh
83423022b93SAdam Balogh // Member dereference - operator->()
83542d241fcSAdam Balogh
arrow_deref_begin(const std::vector<S> & V)83623022b93SAdam Balogh void arrow_deref_begin(const std::vector<S> &V) {
83723022b93SAdam Balogh auto i = V.begin();
83823022b93SAdam Balogh int n = i->n; // no-warning
83942d241fcSAdam Balogh }
8408557f17dSAdam Balogh
arrow_deref_end(const std::vector<S> & V)84123022b93SAdam Balogh void arrow_deref_end(const std::vector<S> &V) {
84223022b93SAdam Balogh auto i = V.end();
84323022b93SAdam Balogh int n = i->n; // expected-warning{{Past-the-end iterator dereferenced}}
844a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
845a3f4d17aSAdam Balogh }
846a3f4d17aSAdam Balogh
847a3f4d17aSAdam Balogh // Container modification - test path notes
848a3f4d17aSAdam Balogh
deref_end_after_pop_back(std::vector<int> & V)849a3f4d17aSAdam Balogh void deref_end_after_pop_back(std::vector<int> &V) {
850a3f4d17aSAdam Balogh const auto i = --V.end();
851a3f4d17aSAdam Balogh
852a3f4d17aSAdam Balogh V.pop_back(); // expected-note{{Container 'V' shrank from the back by 1 position}}
853a3f4d17aSAdam Balogh
854a3f4d17aSAdam Balogh *i; // expected-warning{{Past-the-end iterator dereferenced}}
855a3f4d17aSAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
8568557f17dSAdam Balogh }
8579e63b190SAdam Balogh
8589e63b190SAdam Balogh template<typename T>
8599e63b190SAdam Balogh struct cont_with_ptr_iterator {
8609e63b190SAdam Balogh T* begin() const;
8619e63b190SAdam Balogh T* end() const;
8629e63b190SAdam Balogh };
8639e63b190SAdam Balogh
deref_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)8649e63b190SAdam Balogh void deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
8659e63b190SAdam Balogh auto i = c.end();
8669e63b190SAdam Balogh (void) *i; // expected-warning{{Past-the-end iterator dereferenced}}
8679e63b190SAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
8689e63b190SAdam Balogh }
8699e63b190SAdam Balogh
array_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)8709e63b190SAdam Balogh void array_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
8719e63b190SAdam Balogh auto i = c.end();
8729e63b190SAdam Balogh (void) i[0]; // expected-warning{{Past-the-end iterator dereferenced}}
8739e63b190SAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
8749e63b190SAdam Balogh }
8759e63b190SAdam Balogh
arrow_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)8769e63b190SAdam Balogh void arrow_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
8779e63b190SAdam Balogh auto i = c.end();
8789e63b190SAdam Balogh (void) i->n; // expected-warning{{Past-the-end iterator dereferenced}}
8799e63b190SAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
8809e63b190SAdam Balogh }
8819e63b190SAdam Balogh
arrow_star_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> & c,int S::* p)8829e63b190SAdam Balogh void arrow_star_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c,
8839e63b190SAdam Balogh int S::*p) {
8849e63b190SAdam Balogh auto i = c.end();
8859e63b190SAdam Balogh (void)(i->*p); // expected-warning{{Past-the-end iterator dereferenced}}
8869e63b190SAdam Balogh // expected-note@-1{{Past-the-end iterator dereferenced}}
8879e63b190SAdam Balogh }
8889e63b190SAdam Balogh
prefix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)8899e63b190SAdam Balogh void prefix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
8909e63b190SAdam Balogh auto i = c.end();
8919e63b190SAdam Balogh ++i; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
8929e63b190SAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
8939e63b190SAdam Balogh }
8949e63b190SAdam Balogh
postfix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)8959e63b190SAdam Balogh void postfix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
8969e63b190SAdam Balogh auto i = c.end();
8979e63b190SAdam Balogh i++; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
8989e63b190SAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
8999e63b190SAdam Balogh }
9009e63b190SAdam Balogh
prefix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> & c)9019e63b190SAdam Balogh void prefix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
9029e63b190SAdam Balogh auto i = c.begin();
9039e63b190SAdam Balogh --i; // expected-warning{{Iterator decremented ahead of its valid range}}
9049e63b190SAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
9059e63b190SAdam Balogh }
9069e63b190SAdam Balogh
postfix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> & c)9079e63b190SAdam Balogh void postfix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
9089e63b190SAdam Balogh auto i = c.begin();
9099e63b190SAdam Balogh i--; // expected-warning{{Iterator decremented ahead of its valid range}}
9109e63b190SAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
9119e63b190SAdam Balogh }
9129e63b190SAdam Balogh
prefix_add_2_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)9139e63b190SAdam Balogh void prefix_add_2_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
9149e63b190SAdam Balogh auto i = c.end();
9159e63b190SAdam Balogh (void)(i + 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}
9169e63b190SAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
9179e63b190SAdam Balogh }
9189e63b190SAdam Balogh
postfix_add_assign_2_end_ptr_iterator(const cont_with_ptr_iterator<S> & c)9199e63b190SAdam Balogh void postfix_add_assign_2_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
9209e63b190SAdam Balogh auto i = c.end();
9219e63b190SAdam Balogh i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}
9229e63b190SAdam Balogh // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}
9239e63b190SAdam Balogh }
9249e63b190SAdam Balogh
prefix_minus_2_begin_ptr_iterator(const cont_with_ptr_iterator<S> & c)9259e63b190SAdam Balogh void prefix_minus_2_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {
9269e63b190SAdam Balogh auto i = c.begin();
9279e63b190SAdam Balogh (void)(i - 2); // expected-warning{{Iterator decremented ahead of its valid range}}
9289e63b190SAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
9299e63b190SAdam Balogh }
9309e63b190SAdam Balogh
postfix_minus_assign_2_begin_ptr_iterator(const cont_with_ptr_iterator<S> & c)9319e63b190SAdam Balogh void postfix_minus_assign_2_begin_ptr_iterator(
9329e63b190SAdam Balogh const cont_with_ptr_iterator<S> &c) {
9339e63b190SAdam Balogh auto i = c.begin();
9349e63b190SAdam Balogh i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}
9359e63b190SAdam Balogh // expected-note@-1{{Iterator decremented ahead of its valid range}}
9369e63b190SAdam Balogh }
9379e63b190SAdam Balogh
ptr_iter_diff(cont_with_ptr_iterator<S> & c)938a59d4ae4SAdam Balogh void ptr_iter_diff(cont_with_ptr_iterator<S> &c) {
939a59d4ae4SAdam Balogh auto i0 = c.begin(), i1 = c.end();
940a59d4ae4SAdam Balogh ptrdiff_t len = i1 - i0; // no-crash
941a59d4ae4SAdam Balogh }
942*bcc66248SAdam Balogh
uninit_var(int n)943*bcc66248SAdam Balogh int uninit_var(int n) {
944*bcc66248SAdam Balogh int uninit; // expected-note{{'uninit' declared without an initial value}}
945*bcc66248SAdam Balogh return n - uninit; // no-crash
946*bcc66248SAdam Balogh // expected-warning@-1 {{The right operand of '-' is a garbage value}}
947*bcc66248SAdam Balogh // expected-note@-2 {{The right operand of '-' is a garbage value}}
948*bcc66248SAdam Balogh }
949