Clang Project

clang_source_code/test/Analysis/osobjectcstylecastchecker_test.cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=optin.osx.OSObjectCStyleCast %s -verify
2#include "os_object_base.h"
3
4struct OSArray : public OSObject {
5  unsigned getCount();
6};
7
8struct A {
9  int x;
10};
11struct B : public A {
12  unsigned getCount();
13};
14
15unsigned warn_on_explicit_downcast(OSObject * obj) {
16  OSArray *a = (OSArray *) obj; // expected-warning{{C-style cast of OSObject. Use OSDynamicCast instead}}
17  return a->getCount();
18}
19
20void no_warn_on_upcast(OSArray *arr) {
21  OSObject *obj = (OSObject *) arr;
22  obj->retain();
23  obj->release();
24}
25
26unsigned no_warn_on_dynamic_cast(OSObject *obj) {
27  OSArray *a = OSDynamicCast(OSArray, obj);
28  return a->getCount();
29}
30
31__SIZE_TYPE__ no_warn_on_primitive_conversion(OSArray *arr) {
32  return (__SIZE_TYPE__) arr;
33}
34
35unsigned no_warn_on_other_type_cast(A *a) {
36  B *b = (B *) a;
37  return b->getCount();
38}
39
40