960 lines · cpp
1// 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 -verify2// 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 -verify3 4#include "Inputs/system-header-simulator-cxx.h"5 6void clang_analyzer_warnIfReached();7 8// Dereference - operator*()9 10void deref_begin(const std::vector<int> &V) {11 auto i = V.begin();12 *i; // no-warning13}14 15void deref_begind_begin(const std::vector<int> &V) {16 auto i = ++V.begin();17 *i; // no-warning18}19 20template <typename Iter> Iter return_any_iterator(const Iter &It);21 22void deref_unknown(const std::vector<int> &V) {23 auto i = return_any_iterator(V.begin());24 *i; // no-warning25}26 27void deref_ahead_of_end(const std::vector<int> &V) {28 auto i = --V.end();29 *i; // no-warning30}31 32void deref_end(const std::vector<int> &V) {33 auto i = V.end();34 *i; // expected-warning{{Past-the-end iterator dereferenced}}35 // expected-note@-1{{Past-the-end iterator dereferenced}}36}37 38// Prefix increment - operator++()39 40void incr_begin(const std::vector<int> &V) {41 auto i = V.begin();42 ++i; // no-warning43}44 45void incr_behind_begin(const std::vector<int> &V) {46 auto i = ++V.begin();47 ++i; // no-warning48}49 50void incr_unknown(const std::vector<int> &V) {51 auto i = return_any_iterator(V.begin());52 ++i; // no-warning53}54 55void incr_ahead_of_end(const std::vector<int> &V) {56 auto i = --V.end();57 ++i; // no-warning58}59 60void incr_end(const std::vector<int> &V) {61 auto i = V.end();62 ++i; // expected-warning{{Iterator incremented behind the past-the-end iterator}}63 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}64}65 66// Postfix increment - operator++(int)67 68void begin_incr(const std::vector<int> &V) {69 auto i = V.begin();70 i++; // no-warning71}72 73void behind_begin_incr(const std::vector<int> &V) {74 auto i = ++V.begin();75 i++; // no-warning76}77 78void unknown_incr(const std::vector<int> &V) {79 auto i = return_any_iterator(V.begin());80 i++; // no-warning81}82 83void ahead_of_end_incr(const std::vector<int> &V) {84 auto i = --V.end();85 i++; // no-warning86}87 88void end_incr(const std::vector<int> &V) {89 auto i = V.end();90 i++; // expected-warning{{Iterator incremented behind the past-the-end iterator}}91 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}92}93 94// Prefix decrement - operator--()95 96void decr_begin(const std::vector<int> &V) {97 auto i = V.begin();98 --i; // expected-warning{{Iterator decremented ahead of its valid range}}99 // expected-note@-1{{Iterator decremented ahead of its valid range}}100}101 102void decr_behind_begin(const std::vector<int> &V) {103 auto i = ++V.begin();104 --i; // no-warning105}106 107void decr_unknown(const std::vector<int> &V) {108 auto i = return_any_iterator(V.begin());109 --i; // no-warning110}111 112void decr_ahead_of_end(const std::vector<int> &V) {113 auto i = --V.end();114 --i; // no-warning115}116 117void decr_end(const std::vector<int> &V) {118 auto i = V.end();119 --i; // no-warning120}121 122// Postfix decrement - operator--(int)123 124void begin_decr(const std::vector<int> &V) {125 auto i = V.begin();126 i--; // expected-warning{{Iterator decremented ahead of its valid range}}127 // expected-note@-1{{Iterator decremented ahead of its valid range}}128}129 130void behind_begin_decr(const std::vector<int> &V) {131 auto i = ++V.begin();132 i--; // no-warning133}134 135void unknown_decr(const std::vector<int> &V) {136 auto i = return_any_iterator(V.begin());137 i--; // no-warning138}139 140void ahead_of_end_decr(const std::vector<int> &V) {141 auto i = --V.end();142 i--; // no-warning143}144 145void end_decr(const std::vector<int> &V) {146 auto i = V.end();147 i--; // no-warning148}149 150// Addition assignment - operator+=(int)151 152void incr_by_2_begin(const std::vector<int> &V) {153 auto i = V.begin();154 i += 2; // no-warning155}156 157void incr_by_2_behind_begin(const std::vector<int> &V) {158 auto i = ++V.begin();159 i += 2; // no-warning160}161 162void incr_by_2_unknown(const std::vector<int> &V) {163 auto i = return_any_iterator(V.begin());164 i += 2; // no-warning165}166 167void incr_by_2_ahead_by_2_of_end(const std::vector<int> &V) {168 auto i = --V.end();169 --i;170 i += 2; // no-warning171}172 173void incr_by_2_ahead_of_end(const std::vector<int> &V) {174 auto i = --V.end();175 i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}176 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}177}178 179void incr_by_2_end(const std::vector<int> &V) {180 auto i = V.end();181 i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}182 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}183}184 185// Addition - operator+(int)186 187void incr_by_2_copy_begin(const std::vector<int> &V) {188 auto i = V.begin();189 auto j = i + 2; // no-warning190}191 192void incr_by_2_copy_behind_begin(const std::vector<int> &V) {193 auto i = ++V.begin();194 auto j = i + 2; // no-warning195}196 197void incr_by_2_copy_unknown(const std::vector<int> &V) {198 auto i = return_any_iterator(V.begin());199 auto j = i + 2; // no-warning200}201 202void incr_by_2_copy_ahead_by_2_of_end(const std::vector<int> &V) {203 auto i = --V.end();204 --i;205 auto j = i + 2; // no-warning206}207 208void incr_by_2_copy_ahead_of_end(const std::vector<int> &V) {209 auto i = --V.end();210 auto j = i + 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}211 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}212}213 214void incr_by_2_copy_end(const std::vector<int> &V) {215 auto i = V.end();216 auto j = i + 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}217 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}218}219 220// Subtraction assignment - operator-=(int)221 222void decr_by_2_begin(const std::vector<int> &V) {223 auto i = V.begin();224 i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}225 // expected-note@-1{{Iterator decremented ahead of its valid range}}226}227 228void decr_by_2_behind_begin(const std::vector<int> &V) {229 auto i = ++V.begin();230 i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}231 // expected-note@-1{{Iterator decremented ahead of its valid range}}232}233 234void decr_by_2_behind_begin_by_2(const std::vector<int> &V) {235 auto i = ++V.begin();236 ++i;237 i -= 2; // no-warning238}239 240void decr_by_2_unknown(const std::vector<int> &V) {241 auto i = return_any_iterator(V.begin());242 i -= 2; // no-warning243}244 245void decr_by_2_ahead_of_end(const std::vector<int> &V) {246 auto i = --V.end();247 i -= 2; // no-warning248}249 250void decr_by_2_end(const std::vector<int> &V) {251 auto i = V.end();252 i -= 2; // no-warning253}254 255// Subtraction - operator-(int)256 257void decr_by_2_copy_begin(const std::vector<int> &V) {258 auto i = V.begin();259 auto j = i - 2; // expected-warning{{Iterator decremented ahead of its valid range}}260 // expected-note@-1{{Iterator decremented ahead of its valid range}}261}262 263void decr_by_2_copy_behind_begin(const std::vector<int> &V) {264 auto i = ++V.begin();265 auto j = i - 2; // expected-warning{{Iterator decremented ahead of its valid range}}266 // expected-note@-1{{Iterator decremented ahead of its valid range}}267}268 269void decr_by_2_copy_behind_begin_by_2(const std::vector<int> &V) {270 auto i = ++V.begin();271 ++i;272 auto j = i - 2; // no-warning273}274 275void decr_by_2_copy_unknown(const std::vector<int> &V) {276 auto i = return_any_iterator(V.begin());277 auto j = i - 2; // no-warning278}279 280void decr_by_2_copy_ahead_of_end(const std::vector<int> &V) {281 auto i = --V.end();282 auto j = i - 2; // no-warning283}284 285void decr_by_2_copy_end(const std::vector<int> &V) {286 auto i = V.end();287 auto j = i - 2; // no-warning288}289 290//291// Subscript - operator[](int)292//293 294// By zero295 296void subscript_zero_begin(const std::vector<int> &V) {297 auto i = V.begin();298 auto j = i[0]; // no-warning299}300 301void subscript_zero_behind_begin(const std::vector<int> &V) {302 auto i = ++V.begin();303 auto j = i[0]; // no-warning304}305 306void subscript_zero_unknown(const std::vector<int> &V) {307 auto i = return_any_iterator(V.begin());308 auto j = i[0]; // no-warning309}310 311void subscript_zero_ahead_of_end(const std::vector<int> &V) {312 auto i = --V.end();313 auto j = i[0]; // no-warning314}315 316void subscript_zero_end(const std::vector<int> &V) {317 auto i = V.end();318 auto j = i[0]; // expected-warning{{Past-the-end iterator dereferenced}}319 // expected-note@-1{{Past-the-end iterator dereferenced}}320}321 322// By negative number323 324void subscript_negative_begin(const std::vector<int> &V) {325 auto i = V.begin();326 auto j = i[-1]; // no-warning FIXME: expect warning Iterator decremented ahead of its valid range327}328 329void subscript_negative_behind_begin(const std::vector<int> &V) {330 auto i = ++V.begin();331 auto j = i[-1]; // no-warning332}333 334void subscript_negative_unknown(const std::vector<int> &V) {335 auto i = return_any_iterator(V.begin());336 auto j = i[-1]; // no-warning337}338 339void subscript_negative_ahead_of_end(const std::vector<int> &V) {340 auto i = --V.end();341 auto j = i[-1]; // no-warning342}343 344void subscript_negative_end(const std::vector<int> &V) {345 auto i = V.end();346 auto j = i[-1]; // expected-warning{{Past-the-end iterator dereferenced}} FIXME: expect no warning347 // expected-note@-1{{Past-the-end iterator dereferenced}}348}349 350// By positive number351 352void subscript_positive_begin(const std::vector<int> &V) {353 auto i = V.begin();354 auto j = i[1]; // no-warning355}356 357void subscript_positive_behind_begin(const std::vector<int> &V) {358 auto i = ++V.begin();359 auto j = i[1]; // no-warning360}361 362void subscript_positive_unknown(const std::vector<int> &V) {363 auto i = return_any_iterator(V.begin());364 auto j = i[1]; // no-warning365}366 367void subscript_positive_ahead_of_end(const std::vector<int> &V) {368 auto i = --V.end();369 auto j = i[1]; // no-warning FIXME: expected warning Past-the-end iterator dereferenced370}371 372void subscript_positive_end(const std::vector<int> &V) {373 auto i = V.end();374 auto j = i[1]; // expected-warning{{Past-the-end iterator dereferenced}} FIXME: expect warning Iterator incremented behind the past-the-end iterator375 // expected-note@-1{{Past-the-end iterator dereferenced}} FIXME: expect note@-1 Iterator incremented behind the past-the-end iterator376}377 378//379// std::advance()380//381 382// std::advance() by +1383 384void advance_plus_1_begin(const std::vector<int> &V) {385 auto i = V.begin();386 std::advance(i, 1); // no-warning387}388 389void advance_plus_1_behind_begin(const std::vector<int> &V) {390 auto i = ++V.begin();391 std::advance(i, 1); // no-warning392}393 394void advance_plus_1_unknown(const std::vector<int> &V) {395 auto i = return_any_iterator(V.begin());396 std::advance(i, 1); // no-warning397}398 399void advance_plus_1_ahead_of_end(const std::vector<int> &V) {400 auto i = --V.end();401 std::advance(i, 1); // no-warning402}403 404void advance_plus_1_end(const std::vector<int> &V) {405 auto i = V.end();406 std::advance(i, 1); // expected-warning{{Iterator incremented behind the past-the-end iterator}}407 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}408}409 410// std::advance() by -1411 412void advance_minus_1_begin(const std::vector<int> &V) {413 auto i = V.begin();414 std::advance(i, -1); // expected-warning{{Iterator decremented ahead of its valid range}}415 // expected-note@-1{{Iterator decremented ahead of its valid range}}416}417 418void advance_minus_1_behind_begin(const std::vector<int> &V) {419 auto i = ++V.begin();420 std::advance(i, -1); // no-warning421}422 423void advance_minus_1_unknown(const std::vector<int> &V) {424 auto i = return_any_iterator(V.begin());425 std::advance(i, -1); // no-warning426}427 428void advance_minus_1_ahead_of_end(const std::vector<int> &V) {429 auto i = --V.end();430 std::advance(i, -1); // no-warning431}432 433void advance_minus_1_end(const std::vector<int> &V) {434 auto i = V.end();435 std::advance(i, -1); // no-warning436}437 438// std::advance() by +2439 440void advance_plus_2_begin(const std::vector<int> &V) {441 auto i = V.begin();442 std::advance(i, 2); // no-warning443}444 445void advance_plus_2_behind_begin(const std::vector<int> &V) {446 auto i = ++V.begin();447 std::advance(i, 2); // no-warning448}449 450void advance_plus_2_unknown(const std::vector<int> &V) {451 auto i = return_any_iterator(V.begin());452 std::advance(i, 2); // no-warning453}454 455void advance_plus_2_ahead_of_end(const std::vector<int> &V) {456 auto i = --V.end();457 std::advance(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}458 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}459}460 461void advance_plus_2_end(const std::vector<int> &V) {462 auto i = V.end();463 std::advance(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}464 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}465}466 467// std::advance() by -2468 469void advance_minus_2_begin(const std::vector<int> &V) {470 auto i = V.begin();471 std::advance(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}472 // expected-note@-1{{Iterator decremented ahead of its valid range}}473}474 475void advance_minus_2_behind_begin(const std::vector<int> &V) {476 auto i = ++V.begin();477 std::advance(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}478 // expected-note@-1{{Iterator decremented ahead of its valid range}}479}480 481void advance_minus_2_unknown(const std::vector<int> &V) {482 auto i = return_any_iterator(V.begin());483 std::advance(i, -2); // no-warning484}485 486void advance_minus_2_ahead_of_end(const std::vector<int> &V) {487 auto i = --V.end();488 std::advance(i, -2); // no-warning489}490 491void advance_minus_2_end(const std::vector<int> &V) {492 auto i = V.end();493 std::advance(i, -2); // no-warning494}495 496// std::advance() by 0497 498void advance_0_begin(const std::vector<int> &V) {499 auto i = V.begin();500 std::advance(i, 0); // no-warning501}502 503void advance_0_behind_begin(const std::vector<int> &V) {504 auto i = ++V.begin();505 std::advance(i, 0); // no-warning506}507 508void advance_0_unknown(const std::vector<int> &V) {509 auto i = return_any_iterator(V.begin());510 std::advance(i, 0); // no-warning511}512 513void advance_0_ahead_of_end(const std::vector<int> &V) {514 auto i = --V.end();515 std::advance(i, 0); // no-warning516}517 518void advance_0_end(const std::vector<int> &V) {519 auto i = V.end();520 std::advance(i, 0); // no-warning521}522 523//524// std::next()525//526 527// std::next() by +1 (default)528 529void next_plus_1_begin(const std::vector<int> &V) {530 auto i = V.begin();531 auto j = std::next(i); // no-warning532}533 534void next_plus_1_behind_begin(const std::vector<int> &V) {535 auto i = ++V.begin();536 auto j = std::next(i); // no-warning537}538 539void next_plus_1_unknown(const std::vector<int> &V) {540 auto i = return_any_iterator(V.begin());541 auto j = std::next(i); // no-warning542}543 544void next_plus_1_ahead_of_end(const std::vector<int> &V) {545 auto i = --V.end();546 auto j = std::next(i); // no-warning547}548 549void next_plus_1_end(const std::vector<int> &V) {550 auto i = V.end();551 auto j = std::next(i); // expected-warning{{Iterator incremented behind the past-the-end iterator}}552 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}553}554 555// std::next() by -1556 557void next_minus_1_begin(const std::vector<int> &V) {558 auto i = V.begin();559 auto j = std::next(i, -1); // expected-warning{{Iterator decremented ahead of its valid range}}560 // expected-note@-1{{Iterator decremented ahead of its valid range}}561}562 563void next_minus_1_behind_begin(const std::vector<int> &V) {564 auto i = ++V.begin();565 auto j = std::next(i, -1); // no-warning566}567 568void next_minus_1_unknown(const std::vector<int> &V) {569 auto i = return_any_iterator(V.begin());570 auto j = std::next(i, -1); // no-warning571}572 573void next_minus_1_ahead_of_end(const std::vector<int> &V) {574 auto i = --V.end();575 auto j = std::next(i, -1); // no-warning576}577 578void next_minus_1_end(const std::vector<int> &V) {579 auto i = V.end();580 auto j = std::next(i, -1); // no-warning581}582 583// std::next() by +2584 585void next_plus_2_begin(const std::vector<int> &V) {586 auto i = V.begin();587 auto j = std::next(i, 2); // no-warning588}589 590void next_plus_2_behind_begin(const std::vector<int> &V) {591 auto i = ++V.begin();592 auto j = std::next(i, 2); // no-warning593}594 595void next_plus_2_unknown(const std::vector<int> &V) {596 auto i = return_any_iterator(V.begin());597 auto j = std::next(i, 2); // no-warning598}599 600void next_plus_2_ahead_of_end(const std::vector<int> &V) {601 auto i = --V.end();602 auto j = std::next(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}603 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}604}605 606void next_plus_2_end(const std::vector<int> &V) {607 auto i = V.end();608 auto j = std::next(i, 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}609 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}610}611 612// std::next() by -2613 614void next_minus_2_begin(const std::vector<int> &V) {615 auto i = V.begin();616 auto j = std::next(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}617 // expected-note@-1{{Iterator decremented ahead of its valid range}}618}619 620void next_minus_2_behind_begin(const std::vector<int> &V) {621 auto i = ++V.begin();622 auto j = std::next(i, -2); // expected-warning{{Iterator decremented ahead of its valid range}}623 // expected-note@-1{{Iterator decremented ahead of its valid range}}624}625 626void next_minus_2_unknown(const std::vector<int> &V) {627 auto i = return_any_iterator(V.begin());628 auto j = std::next(i, -2); // no-warning629}630 631void next_minus_2_ahead_of_end(const std::vector<int> &V) {632 auto i = --V.end();633 auto j = std::next(i, -2); // no-warning634}635 636void next_minus_2_end(const std::vector<int> &V) {637 auto i = V.end();638 auto j = std::next(i, -2); // no-warning639}640 641// std::next() by 0642 643void next_0_begin(const std::vector<int> &V) {644 auto i = V.begin();645 auto j = std::next(i, 0); // no-warning646}647 648void next_0_behind_begin(const std::vector<int> &V) {649 auto i = ++V.begin();650 auto j = std::next(i, 0); // no-warning651}652 653void next_0_unknown(const std::vector<int> &V) {654 auto i = return_any_iterator(V.begin());655 auto j = std::next(i, 0); // no-warning656}657 658void next_0_ahead_of_end(const std::vector<int> &V) {659 auto i = --V.end();660 auto j = std::next(i, 0); // no-warning661}662 663void next_0_end(const std::vector<int> &V) {664 auto i = V.end();665 auto j = std::next(i, 0); // no-warning666}667 668//669// std::prev()670//671 672// std::prev() by +1 (default)673 674void prev_plus_1_begin(const std::vector<int> &V) {675 auto i = V.begin();676 auto j = std::prev(i); // expected-warning{{Iterator decremented ahead of its valid range}}677 // expected-note@-1{{Iterator decremented ahead of its valid range}}678}679 680void prev_plus_1_behind_begin(const std::vector<int> &V) {681 auto i = ++V.begin();682 auto j = std::prev(i); // no-warning683}684 685void prev_plus_1_unknown(const std::vector<int> &V) {686 auto i = return_any_iterator(V.begin());687 auto j = std::prev(i); // no-warning688}689 690void prev_plus_1_ahead_of_end(const std::vector<int> &V) {691 auto i = --V.end();692 auto j = std::prev(i); // no-warning693}694 695void prev_plus_1_end(const std::vector<int> &V) {696 auto i = V.end();697 auto j = std::prev(i); // no-warning698}699 700// std::prev() by -1701 702void prev_minus_1_begin(const std::vector<int> &V) {703 auto i = V.begin();704 auto j = std::prev(i, -1); // no-warning705}706 707void prev_minus_1_behind_begin(const std::vector<int> &V) {708 auto i = ++V.begin();709 auto j = std::prev(i, -1); // no-warning710}711 712void prev_minus_1_unknown(const std::vector<int> &V) {713 auto i = return_any_iterator(V.begin());714 auto j = std::prev(i, -1); // no-warning715}716 717void prev_minus_1_ahead_of_end(const std::vector<int> &V) {718 auto i = --V.end();719 auto j = std::prev(i, -1); // no-warning720}721 722void prev_minus_1_end(const std::vector<int> &V) {723 auto i = V.end();724 auto j = std::prev(i, -1); // expected-warning{{Iterator incremented behind the past-the-end iterator}}725 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}726}727 728// std::prev() by +2729 730void prev_plus_2_begin(const std::vector<int> &V) {731 auto i = V.begin();732 auto j = std::prev(i, 2); // expected-warning{{Iterator decremented ahead of its valid range}}733 // expected-note@-1{{Iterator decremented ahead of its valid range}}734}735 736void prev_plus_2_behind_begin(const std::vector<int> &V) {737 auto i = ++V.begin();738 auto j = std::prev(i, 2); // expected-warning{{Iterator decremented ahead of its valid range}}739 // expected-note@-1{{Iterator decremented ahead of its valid range}}740}741 742void prev_plus_2_unknown(const std::vector<int> &V) {743 auto i = return_any_iterator(V.begin());744 auto j = std::prev(i, 2); // no-warning745}746 747void prev_plus_2_ahead_of_end(const std::vector<int> &V) {748 auto i = --V.end();749 auto j = std::prev(i, 2); // no-warning750}751 752void prev_plus_2_end(const std::vector<int> &V) {753 auto i = V.end();754 auto j = std::prev(i, 2); // no-warning755}756 757// std::prev() by -2758 759void prev_minus_2_begin(const std::vector<int> &V) {760 auto i = V.begin();761 auto j = std::prev(i, -2); // no-warning762}763 764void prev_minus_2_behind_begin(const std::vector<int> &V) {765 auto i = ++V.begin();766 auto j = std::prev(i, -2); // no-warning767}768 769void prev_minus_2_unknown(const std::vector<int> &V) {770 auto i = return_any_iterator(V.begin());771 auto j = std::prev(i, -2); // no-warning772}773 774void prev_minus_2_ahead_of_end(const std::vector<int> &V) {775 auto i = --V.end();776 auto j = std::prev(i, -2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}777 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}778}779 780void prev_minus_2_end(const std::vector<int> &V) {781 auto i = V.end();782 auto j = std::prev(i, -2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}783 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}784}785 786// std::prev() by 0787 788void prev_0_begin(const std::vector<int> &V) {789 auto i = V.begin();790 auto j = std::prev(i, 0); // no-warning791}792 793void prev_0_behind_begin(const std::vector<int> &V) {794 auto i = ++V.begin();795 auto j = std::prev(i, 0); // no-warning796}797 798void prev_0_unknown(const std::vector<int> &V) {799 auto i = return_any_iterator(V.begin());800 auto j = std::prev(i, 0); // no-warning801}802 803void prev_0_ahead_of_end(const std::vector<int> &V) {804 auto i = --V.end();805 auto j = std::prev(i, 0); // no-warning806}807 808void prev_0_end(const std::vector<int> &V) {809 auto i = V.end();810 auto j = std::prev(i, 0); // no-warning811}812 813// std::prev() with int* for checking Loc value argument814namespace std {815template <typename T>816T prev(T, int *);817}818 819void prev_loc_value(const std::vector<int> &V, int o) {820 821 auto i = return_any_iterator(V.begin());822 int *offset = &o;823 auto j = std::prev(i, offset); // no-warning824}825 826//827// Structure member dereference operators828//829 830struct S {831 int n;832};833 834// Member dereference - operator->()835 836void arrow_deref_begin(const std::vector<S> &V) {837 auto i = V.begin();838 int n = i->n; // no-warning839}840 841void arrow_deref_end(const std::vector<S> &V) {842 auto i = V.end();843 int n = i->n; // expected-warning{{Past-the-end iterator dereferenced}}844 // expected-note@-1{{Past-the-end iterator dereferenced}}845}846 847// Container modification - test path notes848 849void deref_end_after_pop_back(std::vector<int> &V) {850 const auto i = --V.end();851 852 V.pop_back(); // expected-note{{Container 'V' shrank from the back by 1 position}}853 854 *i; // expected-warning{{Past-the-end iterator dereferenced}}855 // expected-note@-1{{Past-the-end iterator dereferenced}}856}857 858template<typename T>859struct cont_with_ptr_iterator {860 T* begin() const;861 T* end() const;862};863 864void deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {865 auto i = c.end();866 (void) *i; // expected-warning{{Past-the-end iterator dereferenced}}867 // expected-note@-1{{Past-the-end iterator dereferenced}}868}869 870void array_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {871 auto i = c.end();872 (void) i[0]; // expected-warning{{Past-the-end iterator dereferenced}}873 // expected-note@-1{{Past-the-end iterator dereferenced}}874}875 876void arrow_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {877 auto i = c.end();878 (void) i->n; // expected-warning{{Past-the-end iterator dereferenced}}879 // expected-note@-1{{Past-the-end iterator dereferenced}}880}881 882void arrow_star_deref_end_ptr_iterator(const cont_with_ptr_iterator<S> &c,883 int S::*p) {884 auto i = c.end();885 (void)(i->*p); // expected-warning{{Past-the-end iterator dereferenced}}886 // expected-note@-1{{Past-the-end iterator dereferenced}}887}888 889void prefix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {890 auto i = c.end();891 ++i; // expected-warning{{Iterator incremented behind the past-the-end iterator}}892 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}893}894 895void postfix_incr_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {896 auto i = c.end();897 i++; // expected-warning{{Iterator incremented behind the past-the-end iterator}}898 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}899}900 901void prefix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {902 auto i = c.begin();903 --i; // expected-warning{{Iterator decremented ahead of its valid range}}904 // expected-note@-1{{Iterator decremented ahead of its valid range}}905}906 907void postfix_decr_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {908 auto i = c.begin();909 i--; // expected-warning{{Iterator decremented ahead of its valid range}}910 // expected-note@-1{{Iterator decremented ahead of its valid range}}911}912 913void prefix_add_2_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {914 auto i = c.end();915 (void)(i + 2); // expected-warning{{Iterator incremented behind the past-the-end iterator}}916 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}917}918 919void postfix_add_assign_2_end_ptr_iterator(const cont_with_ptr_iterator<S> &c) {920 auto i = c.end();921 i += 2; // expected-warning{{Iterator incremented behind the past-the-end iterator}}922 // expected-note@-1{{Iterator incremented behind the past-the-end iterator}}923}924 925void prefix_minus_2_begin_ptr_iterator(const cont_with_ptr_iterator<S> &c) {926 auto i = c.begin();927 (void)(i - 2); // expected-warning{{Iterator decremented ahead of its valid range}}928 // expected-note@-1{{Iterator decremented ahead of its valid range}}929}930 931void postfix_minus_assign_2_begin_ptr_iterator(932 const cont_with_ptr_iterator<S> &c) {933 auto i = c.begin();934 i -= 2; // expected-warning{{Iterator decremented ahead of its valid range}}935 // expected-note@-1{{Iterator decremented ahead of its valid range}}936}937 938void ptr_iter_diff(cont_with_ptr_iterator<S> &c) {939 auto i0 = c.begin(), i1 = c.end();940 ptrdiff_t len = i1 - i0; // no-crash941}942 943int uninit_var(int n) {944 int uninit; // expected-note{{'uninit' declared without an initial value}}945 return n - uninit; // no-crash946 // expected-warning@-1 {{The right operand of '-' is a garbage value}}947 // expected-note@-2 {{The right operand of '-' is a garbage value}}948}949 950namespace std {951namespace ranges {952 template <class InOutIter, class Sentinel>953 InOutIter next(InOutIter, Sentinel);954} // namespace ranges955} // namespace std956 957void gh65009__no_crash_on_ranges_next(int **begin, int **end) {958 (void)std::ranges::next(begin, end); // no-crash959}960