Skip to content

[Analyzer] Support RefAllowingPartiallyDestroyed and RefPtrAllowingPartiallyDestroyed #82209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ std::optional<bool> isRefCountable(const CXXRecordDecl* R)
return hasRef && hasDeref;
}

bool isRefType(const std::string &Name) {
return Name == "Ref" || Name == "RefAllowingPartiallyDestroyed" ||
Name == "RefPtr" || Name == "RefPtrAllowingPartiallyDestroyed";
}

bool isCtorOfRefCounted(const clang::FunctionDecl *F) {
assert(F);
const auto &FunctionName = safeGetName(F);

return FunctionName == "Ref" || FunctionName == "makeRef"

|| FunctionName == "RefPtr" || FunctionName == "makeRefPtr"
const std::string &FunctionName = safeGetName(F);

|| FunctionName == "UniqueRef" || FunctionName == "makeUniqueRef" ||
return isRefType(FunctionName) || FunctionName == "makeRef" ||
FunctionName == "makeRefPtr" || FunctionName == "UniqueRef" ||
FunctionName == "makeUniqueRef" ||
FunctionName == "makeUniqueRefWithoutFastMallocCheck"

|| FunctionName == "String" || FunctionName == "AtomString" ||
Expand All @@ -131,7 +134,7 @@ bool isReturnValueRefCounted(const clang::FunctionDecl *F) {
if (auto *specialT = type->getAs<TemplateSpecializationType>()) {
if (auto *decl = specialT->getTemplateName().getAsTemplateDecl()) {
auto name = decl->getNameAsString();
return name == "Ref" || name == "RefPtr";
return isRefType(name);
}
return false;
}
Expand Down Expand Up @@ -172,20 +175,18 @@ std::optional<bool> isGetterOfRefCounted(const CXXMethodDecl* M)
if (isa<CXXMethodDecl>(M)) {
const CXXRecordDecl *calleeMethodsClass = M->getParent();
auto className = safeGetName(calleeMethodsClass);
auto methodName = safeGetName(M);
auto method = safeGetName(M);

if (((className == "Ref" || className == "RefPtr") &&
methodName == "get") ||
(className == "Ref" && methodName == "ptr") ||
if ((isRefType(className) && (method == "get" || method == "ptr")) ||
((className == "String" || className == "AtomString" ||
className == "AtomStringImpl" || className == "UniqueString" ||
className == "UniqueStringImpl" || className == "Identifier") &&
methodName == "impl"))
method == "impl"))
return true;

// Ref<T> -> T conversion
// FIXME: Currently allowing any Ref<T> -> whatever cast.
if (className == "Ref" || className == "RefPtr") {
if (isRefType(className)) {
if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M)) {
if (auto *targetConversionType =
maybeRefToRawOperator->getConversionType().getTypePtrOrNull()) {
Expand All @@ -202,7 +203,7 @@ bool isRefCounted(const CXXRecordDecl *R) {
if (auto *TmplR = R->getTemplateInstantiationPattern()) {
// FIXME: String/AtomString/UniqueString
const auto &ClassName = safeGetName(TmplR);
return ClassName == "RefPtr" || ClassName == "Ref";
return isRefType(ClassName);
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions clang/test/Analysis/Checkers/WebKit/mock-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ template <typename T> struct Ref {
}
T *get() { return t; }
T *ptr() { return t; }
T *operator->() { return t; }
operator const T &() const { return *t; }
operator T &() { return *t; }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
// expected-no-diagnostics

#include "mock-types.h"

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

RefAllowingPartiallyDestroyed() : t{} {};
RefAllowingPartiallyDestroyed(T &) {}
T *get() { return t; }
T *ptr() { return t; }
T *operator->() { return t; }
operator const T &() const { return *t; }
operator T &() { return *t; }
};

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

RefPtrAllowingPartiallyDestroyed() : t(new T) {}
RefPtrAllowingPartiallyDestroyed(T *t) : t(t) {}
T *get() { return t; }
T *operator->() { return t; }
const T *operator->() const { return t; }
T &operator*() { return *t; }
RefPtrAllowingPartiallyDestroyed &operator=(T *) { return *this; }
operator bool() { return t; }
};

class RefCounted {
public:
void ref() const;
void deref() const;
void someFunction();
};

RefAllowingPartiallyDestroyed<RefCounted> object1();
RefPtrAllowingPartiallyDestroyed<RefCounted> object2();

void testFunction() {
object1()->someFunction();
object2()->someFunction();
}