Hi all,

The attached patch allows StmtPrinter to print old style field
designators in initializers. Given the code:
struct A { int b; int c; };
struct A a = {b: 3, .c = 4};

The initializer is presently printed as:
struct A a = {b: = 3, .c = 4};

The combination of ':' and '=' is invalid.

With the patch, the initializer is printed as:
struct A a = {b: 3, .c = 4};

Best,
Nick
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp	(revision 238263)
+++ lib/AST/StmtPrinter.cpp	(working copy)
@@ -1395,13 +1395,16 @@
 }
 
 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
+  bool needsEquals = true;
   for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
                       DEnd = Node->designators_end();
        D != DEnd; ++D) {
     if (D->isFieldDesignator()) {
       if (D->getDotLoc().isInvalid()) {
-        if (IdentifierInfo *II = D->getFieldName())
+        if (IdentifierInfo *II = D->getFieldName()) {
           OS << II->getName() << ":";
+          needsEquals = false;
+        }
       } else {
         OS << "." << D->getFieldName()->getName();
       }
@@ -1418,7 +1421,10 @@
     }
   }
 
-  OS << " = ";
+  if (needsEquals)
+    OS << " = ";
+  else
+    OS << " ";
   PrintExpr(Node->getInit());
 }
 
struct A { int b; int c; };
struct A a = {b: 3, .c = 4};

struct D { struct A d[10]; struct A e[10]; };
struct D d = { .d[5].c = 3,  e: { [5].c = 4 } };
// e:[5].c is invalid syntax

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to