]> granicus.if.org Git - clang/commitdiff
AST: Fix printing GNU old-style field designators
authorJustin Bogner <mail@justinbogner.com>
Thu, 28 May 2015 22:19:36 +0000 (22:19 +0000)
committerJustin Bogner <mail@justinbogner.com>
Thu, 28 May 2015 22:19:36 +0000 (22:19 +0000)
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!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@238517 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/StmtPrinter.cpp
test/Sema/ast-print.c

index dc4f9964c7a6f665757f553f61fc77293ef820bd..cd65d729730d0ed9c0246a2d5503f376611e4acf 100644 (file)
@@ -1395,13 +1395,16 @@ void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
 }
 
 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::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
     }
   }
 
-  OS << " = ";
+  if (NeedsEquals)
+    OS << " = ";
+  else
+    OS << " ";
   PrintExpr(Node->getInit());
 }
 
index 4b2b43190d42425df18984c1b9e6c3d747ab27d9..b4d76844fef780812aa45291135fb7b9f6cc721b 100644 (file)
@@ -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};