[PATCH] Fix printing GNU old-style field designators

Nick Sumner nick.sumner at gmail.com
Wed May 27 22:46:33 PDT 2015


Revised patch attached.

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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: StmtPrinter.patch
Type: text/x-patch
Size: 1411 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150527/b4e6a11e/attachment.bin>


More information about the cfe-commits mailing list