From: Anton Yartsev Date: Sat, 7 Mar 2015 00:31:53 +0000 (+0000) Subject: [analyzer] Revert changes from r229593; an enhancement is under discussion X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8fe0b8fe52531ce305f0d62e495a00fbf28191ac;p=clang [analyzer] Revert changes from r229593; an enhancement is under discussion git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@231540 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index f2afcca971..498465d514 100644 --- a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -333,16 +333,12 @@ private: ///@{ /// Tells if a given family/call/symbol is tracked by the current checker. - /// Looks through incoming CheckKind(s) and returns the kind of the checker - /// responsible for this family/call/symbol. - Optional getCheckIfTracked(CheckKind CK, - AllocationFamily Family) const; - Optional getCheckIfTracked(CKVecTy CKVec, - AllocationFamily Family) const; - Optional getCheckIfTracked(CKVecTy CKVec, CheckerContext &C, + /// Sets CheckKind to the kind of the checker responsible for this + /// family/call/symbol. + Optional getCheckIfTracked(AllocationFamily Family) const; + Optional getCheckIfTracked(CheckerContext &C, const Stmt *AllocDeallocStmt) const; - Optional getCheckIfTracked(CKVecTy CKVec, CheckerContext &C, - SymbolRef Sym) const; + Optional getCheckIfTracked(CheckerContext &C, SymbolRef Sym) const; ///@} static bool SummarizeValue(raw_ostream &os, SVal V); static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR); @@ -1347,32 +1343,20 @@ ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, } Optional -MallocChecker::getCheckIfTracked(MallocChecker::CheckKind CK, - AllocationFamily Family) const { - - if (CK == CK_NumCheckKinds || !ChecksEnabled[CK]) - return Optional(); - - // C/C++ checkers. - if (CK == CK_MismatchedDeallocatorChecker) - return CK; - +MallocChecker::getCheckIfTracked(AllocationFamily Family) const { switch (Family) { case AF_Malloc: - case AF_IfNameIndex: - case AF_Alloca: { - // C checkers. - if (CK == CK_MallocChecker) { - return CK; - } + case AF_Alloca: + case AF_IfNameIndex: { + if (ChecksEnabled[CK_MallocChecker]) + return CK_MallocChecker; + return Optional(); } case AF_CXXNew: case AF_CXXNewArray: { - // C++ checkers. - if (CK == CK_NewDeleteChecker || - CK == CK_NewDeleteLeaksChecker) { - return CK; + if (ChecksEnabled[CK_NewDeleteChecker]) { + return CK_NewDeleteChecker; } return Optional(); } @@ -1383,45 +1367,18 @@ MallocChecker::getCheckIfTracked(MallocChecker::CheckKind CK, llvm_unreachable("unhandled family"); } -static MallocChecker::CKVecTy MakeVecFromCK(MallocChecker::CheckKind CK1, - MallocChecker::CheckKind CK2 = MallocChecker::CK_NumCheckKinds, - MallocChecker::CheckKind CK3 = MallocChecker::CK_NumCheckKinds, - MallocChecker::CheckKind CK4 = MallocChecker::CK_NumCheckKinds) { - MallocChecker::CKVecTy CKVec; - CKVec.push_back(CK1); - if (CK2 != MallocChecker::CK_NumCheckKinds) { - CKVec.push_back(CK2); - if (CK3 != MallocChecker::CK_NumCheckKinds) { - CKVec.push_back(CK3); - if (CK4 != MallocChecker::CK_NumCheckKinds) - CKVec.push_back(CK4); - } - } - return CKVec; -} - Optional -MallocChecker::getCheckIfTracked(CKVecTy CKVec, AllocationFamily Family) const { - for (auto CK: CKVec) { - auto RetCK = getCheckIfTracked(CK, Family); - if (RetCK.hasValue()) - return RetCK; - } - return Optional(); -} - -Optional -MallocChecker::getCheckIfTracked(CKVecTy CKVec, CheckerContext &C, +MallocChecker::getCheckIfTracked(CheckerContext &C, const Stmt *AllocDeallocStmt) const { - return getCheckIfTracked(CKVec, getAllocationFamily(C, AllocDeallocStmt)); + return getCheckIfTracked(getAllocationFamily(C, AllocDeallocStmt)); } Optional -MallocChecker::getCheckIfTracked(CKVecTy CKVec, CheckerContext &C, - SymbolRef Sym) const { +MallocChecker::getCheckIfTracked(CheckerContext &C, SymbolRef Sym) const { + const RefState *RS = C.getState()->get(Sym); assert(RS); - return getCheckIfTracked(CKVec, RS->getAllocationFamily()); + return getCheckIfTracked(RS->getAllocationFamily()); } bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { @@ -1515,9 +1472,12 @@ void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange Range, const Expr *DeallocExpr) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteChecker), - C, DeallocExpr); + if (!ChecksEnabled[CK_MallocChecker] && + !ChecksEnabled[CK_NewDeleteChecker]) + return; + + Optional CheckKind = + getCheckIfTracked(C, DeallocExpr); if (!CheckKind.hasValue()) return; @@ -1557,10 +1517,13 @@ void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, void MallocChecker::ReportFreeAlloca(CheckerContext &C, SVal ArgVal, SourceRange Range) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_MismatchedDeallocatorChecker), - AF_Alloca); - if (!CheckKind.hasValue()) + Optional CheckKind; + + if (ChecksEnabled[CK_MallocChecker]) + CheckKind = CK_MallocChecker; + else if (ChecksEnabled[CK_MismatchedDeallocatorChecker]) + CheckKind = CK_MismatchedDeallocatorChecker; + else return; if (ExplodedNode *N = C.generateSink()) { @@ -1636,9 +1599,12 @@ void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal, const Expr *AllocExpr) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteChecker), - C, AllocExpr); + if (!ChecksEnabled[CK_MallocChecker] && + !ChecksEnabled[CK_NewDeleteChecker]) + return; + + Optional CheckKind = + getCheckIfTracked(C, AllocExpr); if (!CheckKind.hasValue()) return; @@ -1688,9 +1654,11 @@ void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal, void MallocChecker::ReportUseAfterFree(CheckerContext &C, SourceRange Range, SymbolRef Sym) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteChecker), - C, Sym); + if (!ChecksEnabled[CK_MallocChecker] && + !ChecksEnabled[CK_NewDeleteChecker]) + return; + + Optional CheckKind = getCheckIfTracked(C, Sym); if (!CheckKind.hasValue()) return; @@ -1713,9 +1681,11 @@ void MallocChecker::ReportDoubleFree(CheckerContext &C, SourceRange Range, bool Released, SymbolRef Sym, SymbolRef PrevSym) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteChecker), - C, Sym); + if (!ChecksEnabled[CK_MallocChecker] && + !ChecksEnabled[CK_NewDeleteChecker]) + return; + + Optional CheckKind = getCheckIfTracked(C, Sym); if (!CheckKind.hasValue()) return; @@ -1740,8 +1710,10 @@ void MallocChecker::ReportDoubleFree(CheckerContext &C, SourceRange Range, void MallocChecker::ReportDoubleDelete(CheckerContext &C, SymbolRef Sym) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_NewDeleteChecker), - C, Sym); + if (!ChecksEnabled[CK_NewDeleteChecker]) + return; + + Optional CheckKind = getCheckIfTracked(C, Sym); if (!CheckKind.hasValue()) return; @@ -1928,14 +1900,23 @@ MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym, void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const { - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteLeaksChecker), - C, Sym); - if (!CheckKind.hasValue()) + if (!ChecksEnabled[CK_MallocChecker] && + !ChecksEnabled[CK_NewDeleteLeaksChecker]) return; const RefState *RS = C.getState()->get(Sym); - assert(RS); + assert(RS && "cannot leak an untracked symbol"); + AllocationFamily Family = RS->getAllocationFamily(); + Optional CheckKind = getCheckIfTracked(Family); + if (!CheckKind.hasValue()) + return; + + // Special case for new and new[]; these are controlled by a separate checker + // flag so that they can be selectively disabled. + if (Family == AF_CXXNew || Family == AF_CXXNewArray) + if (!ChecksEnabled[CK_NewDeleteLeaksChecker]) + return; + if (RS->getAllocationFamily() == AF_Alloca) return; @@ -2548,9 +2529,8 @@ void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State, for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { const RefState *RefS = State->get(I.getKey()); AllocationFamily Family = RefS->getAllocationFamily(); - auto CheckKind = getCheckIfTracked(MakeVecFromCK(CK_MallocChecker, - CK_NewDeleteChecker), - Family); + Optional CheckKind = getCheckIfTracked(Family); + I.getKey()->dumpToStream(Out); Out << " : "; I.getData().dump(Out);