[PATCH] D46984: [analyzer] Exrtend ObjCAutoreleaseWriteChecker to also catch block declarations with autoreleasing variables
George Karpenkov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed May 16 15:51:04 PDT 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rL332546: [analyzer] Extend ObjCAutoreleaseWriteChecker to catch block declarations with… (authored by george.karpenkov, committed by ).
Herald added a subscriber: llvm-commits.
Changed prior to commit:
https://reviews.llvm.org/D46984?vs=147201&id=147208#toc
Repository:
rL LLVM
https://reviews.llvm.org/D46984
Files:
cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
cfe/trunk/test/Analysis/autoreleasewritechecker_test.m
Index: cfe/trunk/test/Analysis/autoreleasewritechecker_test.m
===================================================================
--- cfe/trunk/test/Analysis/autoreleasewritechecker_test.m
+++ cfe/trunk/test/Analysis/autoreleasewritechecker_test.m
@@ -265,5 +265,17 @@
}];
}
+typedef void (^errBlock)(NSError *__autoreleasing *error);
+
+extern void expectError(errBlock);
+
+void captureAutoreleasingVarFromBlock(NSDictionary *dict) {
+ expectError(^(NSError *__autoreleasing *err) {
+ [dict enumerateKeysAndObjectsUsingBlock:^{
+ writeIntoError(err); // expected-warning{{Capture of autoreleasing out parameter 'err'}}
+ }];
+ });
+}
+
#endif
Index: cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
===================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCAutoreleaseWriteChecker.cpp
@@ -115,27 +115,31 @@
QualType Ty = PVD->getType();
if (Ty->getPointeeType().getObjCLifetime() != Qualifiers::OCL_Autoreleasing)
return;
- const char *WarningMsg = "Write to";
+ const char *ActionMsg = "Write to";
const auto *MarkedStmt = Match.getNodeAs<Expr>(ProblematicWriteBind);
+ bool IsCapture = false;
// Prefer to warn on write, but if not available, warn on capture.
if (!MarkedStmt) {
MarkedStmt = Match.getNodeAs<Expr>(CapturedBind);
assert(MarkedStmt);
- WarningMsg = "Capture of";
+ ActionMsg = "Capture of";
+ IsCapture = true;
}
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";
+
BR.EmitBasicReport(
ADC->getDecl(), Checker,
- /*Name=*/(llvm::Twine(WarningMsg)
+ /*Name=*/(llvm::Twine(ActionMsg)
+ " autoreleasing out parameter inside autorelease pool").str(),
/*Category=*/"Memory",
- (llvm::Twine(WarningMsg) + " autoreleasing out parameter inside " +
+ (llvm::Twine(ActionMsg) + " autoreleasing out parameter " +
+ (IsCapture ? "'" + PVD->getName() + "'" + " " : "") + "inside " +
"autorelease pool that may exit before " + Name + " returns; consider "
"writing first to a strong local variable declared outside of the block")
.str(),
@@ -153,7 +157,7 @@
.bind(ParamBind);
auto ReferencedParamM =
- declRefExpr(to(parmVarDecl(DoublePointerParamM)));
+ declRefExpr(to(parmVarDecl(DoublePointerParamM))).bind(CapturedBind);
// Write into a binded object, e.g. *ParamBind = X.
auto WritesIntoM = binaryOperator(
@@ -169,7 +173,7 @@
ignoringParenImpCasts(ReferencedParamM));
auto CapturedInParamM = stmt(anyOf(
callExpr(ArgumentCaptureM),
- objcMessageExpr(ArgumentCaptureM))).bind(CapturedBind);
+ objcMessageExpr(ArgumentCaptureM)));
// WritesIntoM happens inside a block passed as an argument.
auto WritesOrCapturesInBlockM = hasAnyArgument(allOf(
@@ -192,7 +196,8 @@
auto MatcherM = decl(anyOf(
objcMethodDecl(HasParamAndWritesInMarkedFuncM).bind(IsMethodBind),
- functionDecl(HasParamAndWritesInMarkedFuncM)));
+ functionDecl(HasParamAndWritesInMarkedFuncM),
+ blockDecl(HasParamAndWritesInMarkedFuncM)));
auto Matches = match(MatcherM, *D, AM.getASTContext());
for (BoundNodes Match : Matches)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46984.147208.patch
Type: text/x-patch
Size: 3572 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180516/ec4c8583/attachment.bin>
More information about the llvm-commits
mailing list