[RISCV][FMV] Support target_clones (#85786)
[llvm-project.git] / clang / test / Analysis / osobjectcstylecastchecker_test.cpp
blobaa43a18e6346f83516e12cdc639fb91980d60656
1 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=optin.osx.OSObjectCStyleCast %s -verify
2 #include "os_object_base.h"
4 struct OSArray : public OSObject {
5 unsigned getCount();
6 };
8 struct A {
9 int x;
11 struct B : public A {
12 unsigned getCount();
15 unsigned warn_on_explicit_downcast(OSObject * obj) {
16 OSArray *a = (OSArray *) obj; // expected-warning{{C-style cast of an OSObject is prone to type confusion attacks; use 'OSRequiredCast' if the object is definitely of type 'OSArray', or 'OSDynamicCast' followed by a null check if unsure}}
17 return a->getCount();
20 void no_warn_on_upcast(OSArray *arr) {
21 OSObject *obj = (OSObject *) arr;
22 obj->retain();
23 obj->release();
26 unsigned no_warn_on_dynamic_cast(OSObject *obj) {
27 OSArray *a = OSDynamicCast(OSArray, obj);
28 return a->getCount();
31 __SIZE_TYPE__ no_warn_on_primitive_conversion(OSArray *arr) {
32 return (__SIZE_TYPE__) arr;
35 unsigned no_warn_on_other_type_cast(A *a) {
36 B *b = (B *) a;
37 return b->getCount();
40 unsigned no_warn_alloc_class_with_name() {
41 OSArray *a = (OSArray *)OSMetaClass::allocClassWithName("OSArray"); // no warning
42 return a->getCount();
45 unsigned warn_alloc_class_with_name() {
46 OSArray *a = (OSArray *)OSMetaClass::allocClassWithName("OSObject"); // expected-warning{{C-style cast of an OSObject is prone to type confusion attacks; use 'OSRequiredCast' if the object is definitely of type 'OSArray', or 'OSDynamicCast' followed by a null check if unsure}}
47 return a->getCount();