1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UnretainedLambdaCapturesChecker -fobjc-arc -verify %s
#include "objc-mock-types.h"
namespace std {
template <typename T>
class unique_ptr {
private:
T *t;
public:
unique_ptr() : t(nullptr) { }
unique_ptr(T *t) : t(t) { }
~unique_ptr() {
if (t)
delete t;
}
template <typename U> unique_ptr(unique_ptr<U>&& u)
: t(u.t)
{
u.t = nullptr;
}
T *get() const { return t; }
T *operator->() const { return t; }
T &operator*() const { return *t; }
unique_ptr &operator=(T *) { return *this; }
explicit operator bool() const { return !!t; }
};
};
namespace WTF {
namespace Detail {
template<typename Out, typename... In>
class CallableWrapperBase {
public:
virtual ~CallableWrapperBase() { }
virtual Out call(In...) = 0;
};
template<typename, typename, typename...> class CallableWrapper;
template<typename CallableType, typename Out, typename... In>
class CallableWrapper : public CallableWrapperBase<Out, In...> {
public:
explicit CallableWrapper(CallableType& callable)
: m_callable(callable) { }
Out call(In... in) final { return m_callable(in...); }
private:
CallableType m_callable;
};
} // namespace Detail
template<typename> class Function;
template<typename Out, typename... In> Function<Out(In...)> adopt(Detail::CallableWrapperBase<Out, In...>*);
template <typename Out, typename... In>
class Function<Out(In...)> {
public:
using Impl = Detail::CallableWrapperBase<Out, In...>;
Function() = default;
template<typename FunctionType>
Function(FunctionType f)
: m_callableWrapper(new Detail::CallableWrapper<FunctionType, Out, In...>(f)) { }
Out operator()(In... in) const { return m_callableWrapper->call(in...); }
explicit operator bool() const { return !!m_callableWrapper; }
private:
enum AdoptTag { Adopt };
Function(Impl* impl, AdoptTag)
: m_callableWrapper(impl)
{
}
friend Function adopt<Out, In...>(Impl*);
std::unique_ptr<Impl> m_callableWrapper;
};
template<typename Out, typename... In> Function<Out(In...)> adopt(Detail::CallableWrapperBase<Out, In...>* impl)
{
return Function<Out(In...)>(impl, Function<Out(In...)>::Adopt);
}
template <typename KeyType, typename ValueType>
class HashMap {
public:
HashMap();
HashMap([[clang::noescape]] const Function<ValueType()>&);
void ensure(const KeyType&, [[clang::noescape]] const Function<ValueType()>&);
bool operator+([[clang::noescape]] const Function<ValueType()>&) const;
static void ifAny(HashMap, [[clang::noescape]] const Function<bool(ValueType)>&);
private:
ValueType* m_table { nullptr };
};
} // namespace WTF
struct A {
static void b();
};
SomeObj* make_obj();
CFMutableArrayRef make_cf();
dispatch_queue_t make_os();
void someFunction();
template <typename Callback> void call(Callback callback) {
someFunction();
callback();
}
void callAsync(const WTF::Function<void()>&);
void raw_ptr() {
SomeObj* obj = make_obj();
auto foo1 = [obj](){
[obj doWork];
};
auto foo2 = [&obj](){
[obj doWork];
};
auto foo3 = [&](){
[obj doWork];
obj = nullptr;
};
auto foo4 = [=](){
[obj doWork];
};
auto cf = make_cf();
auto bar1 = [cf](){
// expected-warning@-1{{Captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
CFArrayAppendValue(cf, nullptr);
};
auto bar2 = [&cf](){
// expected-warning@-1{{Captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
CFArrayAppendValue(cf, nullptr);
};
auto bar3 = [&](){
CFArrayAppendValue(cf, nullptr);
// expected-warning@-1{{Implicitly captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
cf = nullptr;
};
auto bar4 = [=](){
CFArrayAppendValue(cf, nullptr);
// expected-warning@-1{{Implicitly captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
};
auto os = make_os();
auto baz1 = [os](){
dispatch_queue_get_label(os);
};
auto baz2 = [&os](){
dispatch_queue_get_label(os);
};
auto baz3 = [&](){
dispatch_queue_get_label(os);
os = nullptr;
};
auto baz4 = [=](){
dispatch_queue_get_label(os);
};
call(foo1);
call(foo2);
call(foo3);
call(foo4);
call(bar1);
call(bar2);
call(bar3);
call(bar4);
call(baz1);
call(baz2);
call(baz3);
call(baz4);
}
void quiet() {
// This code is not expected to trigger any warnings.
SomeObj *obj;
auto foo3 = [&]() {};
auto foo4 = [=]() {};
call(foo3);
call(foo4);
obj = nullptr;
}
template <typename Callback>
void map(SomeObj* start, [[clang::noescape]] Callback&& callback)
{
while (start) {
callback(start);
start = [start next];
}
}
template <typename Callback1, typename Callback2>
void doubleMap(SomeObj* start, [[clang::noescape]] Callback1&& callback1, Callback2&& callback2)
{
while (start) {
callback1(start);
callback2(start);
start = [start next];
}
}
template <typename Callback1, typename Callback2>
void get_count_cf(CFArrayRef array, [[clang::noescape]] Callback1&& callback1, Callback2&& callback2)
{
auto count = CFArrayGetCount(array);
callback1(count);
callback2(count);
}
template <typename Callback1, typename Callback2>
void get_count_os(dispatch_queue_t queue, [[clang::noescape]] Callback1&& callback1, Callback2&& callback2)
{
auto* label = dispatch_queue_get_label(queue);
callback1(label);
callback2(label);
}
void noescape_lambda() {
SomeObj* someObj = make_obj();
SomeObj* otherObj = make_obj();
map(make_obj(), [&](SomeObj *obj) {
[otherObj doWork];
});
doubleMap(make_obj(), [&](SomeObj *obj) {
[otherObj doWork];
}, [&](SomeObj *obj) {
[otherObj doWork];
});
([&] {
[someObj doWork];
})();
CFMutableArrayRef someCF = make_cf();
get_count_cf(make_cf(), [&](CFIndex count) {
CFArrayAppendValue(someCF, nullptr);
}, [&](CFIndex count) {
CFArrayAppendValue(someCF, nullptr);
// expected-warning@-1{{Implicitly captured reference 'someCF' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
});
dispatch_queue_t someOS = make_os();
get_count_os(make_os(), [&](const char* label) {
dispatch_queue_get_label(someOS);
}, [&](const char* label) {
dispatch_queue_get_label(someOS);
});
}
void callFunctionOpaque(WTF::Function<void()>&&);
void callFunction(WTF::Function<void()>&& function) {
someFunction();
function();
}
void lambda_converted_to_function(SomeObj* obj, CFMutableArrayRef cf, dispatch_queue_t os)
{
callFunction([&]() {
[obj doWork];
CFArrayAppendValue(cf, nullptr);
// expected-warning@-1{{Implicitly captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
dispatch_queue_get_label(os);
});
callFunctionOpaque([&]() {
[obj doWork];
CFArrayAppendValue(cf, nullptr);
// expected-warning@-1{{Implicitly captured reference 'cf' to unretained type is unsafe [alpha.webkit.UnretainedLambdaCapturesChecker]}}
dispatch_queue_get_label(os);
});
}
@interface ObjWithSelf : NSObject {
RetainPtr<id> delegate;
OSObjectPtr<dispatch_queue_t> queue;
}
-(void)doWork;
-(void)doMoreWork;
-(void)run;
@end
@implementation ObjWithSelf
-(void)doWork {
auto doWork = [&] {
someFunction();
[delegate doWork];
};
auto doMoreWork = [=] {
someFunction();
[delegate doWork];
};
auto doExtraWork = [&, protectedSelf = retainPtr(self)] {
someFunction();
[delegate doWork];
};
auto* queuePtr = queue.get();
auto doAdditionalWork = [&] {
someFunction();
dispatch_queue_get_label(queuePtr);
};
callFunctionOpaque(doWork);
callFunctionOpaque(doMoreWork);
callFunctionOpaque(doExtraWork);
callFunctionOpaque(doAdditionalWork);
}
-(void)doMoreWork {
auto doWork = [self, protectedSelf = retainPtr(self)] {
someFunction();
[self doWork];
};
callFunctionOpaque(doWork);
}
-(void)run {
someFunction();
}
@end
|