From da27efed92b9c30a096e2906f5df4716bf77ed33 Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Fri, 2 Nov 2012 21:30:04 +0000 Subject: [PATCH] [analyzer] Factor SimpleStreamChecker pulling out isLeaked(). llvm-svn: 167316 --- .../Checkers/SimpleStreamChecker.cpp | 36 +++++++++++++--------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp index 203271a..fd548bc 100644 --- a/clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp @@ -136,29 +136,35 @@ void SimpleStreamChecker::checkPreStmt(const CallExpr *Call, C.addTransition(State); } +static bool isLeaked(SymbolRef Sym, const StreamState &SS, + bool IsSymDead, ProgramStateRef State) { + if (IsSymDead && SS.isOpened()) { + // If a symbol is NULL, assume that fopen failed on this path. + // A symbol should only be considered leaked if it is non-null. + ConstraintManager &CMgr = State->getConstraintManager(); + ConditionTruthVal OpenFailed = CMgr.isNull(State, Sym); + return !OpenFailed.isConstrainedTrue(); + } + return false; +} + void SimpleStreamChecker::checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const { ProgramStateRef State = C.getState(); - StreamMapTy TrackedStreams = State->get(); - SymbolVector LeakedStreams; + StreamMapTy TrackedStreams = State->get(); for (StreamMapTy::iterator I = TrackedStreams.begin(), E = TrackedStreams.end(); I != E; ++I) { SymbolRef Sym = I->first; - if (SymReaper.isDead(Sym)) { - const StreamState &SS = I->second; - if (SS.isOpened()) { - // If a symbol is NULL, assume that fopen failed on this path - // and do not report a leak. - ConstraintManager &CMgr = State->getConstraintManager(); - ConditionTruthVal OpenFailed = CMgr.isNull(State, Sym); - if (!OpenFailed.isConstrainedTrue()) - LeakedStreams.push_back(Sym); - } - - // Remove the dead symbol from the streams map. + bool IsSymDead = SymReaper.isDead(Sym); + + // Collect leaked symbols. + if (isLeaked(Sym, I->second, IsSymDead, State)) + LeakedStreams.push_back(Sym); + + // Remove the dead symbol from the streams map. + if (IsSymDead) State = State->remove(Sym); - } } ExplodedNode *N = C.addTransition(State); -- 2.7.4