[PATCH] D128708: [pseudo] Simplify/loosen the grammar around lambda captures.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 28 01:18:55 PDT 2022


sammccall created this revision.
sammccall added a reviewer: hokein.
Herald added a project: All.
sammccall requested review of this revision.
Herald added subscribers: cfe-commits, alextsao1999.
Herald added a project: clang-tools-extra.

Treat captures as a uniform list, rather than default-captures being special
snowflakes that may only appear at the start.

This accepts a larger set of (incorrect) code, and simplifies error-handling
by making this fit into the usual homogeneous-list pattern.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128708

Files:
  clang-tools-extra/pseudo/lib/cxx.bnf
  clang-tools-extra/pseudo/test/cxx/capture-list.cpp


Index: clang-tools-extra/pseudo/test/cxx/capture-list.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/pseudo/test/cxx/capture-list.cpp
@@ -0,0 +1,23 @@
+// RUN: clang-pseudo -grammar=%cxx-bnf-file -source=%s --print-forest | FileCheck %s
+// We loosely allow capture defaults in any position/multiple times.
+auto lambda = [&, &foo, bar(x), =]{};
+// CHECK:      lambda-introducer := [ capture-list ]
+// CHECK-NEXT: ├─[
+// CHECK-NEXT: ├─capture-list
+// CHECK-NEXT: │ ├─capture-list
+// CHECK-NEXT: │ │ ├─capture-list
+// CHECK-NEXT: │ │ │ ├─capture-list~& := tok[4]
+// CHECK-NEXT: │ │ │ ├─,
+// CHECK-NEXT: │ │ │ └─capture~simple-capture
+// CHECK-NEXT: │ │ │   ├─&
+// CHECK-NEXT: │ │ │   └─IDENTIFIER := tok[7]
+// CHECK-NEXT: │ │ ├─,
+// CHECK-NEXT: │ │ └─capture~init-capture
+// CHECK-NEXT: │ │   ├─IDENTIFIER := tok[9]
+// CHECK-NEXT: │ │   └─initializer := ( expression-list )
+// CHECK-NEXT: │ │     ├─(
+// CHECK-NEXT: │ │     ├─expression-list~IDENTIFIER := tok[11]
+// CHECK-NEXT: │ │     └─)
+// CHECK-NEXT: │ ├─,
+// CHECK-NEXT: │ └─capture~=
+// CHECK-NEXT: └─]
Index: clang-tools-extra/pseudo/lib/cxx.bnf
===================================================================
--- clang-tools-extra/pseudo/lib/cxx.bnf
+++ clang-tools-extra/pseudo/lib/cxx.bnf
@@ -76,17 +76,17 @@
 nested-name-specifier := nested-name-specifier TEMPLATE_opt simple-template-id ::
 lambda-expression := lambda-introducer lambda-declarator_opt compound-statement
 lambda-expression := lambda-introducer < template-parameter-list > requires-clause_opt lambda-declarator_opt compound-statement
-lambda-introducer := [ lambda-capture_opt ]
+#! We allow a capture-default to appear anywhere in a capture-list.
+# This simplifies the grammar and error recovery.
+lambda-introducer := [ capture-list_opt ]
 lambda-declarator := ( parameter-declaration-clause_opt ) decl-specifier-seq_opt noexcept-specifier_opt trailing-return-type_opt requires-clause_opt
-lambda-capture := capture-default
-lambda-capture := capture-list
-lambda-capture := capture-default , capture-list
-capture-default := &
-capture-default := =
 capture-list := capture
 capture-list := capture-list , capture
+capture := capture-default
 capture := simple-capture
 capture := init-capture
+capture-default := &
+capture-default := =
 simple-capture := IDENTIFIER ..._opt
 simple-capture := & IDENTIFIER ..._opt
 simple-capture := THIS


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128708.440520.patch
Type: text/x-patch
Size: 2601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220628/146ff5be/attachment.bin>


More information about the cfe-commits mailing list