[PATCH] D36357: Added a better diagnostic when using the delete operator with lambdas

Blitz Rakete via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 5 08:10:11 PDT 2017


Rakete1111 created this revision.

This adds a new error for missing parentheses around lambdas in delete operators.

  int main() {
    delete []() { return new int(); }();
  }

This will result in:

  test.cpp:2:10: error: missing parentheses around lambda expression
    delete []() { return new int(); }();
               ^~~~~~~~~~~~~~~
  1 error generated.




https://reviews.llvm.org/D36357

Files:
  include/clang/Basic/DiagnosticParseKinds.td
  lib/Parse/ParseExprCXX.cpp
  test/Parser/cxx0x-lambda-expressions.cpp
  test/SemaCXX/new-delete-0x.cpp


Index: test/SemaCXX/new-delete-0x.cpp
===================================================================
--- test/SemaCXX/new-delete-0x.cpp
+++ test/SemaCXX/new-delete-0x.cpp
@@ -34,6 +34,5 @@
 void bad_deletes()
 {
   // 'delete []' is always array delete, per [expr.delete]p1.
-  // FIXME: Give a better diagnostic.
-  delete []{ return (int*)0; }(); // expected-error {{expected expression}}
+  delete []{ return (int*)0; }(); // expected-error{{missing parentheses around lambda expression}}
 }
Index: test/Parser/cxx0x-lambda-expressions.cpp
===================================================================
--- test/Parser/cxx0x-lambda-expressions.cpp
+++ test/Parser/cxx0x-lambda-expressions.cpp
@@ -53,7 +53,7 @@
   void delete_lambda(int *p) {
     delete [] p;
     delete [] (int*) { new int }; // ok, compound-literal, not lambda
-    delete [] { return new int; } (); // expected-error{{expected expression}}
+    delete [] { return new int; } (); // expected-error{{missing parentheses around lambda expression}}
     delete [&] { return new int; } (); // ok, lambda
   }
 
Index: lib/Parse/ParseExprCXX.cpp
===================================================================
--- lib/Parse/ParseExprCXX.cpp
+++ lib/Parse/ParseExprCXX.cpp
@@ -2884,15 +2884,39 @@
     //   [Footnote: A lambda expression with a lambda-introducer that consists
     //              of empty square brackets can follow the delete keyword if
     //              the lambda expression is enclosed in parentheses.]
-    // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
-    //        lambda-introducer.
-    ArrayDelete = true;
-    BalancedDelimiterTracker T(*this, tok::l_square);
+    TentativeParsingAction TPA(*this);
 
-    T.consumeOpen();
-    T.consumeClose();
-    if (T.getCloseLocation().isInvalid())
-      return ExprError();
+    // Basic lookahead to check if we have a lambda expression. If we
+    // encounter two braces with a semicolon, we can be pretty sure
+    // that this is a lambda, not say a compound literal. 
+    if (!SkipUntil(tok::l_brace, SkipUntilFlags::StopAtSemi) ||
+        (NextToken().isNot(tok::r_brace) && !SkipUntil(tok::semi)) ||
+        !SkipUntil(tok::r_brace, SkipUntilFlags::StopAtSemi)) {
+      TPA.Revert();
+      ArrayDelete = true;
+      BalancedDelimiterTracker T(*this, tok::l_square);
+
+      T.consumeOpen();
+      T.consumeClose();
+      if (T.getCloseLocation().isInvalid())
+        return ExprError();
+    } else {
+      TPA.Revert();
+
+      // Warn if the non-capturing lambda isn't surrounded by parenthesis
+      // to disambiguate it from 'delete[]'. 
+      ExprResult Lambda = TryParseLambdaExpression();
+      if (!Lambda.isInvalid()) {
+        SourceLocation StartLoc = Lambda.get()->getLocStart();
+        Diag(StartLoc, diag::err_lambda_after_array_delete)
+          << SourceRange(StartLoc, Lambda.get()->getLocEnd());
+
+        // Evaluate any postfix expressions used on the lambda.
+        Lambda = ParsePostfixExpressionSuffix(Lambda);
+        return Actions.ActOnCXXDelete(Start, UseGlobal, /*ArrayForm=*/false,
+                                      Lambda.get());
+      }
+    }
   }
 
   ExprResult Operand(ParseCastExpression(false));
Index: include/clang/Basic/DiagnosticParseKinds.td
===================================================================
--- include/clang/Basic/DiagnosticParseKinds.td
+++ include/clang/Basic/DiagnosticParseKinds.td
@@ -99,6 +99,8 @@
   InGroup<CXX98Compat>, DefaultIgnore;
 def ext_alignof_expr : ExtWarn<
   "%0 applied to an expression is a GNU extension">, InGroup<GNUAlignofExpression>;
+def err_lambda_after_array_delete : Error<
+  "missing parentheses around lambda expression">;
 
 def warn_microsoft_dependent_exists : Warning<
   "dependent %select{__if_not_exists|__if_exists}0 declarations are ignored">, 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36357.109873.patch
Type: text/x-patch
Size: 3870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170805/9ca1b799/attachment.bin>


More information about the cfe-commits mailing list