[PATCH] D40445: [C++17] Allow an empty expression in an if init statement

Nicolas Lesser via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 24 11:54:04 PST 2017


Rakete1111 updated this revision to Diff 124235.
Rakete1111 added a comment.

Added a test for the switch statement and added full context to the diff.


https://reviews.llvm.org/D40445

Files:
  lib/Parse/ParseExprCXX.cpp
  test/CXX/stmt.stmt/stmt.select/p3.cpp


Index: test/CXX/stmt.stmt/stmt.select/p3.cpp
===================================================================
--- test/CXX/stmt.stmt/stmt.select/p3.cpp
+++ test/CXX/stmt.stmt/stmt.select/p3.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++1z -verify %s -DCXX17
 
 int f();
 
@@ -10,10 +11,41 @@
   }
 }
 
-
 void h() {
   if (int x = f()) // expected-note 2{{previous definition}}
     int x; // expected-error{{redefinition of 'x'}}
   else
     int x; // expected-error{{redefinition of 'x'}}
 }
+
+#ifdef CXX17
+int ifInitStatement() {
+  if (int I = 0; ++I == 1)
+    return I;
+
+  int Var = 0;
+  if (Var + Var; Var == 0)
+    return Var;
+
+  if (; true)
+    return 1;
+}
+
+int switchInitStatement() {
+  switch (int I = 1; I) {
+  case 1:
+    return I;
+  }
+
+  int Var = 0;
+  switch (Var + Var; Var) {
+  case 0:
+    return Var;
+  }
+
+  switch (; 0) {
+  case 0:
+    return 0;
+  }
+}
+#endif
Index: lib/Parse/ParseExprCXX.cpp
===================================================================
--- lib/Parse/ParseExprCXX.cpp
+++ lib/Parse/ParseExprCXX.cpp
@@ -1745,6 +1745,11 @@
   case ConditionOrInitStatement::Expression: {
     ProhibitAttributes(attrs);
 
+    // We can have an empty expression here.
+    //   if (; true);
+    if (TryConsumeToken(tok::semi))
+      return ParseCXXCondition(nullptr, Loc, CK);
+
     // Parse the expression.
     ExprResult Expr = ParseExpression(); // expression
     if (Expr.isInvalid())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40445.124235.patch
Type: text/x-patch
Size: 1513 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171124/f594c2c9/attachment.bin>


More information about the cfe-commits mailing list