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!

llvm-svn: 238517
This commit is contained in:
Justin Bogner 2015-05-28 22:19:36 +00:00
parent 2607a0d655
commit cb337035f2
2 changed files with 16 additions and 2 deletions

View 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());
}

View 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};