Skip to content

Commit

Permalink
[analyzer] Add the support for calling Ref::ptr accessor. (#80919)
Browse files Browse the repository at this point in the history
This accessor returns a pointer from Ref type and is therefore safe.
  • Loading branch information
rniwa committed Feb 12, 2024
1 parent 7ff5dfb commit 8256804
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ std::optional<bool> isGetterOfRefCounted(const CXXMethodDecl* M)

if (((className == "Ref" || className == "RefPtr") &&
methodName == "get") ||
(className == "Ref" && methodName == "ptr") ||
((className == "String" || className == "AtomString" ||
className == "AtomStringImpl" || className == "UniqueString" ||
className == "UniqueStringImpl" || className == "Identifier") &&
Expand Down
10 changes: 6 additions & 4 deletions clang/test/Analysis/Checkers/WebKit/mock-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#define mock_types_1103988513531

template <typename T> struct Ref {
T t;
T *t;

Ref() : t{} {};
Ref(T *) {}
T *get() { return nullptr; }
operator const T &() const { return t; }
operator T &() { return t; }
T *get() { return t; }
T *ptr() { return t; }
operator const T &() const { return *t; }
operator T &() { return *t; }
};

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

struct RefCountable {
static Ref<RefCountable> create();
void ref() {}
void deref() {}
};
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/ref-ptr-accessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
// expected-no-diagnostics

#include "mock-types.h"

void someFunction(RefCountable*);

void testFunction()
{
Ref item = RefCountable::create();
someFunction(item.ptr());
}

0 comments on commit 8256804

Please sign in to comment.