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

Danila Kutenin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 14 09:07:39 PDT 2021


danlark updated this revision to Diff 345458.
danlark added a comment.

- Add codegen test


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/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<true ? 0 : 1>::*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/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple i386-unknown-unknown -O3 -emit-llvm -o - %s | FileCheck %s
+
+// CHECK-LABEL: define{{.*}} i32 @_Z1fv()
+int f() {
+  // CHECK: ret i32 1
+  for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
+    return x;
+  }
+}
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))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102502.345458.patch
Type: text/x-patch
Size: 1608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210514/760e88eb/attachment.bin>


More information about the cfe-commits mailing list