Skip to content

Commit 04afd3d

Browse files
rniwaflovent
authored andcommitted
[WebKit Checkers] Allow operator T&() in a const member function (llvm#126470)
Allow operator T&() in a member function which returns a const member variable. In particular, this will allow UniqueRef::operator T&() and Ref::operator T&() to be treated as a safe pointer origin when they're called on a const member.
1 parent e9a950c commit 04afd3d

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
154154
if (auto *MCE = dyn_cast<CXXMemberCallExpr>(E)) {
155155
if (auto *Callee = MCE->getDirectCallee()) {
156156
auto Name = safeGetName(Callee);
157-
if (Name == "get" || Name == "ptr") {
158-
auto *ThisArg = MCE->getImplicitObjectArgument();
159-
E = ThisArg;
160-
}
157+
if (Name == "get" || Name == "ptr")
158+
E = MCE->getImplicitObjectArgument();
159+
if (auto *CD = dyn_cast<CXXConversionDecl>(Callee))
160+
E = MCE->getImplicitObjectArgument();
161161
}
162162
} else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
163163
if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)

clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Foo {
3131
public:
3232
Foo();
3333
void bar();
34+
RefCountable& obj1() const { return m_obj1; }
3435

3536
private:
3637
const Ref<RefCountable> m_obj1;
@@ -41,6 +42,7 @@ void Foo::bar() {
4142
m_obj1->method();
4243
m_obj2->method();
4344
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
45+
obj1().method();
4446
}
4547

4648
} // namespace call_args_const_ref_member
@@ -100,6 +102,7 @@ class Foo {
100102
public:
101103
Foo();
102104
void bar();
105+
RefCountable& obj1() { return m_obj1; }
103106

104107
private:
105108
const UniqueRef<RefCountable> m_obj1;
@@ -110,6 +113,7 @@ void Foo::bar() {
110113
m_obj1->method();
111114
m_obj2->method();
112115
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
116+
obj1().method();
113117
}
114118

115119
} // namespace call_args_const_unique_ref

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

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class UniqueRef {
289289
u.t = nullptr;
290290
}
291291
T &get() const { return *t; }
292+
operator T&() const { return *t; }
292293
T *operator->() const { return t; }
293294
UniqueRef &operator=(T &) { return *this; }
294295
};

0 commit comments

Comments
 (0)