Skip to content

Commit 8370ba4

Browse files
authored
[analyzer][NFC] Eliminate a dyn_cast (llvm#100719)
Response to the catch in this comment: https://github.com/llvm/llvm-project/pull/94357/files/07f6daf2cf0f5d5bd4fc9950f2585a3f52b4ad2f#r1692084074
1 parent 6f8ef5a commit 8370ba4

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,18 @@ class NoMemOwnershipChangeVisitor final : public NoOwnershipChangeVisitor {
832832
/// information.
833833
bool doesFnIntendToHandleOwnership(const Decl *Callee,
834834
ASTContext &ACtx) final {
835-
using namespace clang::ast_matchers;
836835
const FunctionDecl *FD = dyn_cast<FunctionDecl>(Callee);
837836

837+
// Given that the stack frame was entered, the body should always be
838+
// theoretically obtainable. In case of body farms, the synthesized body
839+
// is not attached to declaration, thus triggering the '!FD->hasBody()'
840+
// branch. That said, would a synthesized body ever intend to handle
841+
// ownership? As of today they don't. And if they did, how would we
842+
// put notes inside it, given that it doesn't match any source locations?
843+
if (!FD || !FD->hasBody())
844+
return false;
845+
using namespace clang::ast_matchers;
846+
838847
auto Matches = match(findAll(stmt(anyOf(cxxDeleteExpr().bind("delete"),
839848
callExpr().bind("call")))),
840849
*FD->getBody(), ACtx);

clang/lib/StaticAnalyzer/Checkers/NoOwnershipChangeVisitor.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,6 @@ bool NoOwnershipChangeVisitor::wasModifiedInFunction(
7272
const ExplodedNode *CallEnterN, const ExplodedNode *CallExitEndN) {
7373
const Decl *Callee =
7474
CallExitEndN->getFirstPred()->getLocationContext()->getDecl();
75-
const FunctionDecl *FD = dyn_cast<FunctionDecl>(Callee);
76-
77-
// Given that the stack frame was entered, the body should always be
78-
// theoretically obtainable. In case of body farms, the synthesized body
79-
// is not attached to declaration, thus triggering the '!FD->hasBody()'
80-
// branch. That said, would a synthesized body ever intend to handle
81-
// ownership? As of today they don't. And if they did, how would we
82-
// put notes inside it, given that it doesn't match any source locations?
83-
if (!FD || !FD->hasBody())
84-
return false;
8575
if (!doesFnIntendToHandleOwnership(
8676
Callee,
8777
CallExitEndN->getState()->getAnalysisManager().getASTContext()))

clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,18 @@ class NoStreamStateChangeVisitor final : public NoOwnershipChangeVisitor {
755755

756756
bool doesFnIntendToHandleOwnership(const Decl *Callee,
757757
ASTContext &ACtx) final {
758-
using namespace clang::ast_matchers;
759758
const FunctionDecl *FD = dyn_cast<FunctionDecl>(Callee);
760759

760+
// Given that the stack frame was entered, the body should always be
761+
// theoretically obtainable. In case of body farms, the synthesized body
762+
// is not attached to declaration, thus triggering the '!FD->hasBody()'
763+
// branch. That said, would a synthesized body ever intend to handle
764+
// ownership? As of today they don't. And if they did, how would we
765+
// put notes inside it, given that it doesn't match any source locations?
766+
if (!FD || !FD->hasBody())
767+
return false;
768+
using namespace clang::ast_matchers;
769+
761770
auto Matches =
762771
match(findAll(callExpr().bind("call")), *FD->getBody(), ACtx);
763772
for (BoundNodes Match : Matches) {

0 commit comments

Comments
 (0)