[PATCH] D128687: [pseudo] Allow mixed designated/undesignated init lists.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 27 16:13:14 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.

This isn't allowed by the standard grammar but is allowed in C, and clang/GCC
permit it as an extension.

It avoids the need to determine which type of list we have in error-recovery.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128687

Files:
  clang-tools-extra/pseudo/lib/cxx.bnf
  clang-tools-extra/pseudo/test/cxx/mixed-designator.cpp


Index: clang-tools-extra/pseudo/test/cxx/mixed-designator.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/pseudo/test/cxx/mixed-designator.cpp
@@ -0,0 +1,27 @@
+// RUN: clang-pseudo -grammar=%cxx-bnf-file -source=%s --print-forest | FileCheck %s
+// FIXME: tighten CHECK to CHECK-NEXT once numeric literals are unambiguous.
+auto x = { 1, .f = 2, [c]{3} };
+// CHECK:      initializer-clause~braced-init-list
+// CHECK-NEXT: ├─{ := tok[3]
+// CHECK-NEXT: ├─initializer-list
+// CHECK-NEXT: │ ├─initializer-list
+// CHECK-NEXT: │ │ ├─initializer-list~literal
+// CHECK:      │ │ ├─, := tok[5]
+// CHECK-NEXT: │ │ └─initializer-list-item
+// CHECK-NEXT: │ │   ├─designator
+// CHECK-NEXT: │ │   │ ├─. := tok[6]
+// CHECK-NEXT: │ │   │ └─IDENTIFIER := tok[7]
+// CHECK-NEXT: │ │   └─brace-or-equal-initializer
+// CHECK-NEXT: │ │     ├─= := tok[8]
+// CHECK-NEXT: │ │     └─initializer-clause~literal
+// CHECK:      │ ├─, := tok[10]
+// CHECK-NEXT: │ └─initializer-list-item
+// CHECK-NEXT: │   ├─designator
+// CHECK-NEXT: │   │ ├─[ := tok[11]
+// CHECK-NEXT: │   │ ├─expression~IDENTIFIER := tok[12]
+// CHECK-NEXT: │   │ └─] := tok[13]
+// CHECK-NEXT: │   └─brace-or-equal-initializer~braced-init-list
+// CHECK-NEXT: │     ├─{ := tok[14]
+// CHECK-NEXT: │     ├─initializer-list~literal
+// CHECK:      │     └─} := tok[16]
+// CHECK-NEXT: └─} := tok[17]
Index: clang-tools-extra/pseudo/lib/cxx.bnf
===================================================================
--- clang-tools-extra/pseudo/lib/cxx.bnf
+++ clang-tools-extra/pseudo/lib/cxx.bnf
@@ -158,7 +158,10 @@
 postfix-expression := CONST_CAST < type-id > ( expression )
 postfix-expression := TYPEID ( expression )
 postfix-expression := TYPEID ( type-id )
-expression-list := initializer-list
+#! Standard defines expression-list in terms of initializer-list, but our
+#  initializer-list allows designators.
+expression-list := initializer-clause ..._opt
+expression-list := expression-list , initializer-clause ..._opt
 # expr.unary
 unary-expression := postfix-expression
 unary-expression := unary-operator cast-expression
@@ -453,15 +456,16 @@
 brace-or-equal-initializer := braced-init-list
 initializer-clause := assignment-expression
 initializer-clause := braced-init-list
+#! Allow mixed designated/non-designated init-list.
+# This is standard C, and accepted by clang and others as an extension.
 braced-init-list := { initializer-list ,_opt }
-braced-init-list := { designated-initializer-list ,_opt }
 braced-init-list := { }
-initializer-list := initializer-clause ..._opt
-initializer-list := initializer-list , initializer-clause ..._opt
-designated-initializer-list := designated-initializer-clause
-designated-initializer-list := designated-initializer-list , designated-initializer-clause
-designated-initializer-clause := designator brace-or-equal-initializer
+initializer-list := initializer-list-item ..._opt
+initializer-list := initializer-list , initializer-list-item ..._opt
+initializer-list-item := initializer-clause
+initializer-list-item := designator brace-or-equal-initializer
 designator := . IDENTIFIER
+designator := [ expression ]
 expr-or-braced-init-list := expression
 expr-or-braced-init-list := braced-init-list
 # dcl.fct


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128687.440426.patch
Type: text/x-patch
Size: 3450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220627/ac5ed0bb/attachment.bin>


More information about the cfe-commits mailing list