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

Ismail Pazarbasi ismail.pazarbasi at gmail.com
Wed May 20 00:17:32 PDT 2015


Thank you both. Richard's example triggered assertion. I'll work on a
fix, and update the patch.

On Wed, May 20, 2015 at 2:09 AM, Nick Lewycky <nlewycky at google.com> wrote:
> On 19 May 2015 at 15:48, Richard Trieu <rtrieu at google.com> wrote:
>>
>> 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.
>
>
> Creduce coughed up this awkward piece of code:
>
> struct Task {
>   Task *task_ { delete task_ };
> };
>
> I have failed at turning it into valid code so far.



More information about the cfe-commits mailing list