summaryrefslogtreecommitdiffstats
path: root/clang/lib/StaticAnalyzer/Checkers
diff options
context:
space:
mode:
authorGeorge Karpenkov <ekarpenkov@apple.com>2018-05-14 21:39:54 +0000
committerGeorge Karpenkov <ekarpenkov@apple.com>2018-05-14 21:39:54 +0000
commit434019a61744e23c6c90bf62a708ff38a0826137 (patch)
tree8000ba02f14864f789e69af6b4dc65b9c89df638 /clang/lib/StaticAnalyzer/Checkers
parenteda977f414fead94b5072dd35d53be2fa71c5f00 (diff)
downloadbcm5719-llvm-434019a61744e23c6c90bf62a708ff38a0826137.tar.gz
bcm5719-llvm-434019a61744e23c6c90bf62a708ff38a0826137.zip
[analyzer] Extend the ObjCAutoreleaseWriteChecker to warn on captures as well
A common pattern is that the code in the block does not write into the variable explicitly, but instead passes it to a helper function which performs the write. Differential Revision: https://reviews.llvm.org/D46772 llvm-svn: 332300
Diffstat (limited to 'clang/lib/StaticAnalyzer/Checkers')
-rw-r--r--clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp80
1 files changed, 51 insertions, 29 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
index 3e85256b539..5199959dee9 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
@@ -8,7 +8,7 @@
//===----------------------------------------------------------------------===//
//
// This file defines ObjCAutoreleaseWriteChecker which warns against writes
-// into autoreleased out parameters which are likely to cause crashes.
+// into autoreleased out parameters which cause crashes.
// An example of a problematic write is a write to {@code error} in the example
// below:
//
@@ -21,8 +21,9 @@
// return false;
// }
//
-// Such code is very likely to crash due to the other queue autorelease pool
-// begin able to free the error object.
+// Such code will crash on read from `*error` due to the autorelease pool
+// in `enumerateObjectsUsingBlock` implementation freeing the error object
+// on exit from the function.
//
//===----------------------------------------------------------------------===//
@@ -41,8 +42,9 @@ using namespace ast_matchers;
namespace {
const char *ProblematicWriteBind = "problematicwrite";
+const char *CapturedBind = "capturedbind";
const char *ParamBind = "parambind";
-const char *MethodBind = "methodbind";
+const char *IsMethodBind = "ismethodbind";
class ObjCAutoreleaseWriteChecker : public Checker<check::ASTCodeBody> {
public:
@@ -110,67 +112,87 @@ static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR,
AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
const auto *PVD = Match.getNodeAs<ParmVarDecl>(ParamBind);
- assert(PVD);
QualType Ty = PVD->getType();
if (Ty->getPointeeType().getObjCLifetime() != Qualifiers::OCL_Autoreleasing)
return;
- const auto *SW = Match.getNodeAs<Expr>(ProblematicWriteBind);
- bool IsMethod = Match.getNodeAs<ObjCMethodDecl>(MethodBind) != nullptr;
+ const char *WarningMsg = "Write to";
+ const auto *MarkedStmt = Match.getNodeAs<Expr>(ProblematicWriteBind);
+
+ // Prefer to warn on write, but if not available, warn on capture.
+ if (!MarkedStmt) {
+ MarkedStmt = Match.getNodeAs<Expr>(CapturedBind);
+ assert(MarkedStmt);
+ WarningMsg = "Capture of";
+ }
+
+ SourceRange Range = MarkedStmt->getSourceRange();
+ PathDiagnosticLocation Location = PathDiagnosticLocation::createBegin(
+ MarkedStmt, BR.getSourceManager(), ADC);
+ bool IsMethod = Match.getNodeAs<ObjCMethodDecl>(IsMethodBind) != nullptr;
const char *Name = IsMethod ? "method" : "function";
- assert(SW);
BR.EmitBasicReport(
ADC->getDecl(), Checker,
- /*Name=*/"Write to autoreleasing out parameter inside autorelease pool",
+ /*Name=*/(llvm::Twine(WarningMsg)
+ + " autoreleasing out parameter inside autorelease pool").str(),
/*Category=*/"Memory",
- (llvm::Twine("Write to autoreleasing out parameter inside ") +
+ (llvm::Twine(WarningMsg) + " autoreleasing out parameter inside " +
"autorelease pool that may exit before " + Name + " returns; consider "
"writing first to a strong local variable declared outside of the block")
.str(),
- PathDiagnosticLocation::createBegin(SW, BR.getSourceManager(), ADC),
- SW->getSourceRange());
+ Location,
+ Range);
}
void ObjCAutoreleaseWriteChecker::checkASTCodeBody(const Decl *D,
AnalysisManager &AM,
BugReporter &BR) const {
+ auto DoublePointerParamM =
+ parmVarDecl(hasType(hasCanonicalType(pointerType(
+ pointee(hasCanonicalType(objcObjectPointerType()))))))
+ .bind(ParamBind);
+
+ auto ReferencedParamM =
+ declRefExpr(to(parmVarDecl(DoublePointerParamM)));
+
// Write into a binded object, e.g. *ParamBind = X.
auto WritesIntoM = binaryOperator(
hasLHS(unaryOperator(
hasOperatorName("*"),
hasUnaryOperand(
- ignoringParenImpCasts(
- declRefExpr(to(parmVarDecl(equalsBoundNode(ParamBind))))))
+ ignoringParenImpCasts(ReferencedParamM))
)),
hasOperatorName("=")
).bind(ProblematicWriteBind);
+ auto ArgumentCaptureM = hasAnyArgument(
+ ignoringParenImpCasts(ReferencedParamM));
+ auto CapturedInParamM = stmt(anyOf(
+ callExpr(ArgumentCaptureM),
+ objcMessageExpr(ArgumentCaptureM))).bind(CapturedBind);
+
// WritesIntoM happens inside a block passed as an argument.
- auto WritesInBlockM = hasAnyArgument(allOf(
+ auto WritesOrCapturesInBlockM = hasAnyArgument(allOf(
hasType(hasCanonicalType(blockPointerType())),
- forEachDescendant(WritesIntoM)
- ));
+ forEachDescendant(
+ stmt(anyOf(WritesIntoM, CapturedInParamM))
+ )));
- auto CallsAsyncM = stmt(anyOf(
+ auto BlockPassedToMarkedFuncM = stmt(anyOf(
callExpr(allOf(
- callsNames(FunctionsWithAutoreleasingPool), WritesInBlockM)),
+ callsNames(FunctionsWithAutoreleasingPool), WritesOrCapturesInBlockM)),
objcMessageExpr(allOf(
hasAnySelector(toRefs(SelectorsWithAutoreleasingPool)),
- WritesInBlockM))
+ WritesOrCapturesInBlockM))
));
- auto DoublePointerParamM =
- parmVarDecl(hasType(hasCanonicalType(pointerType(
- pointee(hasCanonicalType(objcObjectPointerType()))))))
- .bind(ParamBind);
-
- auto HasParamAndWritesAsyncM = allOf(
+ auto HasParamAndWritesInMarkedFuncM = allOf(
hasAnyParameter(DoublePointerParamM),
- forEachDescendant(CallsAsyncM));
+ forEachDescendant(BlockPassedToMarkedFuncM));
auto MatcherM = decl(anyOf(
- objcMethodDecl(HasParamAndWritesAsyncM).bind(MethodBind),
- functionDecl(HasParamAndWritesAsyncM)));
+ objcMethodDecl(HasParamAndWritesInMarkedFuncM).bind(IsMethodBind),
+ functionDecl(HasParamAndWritesInMarkedFuncM)));
auto Matches = match(MatcherM, *D, AM.getASTContext());
for (BoundNodes Match : Matches)
OpenPOWER on IntegriCloud