]> granicus.if.org Git - clang/commitdiff
MismatchingNewDeleteDetector uses incorrect field, and finds no initializer
authorIsmail Pazarbasi <ismail.pazarbasi@gmail.com>
Mon, 26 Oct 2015 19:20:24 +0000 (19:20 +0000)
committerIsmail Pazarbasi <ismail.pazarbasi@gmail.com>
Mon, 26 Oct 2015 19:20:24 +0000 (19:20 +0000)
Summary:
In `MismatchingNewDeleteDetector::analyzeInClassInitializer`, if
`Field`'s initializer expression is null, lookup the field in
implicit instantiation, and use found field's the initializer.

Reviewers: rsmith, rtrieu

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D9898

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251335 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprCXX.cpp
test/SemaCXX/delete.cpp

index 5ce34bb05ddb9bf8a07dd8d685120fc4b7fb4c43..348f29638e02849d431c4f0d7b6a3a3735b0bde1 100644 (file)
@@ -2500,8 +2500,10 @@ bool MismatchingNewDeleteDetector::hasMatchingNewInCtor(
 MismatchingNewDeleteDetector::MismatchResult
 MismatchingNewDeleteDetector::analyzeInClassInitializer() {
   assert(Field != nullptr && "This should be called only for members");
-  if (const CXXNewExpr *NE =
-          getNewExprFromInitListOrExpr(Field->getInClassInitializer())) {
+  const Expr *InitExpr = Field->getInClassInitializer();
+  if (!InitExpr)
+    return EndOfTU ? NoMismatch : AnalyzeLater;
+  if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) {
     if (NE->isArray() != IsArrayForm) {
       NewExprs.push_back(NE);
       return MemberInitMismatches;
index f94a8631613e436bdbd0886c2acde43643b75601..0c853f68c061095d2c4eab532d78314f97a9df2a 100644 (file)
@@ -120,6 +120,22 @@ void f() {
   DELETE(d);          // expected-warning {{'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'?}}
 }
 }
+
+namespace MissingInitializer {
+template<typename T>
+struct Base {
+  struct S {
+    const T *p1 = nullptr;
+    const T *p2 = new T[3];
+  };
+};
+
+void null_init(Base<double>::S s) {
+  delete s.p1;
+  delete s.p2;
+}
+}
+
 #ifndef WITH_PCH
 pch_test::X::X()
   : a(new int[1])  // expected-note{{allocated with 'new[]' here}}