Skip to content

Commit 0a7592b

Browse files
author
Adam Balogh
committed
[Analyzer] Mark SymbolData parts of iterator position as live in program state maps
Marking a symbolic expression as live is non-recursive. In our checkers we either use conjured symbols or conjured symbols plus/minus integers to represent abstract position of iterators, so in this latter case we also must mark the `SymbolData` part of these symbolic expressions as live to prevent them from getting reaped. Differential Revision: https://reviews.llvm.org/D48764 llvm-svn: 337151
1 parent 0a75de4 commit 0a7592b

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

clang/lib/StaticAnalyzer/Checkers/IteratorChecker.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,18 @@ void IteratorChecker::checkLiveSymbols(ProgramStateRef State,
488488
// alive
489489
auto RegionMap = State->get<IteratorRegionMap>();
490490
for (const auto Reg : RegionMap) {
491-
const auto Pos = Reg.second;
492-
SR.markLive(Pos.getOffset());
491+
const auto Offset = Reg.second.getOffset();
492+
for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
493+
if (isa<SymbolData>(*i))
494+
SR.markLive(*i);
493495
}
494496

495497
auto SymbolMap = State->get<IteratorSymbolMap>();
496498
for (const auto Sym : SymbolMap) {
497-
const auto Pos = Sym.second;
498-
SR.markLive(Pos.getOffset());
499+
const auto Offset = Sym.second.getOffset();
500+
for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
501+
if (isa<SymbolData>(*i))
502+
SR.markLive(*i);
499503
}
500504

501505
auto ContMap = State->get<ContainerMap>();
@@ -1157,21 +1161,31 @@ ProgramStateRef relateIteratorPositions(ProgramStateRef State,
11571161
const IteratorPosition &Pos2,
11581162
bool Equal) {
11591163
auto &SVB = State->getStateManager().getSValBuilder();
1164+
1165+
// FIXME: This code should be reworked as follows:
1166+
// 1. Subtract the operands using evalBinOp().
1167+
// 2. Assume that the result doesn't overflow.
1168+
// 3. Compare the result to 0.
1169+
// 4. Assume the result of the comparison.
11601170
const auto comparison =
11611171
SVB.evalBinOp(State, BO_EQ, nonloc::SymbolVal(Pos1.getOffset()),
1162-
nonloc::SymbolVal(Pos2.getOffset()), SVB.getConditionType())
1163-
.getAs<DefinedSVal>();
1164-
1165-
if (comparison) {
1166-
auto NewState = State->assume(*comparison, Equal);
1167-
if (const auto CompSym = comparison->getAsSymbol()) {
1168-
return assumeNoOverflow(NewState, cast<SymIntExpr>(CompSym)->getLHS(), 2);
1169-
}
1170-
1171-
return NewState;
1172+
nonloc::SymbolVal(Pos2.getOffset()),
1173+
SVB.getConditionType());
1174+
1175+
assert(comparison.getAs<DefinedSVal>() &&
1176+
"Symbol comparison must be a `DefinedSVal`");
1177+
1178+
auto NewState = State->assume(comparison.castAs<DefinedSVal>(), Equal);
1179+
if (const auto CompSym = comparison.getAsSymbol()) {
1180+
assert(isa<SymIntExpr>(CompSym) &&
1181+
"Symbol comparison must be a `SymIntExpr`");
1182+
assert(BinaryOperator::isComparisonOp(
1183+
cast<SymIntExpr>(CompSym)->getOpcode()) &&
1184+
"Symbol comparison must be a comparison");
1185+
return assumeNoOverflow(NewState, cast<SymIntExpr>(CompSym)->getLHS(), 2);
11721186
}
11731187

1174-
return State;
1188+
return NewState;
11751189
}
11761190

11771191
bool isZero(ProgramStateRef State, const NonLoc &Val) {
@@ -1225,14 +1239,12 @@ bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
12251239
auto &SVB = State->getStateManager().getSValBuilder();
12261240

12271241
const auto comparison =
1228-
SVB.evalBinOp(State, Opc, NL1, NL2, SVB.getConditionType())
1229-
.getAs<DefinedSVal>();
1242+
SVB.evalBinOp(State, Opc, NL1, NL2, SVB.getConditionType());
12301243

1231-
if (comparison) {
1232-
return !State->assume(*comparison, false);
1233-
}
1244+
assert(comparison.getAs<DefinedSVal>() &&
1245+
"Symbol comparison must be a `DefinedSVal`");
12341246

1235-
return false;
1247+
return !State->assume(comparison.castAs<DefinedSVal>(), false);
12361248
}
12371249

12381250
} // namespace

0 commit comments

Comments
 (0)