aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVaibhav Thakkar <vaibhav.thakkar.22.12.99@gmail.com>2023-06-24 19:27:10 -0700
committerFangrui Song <i@maskray.me>2023-06-24 19:27:10 -0700
commitdf8d6d95ca64c70b3acc5a4266326966f3e6f93e (patch)
treed22d59d15b0966f7028c96cd453755e94784ad81
parentdb8e902b30d22528f0d72918eb46f141b73c8116 (diff)
downloadllvm-df8d6d95ca64c70b3acc5a4266326966f3e6f93e.zip
llvm-df8d6d95ca64c70b3acc5a4266326966f3e6f93e.tar.gz
llvm-df8d6d95ca64c70b3acc5a4266326966f3e6f93e.tar.bz2
[clang] Fix pretty-printing for variables declared in a for-loop condition
Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D153699
-rw-r--r--clang/lib/AST/StmtPrinter.cpp4
-rw-r--r--clang/test/PCH/for-loop-init-ternary-operator-statement.cpp2
-rw-r--r--clang/test/SemaCXX/ast-print.cpp2
3 files changed, 6 insertions, 2 deletions
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index d62b7e5..c3db500 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -400,7 +400,9 @@ void StmtPrinter::VisitForStmt(ForStmt *Node) {
PrintInitStmt(Node->getInit(), 5);
else
OS << (Node->getCond() ? "; " : ";");
- if (Node->getCond())
+ if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
+ PrintRawDeclStmt(DS);
+ else if (Node->getCond())
PrintExpr(Node->getCond());
OS << ";";
if (Node->getInc()) {
diff --git a/clang/test/PCH/for-loop-init-ternary-operator-statement.cpp b/clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
index 50819e7..8bce3e8 100644
--- a/clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
+++ b/clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
@@ -2,7 +2,7 @@
// RUN: %clang_cc1 -x ast -ast-print %t | FileCheck %s
int f() {
- // CHECK: for (int i = 0; x; i++) {
+ // CHECK: for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
return x;
}
diff --git a/clang/test/SemaCXX/ast-print.cpp b/clang/test/SemaCXX/ast-print.cpp
index fd1d3fe..2cb1ec4 100644
--- a/clang/test/SemaCXX/ast-print.cpp
+++ b/clang/test/SemaCXX/ast-print.cpp
@@ -21,12 +21,14 @@ void test1() {
// CHECK: if (int a = 1)
// CHECK: while (int a = 1)
// CHECK: switch (int a = 1)
+// CHECK: for (; int a = 1;)
void test2()
{
if (int a = 1) { }
while (int a = 1) { }
switch (int a = 1) { }
+ for(; int a = 1; ) { }
}
// CHECK: new (1) int;