[PATCH] D83855: [clang] fix printing of lambdas with capture expressions

Ilya Golovenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 15 00:31:17 PDT 2020


walrus created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83855

Files:
  clang/lib/AST/StmtPrinter.cpp
  clang/test/AST/ast-printer-lambda.cpp


Index: clang/test/AST/ast-printer-lambda.cpp
===================================================================
--- clang/test/AST/ast-printer-lambda.cpp
+++ clang/test/AST/ast-printer-lambda.cpp
@@ -15,6 +15,22 @@
   auto lambda = [&]{};
   //CHECK: [&] {
 }
+{
+  auto lambda = [k{i}] {};
+  //CHECK: [k{i}] {
+}
+{
+  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] {
Index: clang/lib/AST/StmtPrinter.cpp
===================================================================
--- clang/lib/AST/StmtPrinter.cpp
+++ clang/lib/AST/StmtPrinter.cpp
@@ -2005,8 +2005,17 @@
     if (C->isPackExpansion())
       OS << "...";
 
-    if (Node->isInitCapture(C))
-      PrintExpr(C->getCapturedVar()->getInit());
+    if (Node->isInitCapture(C)) {
+      VarDecl *D = C->getCapturedVar();
+      Expr *Init = D->getInit();
+      if (D->getInitStyle() == VarDecl::CallInit && !isa<ParenListExpr>(Init))
+        OS << "(";
+      else if (D->getInitStyle() == VarDecl::CInit)
+        OS << " = ";
+      PrintExpr(Init);
+      if (D->getInitStyle() == VarDecl::CallInit && !isa<ParenListExpr>(Init))
+        OS << ")";
+    }
   }
   OS << ']';
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83855.278091.patch
Type: text/x-patch
Size: 1313 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200715/757770b3/attachment.bin>


More information about the cfe-commits mailing list