From a130cf8ae8ab56ba1cfa7edc52b637c9d0c3fd38 Mon Sep 17 00:00:00 2001 From: Ilya Golovenko Date: Thu, 16 Jul 2020 12:47:44 +0200 Subject: [PATCH] [clang] Fix printing of lambdas with capture expressions Patch by @walrus ! Reviewers: lattner, kadircet Reviewed By: kadircet Subscribers: riccibruno, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D83855 --- clang/lib/AST/StmtPrinter.cpp | 19 +++++++++++++++++-- clang/test/AST/ast-printer-lambda.cpp | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp index f797f5f..ea16002 100644 --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -2005,8 +2005,23 @@ void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { if (C->isPackExpansion()) OS << "..."; - if (Node->isInitCapture(C)) - PrintExpr(C->getCapturedVar()->getInit()); + if (Node->isInitCapture(C)) { + VarDecl *D = C->getCapturedVar(); + + llvm::StringRef Pre; + llvm::StringRef Post; + if (D->getInitStyle() == VarDecl::CallInit && + !isa(D->getInit())) { + Pre = "("; + Post = ")"; + } else if (D->getInitStyle() == VarDecl::CInit) { + Pre = " = "; + } + + OS << Pre; + PrintExpr(D->getInit()); + OS << Post; + } } OS << ']'; diff --git a/clang/test/AST/ast-printer-lambda.cpp b/clang/test/AST/ast-printer-lambda.cpp index 27a361d..08f1ff5 100644 --- a/clang/test/AST/ast-printer-lambda.cpp +++ b/clang/test/AST/ast-printer-lambda.cpp @@ -16,6 +16,18 @@ void test1(int i, T... t) { //CHECK: [&] { } { + auto lambda = [k{i}] {}; + //CHECK: [k{i}] { +} +{ + auto lambda = [k(i)] {}; + //CHECK: [k(i)] { +} +{ + auto lambda = [k = i] {}; + //CHECK: [k = i] { +} +{ auto lambda = [t..., i]{}; //CHECK: [t..., i] { } @@ -31,6 +43,14 @@ void test1(int i, T... t) { auto lambda = [t..., this]{}; //CHECK: [t..., this] { } +{ + auto lambda = [k(t...)] {}; + //CHECK: [k(t...)] { +} +{ + auto lambda = [k{t...}] {}; + //CHECK: [k{t...}] { +} } }; \ No newline at end of file -- 2.7.4