Skip to content

Commit 8256804

Browse files
authored
[analyzer] Add the support for calling Ref::ptr accessor. (#80919)
This accessor returns a pointer from Ref type and is therefore safe.
1 parent 7ff5dfb commit 8256804

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ std::optional<bool> isGetterOfRefCounted(const CXXMethodDecl* M)
154154

155155
if (((className == "Ref" || className == "RefPtr") &&
156156
methodName == "get") ||
157+
(className == "Ref" && methodName == "ptr") ||
157158
((className == "String" || className == "AtomString" ||
158159
className == "AtomStringImpl" || className == "UniqueString" ||
159160
className == "UniqueStringImpl" || className == "Identifier") &&

clang/test/Analysis/Checkers/WebKit/mock-types.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define mock_types_1103988513531
33

44
template <typename T> struct Ref {
5-
T t;
5+
T *t;
66

77
Ref() : t{} {};
88
Ref(T *) {}
9-
T *get() { return nullptr; }
10-
operator const T &() const { return t; }
11-
operator T &() { return t; }
9+
T *get() { return t; }
10+
T *ptr() { return t; }
11+
operator const T &() const { return *t; }
12+
operator T &() { return *t; }
1213
};
1314

1415
template <typename T> struct RefPtr {
@@ -40,6 +41,7 @@ template <typename T> bool operator!=(const RefPtr<T> &, T *) { return false; }
4041
template <typename T> bool operator!=(const RefPtr<T> &, T &) { return false; }
4142

4243
struct RefCountable {
44+
static Ref<RefCountable> create();
4345
void ref() {}
4446
void deref() {}
4547
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2+
// expected-no-diagnostics
3+
4+
#include "mock-types.h"
5+
6+
void someFunction(RefCountable*);
7+
8+
void testFunction()
9+
{
10+
Ref item = RefCountable::create();
11+
someFunction(item.ptr());
12+
}

0 commit comments

Comments
 (0)