Debian Codegraph @

clang/test/Analysis/NewDelete-atomics.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -DTEST_INLINABLE_ALLOCATORS -verify %s
4 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -DTEST_INLINABLE_ALLOCATORS -verify %s
5 // RUN: %clang_analyze_cc1 -analyzer-inline-max-stack-depth 2 -analyzer-config ipa-always-inline-size=2 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -verify %s
6 // RUN: %clang_analyze_cc1 -analyzer-inline-max-stack-depth 2 -analyzer-config ipa-always-inline-size=2 -analyzer-checker=core,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
7 // RUN: %clang_analyze_cc1 -analyzer-inline-max-stack-depth 2 -analyzer-config ipa-always-inline-size=2 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -DTEST_INLINABLE_ALLOCATORS -verify %s
8 // RUN: %clang_analyze_cc1 -analyzer-inline-max-stack-depth 2 -analyzer-config ipa-always-inline-size=2 -analyzer-checker=core,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -DTEST_INLINABLE_ALLOCATORS -verify %s
9
10 // expected-no-diagnostics
11
12 #include "Inputs/system-header-simulator-cxx.h"
13
14 typedef enum memory_order {
15 memory_order_relaxed = __ATOMIC_RELAXED,
16 memory_order_consume = __ATOMIC_CONSUME,
17 memory_order_acquire = __ATOMIC_ACQUIRE,
18 memory_order_release = __ATOMIC_RELEASE,
19 memory_order_acq_rel = __ATOMIC_ACQ_REL,
20 memory_order_seq_cst = __ATOMIC_SEQ_CST
21 } memory_order;
22
23 class RawObj {
24 int RefCnt;
25
26 public:
27 int incRef() {
28 return __c11_atomic_fetch_add((volatile _Atomic(int) *)&RefCnt, 1,
29 memory_order_relaxed);
30 }
31
32 int decRef() {
33 return __c11_atomic_fetch_sub((volatile _Atomic(int) *)&RefCnt, 1,
34 memory_order_relaxed);
35 }
36
37 void foo();
38 };
39
40 class StdAtomicObj {
41 std::atomic<int> RefCnt;
42
43 public:
44 int incRef() {
45 return ++RefCnt;
46 }
47
48 int decRef() {
49 return --RefCnt;
50 }
51
52 void foo();
53 };
54
55 template <typename T>
56 class IntrusivePtr {
57 T *Ptr;
58
59 public:
60 IntrusivePtr(T *Ptr) : Ptr(Ptr) {
61 Ptr->incRef();
62 }
63
64 IntrusivePtr(const IntrusivePtr &Other) : Ptr(Other.Ptr) {
65 Ptr->incRef();
66 }
67
68 ~IntrusivePtr() {
69 // We should not take the path on which the object is deleted.
70 if (Ptr->decRef() == 1)
71 delete Ptr;
72 }
73
74 T *getPtr() const { return Ptr; } // no-warning
75 };
76
77 // Also IntrusivePtr but let's dodge name-based heuristics.
78 template <typename T>
79 class DifferentlyNamed {
80 T *Ptr;
81
82 public:
83 DifferentlyNamed(T *Ptr) : Ptr(Ptr) {
84 Ptr->incRef();
85 }
86
87 DifferentlyNamed(const DifferentlyNamed &Other) : Ptr(Other.Ptr) {
88 Ptr->incRef();
89 }
90
91 ~DifferentlyNamed() {
92 // We should not take the path on which the object is deleted.
93 if (Ptr->decRef() == 1)
94 delete Ptr;
95 }
96
97 T *getPtr() const { return Ptr; } // no-warning
98 };
99
100 void testDestroyLocalRefPtr() {
101 IntrusivePtr<RawObj> p1(new RawObj());
102 {
103 IntrusivePtr<RawObj> p2(p1);
104 }
105
106 // p1 still maintains ownership. The object is not deleted.
107 p1.getPtr()->foo(); // no-warning
108 }
109
110 void testDestroySymbolicRefPtr(const IntrusivePtr<RawObj> &p1) {
111 {
112 IntrusivePtr<RawObj> p2(p1);
113 }
114
115 // p1 still maintains ownership. The object is not deleted.
116 p1.getPtr()->foo(); // no-warning
117 }
118
119 void testDestroyLocalRefPtrWithAtomics() {
120 IntrusivePtr<StdAtomicObj> p1(new StdAtomicObj());
121 {
122 IntrusivePtr<StdAtomicObj> p2(p1);
123 }
124
125 // p1 still maintains ownership. The object is not deleted.
126 p1.getPtr()->foo(); // no-warning
127 }
128
129
130 void testDestroyLocalRefPtrWithAtomics(const IntrusivePtr<StdAtomicObj> &p1) {
131 {
132 IntrusivePtr<StdAtomicObj> p2(p1);
133 }
134
135 // p1 still maintains ownership. The object is not deleted.
136 p1.getPtr()->foo(); // no-warning
137 }
138
139 void testDestroyLocalRefPtrDifferentlyNamed() {
140 DifferentlyNamed<RawObj> p1(new RawObj());
141 {
142 DifferentlyNamed<RawObj> p2(p1);
143 }
144
145 // p1 still maintains ownership. The object is not deleted.
146 p1.getPtr()->foo(); // no-warning
147 }
148
149 void testDestroySymbolicRefPtrDifferentlyNamed(
150 const DifferentlyNamed<RawObj> &p1) {
151 {
152 DifferentlyNamed<RawObj> p2(p1);
153 }
154
155 // p1 still maintains ownership. The object is not deleted.
156 p1.getPtr()->foo(); // no-warning
157 }
158
159 void testDestroyLocalRefPtrWithAtomicsDifferentlyNamed() {
160 DifferentlyNamed<StdAtomicObj> p1(new StdAtomicObj());
161 {
162 DifferentlyNamed<StdAtomicObj> p2(p1);
163 }
164
165 // p1 still maintains ownership. The object is not deleted.
166 p1.getPtr()->foo(); // no-warning
167 }
168
169
170 void testDestroyLocalRefPtrWithAtomicsDifferentlyNamed(
171 const DifferentlyNamed<StdAtomicObj> &p1) {
172 {
173 DifferentlyNamed<StdAtomicObj> p2(p1);
174 }
175
176 // p1 still maintains ownership. The object is not deleted.
177 p1.getPtr()->foo(); // no-warning
178 }