Skip to content

Commit

Permalink
[analyzer] WebKit checkers: recognize dynamicDowncast as a safe funct…
Browse files Browse the repository at this point in the history
…ion.

It can take raw pointers without triggering a warning.

Also retire the support for makeRef and makeWeakPtr as they have been removed
from WebKit.
  • Loading branch information
rniwa authored and haoNoQ committed Oct 19, 2023
1 parent dd473f1 commit 5070c1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ bool isPtrConversion(const FunctionDecl *F) {
// FIXME: check # of params == 1
const auto FunctionName = safeGetName(F);
if (FunctionName == "getPtr" || FunctionName == "WeakPtr" ||
FunctionName == "makeWeakPtr"

FunctionName == "dynamicDowncast"
|| FunctionName == "downcast" || FunctionName == "bitwise_cast")
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class UncountedCallArgsChecker

auto name = safeGetName(Callee);
if (name == "adoptRef" || name == "getPtr" || name == "WeakPtr" ||
name == "makeWeakPtr" || name == "downcast" || name == "bitwise_cast" ||
name == "dynamicDowncast" || name == "downcast" || name == "bitwise_cast" ||
name == "is" || name == "equal" || name == "hash" ||
name == "isType"
// FIXME: Most/all of these should be implemented via attributes.
Expand Down
35 changes: 35 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/call-args-dynamic-downcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
// expected-no-diagnostics

class Base {
public:
inline void ref();
inline void deref();
};

class Derived : public Base {
public:
virtual ~Derived();

void ref() const;
void deref() const;
};

class SubDerived final : public Derived {
};

class OtherObject {
public:
Derived* obj();
};

template<typename Target, typename Source>
inline Target* dynamicDowncast(Source* source)
{
return static_cast<Target*>(source);
}

void foo(OtherObject* other)
{
dynamicDowncast<SubDerived>(other->obj());
}
16 changes: 0 additions & 16 deletions clang/test/Analysis/Checkers/WebKit/call-args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,6 @@ namespace param_forwarding_method {
}
}

namespace make_ref {
void makeRef(RefCountable*) {}
void makeRefPtr(RefCountable*) {}
void makeWeakPtr(RefCountable*) {}
void makeWeakPtr(RefCountable&) {}

void foo() {
makeRef(provide());
makeRefPtr(provide());
RefPtr<RefCountable> a(provide());
Ref<RefCountable> b(provide());
makeWeakPtr(provide());
makeWeakPtr(*provide());
}
}

namespace downcast {
void consume_ref_countable(RefCountable*) {}
RefCountable* downcast(RefCountable*) { return nullptr; }
Expand Down

0 comments on commit 5070c1e

Please sign in to comment.