| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.MemoryUnsafeCastChecker -verify %s |
| 2 |
|
| 3 |
@protocol NSObject |
| 4 |
+alloc; |
| 5 |
-init; |
| 6 |
@end |
| 7 |
|
| 8 |
@interface NSObject <NSObject> {} |
| 9 |
@end |
| 10 |
|
| 11 |
@interface BaseClass : NSObject |
| 12 |
@end |
| 13 |
|
| 14 |
@interface DerivedClass : BaseClass |
| 15 |
-(void)testCasts:(BaseClass*)base; |
| 16 |
@end |
| 17 |
|
| 18 |
@implementation DerivedClass |
| 19 |
-(void)testCasts:(BaseClass*)base { |
| 20 |
DerivedClass *derived = (DerivedClass*)base; |
| 21 |
// expected-warning@-1{{Unsafe cast from base type 'BaseClass' to derived type 'DerivedClass'}} |
| 22 |
DerivedClass *derived_static = static_cast<DerivedClass*>(base); |
| 23 |
// expected-warning@-1{{Unsafe cast from base type 'BaseClass' to derived type 'DerivedClass'}} |
| 24 |
DerivedClass *derived_reinterpret = reinterpret_cast<DerivedClass*>(base); |
| 25 |
// expected-warning@-1{{Unsafe cast from base type 'BaseClass' to derived type 'DerivedClass'}} |
| 26 |
base = (BaseClass*)derived; // no warning |
| 27 |
base = (BaseClass*)base; // no warning |
| 28 |
} |
| 29 |
@end |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
class WrappedObject |
| 33 |
{ |
| 34 |
public: |
| 35 |
T get() const { return mMetalObject; } |
| 36 |
T mMetalObject = nullptr; |
| 37 |
}; |
| 38 |
|
| 39 |
@protocol MTLCommandEncoder |
| 40 |
@end |
| 41 |
@protocol MTLRenderCommandEncoder |
| 42 |
@end |
| 43 |
class CommandEncoder : public WrappedObject<id<MTLCommandEncoder>> { }; |
| 44 |
|
| 45 |
class RenderCommandEncoder final : public CommandEncoder |
| 46 |
{ |
| 47 |
private: |
| 48 |
id<MTLRenderCommandEncoder> get() |
| 49 |
{ |
| 50 |
return static_cast<id<MTLRenderCommandEncoder>>(CommandEncoder::get()); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
@interface Class1 |
| 55 |
@end |
| 56 |
|
| 57 |
@interface Class2 |
| 58 |
@end |
| 59 |
|
| 60 |
void testUnrelated(Class1 *c1) { |
| 61 |
Class2 *c2 = (Class2*)c1; |
| 62 |
// expected-warning@-1{{Unsafe cast from type 'Class1' to an unrelated type 'Class2'}} |
| 63 |
Class1 *c1_same = reinterpret_cast<Class1*>(c1); // no warning |
| 64 |
} |