r238517 - AST: Fix printing GNU old-style field designators

Justin Bogner mail at justinbogner.com
Thu May 28 15:19:36 PDT 2015


Author: bogner
Date: Thu May 28 17:19:36 2015
New Revision: 238517

URL: http://llvm.org/viewvc/llvm-project?rev=238517&view=rev
Log:
AST: Fix printing GNU old-style field designators

Allows StmtPrinter to print old style field designators in
initializers, fixing an issue where we would print the following
invalid code:

  struct A a = {b: = 3, .c = 4};

Patch by Nick Sumner. Thanks!

Modified:
    cfe/trunk/lib/AST/StmtPrinter.cpp
    cfe/trunk/test/Sema/ast-print.c

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=238517&r1=238516&r2=238517&view=diff
==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Thu May 28 17:19:36 2015
@@ -1395,13 +1395,16 @@ void StmtPrinter::VisitParenListExpr(Par
 }
 
 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 @@ void StmtPrinter::VisitDesignatedInitExp
     }
   }
 
-  OS << " = ";
+  if (NeedsEquals)
+    OS << " = ";
+  else
+    OS << " ";
   PrintExpr(Node->getInit());
 }
 

Modified: cfe/trunk/test/Sema/ast-print.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/ast-print.c?rev=238517&r1=238516&r2=238517&view=diff
==============================================================================
--- cfe/trunk/test/Sema/ast-print.c (original)
+++ cfe/trunk/test/Sema/ast-print.c Thu May 28 17:19:36 2015
@@ -45,3 +45,11 @@ typedef struct {
 
 // CHECK: struct __attribute__((visibility("default"))) S;
 struct __attribute__((visibility("default"))) S;
+
+struct pair_t {
+  int a;
+  int b;
+};
+
+// CHECK: struct pair_t p = {a: 3, .b = 4};
+struct pair_t p = {a: 3, .b = 4};





More information about the cfe-commits mailing list