Index: clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp +++ clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp @@ -177,43 +177,48 @@ handleAssignment(C, InstCall->getCXXThisVal()); return; } - } else { - if (const auto *InstCall = dyn_cast(&Call)) { - const NoItParamFn *Handler0 = NoIterParamFunctions.lookup(Call); - if (Handler0) { - (this->**Handler0)(C, InstCall->getCXXThisVal(), - InstCall->getCXXThisExpr()); - return; - } + } else if (const auto *InstCall = dyn_cast(&Call)) { + const auto *OrigExpr = Call.getOriginExpr(); + if (!OrigExpr) + return; - const OneItParamFn *Handler1 = OneIterParamFunctions.lookup(Call); - if (Handler1) { - (this->**Handler1)(C, InstCall->getCXXThisVal(), Call.getArgSVal(0)); - return; - } + if (isBeginCall(Func)) { + handleBegin(C, OrigExpr, getReturnIterator(Call), + InstCall->getCXXThisVal()); + return; + } - const TwoItParamFn *Handler2 = TwoIterParamFunctions.lookup(Call); - if (Handler2) { - (this->**Handler2)(C, InstCall->getCXXThisVal(), Call.getArgSVal(0), - Call.getArgSVal(1)); - return; - } + if (isEndCall(Func)) { + handleEnd(C, OrigExpr, getReturnIterator(Call), + InstCall->getCXXThisVal()); + return; + } - const auto *OrigExpr = Call.getOriginExpr(); - if (!OrigExpr) - return; + const NoItParamFn *Handler0 = NoIterParamFunctions.lookup(Call); + if (Handler0) { + (this->**Handler0)(C, InstCall->getCXXThisVal(), + InstCall->getCXXThisExpr()); + return; + } - if (isBeginCall(Func)) { - handleBegin(C, OrigExpr, Call.getReturnValue(), - InstCall->getCXXThisVal()); - return; - } + if (Call.getNumArgs() < 1) + return; - if (isEndCall(Func)) { - handleEnd(C, OrigExpr, Call.getReturnValue(), - InstCall->getCXXThisVal()); - return; - } + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + const OneItParamFn *Handler1 = OneIterParamFunctions.lookup(Call); + if (Handler1) { + (this->**Handler1)(C, InstCall->getCXXThisVal(), Arg0); + return; + } + + if (Call.getNumArgs() < 2) + return; + + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); + const TwoItParamFn *Handler2 = TwoIterParamFunctions.lookup(Call); + if (Handler2) { + (this->**Handler2)(C, InstCall->getCXXThisVal(), Arg0, Arg1); + return; } } } @@ -257,7 +262,7 @@ } void ContainerModeling::handleBegin(CheckerContext &C, const Expr *CE, - SVal RetVal, SVal Cont) const { + SVal RetVal, SVal Cont) const { const auto *ContReg = Cont.getAsRegion(); if (!ContReg) return; @@ -273,13 +278,14 @@ C.getLocationContext(), C.blockCount()); BeginSym = getContainerBegin(State, ContReg); } + State = setIteratorPosition(State, RetVal, IteratorPosition::getPosition(ContReg, BeginSym)); C.addTransition(State); } void ContainerModeling::handleEnd(CheckerContext &C, const Expr *CE, - SVal RetVal, SVal Cont) const { + SVal RetVal, SVal Cont) const { const auto *ContReg = Cont.getAsRegion(); if (!ContReg) return; @@ -295,6 +301,7 @@ C.getLocationContext(), C.blockCount()); EndSym = getContainerEnd(State, ContReg); } + State = setIteratorPosition(State, RetVal, IteratorPosition::getPosition(ContReg, EndSym)); C.addTransition(State); Index: clang/lib/StaticAnalyzer/Checkers/DebugIteratorModeling.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/DebugIteratorModeling.cpp +++ clang/lib/StaticAnalyzer/Checkers/DebugIteratorModeling.cpp @@ -30,14 +30,15 @@ std::unique_ptr DebugMsgBugType; template - void analyzerIteratorDataField(const CallExpr *CE, CheckerContext &C, + void analyzerIteratorDataField(const CallEvent &Call, CheckerContext &C, Getter get, SVal Default) const; - void analyzerIteratorPosition(const CallExpr *CE, CheckerContext &C) const; - void analyzerIteratorContainer(const CallExpr *CE, CheckerContext &C) const; - void analyzerIteratorValidity(const CallExpr *CE, CheckerContext &C) const; + void analyzerIteratorPosition(const CallEvent &Call, CheckerContext &C) const; + void analyzerIteratorContainer(const CallEvent &Call, + CheckerContext &C) const; + void analyzerIteratorValidity(const CallEvent &Call, CheckerContext &C) const; ExplodedNode *reportDebugMsg(llvm::StringRef Msg, CheckerContext &C) const; - typedef void (DebugIteratorModeling::*FnCheck)(const CallExpr *, + typedef void (DebugIteratorModeling::*FnCheck)(const CallEvent &, CheckerContext &) const; CallDescriptionMap Callbacks = { @@ -65,30 +66,30 @@ bool DebugIteratorModeling::evalCall(const CallEvent &Call, CheckerContext &C) const { - const auto *CE = dyn_cast_or_null(Call.getOriginExpr()); - if (!CE) - return false; - const FnCheck *Handler = Callbacks.lookup(Call); if (!Handler) return false; - (this->**Handler)(CE, C); + (this->**Handler)(Call, C); return true; } template -void DebugIteratorModeling::analyzerIteratorDataField(const CallExpr *CE, +void DebugIteratorModeling::analyzerIteratorDataField(const CallEvent &Call, CheckerContext &C, Getter get, SVal Default) const { + const CallExpr *CE = dyn_cast_or_null(Call.getOriginExpr()); + if (!CE) + return; + if (CE->getNumArgs() == 0) { reportDebugMsg("Missing iterator argument", C); return; } auto State = C.getState(); - SVal V = C.getSVal(CE->getArg(0)); + SVal V = getIteratorArg(Call, 0, C.blockCount()); const auto *Pos = getIteratorPosition(State, V); if (Pos) { State = State->BindExpr(CE, C.getLocationContext(), get(Pos)); @@ -98,26 +99,26 @@ C.addTransition(State); } -void DebugIteratorModeling::analyzerIteratorPosition(const CallExpr *CE, +void DebugIteratorModeling::analyzerIteratorPosition(const CallEvent &Call, CheckerContext &C) const { auto &BVF = C.getSValBuilder().getBasicValueFactory(); - analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) { + analyzerIteratorDataField(Call, C, [](const IteratorPosition *P) { return nonloc::SymbolVal(P->getOffset()); }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); } -void DebugIteratorModeling::analyzerIteratorContainer(const CallExpr *CE, +void DebugIteratorModeling::analyzerIteratorContainer(const CallEvent &Call, CheckerContext &C) const { auto &BVF = C.getSValBuilder().getBasicValueFactory(); - analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) { + analyzerIteratorDataField(Call, C, [](const IteratorPosition *P) { return loc::MemRegionVal(P->getContainer()); }, loc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); } -void DebugIteratorModeling::analyzerIteratorValidity(const CallExpr *CE, +void DebugIteratorModeling::analyzerIteratorValidity(const CallEvent &Call, CheckerContext &C) const { auto &BVF = C.getSValBuilder().getBasicValueFactory(); - analyzerIteratorDataField(CE, C, [&BVF](const IteratorPosition *P) { + analyzerIteratorDataField(Call, C, [&BVF](const IteratorPosition *P) { return nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get((P->isValid())))); }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); Index: clang/lib/StaticAnalyzer/Checkers/InvalidatedIteratorChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/InvalidatedIteratorChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/InvalidatedIteratorChecker.cpp @@ -33,9 +33,9 @@ std::unique_ptr InvalidatedBugType; - void verifyAccess(CheckerContext &C, const SVal &Val) const; - void reportBug(const StringRef &Message, const SVal &Val, - CheckerContext &C, ExplodedNode *ErrNode) const; + void verifyAccess(CheckerContext &C, SVal Val) const; + void reportBug(const StringRef &Message, SVal Val, CheckerContext &C, + ExplodedNode *ErrNode) const; public: InvalidatedIteratorChecker(); @@ -67,7 +67,8 @@ if (const auto *InstCall = dyn_cast(&Call)) { verifyAccess(C, InstCall->getCXXThisVal()); } else { - verifyAccess(C, Call.getArgSVal(0)); + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + verifyAccess(C, Arg0); } } } @@ -114,7 +115,8 @@ verifyAccess(C, BaseVal); } -void InvalidatedIteratorChecker::verifyAccess(CheckerContext &C, const SVal &Val) const { +void InvalidatedIteratorChecker::verifyAccess(CheckerContext &C, + SVal Val) const { auto State = C.getState(); const auto *Pos = getIteratorPosition(State, Val); if (Pos && !Pos->isValid()) { @@ -127,7 +129,7 @@ } void InvalidatedIteratorChecker::reportBug(const StringRef &Message, - const SVal &Val, CheckerContext &C, + SVal Val, CheckerContext &C, ExplodedNode *ErrNode) const { auto R = std::make_unique(*InvalidatedBugType, Message, ErrNode); Index: clang/lib/StaticAnalyzer/Checkers/Iterator.h =================================================================== --- clang/lib/StaticAnalyzer/Checkers/Iterator.h +++ clang/lib/StaticAnalyzer/Checkers/Iterator.h @@ -184,6 +184,17 @@ bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2, BinaryOperator::Opcode Opc); +// Returns the value of the iterator argument if it non-class value (a pointer +// or a reference). Otherwise it returns the iterator parameter location (see +// CallEvent::getParameterLocation()). +SVal getIteratorArg(const CallEvent &Call, unsigned Index, unsigned BlockCount); + +// If the call returns an class instance iterator by value then this function +// retrieves and returns it from the construction context of the call. +// Otherwise (for non-class types) it simply returns the return value of the +// call. +SVal getReturnIterator(const CallEvent &Call); + } // namespace iterator } // namespace ento } // namespace clang Index: clang/lib/StaticAnalyzer/Checkers/Iterator.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/Iterator.cpp +++ clang/lib/StaticAnalyzer/Checkers/Iterator.cpp @@ -10,6 +10,8 @@ // //===----------------------------------------------------------------------===// +#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" + #include "Iterator.h" namespace clang { @@ -188,8 +190,6 @@ return State->get(Reg); } else if (const auto Sym = Val.getAsSymbol()) { return State->get(Sym); - } else if (const auto LCVal = Val.getAs()) { - return State->get(LCVal->getRegion()); } return nullptr; } @@ -201,10 +201,8 @@ return State->set(Reg, Pos); } else if (const auto Sym = Val.getAsSymbol()) { return State->set(Sym, Pos); - } else if (const auto LCVal = Val.getAs()) { - return State->set(LCVal->getRegion(), Pos); } - return nullptr; + return State; } ProgramStateRef createIteratorPosition(ProgramStateRef State, const SVal &Val, @@ -314,6 +312,39 @@ return !State->assume(comparison.castAs(), false); } +SVal getIteratorArg(const CallEvent& Call, unsigned Index, + unsigned BlockCount) { + SVal Arg = Call.getArgSVal(Index); + const Expr *ArgExpr = Call.getArgExpr(Index); + + // If the argument is not passed by value (thus it is not an rvalue) then + // return the argument value. + if (!ArgExpr->isRValue()) + return Arg; + + // If the argument is not a record type, then return the argument value. + // Iterators implemented as class instances are record types. + if (!ArgExpr->getType().getDesugaredType( + Call.getState()->getStateManager().getContext())->isRecordType()) + return Arg; + + // For iterators implemented as class instances and passed by value the + // iterator-processing functions need the location of the parameter which + // is the region where the iterator resided. + return loc::MemRegionVal(Call.getParameterLocation(Index, BlockCount)); +} + +SVal getReturnIterator(const CallEvent &Call) { + // If the called function returns an iterator implemented as a class instance + // by value then the return the region where this value is constructed. + Optional RetValUnderConstr = Call.getReturnValueUnderConstruction(); + if (RetValUnderConstr.hasValue()) + return *RetValUnderConstr; + + // In other cases just return the return value. + return Call.getReturnValue(); +} + } // namespace iterator } // namespace ento } // namespace clang Index: clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp +++ clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp @@ -85,8 +85,8 @@ class IteratorModeling : public Checker, check::PostStmt, - check::PostStmt, - check::Bind, check::LiveSymbols, check::DeadSymbols> { + check::PostStmt, check::Bind, + check::LiveSymbols, check::DeadSymbols> { using AdvanceFn = void (IteratorModeling::*)(CheckerContext &, const Expr *, SVal, SVal, SVal) const; @@ -98,28 +98,28 @@ const AdvanceFn *Handler) const; void handleComparison(CheckerContext &C, const Expr *CE, SVal RetVal, - const SVal &LVal, const SVal &RVal, - OverloadedOperatorKind Op) const; + SVal LVal, SVal RVal, OverloadedOperatorKind Op) const; void processComparison(CheckerContext &C, ProgramStateRef State, - SymbolRef Sym1, SymbolRef Sym2, const SVal &RetVal, + SymbolRef Sym1, SymbolRef Sym2, SVal RetVal, OverloadedOperatorKind Op) const; - void handleIncrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter, - bool Postfix) const; - void handleDecrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter, - bool Postfix) const; + void handleAssignment(CheckerContext &C, SVal RetVal, SVal Iter) const; + void handleIncrement(CheckerContext &C, SVal RetVal, + SVal Iter, bool Postfix) const; + void handleDecrement(CheckerContext &C, SVal RetVal, + SVal Iter, bool Postfix) const; void handleRandomIncrOrDecr(CheckerContext &C, const Expr *CE, - OverloadedOperatorKind Op, const SVal &RetVal, - const SVal &Iterator, const SVal &Amount) const; + OverloadedOperatorKind Op, SVal RetVal, + SVal Iterator, SVal Amount) const; void handlePtrIncrOrDecr(CheckerContext &C, const Expr *Iterator, OverloadedOperatorKind OK, SVal Offset, Optional OperandLValue) const; - void handleAdvance(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter, - SVal Amount) const; - void handlePrev(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter, - SVal Amount) const; - void handleNext(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter, - SVal Amount) const; - void assignToContainer(CheckerContext &C, const Expr *CE, const SVal &RetVal, + void handleAdvance(CheckerContext &C, const Expr *CE, SVal RetVal, + SVal Iter, SVal Amount) const; + void handlePrev(CheckerContext &C, const Expr *CE, SVal RetVal, + SVal Iter, SVal Amount) const; + void handleNext(CheckerContext &C, const Expr *CE, SVal RetVal, + SVal Iter, SVal Amount) const; + void assignToContainer(CheckerContext &C, const Expr *CE, SVal RetVal, const MemRegion *Cont) const; bool noChangeInAdvance(CheckerContext &C, SVal Iter, const Expr *CE) const; Optional getOperandLValue(CheckerContext &C, Expr *Operand) const; @@ -150,23 +150,21 @@ void checkPostCall(const CallEvent &Call, CheckerContext &C) const; void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const; + void checkPostStmt(const MaterializeTemporaryExpr *MTE, + CheckerContext &C) const; void checkPostStmt(const UnaryOperator *UO, CheckerContext &C) const; void checkPostStmt(const BinaryOperator *BO, CheckerContext &C) const; void checkPostStmt(const CXXConstructExpr *CCE, CheckerContext &C) const; void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const; - void checkPostStmt(const MaterializeTemporaryExpr *MTE, - CheckerContext &C) const; void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const; void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; }; bool isSimpleComparisonOperator(OverloadedOperatorKind OK); bool isSimpleComparisonOperator(BinaryOperatorKind OK); -ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val); +ProgramStateRef removeIteratorPosition(ProgramStateRef State, SVal Val); ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2, bool Equal); -bool isBoundThroughLazyCompoundVal(const Environment &Env, - const MemRegion *Reg); const ExplodedNode *findCallEnter(const ExplodedNode *Node, const Expr *Call); } // namespace @@ -200,15 +198,17 @@ auto State = C.getState(); // Already bound to container? - if (getIteratorPosition(State, Call.getReturnValue())) + SVal RetVal = getReturnIterator(Call); + if (getIteratorPosition(State, RetVal)) return; // Copy-like and move constructors if (isa(&Call) && Call.getNumArgs() == 1) { - if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(0))) { - State = setIteratorPosition(State, Call.getReturnValue(), *Pos); + SVal Arg = getIteratorArg(Call, 0, C.blockCount()); + if (const auto *Pos = getIteratorPosition(State, Arg)) { + State = setIteratorPosition(State, RetVal, *Pos); if (cast(Func)->isMoveConstructor()) { - State = removeIteratorPosition(State, Call.getArgSVal(0)); + State = removeIteratorPosition(State, Arg); } C.addTransition(State); return; @@ -226,8 +226,7 @@ C.getASTContext()).getTypePtr() == Call.getResultType().getDesugaredType(C.getASTContext()).getTypePtr()) { if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(i))) { - assignToContainer(C, OrigExpr, Call.getReturnValue(), - Pos->getContainer()); + assignToContainer(C, OrigExpr, RetVal, Pos->getContainer()); return; } } @@ -237,6 +236,9 @@ void IteratorModeling::checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const { auto State = C.getState(); + if (Val.getAs()) + return; + const auto *Pos = getIteratorPosition(State, Val); if (Pos) { State = setIteratorPosition(State, Loc, *Pos); @@ -250,6 +252,25 @@ } } +void IteratorModeling::checkPostStmt(const MaterializeTemporaryExpr *MTE, + CheckerContext &C) const { + /* Transfer iterator state to temporary objects */ + auto State = C.getState(); + + SVal SrcVal = C.getSVal(MTE->getSubExpr()); + if (SrcVal.getAs()) + return; + + const auto *Pos = getIteratorPosition(State, SrcVal); + if (!Pos) + return; + + SVal TempVal = C.getSVal(MTE); + State = setIteratorPosition(State, TempVal, *Pos); + C.addTransition(State); +} + + void IteratorModeling::checkPostStmt(const UnaryOperator *UO, CheckerContext &C) const { UnaryOperatorKind OK = UO->getOpcode(); @@ -313,17 +334,6 @@ } } -void IteratorModeling::checkPostStmt(const MaterializeTemporaryExpr *MTE, - CheckerContext &C) const { - /* Transfer iterator state to temporary objects */ - auto State = C.getState(); - const auto *Pos = getIteratorPosition(State, C.getSVal(MTE->getSubExpr())); - if (!Pos) - return; - State = setIteratorPosition(State, C.getSVal(MTE), *Pos); - C.addTransition(State); -} - void IteratorModeling::checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const { // Keep symbolic expressions of iterator positions alive @@ -331,8 +341,9 @@ for (const auto &Reg : RegionMap) { const auto Offset = Reg.second.getOffset(); for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i) - if (isa(*i)) + if (isa(*i)) { SR.markLive(*i); + } } auto SymbolMap = State->get(); @@ -353,12 +364,7 @@ auto RegionMap = State->get(); for (const auto &Reg : RegionMap) { if (!SR.isLiveRegion(Reg.first)) { - // The region behind the `LazyCompoundVal` is often cleaned up before - // the `LazyCompoundVal` itself. If there are iterator positions keyed - // by these regions their cleanup must be deferred. - if (!isBoundThroughLazyCompoundVal(State->getEnvironment(), Reg.first)) { - State = State->remove(Reg.first); - } + State = State->remove(Reg.first); } } @@ -376,74 +382,88 @@ IteratorModeling::handleOverloadedOperator(CheckerContext &C, const CallEvent &Call, OverloadedOperatorKind Op) const { - if (isSimpleComparisonOperator(Op)) { - const auto *OrigExpr = Call.getOriginExpr(); - if (!OrigExpr) - return; + const auto *OrigExpr = Call.getOriginExpr(); + if (!OrigExpr) + return; - if (const auto *InstCall = dyn_cast(&Call)) { - handleComparison(C, OrigExpr, Call.getReturnValue(), - InstCall->getCXXThisVal(), Call.getArgSVal(0), Op); - return; - } + if (Op == OO_Equal) { + handleAssignment(C, getReturnIterator(Call), + getIteratorArg(Call, 0, C.blockCount())); + return; + } + + if (isSimpleComparisonOperator(Op)) { + const auto *OrigExpr = Call.getOriginExpr(); + if (!OrigExpr) + return; - handleComparison(C, OrigExpr, Call.getReturnValue(), Call.getArgSVal(0), - Call.getArgSVal(1), Op); + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + if (const auto *InstCall = dyn_cast(&Call)) { + handleComparison(C, OrigExpr, Call.getReturnValue(), + InstCall->getCXXThisVal(), Arg0, Op); return; - } else if (isRandomIncrOrDecrOperator(Op)) { - const auto *OrigExpr = Call.getOriginExpr(); - if (!OrigExpr) - return; + } - if (const auto *InstCall = dyn_cast(&Call)) { - if (Call.getNumArgs() >= 1 && - Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) { - handleRandomIncrOrDecr(C, OrigExpr, Op, Call.getReturnValue(), - InstCall->getCXXThisVal(), Call.getArgSVal(0)); - return; - } - } else if (Call.getNumArgs() >= 2) { - const Expr *FirstArg = Call.getArgExpr(0); - const Expr *SecondArg = Call.getArgExpr(1); - const QualType FirstType = FirstArg->getType(); - const QualType SecondType = SecondArg->getType(); - - if (FirstType->isIntegralOrEnumerationType() || - SecondType->isIntegralOrEnumerationType()) { - // In case of operator+ the iterator can be either on the LHS (eg.: - // it + 1), or on the RHS (eg.: 1 + it). Both cases are modeled. - const bool IsIterFirst = FirstType->isStructureOrClassType(); - const SVal FirstArg = Call.getArgSVal(0); - const SVal SecondArg = Call.getArgSVal(1); - const SVal &Iterator = IsIterFirst ? FirstArg : SecondArg; - const SVal &Amount = IsIterFirst ? SecondArg : FirstArg; - - handleRandomIncrOrDecr(C, OrigExpr, Op, Call.getReturnValue(), - Iterator, Amount); - return; - } + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); + handleComparison(C, OrigExpr, Call.getReturnValue(), Arg0, Arg1, Op); + return; + } else if (isRandomIncrOrDecrOperator(Op)) { + if (const auto *InstCall = dyn_cast(&Call)) { + if (Call.getNumArgs() >= 1 && + Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) { + handleRandomIncrOrDecr(C, OrigExpr, Op, getReturnIterator(Call), + InstCall->getCXXThisVal(), Call.getArgSVal(0)); + return; } - } else if (isIncrementOperator(Op)) { - if (const auto *InstCall = dyn_cast(&Call)) { - handleIncrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(), - Call.getNumArgs()); + } else if (Call.getNumArgs() >= 2) { + const Expr *FirstArg = Call.getArgExpr(0); + const Expr *SecondArg = Call.getArgExpr(1); + const QualType FirstType = FirstArg->getType(); + const QualType SecondType = SecondArg->getType(); + + if (FirstType->isIntegralOrEnumerationType() || + SecondType->isIntegralOrEnumerationType()) { + // In case of operator+ the iterator can be either on the LHS (eg.: + // it + 1), or on the RHS (eg.: 1 + it). Both cases are modeled. + const bool IsIterFirst = FirstType->isStructureOrClassType(); + const SVal Iterator = getIteratorArg(Call, IsIterFirst ? 0 : 1, + C.blockCount()); + const SVal Amount = Call.getArgSVal(IsIterFirst ? 1 : 0); + + handleRandomIncrOrDecr(C, OrigExpr, Op, getReturnIterator(Call), + Iterator, Amount); return; } - - handleIncrement(C, Call.getReturnValue(), Call.getArgSVal(0), - Call.getNumArgs()); - return; - } else if (isDecrementOperator(Op)) { - if (const auto *InstCall = dyn_cast(&Call)) { - handleDecrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(), - Call.getNumArgs()); + } else { + if (Call.getNumArgs() >= 2 && + Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) { + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + handleRandomIncrOrDecr(C, OrigExpr, Op, getReturnIterator(Call), Arg0, + Call.getArgSVal(1)); return; } + } + } else if (isIncrementOperator(Op)) { + if (const auto *InstCall = dyn_cast(&Call)) { + handleIncrement(C, getReturnIterator(Call), InstCall->getCXXThisVal(), + Call.getNumArgs()); + return; + } - handleDecrement(C, Call.getReturnValue(), Call.getArgSVal(0), - Call.getNumArgs()); + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + handleIncrement(C, getReturnIterator(Call), Arg0, Call.getNumArgs()); + return; + } else if (isDecrementOperator(Op)) { + if (const auto *InstCall = dyn_cast(&Call)) { + handleDecrement(C, getReturnIterator(Call), InstCall->getCXXThisVal(), + Call.getNumArgs()); return; } + + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + handleDecrement(C, getReturnIterator(Call), Arg0, Call.getNumArgs()); + return; + } } void @@ -451,9 +471,10 @@ const CallEvent &Call, const Expr *OrigExpr, const AdvanceFn *Handler) const { - if (!C.wasInlined) { - (this->**Handler)(C, OrigExpr, Call.getReturnValue(), - Call.getArgSVal(0), Call.getArgSVal(1)); + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + if (!C.wasInlined) { + (this->**Handler)(C, OrigExpr, getReturnIterator(Call), Arg0, + Call.getArgSVal(1)); return; } @@ -462,23 +483,22 @@ const auto *IdInfo = cast(Call.getDecl())->getIdentifier(); if (IdInfo) { if (IdInfo->getName() == "advance") { - if (noChangeInAdvance(C, Call.getArgSVal(0), OrigExpr)) { - (this->**Handler)(C, OrigExpr, Call.getReturnValue(), - Call.getArgSVal(0), Call.getArgSVal(1)); + if (noChangeInAdvance(C, Arg0, OrigExpr)) { + (this->**Handler)(C, OrigExpr, getReturnIterator(Call), Arg0, + Call.getArgSVal(1)); } } } } void IteratorModeling::handleComparison(CheckerContext &C, const Expr *CE, - SVal RetVal, const SVal &LVal, - const SVal &RVal, - OverloadedOperatorKind Op) const { + SVal RetVal, SVal LVal, SVal RVal, + OverloadedOperatorKind Op) const { // Record the operands and the operator of the comparison for the next // evalAssume, if the result is a symbolic expression. If it is a concrete // value (only one branch is possible), then transfer the state between // the operands according to the operator and the result - auto State = C.getState(); + auto State = C.getState(); const auto *LPos = getIteratorPosition(State, LVal); const auto *RPos = getIteratorPosition(State, RVal); const MemRegion *Cont = nullptr; @@ -531,7 +551,7 @@ void IteratorModeling::processComparison(CheckerContext &C, ProgramStateRef State, SymbolRef Sym1, - SymbolRef Sym2, const SVal &RetVal, + SymbolRef Sym2, SVal RetVal, OverloadedOperatorKind Op) const { if (const auto TruthVal = RetVal.getAs()) { if ((State = relateSymbols(State, Sym1, Sym2, @@ -559,8 +579,20 @@ } } -void IteratorModeling::handleIncrement(CheckerContext &C, const SVal &RetVal, - const SVal &Iter, bool Postfix) const { +void IteratorModeling::handleAssignment(CheckerContext &C, SVal RetVal, + SVal Iter) const { + ProgramStateRef State = C.getState(); + + const IteratorPosition *Pos = getIteratorPosition(State, Iter); + if (!Pos) + return; + + State = setIteratorPosition(State, RetVal, *Pos); + C.addTransition(State); +} + +void IteratorModeling::handleIncrement(CheckerContext &C, SVal RetVal, + SVal Iter, bool Postfix) const { // Increment the symbolic expressions which represents the position of the // iterator auto State = C.getState(); @@ -585,8 +617,8 @@ C.addTransition(State); } -void IteratorModeling::handleDecrement(CheckerContext &C, const SVal &RetVal, - const SVal &Iter, bool Postfix) const { +void IteratorModeling::handleDecrement(CheckerContext &C, SVal RetVal, + SVal Iter, bool Postfix) const { // Decrement the symbolic expressions which represents the position of the // iterator auto State = C.getState(); @@ -613,9 +645,8 @@ void IteratorModeling::handleRandomIncrOrDecr(CheckerContext &C, const Expr *CE, OverloadedOperatorKind Op, - const SVal &RetVal, - const SVal &Iterator, - const SVal &Amount) const { + SVal RetVal, SVal Iterator, + SVal Amount) const { // Increment or decrement the symbolic expressions which represents the // position of the iterator auto State = C.getState(); @@ -640,7 +671,7 @@ auto AdvancedState = advancePosition(State, Iterator, Op, *Value); if (AdvancedState) { const auto *NewPos = getIteratorPosition(AdvancedState, Iterator); - assert(NewPos && + assert(NewPos && "Iterator should have position after successful advancement"); State = setIteratorPosition(State, TgtVal, *NewPos); @@ -705,17 +736,19 @@ } void IteratorModeling::handlePrev(CheckerContext &C, const Expr *CE, - SVal RetVal, SVal Iter, SVal Amount) const { + SVal RetVal, SVal Iter, + SVal Amount) const { handleRandomIncrOrDecr(C, CE, OO_Minus, RetVal, Iter, Amount); } void IteratorModeling::handleNext(CheckerContext &C, const Expr *CE, - SVal RetVal, SVal Iter, SVal Amount) const { + SVal RetVal, SVal Iter, + SVal Amount) const { handleRandomIncrOrDecr(C, CE, OO_Plus, RetVal, Iter, Amount); } void IteratorModeling::assignToContainer(CheckerContext &C, const Expr *CE, - const SVal &RetVal, + SVal RetVal, const MemRegion *Cont) const { Cont = Cont->getMostDerivedObjectRegion(); @@ -817,16 +850,14 @@ return OK == BO_EQ || OK == BO_NE; } -ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val) { +ProgramStateRef removeIteratorPosition(ProgramStateRef State, SVal Val) { if (auto Reg = Val.getAsRegion()) { Reg = Reg->getMostDerivedObjectRegion(); return State->remove(Reg); } else if (const auto Sym = Val.getAsSymbol()) { return State->remove(Sym); - } else if (const auto LCVal = Val.getAs()) { - return State->remove(LCVal->getRegion()); } - return nullptr; + return State; } ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1, @@ -861,18 +892,6 @@ return NewState; } -bool isBoundThroughLazyCompoundVal(const Environment &Env, - const MemRegion *Reg) { - for (const auto &Binding : Env) { - if (const auto LCVal = Binding.second.getAs()) { - if (LCVal->getRegion() == Reg) - return true; - } - } - - return false; -} - const ExplodedNode *findCallEnter(const ExplodedNode *Node, const Expr *Call) { while (Node) { ProgramPoint PP = Node->getLocation(); Index: clang/lib/StaticAnalyzer/Checkers/IteratorRangeChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/IteratorRangeChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/IteratorRangeChecker.cpp @@ -89,18 +89,14 @@ if (const auto *InstCall = dyn_cast(&Call)) { verifyIncrement(C, InstCall->getCXXThisVal()); } else { - if (Call.getNumArgs() >= 1) { - verifyIncrement(C, Call.getArgSVal(0)); - } + verifyIncrement(C, getIteratorArg(Call, 0, C.blockCount())); } } else if (isDecrementOperator(Func->getOverloadedOperator())) { // Check for out-of-range decrementions if (const auto *InstCall = dyn_cast(&Call)) { verifyDecrement(C, InstCall->getCXXThisVal()); } else { - if (Call.getNumArgs() >= 1) { - verifyDecrement(C, Call.getArgSVal(0)); - } + verifyDecrement(C, getIteratorArg(Call, 0, C.blockCount())); } } else if (isRandomIncrOrDecrOperator(Func->getOverloadedOperator())) { if (const auto *InstCall = dyn_cast(&Call)) { @@ -115,7 +111,8 @@ if (Call.getNumArgs() >= 2 && Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) { verifyRandomIncrOrDecr(C, Func->getOverloadedOperator(), - Call.getArgSVal(0), Call.getArgSVal(1)); + getIteratorArg(Call, 0, C.blockCount()), + Call.getArgSVal(1)); } } } else if (isDereferenceOperator(Func->getOverloadedOperator())) { @@ -123,18 +120,20 @@ if (const auto *InstCall = dyn_cast(&Call)) { verifyDereference(C, InstCall->getCXXThisVal()); } else { - verifyDereference(C, Call.getArgSVal(0)); + verifyDereference(C, getIteratorArg(Call, 0, C.blockCount())); } } } else { const AdvanceFn *Verifier = AdvanceFunctions.lookup(Call); if (Verifier) { + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + if (Call.getNumArgs() > 1) { - (this->**Verifier)(C, Call.getArgSVal(0), Call.getArgSVal(1)); + (this->**Verifier)(C, Arg0, Call.getArgSVal(1)); } else { auto &BVF = C.getSValBuilder().getBasicValueFactory(); (this->**Verifier)( - C, Call.getArgSVal(0), + C, Arg0, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1)))); } } Index: clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp @@ -32,16 +32,12 @@ std::unique_ptr MismatchedBugType; - void verifyMatch(CheckerContext &C, const SVal &Iter, - const MemRegion *Cont) const; - void verifyMatch(CheckerContext &C, const SVal &Iter1, - const SVal &Iter2) const; - void reportBug(const StringRef &Message, const SVal &Val1, - const SVal &Val2, CheckerContext &C, - ExplodedNode *ErrNode) const; - void reportBug(const StringRef &Message, const SVal &Val, - const MemRegion *Reg, CheckerContext &C, - ExplodedNode *ErrNode) const; + void verifyMatch(CheckerContext &C, SVal Iter, const MemRegion *Cont) const; + void verifyMatch(CheckerContext &C, SVal Iter1, SVal Iter2) const; + void reportBug(const StringRef &Message, SVal Val1, SVal Val2, + CheckerContext &C, ExplodedNode *ErrNode) const; + void reportBug(const StringRef &Message, SVal Val, const MemRegion *Reg, + CheckerContext &C, ExplodedNode *ErrNode) const; public: MismatchedIteratorChecker(); @@ -66,51 +62,55 @@ if (!Func) return; + // If the call has no arguments, there is nothing to check here + if (Call.getNumArgs() < 1) + return; + if (Func->isOverloadedOperator() && isComparisonOperator(Func->getOverloadedOperator())) { // Check for comparisons of iterators of different containers + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); if (const auto *InstCall = dyn_cast(&Call)) { - if (Call.getNumArgs() < 1) - return; - if (!isIteratorType(InstCall->getCXXThisExpr()->getType()) || !isIteratorType(Call.getArgExpr(0)->getType())) return; - verifyMatch(C, InstCall->getCXXThisVal(), Call.getArgSVal(0)); + verifyMatch(C, InstCall->getCXXThisVal(), Arg0); } else { if (Call.getNumArgs() < 2) return; + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); if (!isIteratorType(Call.getArgExpr(0)->getType()) || !isIteratorType(Call.getArgExpr(1)->getType())) return; - verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1)); + verifyMatch(C, Arg0, Arg1); } } else if (const auto *InstCall = dyn_cast(&Call)) { const auto *ContReg = InstCall->getCXXThisVal().getAsRegion(); if (!ContReg) return; + + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); // Check for erase, insert and emplace using iterator of another container if (isEraseCall(Func) || isEraseAfterCall(Func)) { - verifyMatch(C, Call.getArgSVal(0), - InstCall->getCXXThisVal().getAsRegion()); + verifyMatch(C, Arg0, InstCall->getCXXThisVal().getAsRegion()); if (Call.getNumArgs() == 2) { - verifyMatch(C, Call.getArgSVal(1), - InstCall->getCXXThisVal().getAsRegion()); + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); + verifyMatch(C, Arg1, InstCall->getCXXThisVal().getAsRegion()); } } else if (isInsertCall(Func)) { - verifyMatch(C, Call.getArgSVal(0), - InstCall->getCXXThisVal().getAsRegion()); + verifyMatch(C, Arg0, InstCall->getCXXThisVal().getAsRegion()); if (Call.getNumArgs() == 3 && isIteratorType(Call.getArgExpr(1)->getType()) && isIteratorType(Call.getArgExpr(2)->getType())) { - verifyMatch(C, Call.getArgSVal(1), Call.getArgSVal(2)); + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); + SVal Arg2 = getIteratorArg(Call, 2, C.blockCount()); + verifyMatch(C, Arg1, Arg2); } } else if (isEmplaceCall(Func)) { - verifyMatch(C, Call.getArgSVal(0), - InstCall->getCXXThisVal().getAsRegion()); + verifyMatch(C, Arg0, InstCall->getCXXThisVal().getAsRegion()); } } else if (isa(&Call)) { // Check match of first-last iterator pair in a constructor of a container @@ -129,7 +129,9 @@ !isIteratorType(Call.getArgExpr(1)->getType())) return; - verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1)); + SVal Arg0 = getIteratorArg(Call, 0, C.blockCount()); + SVal Arg1 = getIteratorArg(Call, 1, C.blockCount()); + verifyMatch(C, Arg0, Arg1); } else { // The main purpose of iterators is to abstract away from different // containers and provide a (maybe limited) uniform access to them. @@ -167,7 +169,7 @@ if (!isIteratorType(TAType)) continue; - SVal LHS = UndefinedVal(); + Optional LHS = None; // For every template parameter which is an iterator type in the // instantiation look for all functions' parameters' type by it and @@ -179,10 +181,13 @@ if (!ParamType || ParamType->getReplacedParameter()->getDecl() != TPDecl) continue; - if (LHS.isUndef()) { - LHS = Call.getArgSVal(J); + + SVal ArgJ = getIteratorArg(Call, J, C.blockCount()); + + if (!LHS.hasValue()) { + LHS = ArgJ; } else { - verifyMatch(C, LHS, Call.getArgSVal(J)); + verifyMatch(C, *LHS, ArgJ); } } } @@ -200,7 +205,7 @@ verifyMatch(C, LVal, RVal); } -void MismatchedIteratorChecker::verifyMatch(CheckerContext &C, const SVal &Iter, +void MismatchedIteratorChecker::verifyMatch(CheckerContext &C, SVal Iter, const MemRegion *Cont) const { // Verify match between a container and the container of an iterator Cont = Cont->getMostDerivedObjectRegion(); @@ -231,14 +236,13 @@ if (!N) { return; } - reportBug("Container accessed using foreign iterator argument.", - Iter, Cont, C, N); + reportBug("Container accessed using foreign iterator argument.", Iter, Cont, + C, N); } } -void MismatchedIteratorChecker::verifyMatch(CheckerContext &C, - const SVal &Iter1, - const SVal &Iter2) const { +void MismatchedIteratorChecker::verifyMatch(CheckerContext &C, SVal Iter1, + SVal Iter2) const { // Verify match between the containers of two iterators auto State = C.getState(); const auto *Pos1 = getIteratorPosition(State, Iter1); @@ -275,10 +279,8 @@ } } -void MismatchedIteratorChecker::reportBug(const StringRef &Message, - const SVal &Val1, - const SVal &Val2, - CheckerContext &C, +void MismatchedIteratorChecker::reportBug(const StringRef &Message, SVal Val1, + SVal Val2, CheckerContext &C, ExplodedNode *ErrNode) const { auto R = std::make_unique(*MismatchedBugType, Message, ErrNode); @@ -287,8 +289,8 @@ C.emitReport(std::move(R)); } -void MismatchedIteratorChecker::reportBug(const StringRef &Message, - const SVal &Val, const MemRegion *Reg, +void MismatchedIteratorChecker::reportBug(const StringRef &Message, SVal Val, + const MemRegion *Reg, CheckerContext &C, ExplodedNode *ErrNode) const { auto R = std::make_unique(*MismatchedBugType, Message, Index: clang/lib/StaticAnalyzer/Checkers/STLAlgorithmModeling.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/STLAlgorithmModeling.cpp +++ clang/lib/StaticAnalyzer/Checkers/STLAlgorithmModeling.cpp @@ -24,12 +24,12 @@ namespace { class STLAlgorithmModeling : public Checker { - bool evalFind(CheckerContext &C, const CallExpr *CE) const; + void evalFind(CheckerContext &C, const CallExpr *CE, SVal Begin, + SVal End) const; - void Find(CheckerContext &C, const CallExpr *CE, unsigned paramNum) const; - - using FnCheck = bool (STLAlgorithmModeling::*)(CheckerContext &, - const CallExpr *) const; + using FnCheck = void (STLAlgorithmModeling::*)(CheckerContext &, + const CallExpr *, SVal Begin, + SVal End) const; const CallDescriptionMap Callbacks = { {{{"std", "find"}, 3}, &STLAlgorithmModeling::evalFind}, @@ -67,51 +67,45 @@ bool STLAlgorithmModeling::evalCall(const CallEvent &Call, CheckerContext &C) const { - const auto *CE = dyn_cast_or_null(Call.getOriginExpr()); - if (!CE) - return false; - - const FnCheck *Handler = Callbacks.lookup(Call); - if (!Handler) - return false; - - return (this->**Handler)(C, CE); -} - -bool STLAlgorithmModeling::evalFind(CheckerContext &C, - const CallExpr *CE) const { // std::find()-like functions either take their primary range in the first // two parameters, or if the first parameter is "execution policy" then in // the second and third. This means that the second parameter must always be // an iterator. - if (!isIteratorType(CE->getArg(1)->getType())) + if (Call.getNumArgs() < 2 || !isIteratorType(Call.getArgExpr(1)->getType())) return false; // If no "execution policy" parameter is used then the first argument is the - // beginning of the range. - if (isIteratorType(CE->getArg(0)->getType())) { - Find(C, CE, 0); - return true; + // beginning of the range. If "execution policy" parmeter is used then the + // third argument is the end of the range. + unsigned ArgNum = 0; + if (!isIteratorType(Call.getArgExpr(0)->getType())) { + if (Call.getNumArgs() < 3 || !isIteratorType(Call.getArgExpr(2)->getType())) + return false; + ArgNum = 1; } - // If "execution policy" parameter is used then the second argument is the - // beginning of the range. - if (isIteratorType(CE->getArg(2)->getType())) { - Find(C, CE, 1); - return true; - } + SVal ArgB = getIteratorArg(Call, ArgNum, C.blockCount()); + SVal ArgE = getIteratorArg(Call, ArgNum + 1, C.blockCount()); + + const auto *CE = dyn_cast_or_null(Call.getOriginExpr()); + if (!CE) + return false; - return false; + const FnCheck *Handler = Callbacks.lookup(Call); + if (!Handler) + return false; + + (this->**Handler)(C, CE, ArgB, ArgE); + return true; } -void STLAlgorithmModeling::Find(CheckerContext &C, const CallExpr *CE, - unsigned paramNum) const { +void STLAlgorithmModeling::evalFind(CheckerContext &C, const CallExpr *CE, + SVal Begin, SVal End) const { auto State = C.getState(); auto &SVB = C.getSValBuilder(); const auto *LCtx = C.getLocationContext(); SVal RetVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); - SVal Param = State->getSVal(CE->getArg(paramNum), LCtx); auto StateFound = State->BindExpr(CE, LCtx, RetVal); @@ -119,7 +113,7 @@ // assume that in case of successful search the position of the found element // is not ahead of it. // FIXME: Reverse iterators - const auto *Pos = getIteratorPosition(State, Param); + const auto *Pos = getIteratorPosition(State, Begin); if (Pos) { StateFound = createIteratorPosition(StateFound, RetVal, Pos->getContainer(), CE, LCtx, C.blockCount()); @@ -135,13 +129,11 @@ StateFound = StateFound->assume(GreaterOrEqual.castAs(), true); } - Param = State->getSVal(CE->getArg(paramNum + 1), LCtx); - // If we have an iterator position for the range-end argument then we can // assume that in case of successful search the position of the found element // is ahead of it. // FIXME: Reverse iterators - Pos = getIteratorPosition(State, Param); + Pos = getIteratorPosition(State, End); if (Pos) { StateFound = createIteratorPosition(StateFound, RetVal, Pos->getContainer(), CE, LCtx, C.blockCount()); @@ -160,7 +152,7 @@ C.addTransition(StateFound); if (AggressiveStdFindModeling) { - auto StateNotFound = State->BindExpr(CE, LCtx, Param); + auto StateNotFound = State->BindExpr(CE, LCtx, End); C.addTransition(StateNotFound); } } Index: clang/test/Analysis/iterator-modeling.cpp =================================================================== --- clang/test/Analysis/iterator-modeling.cpp +++ clang/test/Analysis/iterator-modeling.cpp @@ -42,7 +42,7 @@ if (i != v.begin()) { clang_analyzer_warnIfReached(); - } + } } void end(const std::vector &v) { @@ -1034,6 +1034,7 @@ clang_analyzer_express(clang_analyzer_iterator_position(i0)); // expected-warning-re {{$V.begin(){{$}}}} // clang_analyzer_express(clang_analyzer_iterator_position(i3)); FIXME: expecte warning $i1 - 1 + } void vector_insert_ahead_of_end(std::vector &V, int n) { @@ -1942,6 +1943,55 @@ clang_analyzer_eval(clang_analyzer_iterator_container(j) == &V); // expected-warning{{TRUE}} } +template +struct delegated_ctor_iterator { + delegated_ctor_iterator(const T&, int); + delegated_ctor_iterator(const T& t) : delegated_ctor_iterator(t, 0) {} + delegated_ctor_iterator operator++(); + delegated_ctor_iterator operator++(int); + T& operator*(); +}; + +template +struct container_with_delegated_ctor_iterator { + typedef delegated_ctor_iterator iterator; + iterator begin() const { return delegated_ctor_iterator(T()); } +}; + +void +test_delegated_ctor_iterator( + const container_with_delegated_ctor_iterator &c) { + auto i = c.begin(); // no-crash + clang_analyzer_denote(clang_analyzer_container_begin(c), "$c.begin()"); + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning{{$c.begin()}} +} + +template +struct base_ctor_iterator { + base_ctor_iterator(const T&); + base_ctor_iterator operator++(); + base_ctor_iterator operator++(int); + T& operator*(); +}; + +template +struct derived_ctor_iterator: public base_ctor_iterator { + derived_ctor_iterator(const T& t) : base_ctor_iterator(t) {} +}; + +template +struct container_with_derived_ctor_iterator { + typedef derived_ctor_iterator iterator; + iterator begin() const { return derived_ctor_iterator(T()); } +}; + +void +test_derived_ctor_iterator(const container_with_derived_ctor_iterator &c) { + auto i = c.begin(); + clang_analyzer_denote(clang_analyzer_container_begin(c), "$c.begin()"); + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning{{$c.begin()}} +} + void clang_analyzer_printState(); void print_state(std::vector &V) {