[PATCH] Fix printing GNU old-style field designators

Justin Bogner mail at justinbogner.com
Wed May 27 20:18:33 PDT 2015


Nick Sumner <nick.sumner at gmail.com> writes:
> 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};

Looks pretty good. Could you convert the test to one that runs as part
of make check? You can probably add something to test/Sema/ast-print.c,
or at least use that test as an example.

> 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
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list