[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-16 Thread Richard Smith - zygoloid via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd29f7f1a7b47: [clang] Fix ternary operator in the second for 
loop statement (authored by danlark, committed by rsmith).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

Files:
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseTentative.cpp
  clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
  clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
  clang/test/Parser/cxx2a-init-statement.cpp

Index: clang/test/Parser/cxx2a-init-statement.cpp
===
--- clang/test/Parser/cxx2a-init-statement.cpp
+++ clang/test/Parser/cxx2a-init-statement.cpp
@@ -15,6 +15,8 @@
   int A<0>::*arr2[3];
   for (int n = 5; int A::*x : arr2) {}
 
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {}
+
   F (*arr3[3])(int);
   for (int n = 5; F (*p)(int n) : arr3) {}
   for (int n = 5; F (*p)(int (n)) : arr3) {}
Index: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-pch -o %t %s
+// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
+
+int f() {
+  // CHECK: for (int i = 0; x; i++) {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,42 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+// CHECK-LABEL: @_Z1fv(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:[[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[X:%.*]] = alloca i32, align 4
+// CHECK-NEXT:store i32 0, i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND:%.*]]
+// CHECK:   for.cond:
+// CHECK-NEXT:[[TMP0:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[CMP:%.*]] = icmp slt i32 [[TMP0]], 2
+// CHECK-NEXT:[[TMP1:%.*]] = zext i1 [[CMP]] to i64
+// CHECK-NEXT:[[COND:%.*]] = select i1 [[CMP]], i32 1, i32 0
+// CHECK-NEXT:store i32 [[COND]], i32* [[X]], align 4
+// CHECK-NEXT:[[TMP2:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:[[TOBOOL:%.*]] = icmp ne i32 [[TMP2]], 0
+// CHECK-NEXT:br i1 [[TOBOOL]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
+// CHECK:   for.body:
+// CHECK-NEXT:[[TMP3:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:store i32 [[TMP3]], i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN:%.*]]
+// CHECK:   for.inc:
+// CHECK-NEXT:[[TMP4:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[INC:%.*]] = add nsw i32 [[TMP4]], 1
+// CHECK-NEXT:store i32 [[INC]], i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND]], !llvm.loop [[LOOP2:![0-9]+]]
+// CHECK:   for.end:
+// CHECK-NEXT:store i32 0, i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN]]
+// CHECK:   return:
+// CHECK-NEXT:[[TMP5:%.*]] = load i32, i32* [[RETVAL]], align 4
+// CHECK-NEXT:ret i32 [[TMP5]]
+//
+int f() {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/lib/Parse/ParseTentative.cpp
===
--- clang/lib/Parse/ParseTentative.cpp
+++ clang/lib/Parse/ParseTentative.cpp
@@ -353,8 +353,8 @@
   if (CanBeForRangeDecl) {
 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
 // The final case is a for range declaration, the rest are not.
+unsigned QuestionColonDepth = 0;
 while (true) {
-  unsigned QuestionColonDepth = 0;
   P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
   StopBeforeMatch);
   if (P.Tok.is(tok::question))
Index: clang/lib/Parse/ParseExprCXX.cpp
===
--- clang/lib/Parse/ParseExprCXX.cpp
+++ clang/lib/Parse/ParseExprCXX.cpp
@@ -2033,6 +2033,8 @@
 DeclGroupPtrTy DG = ParseSimpleDeclaration(DeclaratorContext::ForInit,
DeclEnd, attrs, false, FRI);
 FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
+assert((FRI->ColonLoc.isValid() || !DG) &&
+   "cannot find for range declaration");
 return Sema::ConditionResult();
   }
 
___
cfe-commits mailing list

[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark added a comment.

Failure looks unrelated, please submit on your behalf as I don't have commit 
rights


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark updated this revision to Diff 345568.
danlark added a comment.

- Address comment from Richard


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

Files:
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseTentative.cpp
  clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
  clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
  clang/test/Parser/cxx2a-init-statement.cpp

Index: clang/test/Parser/cxx2a-init-statement.cpp
===
--- clang/test/Parser/cxx2a-init-statement.cpp
+++ clang/test/Parser/cxx2a-init-statement.cpp
@@ -15,6 +15,8 @@
   int A<0>::*arr2[3];
   for (int n = 5; int A::*x : arr2) {}
 
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {}
+
   F (*arr3[3])(int);
   for (int n = 5; F (*p)(int n) : arr3) {}
   for (int n = 5; F (*p)(int (n)) : arr3) {}
Index: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-pch -o %t %s
+// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
+
+int f() {
+  // CHECK: for (int i = 0; x; i++) {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,42 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+// CHECK-LABEL: @_Z1fv(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:[[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[X:%.*]] = alloca i32, align 4
+// CHECK-NEXT:store i32 0, i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND:%.*]]
+// CHECK:   for.cond:
+// CHECK-NEXT:[[TMP0:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[CMP:%.*]] = icmp slt i32 [[TMP0]], 2
+// CHECK-NEXT:[[TMP1:%.*]] = zext i1 [[CMP]] to i64
+// CHECK-NEXT:[[COND:%.*]] = select i1 [[CMP]], i32 1, i32 0
+// CHECK-NEXT:store i32 [[COND]], i32* [[X]], align 4
+// CHECK-NEXT:[[TMP2:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:[[TOBOOL:%.*]] = icmp ne i32 [[TMP2]], 0
+// CHECK-NEXT:br i1 [[TOBOOL]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
+// CHECK:   for.body:
+// CHECK-NEXT:[[TMP3:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:store i32 [[TMP3]], i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN:%.*]]
+// CHECK:   for.inc:
+// CHECK-NEXT:[[TMP4:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[INC:%.*]] = add nsw i32 [[TMP4]], 1
+// CHECK-NEXT:store i32 [[INC]], i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND]], !llvm.loop [[LOOP2:![0-9]+]]
+// CHECK:   for.end:
+// CHECK-NEXT:store i32 0, i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN]]
+// CHECK:   return:
+// CHECK-NEXT:[[TMP5:%.*]] = load i32, i32* [[RETVAL]], align 4
+// CHECK-NEXT:ret i32 [[TMP5]]
+//
+int f() {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/lib/Parse/ParseTentative.cpp
===
--- clang/lib/Parse/ParseTentative.cpp
+++ clang/lib/Parse/ParseTentative.cpp
@@ -353,8 +353,8 @@
   if (CanBeForRangeDecl) {
 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
 // The final case is a for range declaration, the rest are not.
+unsigned QuestionColonDepth = 0;
 while (true) {
-  unsigned QuestionColonDepth = 0;
   P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
   StopBeforeMatch);
   if (P.Tok.is(tok::question))
Index: clang/lib/Parse/ParseExprCXX.cpp
===
--- clang/lib/Parse/ParseExprCXX.cpp
+++ clang/lib/Parse/ParseExprCXX.cpp
@@ -2033,6 +2033,8 @@
 DeclGroupPtrTy DG = ParseSimpleDeclaration(DeclaratorContext::ForInit,
DeclEnd, attrs, false, FRI);
 FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
+assert((FRI->ColonLoc.isValid() || !DG) &&
+   "cannot find for range declaration");
 return Sema::ConditionResult();
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Richard Smith - zygoloid via Phabricator via cfe-commits
rsmith accepted this revision.
rsmith added inline comments.
This revision is now accepted and ready to land.



Comment at: clang/lib/Parse/ParseExprCXX.cpp:2036
 FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
+assert(FRI->ColonLoc.isValid() && "cannot find for range declaration");
 return Sema::ConditionResult();

If parsing failed, I don't think this assertion should be expected to hold: 
error recovery after the parse error might not have interpreted the `:` as the 
start of the range expression.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark added a comment.

In D102502#2759900 , @rsmith wrote:

> Thanks, nice catch!
>
> Can we also add an assert when parsing a `for` statement that we actually 
> find a range if the tentative parse said we were expecting one?

Done.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark updated this revision to Diff 345561.
danlark added a comment.

- Add assert of finding a for range declaration


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

Files:
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseTentative.cpp
  clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
  clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
  clang/test/Parser/cxx2a-init-statement.cpp

Index: clang/test/Parser/cxx2a-init-statement.cpp
===
--- clang/test/Parser/cxx2a-init-statement.cpp
+++ clang/test/Parser/cxx2a-init-statement.cpp
@@ -15,6 +15,8 @@
   int A<0>::*arr2[3];
   for (int n = 5; int A::*x : arr2) {}
 
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {}
+
   F (*arr3[3])(int);
   for (int n = 5; F (*p)(int n) : arr3) {}
   for (int n = 5; F (*p)(int (n)) : arr3) {}
Index: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-pch -o %t %s
+// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
+
+int f() {
+  // CHECK: for (int i = 0; x; i++) {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,42 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+// CHECK-LABEL: @_Z1fv(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:[[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[X:%.*]] = alloca i32, align 4
+// CHECK-NEXT:store i32 0, i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND:%.*]]
+// CHECK:   for.cond:
+// CHECK-NEXT:[[TMP0:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[CMP:%.*]] = icmp slt i32 [[TMP0]], 2
+// CHECK-NEXT:[[TMP1:%.*]] = zext i1 [[CMP]] to i64
+// CHECK-NEXT:[[COND:%.*]] = select i1 [[CMP]], i32 1, i32 0
+// CHECK-NEXT:store i32 [[COND]], i32* [[X]], align 4
+// CHECK-NEXT:[[TMP2:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:[[TOBOOL:%.*]] = icmp ne i32 [[TMP2]], 0
+// CHECK-NEXT:br i1 [[TOBOOL]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
+// CHECK:   for.body:
+// CHECK-NEXT:[[TMP3:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:store i32 [[TMP3]], i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN:%.*]]
+// CHECK:   for.inc:
+// CHECK-NEXT:[[TMP4:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[INC:%.*]] = add nsw i32 [[TMP4]], 1
+// CHECK-NEXT:store i32 [[INC]], i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND]], !llvm.loop [[LOOP2:![0-9]+]]
+// CHECK:   for.end:
+// CHECK-NEXT:store i32 0, i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN]]
+// CHECK:   return:
+// CHECK-NEXT:[[TMP5:%.*]] = load i32, i32* [[RETVAL]], align 4
+// CHECK-NEXT:ret i32 [[TMP5]]
+//
+int f() {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/lib/Parse/ParseTentative.cpp
===
--- clang/lib/Parse/ParseTentative.cpp
+++ clang/lib/Parse/ParseTentative.cpp
@@ -353,8 +353,8 @@
   if (CanBeForRangeDecl) {
 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
 // The final case is a for range declaration, the rest are not.
+unsigned QuestionColonDepth = 0;
 while (true) {
-  unsigned QuestionColonDepth = 0;
   P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
   StopBeforeMatch);
   if (P.Tok.is(tok::question))
Index: clang/lib/Parse/ParseExprCXX.cpp
===
--- clang/lib/Parse/ParseExprCXX.cpp
+++ clang/lib/Parse/ParseExprCXX.cpp
@@ -2033,6 +2033,7 @@
 DeclGroupPtrTy DG = ParseSimpleDeclaration(DeclaratorContext::ForInit,
DeclEnd, attrs, false, FRI);
 FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
+assert(FRI->ColonLoc.isValid() && "cannot find for range declaration");
 return Sema::ConditionResult();
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark added inline comments.



Comment at: clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp:1
+// RUN: %clang_cc1 -triple i386-unknown-unknown -O3 -emit-llvm -o - %s | 
FileCheck %s
+

rsmith wrote:
> We don't use `-O3` tests for this kind of thing, to avoid depending on the 
> behaviour of the optimizer; instead you should test that the (unoptimized) IR 
> generated by clang is correct.
Done.



Comment at: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp:5
+int f() {
+  // CHECK: for (int i = 0; x; i++) {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {

rsmith wrote:
> This ast-print output looks wrong; I assume that's an unrelated bug?
Yes, I think so, in a buggy version `x` as a second parameter is missing so at 
least it tests correctly


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D102502: [clang] Fix ternary operator in the second for loop statement

2021-05-14 Thread Danila Kutenin via Phabricator via cfe-commits
danlark updated this revision to Diff 345536.
danlark marked 3 inline comments as done.
danlark added a comment.

- Add codegen and AST tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102502/new/

https://reviews.llvm.org/D102502

Files:
  clang/lib/Parse/ParseTentative.cpp
  clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
  clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
  clang/test/Parser/cxx2a-init-statement.cpp


Index: clang/test/Parser/cxx2a-init-statement.cpp
===
--- clang/test/Parser/cxx2a-init-statement.cpp
+++ clang/test/Parser/cxx2a-init-statement.cpp
@@ -15,6 +15,8 @@
   int A<0>::*arr2[3];
   for (int n = 5; int A::*x : arr2) {}
 
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {}
+
   F (*arr3[3])(int);
   for (int n = 5; F (*p)(int n) : arr3) {}
   for (int n = 5; F (*p)(int (n)) : arr3) {}
Index: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-pch -o %t %s
+// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
+
+int f() {
+  // CHECK: for (int i = 0; x; i++) {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,42 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | 
FileCheck %s
+
+// CHECK-LABEL: @_Z1fv(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:[[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[X:%.*]] = alloca i32, align 4
+// CHECK-NEXT:store i32 0, i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND:%.*]]
+// CHECK:   for.cond:
+// CHECK-NEXT:[[TMP0:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[CMP:%.*]] = icmp slt i32 [[TMP0]], 2
+// CHECK-NEXT:[[TMP1:%.*]] = zext i1 [[CMP]] to i64
+// CHECK-NEXT:[[COND:%.*]] = select i1 [[CMP]], i32 1, i32 0
+// CHECK-NEXT:store i32 [[COND]], i32* [[X]], align 4
+// CHECK-NEXT:[[TMP2:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:[[TOBOOL:%.*]] = icmp ne i32 [[TMP2]], 0
+// CHECK-NEXT:br i1 [[TOBOOL]], label [[FOR_BODY:%.*]], label 
[[FOR_END:%.*]]
+// CHECK:   for.body:
+// CHECK-NEXT:[[TMP3:%.*]] = load i32, i32* [[X]], align 4
+// CHECK-NEXT:store i32 [[TMP3]], i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN:%.*]]
+// CHECK:   for.inc:
+// CHECK-NEXT:[[TMP4:%.*]] = load i32, i32* [[I]], align 4
+// CHECK-NEXT:[[INC:%.*]] = add nsw i32 [[TMP4]], 1
+// CHECK-NEXT:store i32 [[INC]], i32* [[I]], align 4
+// CHECK-NEXT:br label [[FOR_COND]], !llvm.loop [[LOOP2:![0-9]+]]
+// CHECK:   for.end:
+// CHECK-NEXT:store i32 0, i32* [[RETVAL]], align 4
+// CHECK-NEXT:br label [[RETURN]]
+// CHECK:   return:
+// CHECK-NEXT:[[TMP5:%.*]] = load i32, i32* [[RETVAL]], align 4
+// CHECK-NEXT:ret i32 [[TMP5]]
+//
+int f() {
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+return x;
+  }
+  return 0;
+}
+
Index: clang/lib/Parse/ParseTentative.cpp
===
--- clang/lib/Parse/ParseTentative.cpp
+++ clang/lib/Parse/ParseTentative.cpp
@@ -353,8 +353,8 @@
   if (CanBeForRangeDecl) {
 // Skip until we hit a ')', ';', or a ':' with no matching '?'.
 // The final case is a for range declaration, the rest are not.
+unsigned QuestionColonDepth = 0;
 while (true) {
-  unsigned QuestionColonDepth = 0;
   P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
   StopBeforeMatch);
   if (P.Tok.is(tok::question))


Index: clang/test/Parser/cxx2a-init-statement.cpp
===
--- clang/test/Parser/cxx2a-init-statement.cpp
+++ clang/test/Parser/cxx2a-init-statement.cpp
@@ -15,6 +15,8 @@
   int A<0>::*arr2[3];
   for (int n = 5; int A::*x : arr2) {}
 
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {}
+
   F (*arr3[3])(int);
   for (int n = 5; F (*p)(int n) : arr3) {}
   for (int n = 5; F (*p)(int (n)) : arr3) {}
Index: clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
===
--- /dev/null
+++ clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-pch -o %t %s
+// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
+
+int f() {
+  // CHECK: for (int i = 0; x;