annotate clang/test/SemaCXX/delete.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // Test without PCH
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -fsyntax-only -include %S/delete-mismatch.h -fdiagnostics-parseable-fixits -std=c++11 %s 2>&1 | FileCheck %s
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 // Test with PCH
anatofuz
parents:
diff changeset
5 // RUN: %clang_cc1 -x c++-header -std=c++11 -emit-pch -o %t %S/delete-mismatch.h
anatofuz
parents:
diff changeset
6 // RUN: %clang_cc1 -std=c++11 -include-pch %t -DWITH_PCH -fsyntax-only -verify %s -ast-dump
anatofuz
parents:
diff changeset
7
anatofuz
parents:
diff changeset
8 void f(int a[10][20]) {
anatofuz
parents:
diff changeset
9 delete a; // expected-warning {{'delete' applied to a pointer-to-array type}}
anatofuz
parents:
diff changeset
10 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:9}:"[]"
anatofuz
parents:
diff changeset
11 }
anatofuz
parents:
diff changeset
12 namespace MemberCheck {
anatofuz
parents:
diff changeset
13 struct S {
anatofuz
parents:
diff changeset
14 int *a = new int[5]; // expected-note4 {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
15 int *b;
anatofuz
parents:
diff changeset
16 int *c;
anatofuz
parents:
diff changeset
17 static int *d;
anatofuz
parents:
diff changeset
18 S();
anatofuz
parents:
diff changeset
19 S(int);
anatofuz
parents:
diff changeset
20 ~S() {
anatofuz
parents:
diff changeset
21 delete a; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
22 delete b; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
23 delete[] c; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
anatofuz
parents:
diff changeset
24 }
anatofuz
parents:
diff changeset
25 void f();
anatofuz
parents:
diff changeset
26 };
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 void S::f()
anatofuz
parents:
diff changeset
29 {
anatofuz
parents:
diff changeset
30 delete a; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
31 delete b; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 S::S()
anatofuz
parents:
diff changeset
35 : b(new int[1]), c(new int) {} // expected-note3 {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
36 // expected-note@-1 {{allocated with 'new' here}}
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 S::S(int i)
anatofuz
parents:
diff changeset
39 : b(new int[i]), c(new int) {} // expected-note3 {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
40 // expected-note@-1 {{allocated with 'new' here}}
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 struct S2 : S {
anatofuz
parents:
diff changeset
43 ~S2() {
anatofuz
parents:
diff changeset
44 delete a; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
45 }
anatofuz
parents:
diff changeset
46 };
anatofuz
parents:
diff changeset
47 int *S::d = new int[42]; // expected-note {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
48 void f(S *s) {
anatofuz
parents:
diff changeset
49 int *a = new int[1]; // expected-note {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
50 delete a; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
51 delete s->a; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
52 delete s->b; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
53 delete s->c;
anatofuz
parents:
diff changeset
54 delete s->d;
anatofuz
parents:
diff changeset
55 delete S::d; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 // At least one constructor initializes field with matching form of 'new'.
anatofuz
parents:
diff changeset
59 struct MatchingNewIsOK {
anatofuz
parents:
diff changeset
60 int *p;
anatofuz
parents:
diff changeset
61 bool is_array_;
anatofuz
parents:
diff changeset
62 MatchingNewIsOK() : p{new int}, is_array_(false) {}
anatofuz
parents:
diff changeset
63 explicit MatchingNewIsOK(unsigned c) : p{new int[c]}, is_array_(true) {}
anatofuz
parents:
diff changeset
64 ~MatchingNewIsOK() {
anatofuz
parents:
diff changeset
65 if (is_array_)
anatofuz
parents:
diff changeset
66 delete[] p;
anatofuz
parents:
diff changeset
67 else
anatofuz
parents:
diff changeset
68 delete p;
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70 };
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 // At least one constructor's body is missing; no proof of mismatch.
anatofuz
parents:
diff changeset
73 struct CantProve_MissingCtorDefinition {
anatofuz
parents:
diff changeset
74 int *p;
anatofuz
parents:
diff changeset
75 CantProve_MissingCtorDefinition();
anatofuz
parents:
diff changeset
76 CantProve_MissingCtorDefinition(int);
anatofuz
parents:
diff changeset
77 ~CantProve_MissingCtorDefinition();
anatofuz
parents:
diff changeset
78 };
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 CantProve_MissingCtorDefinition::CantProve_MissingCtorDefinition()
anatofuz
parents:
diff changeset
81 : p(new int)
anatofuz
parents:
diff changeset
82 { }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 CantProve_MissingCtorDefinition::~CantProve_MissingCtorDefinition()
anatofuz
parents:
diff changeset
85 {
anatofuz
parents:
diff changeset
86 delete[] p;
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 struct base {};
anatofuz
parents:
diff changeset
90 struct derived : base {};
anatofuz
parents:
diff changeset
91 struct InitList {
anatofuz
parents:
diff changeset
92 base *p, *p2 = nullptr, *p3{nullptr}, *p4;
anatofuz
parents:
diff changeset
93 InitList(unsigned c) : p(new derived[c]), p4(nullptr) {} // expected-note {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
94 InitList(unsigned c, unsigned) : p{new derived[c]}, p4{nullptr} {} // expected-note {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
95 ~InitList() {
anatofuz
parents:
diff changeset
96 delete p; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
97 delete [] p;
anatofuz
parents:
diff changeset
98 delete p2;
anatofuz
parents:
diff changeset
99 delete [] p3;
anatofuz
parents:
diff changeset
100 delete p4;
anatofuz
parents:
diff changeset
101 }
anatofuz
parents:
diff changeset
102 };
anatofuz
parents:
diff changeset
103 }
anatofuz
parents:
diff changeset
104
anatofuz
parents:
diff changeset
105 namespace NonMemberCheck {
anatofuz
parents:
diff changeset
106 #define DELETE_ARRAY(x) delete[] (x)
anatofuz
parents:
diff changeset
107 #define DELETE(x) delete (x)
anatofuz
parents:
diff changeset
108 void f() {
anatofuz
parents:
diff changeset
109 int *a = new int(5); // expected-note2 {{allocated with 'new' here}}
anatofuz
parents:
diff changeset
110 delete[] a; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
anatofuz
parents:
diff changeset
111 int *b = new int;
anatofuz
parents:
diff changeset
112 delete b;
anatofuz
parents:
diff changeset
113 int *c{new int}; // expected-note {{allocated with 'new' here}}
anatofuz
parents:
diff changeset
114 int *d{new int[1]}; // expected-note2 {{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
115 delete [ ] c; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
anatofuz
parents:
diff changeset
116 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:17}:""
anatofuz
parents:
diff changeset
117 delete d; // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
118 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:9}:"[]"
anatofuz
parents:
diff changeset
119 DELETE_ARRAY(a); // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
anatofuz
parents:
diff changeset
120 DELETE(d); // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
anatofuz
parents:
diff changeset
121 }
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 namespace MissingInitializer {
anatofuz
parents:
diff changeset
125 template<typename T>
anatofuz
parents:
diff changeset
126 struct Base {
anatofuz
parents:
diff changeset
127 struct S {
anatofuz
parents:
diff changeset
128 const T *p1 = nullptr;
anatofuz
parents:
diff changeset
129 const T *p2 = new T[3];
anatofuz
parents:
diff changeset
130 };
anatofuz
parents:
diff changeset
131 };
anatofuz
parents:
diff changeset
132
anatofuz
parents:
diff changeset
133 void null_init(Base<double>::S s) {
anatofuz
parents:
diff changeset
134 delete s.p1;
anatofuz
parents:
diff changeset
135 delete s.p2;
anatofuz
parents:
diff changeset
136 }
anatofuz
parents:
diff changeset
137 }
anatofuz
parents:
diff changeset
138
anatofuz
parents:
diff changeset
139 #ifndef WITH_PCH
anatofuz
parents:
diff changeset
140 pch_test::X::X()
anatofuz
parents:
diff changeset
141 : a(new int[1]) // expected-note{{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
142 { }
anatofuz
parents:
diff changeset
143 pch_test::X::X(int i)
anatofuz
parents:
diff changeset
144 : a(new int[i]) // expected-note{{allocated with 'new[]' here}}
anatofuz
parents:
diff changeset
145 { }
anatofuz
parents:
diff changeset
146 #endif