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