r237608 - Detect uses of mismatching forms of 'new' and 'delete'

Richard Trieu rtrieu at google.com
Tue May 19 15:48:48 PDT 2015


On Tue, May 19, 2015 at 2:19 PM, Nick Lewycky <nlewycky at google.com> wrote:

> On 18 May 2015 at 12:59, Ismail Pazarbasi <ismail.pazarbasi at gmail.com>
> wrote:
>
>> Author: ismailp
>> Date: Mon May 18 14:59:11 2015
>> New Revision: 237608
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=237608&view=rev
>> Log:
>> Detect uses of mismatching forms of 'new' and 'delete'
>>
>> Emit warning when operand to `delete` is allocated with `new[]` or
>> operand to `delete[]` is allocated with `new`.
>>
>> rev 2 update:
>> `getNewExprFromInitListOrExpr` should return `dyn_cast_or_null`
>> instead of `dyn_cast`, since `E` might be null.
>>
>
> FYI, I'm getting an assertion firing:
>
> clang: llvm/tools/clang/lib/Sema/SemaExprCXX.cpp:2447: const
> clang::CXXNewExpr *(anonymous
> namespace)::MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const
> clang::Expr *): Assertion `E != nullptr && "Expected a valid initializer
> expression"' failed.
>
> on presumed to be valid code (code that builds with a previous version of
> clang). I'm creduce'ing a testcase now.
>
>
Try this reduced case:

template <typename T>
struct Base {
  struct S {
    const T *t = 0;
  };
};

void Foo(Base<double>::S s) { delete s.t; }

I think what is happening is that the warning is looking for the
initializer of Base<double>::S::t, which is never used and not
instantiated.  So, the FieldDecl reports that it has an initializer, but
returns a null pointer when trying to retrieve it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150519/e1b7eb5a/attachment.html>


More information about the cfe-commits mailing list