File: reinterpret-cast.cpp

package info (click to toggle)
llvm-toolchain-3.4 1%3A3.4.2-13
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 253,236 kB
  • ctags: 276,203
  • sloc: cpp: 1,665,580; ansic: 298,647; asm: 206,157; objc: 84,350; python: 73,119; sh: 23,466; perl: 5,679; makefile: 5,542; ml: 5,250; pascal: 2,467; lisp: 1,420; xml: 679; cs: 236; csh: 117
file content (105 lines) | stat: -rw-r--r-- 2,130 bytes parent folder | download | duplicates (4)
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
// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s

void clang_analyzer_eval(bool);

typedef struct Opaque *Data;
struct IntWrapper {
  int x;
};

struct Child : public IntWrapper {
  void set() { x = 42; }
};

void test(Data data) {
  Child *wrapper = reinterpret_cast<Child*>(data);
  // Don't crash when upcasting here.
  // We don't actually know if 'data' is a Child.
  wrapper->set();
  clang_analyzer_eval(wrapper->x == 42); // expected-warning{{TRUE}}
}

namespace PR14872 {
  class Base1 {};
  class Derived1 : public Base1 {};

  Derived1 *f1();

  class Base2 {};
  class Derived2 : public Base2 {};

  void f2(Base2 *foo);

  void f3(void** out)
  {
    Base1 *v;
    v = f1();
    *out = v;
  }

  void test()
  {
    Derived2 *p;
    f3(reinterpret_cast<void**>(&p));
    // Don't crash when upcasting here.
    // In this case, 'p' actually refers to a Derived1.
    f2(p);
  }
}

namespace rdar13249297 {
  struct IntWrapperSubclass : public IntWrapper {};

  struct IntWrapperWrapper {
    IntWrapper w;
  };

  void test(IntWrapperWrapper *ww) {
    reinterpret_cast<IntWrapperSubclass *>(ww)->x = 42;
    clang_analyzer_eval(reinterpret_cast<IntWrapperSubclass *>(ww)->x == 42); // expected-warning{{TRUE}}

    clang_analyzer_eval(ww->w.x == 42); // expected-warning{{TRUE}}
    ww->w.x = 0;

    clang_analyzer_eval(reinterpret_cast<IntWrapperSubclass *>(ww)->x == 42); // expected-warning{{FALSE}}
  }
}

namespace PR15345 {
  class C {};

  class Base {
  public:
    void (*f)();
    int x;
  };

  class Derived : public Base {};

  void test() {
	Derived* p;
	*(reinterpret_cast<void**>(&p)) = new C;
	p->f();

    // We should still be able to do some reasoning about bindings.
    p->x = 42;
    clang_analyzer_eval(p->x == 42); // expected-warning{{TRUE}}
  };
}

int trackpointer_std_addressof() {
  int x;
  int *p = (int*)&reinterpret_cast<const volatile char&>(x);
  *p = 6;
  return x; // no warning
}

void set_x1(int *&);
void set_x2(void *&);
int radar_13146953(void) {
  int *x = 0, *y = 0;

  set_x1(x);
  set_x2((void *&)y);
  return *x + *y; // no warning
}