Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace iterator;
namespace {

class ContainerModeling
: public Checker<check::PostCall, check::LiveSymbols, check::DeadSymbols> {
: public Checker<check::PostCall, check::LiveSymbols, check::DeadSymbols> {

void handleBegin(CheckerContext &C, const Expr *CE, SVal RetVal,
SVal Cont) const;
Expand Down Expand Up @@ -74,19 +74,25 @@ class ContainerModeling
CallDescriptionMap<NoItParamFn> NoIterParamFunctions = {
{{CDM::CXXMethod, {"clear"}, 0}, &ContainerModeling::handleClear},
{{CDM::CXXMethod, {"assign"}, 2}, &ContainerModeling::handleAssign},
{{CDM::CXXMethod, {"assign_range"}, 1}, &ContainerModeling::handleAssign},
{{CDM::CXXMethod, {"push_back"}, 1}, &ContainerModeling::handlePushBack},
{{CDM::CXXMethod, {"emplace_back"}, 1},
&ContainerModeling::handlePushBack},
{{CDM::CXXMethod, {"append_range"}, 1},
&ContainerModeling::handlePushBack},
{{CDM::CXXMethod, {"pop_back"}, 0}, &ContainerModeling::handlePopBack},
{{CDM::CXXMethod, {"push_front"}, 1},
&ContainerModeling::handlePushFront},
{{CDM::CXXMethod, {"emplace_front"}, 1},
&ContainerModeling::handlePushFront},
{{CDM::CXXMethod, {"prepend_range"}, 1},
&ContainerModeling::handlePushFront},
{{CDM::CXXMethod, {"pop_front"}, 0}, &ContainerModeling::handlePopFront},
};

CallDescriptionMap<OneItParamFn> OneIterParamFunctions = {
{{CDM::CXXMethod, {"insert"}, 2}, &ContainerModeling::handleInsert},
{{CDM::CXXMethod, {"insert_range"}, 2}, &ContainerModeling::handleInsert},
{{CDM::CXXMethod, {"emplace"}, 2}, &ContainerModeling::handleInsert},
{{CDM::CXXMethod, {"erase"}, 1}, &ContainerModeling::handleErase},
{{CDM::CXXMethod, {"erase_after"}, 1},
Expand Down Expand Up @@ -143,13 +149,13 @@ ProgramStateRef rebaseSymbolInIteratorPositionsIf(
ProgramStateRef State, SValBuilder &SVB, SymbolRef OldSym,
SymbolRef NewSym, SymbolRef CondSym, BinaryOperator::Opcode Opc);
SymbolRef rebaseSymbol(ProgramStateRef State, SValBuilder &SVB, SymbolRef Expr,
SymbolRef OldSym, SymbolRef NewSym);
SymbolRef OldSym, SymbolRef NewSym);
bool hasLiveIterators(ProgramStateRef State, const MemRegion *Cont);

} // namespace

void ContainerModeling::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
CheckerContext &C) const {
const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
if (!Func)
return;
Expand All @@ -161,7 +167,7 @@ void ContainerModeling::checkPostCall(const CallEvent &Call,
const auto *InstCall = cast<CXXInstanceCall>(&Call);
if (cast<CXXMethodDecl>(Func)->isMoveAssignmentOperator()) {
handleAssignment(C, InstCall->getCXXThisVal(), Call.getOriginExpr(),
Call.getArgSVal(0));
Call.getArgSVal(0));
return;
}

Expand Down Expand Up @@ -248,7 +254,7 @@ void ContainerModeling::checkDeadSymbols(SymbolReaper &SR,
}

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;
Expand All @@ -270,7 +276,7 @@ void ContainerModeling::handleBegin(CheckerContext &C, const Expr *CE,
}

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;
Expand Down Expand Up @@ -439,12 +445,12 @@ void ContainerModeling::handlePushBack(CheckerContext &C, SVal Cont,
auto &BVF = SymMgr.getBasicVals();
auto &SVB = C.getSValBuilder();
const auto newEndSym =
SVB.evalBinOp(State, BO_Add,
nonloc::SymbolVal(EndSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(EndSym)).getAsSymbol();
SVB.evalBinOp(State, BO_Add, nonloc::SymbolVal(EndSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(EndSym))
.getAsSymbol();
const NoteTag *ChangeTag =
getChangeTag(C, "extended to the back by 1 position", ContReg, ContE);
getChangeTag(C, "extended to the back by 1 position", ContReg, ContE);
State = setContainerData(State, ContReg, CData->newEnd(newEndSym));
C.addTransition(State, ChangeTag);
}
Expand All @@ -468,12 +474,12 @@ void ContainerModeling::handlePopBack(CheckerContext &C, SVal Cont,
auto &BVF = SymMgr.getBasicVals();
auto &SVB = C.getSValBuilder();
const auto BackSym =
SVB.evalBinOp(State, BO_Sub,
nonloc::SymbolVal(EndSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(EndSym)).getAsSymbol();
SVB.evalBinOp(State, BO_Sub, nonloc::SymbolVal(EndSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(EndSym))
.getAsSymbol();
const NoteTag *ChangeTag =
getChangeTag(C, "shrank from the back by 1 position", ContReg, ContE);
getChangeTag(C, "shrank from the back by 1 position", ContReg, ContE);
// For vector-like and deque-like containers invalidate the last and the
// past-end iterator positions. For list-like containers only invalidate
// the last position
Expand Down Expand Up @@ -513,10 +519,10 @@ void ContainerModeling::handlePushFront(CheckerContext &C, SVal Cont,
auto &BVF = SymMgr.getBasicVals();
auto &SVB = C.getSValBuilder();
const auto newBeginSym =
SVB.evalBinOp(State, BO_Sub,
nonloc::SymbolVal(BeginSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(BeginSym)).getAsSymbol();
SVB.evalBinOp(State, BO_Sub, nonloc::SymbolVal(BeginSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(BeginSym))
.getAsSymbol();
const NoteTag *ChangeTag =
getChangeTag(C, "extended to the front by 1 position", ContReg, ContE);
State = setContainerData(State, ContReg, CData->newBegin(newBeginSym));
Expand Down Expand Up @@ -550,12 +556,12 @@ void ContainerModeling::handlePopFront(CheckerContext &C, SVal Cont,
auto &BVF = SymMgr.getBasicVals();
auto &SVB = C.getSValBuilder();
const auto newBeginSym =
SVB.evalBinOp(State, BO_Add,
nonloc::SymbolVal(BeginSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(BeginSym)).getAsSymbol();
SVB.evalBinOp(State, BO_Add, nonloc::SymbolVal(BeginSym),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(BeginSym))
.getAsSymbol();
const NoteTag *ChangeTag =
getChangeTag(C, "shrank from the front by 1 position", ContReg, ContE);
getChangeTag(C, "shrank from the front by 1 position", ContReg, ContE);
State = setContainerData(State, ContReg, CData->newBegin(newBeginSym));
C.addTransition(State, ChangeTag);
}
Expand Down Expand Up @@ -663,7 +669,7 @@ void ContainerModeling::handleErase(CheckerContext &C, SVal Cont, SVal Iter1,
}

void ContainerModeling::handleEraseAfter(CheckerContext &C, SVal Cont,
SVal Iter) const {
SVal Iter) const {
auto State = C.getState();
const auto *Pos = getIteratorPosition(State, Iter);
if (!Pos)
Expand All @@ -675,10 +681,10 @@ void ContainerModeling::handleEraseAfter(CheckerContext &C, SVal Cont,
auto &BVF = SymMgr.getBasicVals();
auto &SVB = C.getSValBuilder();
const auto NextSym =
SVB.evalBinOp(State, BO_Add,
nonloc::SymbolVal(Pos->getOffset()),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(Pos->getOffset())).getAsSymbol();
SVB.evalBinOp(State, BO_Add, nonloc::SymbolVal(Pos->getOffset()),
nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
SymMgr.getType(Pos->getOffset()))
.getAsSymbol();
State = invalidateIteratorPositions(State, NextSym, BO_EQ);
C.addTransition(State);
}
Expand All @@ -705,9 +711,9 @@ const NoteTag *ContainerModeling::getChangeTag(CheckerContext &C,
// First try to get the name of the variable from the region
if (const auto *DR = dyn_cast<DeclRegion>(ContReg)) {
Name = DR->getDecl()->getName();
// If the region is not a `DeclRegion` then use the expression instead
// If the region is not a `DeclRegion` then use the expression instead
} else if (const auto *DRE =
dyn_cast<DeclRefExpr>(ContE->IgnoreParenCasts())) {
dyn_cast<DeclRefExpr>(ContE->IgnoreParenCasts())) {
Name = DRE->getDecl()->getName();
}

Expand All @@ -725,7 +731,7 @@ const NoteTag *ContainerModeling::getChangeTag(CheckerContext &C,
}

void ContainerModeling::printState(raw_ostream &Out, ProgramStateRef State,
const char *NL, const char *Sep) const {
const char *NL, const char *Sep) const {
auto ContMap = State->get<ContainerMap>();

if (!ContMap.isEmpty()) {
Expand Down Expand Up @@ -998,7 +1004,7 @@ ProgramStateRef reassignAllIteratorPositionsUnless(ProgramStateRef State,
BinaryOperator::Opcode Opc) {
auto MatchContAndCompare = [&](const IteratorPosition &Pos) {
return Pos.getContainer() == Cont &&
!compare(State, Pos.getOffset(), Offset, Opc);
!compare(State, Pos.getOffset(), Offset, Opc);
};
auto ReAssign = [&](const IteratorPosition &Pos) {
return Pos.reAssign(NewCont);
Expand Down Expand Up @@ -1070,7 +1076,7 @@ bool ento::shouldRegisterContainerModeling(const CheckerManager &mgr) {
if (!mgr.getAnalyzerOptions().ShouldAggressivelySimplifyBinaryOperation) {
mgr.getASTContext().getDiagnostics().Report(
diag::err_analyzer_checker_incompatible_analyzer_option)
<< "aggressive-binary-operation-simplification" << "false";
<< "aggressive-binary-operation-simplification" << "false";
return false;
}

Expand Down
42 changes: 42 additions & 0 deletions clang/test/Analysis/Inputs/system-header-simulator-cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ namespace std {
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

template<typename R>
void assign_range(R&& rg);

template<typename R>
iterator insert_range(const_iterator position, R&& rg);

template<typename R>
void append_range(R&& rg);

T &operator[](size_t n) {
return _start[n];
}
Expand Down Expand Up @@ -414,6 +423,18 @@ namespace std {
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

template<typename R>
void assign_range(R&& rg);

template<typename R>
iterator insert_range(const_iterator position, R&& rg);

template<typename R>
void append_range(R&& rg);

template<typename R>
void prepend_range(R&& rg);

iterator begin() { return iterator(_start); }
const_iterator begin() const { return const_iterator(_start); }
const_iterator cbegin() const { return const_iterator(_start); }
Expand Down Expand Up @@ -488,6 +509,18 @@ namespace std {
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

template<typename R>
void assign_range(R&& rg);

template<typename R>
iterator insert_range(const_iterator position, R&& rg);

template<typename R>
void append_range(R&& rg);

template<typename R>
void prepend_range(R&& rg);

T &operator[](size_t n) {
return _start[n];
}
Expand Down Expand Up @@ -561,6 +594,15 @@ namespace std {
iterator erase_after(const_iterator position);
iterator erase_after(const_iterator first, const_iterator last);

template<typename R>
void assign_range(R&& rg);

template<typename R>
void prepend_range(R&& rg);

template<typename R>
iterator insert_range_after(const_iterator pos, R&& rg);

iterator begin() { return iterator(_start); }
const_iterator begin() const { return const_iterator(_start); }
const_iterator cbegin() const { return const_iterator(_start); }
Expand Down
Loading