[PATCH] Detect mismatching 'new' and 'delete' uses

Richard Smith richard at metafoo.co.uk
Mon Aug 11 11:33:37 PDT 2014


================
Comment at: include/clang/Sema/Sema.h:395-396
@@ -392,1 +394,4 @@
 
+  /// \brief All delete-expressions within the translation unit.
+  llvm::SmallPtrSet<const CXXDeleteExpr *, 4> DeleteExprs;
+
----------------
This is not a reasonable way to track the relevant state here.

The interesting cases are `VarDecl`s and `FieldDecl`s. For `VarDecl`s, at the point where we see the `delete`, you can immediately check whether you have an new-expression as an initializer (and don't worry about the obscure cases where an initializer comes after the use). For `FieldDecl`s, perform the check where you see the `delete`, and if you see either (a) no constructor with a matching `new`, or (b) only constructors with the wrong kind of `new`, then add your `delete` to a list to check at the end of the TU.

That way, in almost all cases we can deal with the diagnostic immediately rather than at end of TU. (This avoids building up a big set of all delete expressions.)

You'll also need to figure out how you're going to handle serialization/deserialization here, in the case where you hit one of these "not sure" cases in a PCH or similar. To that end, you should probably store a list of `{SourceLocation, FieldDecl}` pairs, or -- better -- a map from `FieldDecl` to the first source location where we saw a `delete`. (You'll need one extra bit to describe whether it was `delete` or `delete[]` I suppose...).

http://reviews.llvm.org/D4661






More information about the cfe-commits mailing list