Skip to content

Commit 7249692

Browse files
authored
[analyzer] Detect a return value of Ref<T> & RefPtr<T> (#81580)
This PR makes the checker not emit warning when a function is called with a return value of another function when the return value is of type Ref<T> or RefPtr<T>.
1 parent cbdc760 commit 7249692

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace clang {
1919
std::pair<const Expr *, bool>
2020
tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
2121
while (E) {
22+
if (auto *tempExpr = dyn_cast<MaterializeTemporaryExpr>(E)) {
23+
E = tempExpr->getSubExpr();
24+
continue;
25+
}
2226
if (auto *cast = dyn_cast<CastExpr>(E)) {
2327
if (StopAtFirstRefCountedObj) {
2428
if (auto *ConversionFunc =
@@ -62,6 +66,8 @@ tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
6266
E = call->getArg(0);
6367
continue;
6468
}
69+
if (isReturnValueRefCounted(callee))
70+
return {E, true};
6571

6672
if (isPtrConversion(callee)) {
6773
E = call->getArg(0);

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ bool isCtorOfRefCounted(const clang::FunctionDecl *F) {
119119
|| FunctionName == "Identifier";
120120
}
121121

122+
bool isReturnValueRefCounted(const clang::FunctionDecl *F) {
123+
assert(F);
124+
QualType type = F->getReturnType();
125+
while (!type.isNull()) {
126+
if (auto *elaboratedT = type->getAs<ElaboratedType>()) {
127+
type = elaboratedT->desugar();
128+
continue;
129+
}
130+
if (auto *specialT = type->getAs<TemplateSpecializationType>()) {
131+
if (auto *decl = specialT->getTemplateName().getAsTemplateDecl()) {
132+
auto name = decl->getNameAsString();
133+
return name == "Ref" || name == "RefPtr";
134+
}
135+
return false;
136+
}
137+
return false;
138+
}
139+
return false;
140+
}
141+
122142
std::optional<bool> isUncounted(const CXXRecordDecl* Class)
123143
{
124144
// Keep isRefCounted first as it's cheaper.

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ std::optional<bool> isUncountedPtr(const clang::Type* T);
5050
/// false if not.
5151
bool isCtorOfRefCounted(const clang::FunctionDecl *F);
5252

53+
/// \returns true if \p F returns a ref-counted object, false if not.
54+
bool isReturnValueRefCounted(const clang::FunctionDecl *F);
55+
5356
/// \returns true if \p M is getter of a ref-counted class, false if not.
5457
std::optional<bool> isGetterOfRefCounted(const clang::CXXMethodDecl* Method);
5558

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2+
// expected-no-diagnostics
3+
4+
#include "mock-types.h"
5+
6+
class RefCounted {
7+
public:
8+
void ref();
9+
void deref();
10+
};
11+
12+
class Object {
13+
public:
14+
void someFunction(RefCounted&);
15+
};
16+
17+
RefPtr<Object> object();
18+
RefPtr<RefCounted> protectedTargetObject();
19+
20+
void testFunction() {
21+
if (RefPtr obj = object())
22+
obj->someFunction(*protectedTargetObject());
23+
}

0 commit comments

Comments
 (0)