Skip to content

Commit b74c55a

Browse files
committed
Use more early return in isPtrOfType and add a test case for UniqueRef
1 parent 4efb59c commit b74c55a

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ static bool isPtrOfType(const clang::QualType T, Predicate Pred) {
153153
type = elaboratedT->desugar();
154154
continue;
155155
}
156-
if (auto *specialT = type->getAs<TemplateSpecializationType>()) {
157-
if (auto *decl = specialT->getTemplateName().getAsTemplateDecl())
158-
return Pred(decl->getNameAsString());
156+
auto *SpecialT = type->getAs<TemplateSpecializationType>();
157+
if (!SpecialT)
159158
return false;
160-
}
161-
return false;
159+
auto *Decl = SpecialT->getTemplateName().getAsTemplateDecl();
160+
if (!Decl)
161+
return false;
162+
return Pred(Decl->getNameAsString());
162163
}
163164
return false;
164165
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,23 @@ void Foo::bar() {
6464
}
6565

6666
} // namespace call_args_const_unique_ptr
67+
68+
namespace call_args_const_unique_ref {
69+
70+
class Foo {
71+
public:
72+
Foo();
73+
void bar();
74+
75+
private:
76+
const UniqueRef<RefCountable> m_obj1;
77+
UniqueRef<RefCountable> m_obj2;
78+
};
79+
80+
void Foo::bar() {
81+
m_obj1->method();
82+
m_obj2->method();
83+
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
84+
}
85+
86+
} // namespace call_args_const_unique_ref

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ class RefCountableAndCheckable {
197197
int trivial() { return 0; }
198198
};
199199

200+
template <typename T>
201+
class UniqueRef {
202+
private:
203+
T *t;
204+
205+
public:
206+
UniqueRef(T &t) : t(&t) { }
207+
~UniqueRef() {
208+
if (t)
209+
delete t;
210+
}
211+
template <typename U> UniqueRef(UniqueRef<U>&& u)
212+
: t(u.t)
213+
{
214+
u.t = nullptr;
215+
}
216+
T &get() const { return *t; }
217+
T *operator->() const { return t; }
218+
UniqueRef &operator=(T &) { return *this; }
219+
};
220+
200221
namespace std {
201222

202223
template <typename T>

0 commit comments

Comments
 (0)