[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / reinterpret-cast.cpp
blobb71d588505fc8221f7612f104f88e22c73b6aa31
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
3 void clang_analyzer_eval(bool);
5 typedef struct Opaque *Data;
6 struct IntWrapper {
7 int x;
8 };
10 struct Child : public IntWrapper {
11 void set() { x = 42; }
14 void test(Data data) {
15 Child *wrapper = reinterpret_cast<Child*>(data);
16 // Don't crash when upcasting here.
17 // We don't actually know if 'data' is a Child.
18 wrapper->set();
19 clang_analyzer_eval(wrapper->x == 42); // expected-warning{{TRUE}}
22 namespace PR14872 {
23 class Base1 {};
24 class Derived1 : public Base1 {};
26 Derived1 *f1();
28 class Base2 {};
29 class Derived2 : public Base2 {};
31 void f2(Base2 *foo);
33 void f3(void** out)
35 Base1 *v;
36 v = f1();
37 *out = v;
40 void test()
42 Derived2 *p;
43 f3(reinterpret_cast<void**>(&p));
44 // Don't crash when upcasting here.
45 // In this case, 'p' actually refers to a Derived1.
46 f2(p);
50 namespace rdar13249297 {
51 struct IntWrapperSubclass : public IntWrapper {};
53 struct IntWrapperWrapper {
54 IntWrapper w;
57 void test(IntWrapperWrapper *ww) {
58 reinterpret_cast<IntWrapperSubclass *>(ww)->x = 42;
59 clang_analyzer_eval(reinterpret_cast<IntWrapperSubclass *>(ww)->x == 42); // expected-warning{{TRUE}}
61 clang_analyzer_eval(ww->w.x == 42); // expected-warning{{TRUE}}
62 ww->w.x = 0;
64 clang_analyzer_eval(reinterpret_cast<IntWrapperSubclass *>(ww)->x == 42); // expected-warning{{FALSE}}
68 namespace PR15345 {
69 class C {};
71 class Base {
72 public:
73 void (*f)();
74 int x;
77 class Derived : public Base {};
79 void test() {
80 Derived* p;
81 *(reinterpret_cast<void**>(&p)) = new C;
82 p->f(); // expected-warning{{Called function pointer is an uninitialized pointer value [core.CallAndMessage]}}
84 } // namespace PR15345
86 int trackpointer_std_addressof() {
87 int x;
88 int *p = (int*)&reinterpret_cast<const volatile char&>(x);
89 *p = 6;
90 return x; // no warning
93 void set_x1(int *&);
94 void set_x2(void *&);
95 int radar_13146953(void) {
96 int *x = 0, *y = 0;
98 set_x1(x);
99 set_x2((void *&)y);
100 return *x + *y; // no warning
103 namespace PR25426 {
104 struct Base {
105 int field;
108 struct Derived : Base { };
110 void foo(int &p) {
111 Derived &d = (Derived &)(p);
112 d.field = 2;