[PATCH] Fix printing GNU old-style field designators

Justin Bogner mail at justinbogner.com
Thu May 28 00:17:57 PDT 2015


Nick Sumner <nick.sumner at gmail.com> writes:
> Revised patch attached.

LGTM. Do you need me to commit this for you?

> Nick
>
>
> On Wed, May 27, 2015 at 8:18 PM, Justin Bogner <mail at justinbogner.com> wrote:
>> 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
>
> Index: lib/AST/StmtPrinter.cpp
> ===================================================================
> --- lib/AST/StmtPrinter.cpp	(revision 238393)
> +++ 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());
>  }
>  
> Index: test/Sema/ast-print.c
> ===================================================================
> --- test/Sema/ast-print.c	(revision 238393)
> +++ test/Sema/ast-print.c	(working copy)
> @@ -45,3 +45,12 @@
>  
>  // 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