Skip to content

[webkit.RefCntblBaseVirtualDtor] Ignore WTF::RefCounted<T> and its variants missing virtual destructor #91009

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "ASTUtils.h"
#include "DiagOutputUtils.h"
#include "PtrTypesSemantics.h"
#include "clang/AST/CXXInheritance.h"
Expand Down Expand Up @@ -90,6 +91,9 @@ class RefCntblBaseVirtualDtorChecker
const CXXRecordDecl *C = T->getAsCXXRecordDecl();
if (!C)
return false;
if (isRefCountedClass(C))
return false;

bool AnyInconclusiveBase = false;
const auto hasPublicRefInBase =
[&AnyInconclusiveBase](const CXXBaseSpecifier *Base,
Expand Down Expand Up @@ -164,6 +168,20 @@ class RefCntblBaseVirtualDtorChecker
return false;
}

static bool isRefCountedClass(const CXXRecordDecl *D) {
if (!D->getTemplateInstantiationPattern())
return false;
auto *NsDecl = D->getParent();
if (!NsDecl || !isa<NamespaceDecl>(NsDecl))
return false;
auto NamespaceName = safeGetName(NsDecl);
auto ClsNameStr = safeGetName(D);
StringRef ClsName = ClsNameStr; // FIXME: Make safeGetName return StringRef.
return NamespaceName == "WTF" &&
(ClsName.ends_with("RefCounted") ||
ClsName == "ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr");
}

void reportBug(const CXXRecordDecl *DerivedClass,
const CXXBaseSpecifier *BaseSpec,
const CXXRecordDecl *ProblematicBaseClass) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,63 @@ struct DerivedClassTmpl3 : T { };

typedef DerivedClassTmpl3<RefCntblBase> Foo;
Foo c;


namespace WTF {

class RefCountedBase {
public:
void ref() const { ++count; }

protected:
bool derefBase() const
{
return !--count;
}

private:
mutable unsigned count;
};

template <typename T>
class RefCounted : public RefCountedBase {
public:
void deref() const {
if (derefBase())
delete const_cast<T*>(static_cast<const T*>(this));
}

protected:
RefCounted() { }
};

template <typename T>
class ThreadSafeRefCounted {
public:
void ref() const;
bool deref() const;
};

template <typename T>
class ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr {
public:
void ref() const;
bool deref() const;
};

} // namespace WTF

class DerivedClass4 : public WTF::RefCounted<DerivedClass4> { };

class DerivedClass5 : public DerivedClass4 { };
// expected-warning@-1{{Class 'DerivedClass4' is used as a base of class 'DerivedClass5' but doesn't have virtual destructor}}

class DerivedClass6 : public WTF::ThreadSafeRefCounted<DerivedClass6> { };

class DerivedClass7 : public DerivedClass6 { };
// expected-warning@-1{{Class 'DerivedClass6' is used as a base of class 'DerivedClass7' but doesn't have virtual destructor}}

class DerivedClass8 : public WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<DerivedClass8> { };

class DerivedClass9 : public DerivedClass8 { };
// expected-warning@-1{{Class 'DerivedClass8' is used as a base of class 'DerivedClass9' but doesn't have virtual destructor}}
Loading