nick.sumner created this revision.
nick.sumner added reviewers: bkramer, rsmith.
nick.sumner added a subscriber: cfe-commits.

Allow C style casts of enums to be printed correctly when the incoming 
PrintingPolicy suppresses tags. This can happen, when casts to enums occur 
during the initialization of an anonymous struct. Given the code:
    enum Foo { FOO = 0 };
    struct {
        enum Foo foo;
    } anon = {(enum Foo)0};

The type of the enum is suppressed when printing the initializer, so the cast 
is printed as:
    } anon = {()0};

With the patch, the initialization is printed as:
    } anon = {(enum Foo)0};

http://reviews.llvm.org/D16437

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

Index: test/Sema/ast-print.c
===================================================================
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -57,6 +57,12 @@
 void cast() {
 // CHECK: int *x = ((void *)0), *y = ((void *)0);
   int *x = ((void *)0), *y = ((void *)0);
+
+  enum Foo { FOO = 0 };
+  struct {
+    enum Foo foo;
+// CHECK: } anon = {(enum Foo)0};
+  } anon = {(enum Foo)0};
 }
 
 void stmtExpr() {
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1480,6 +1480,7 @@
   OS << '(';
   PrintingPolicy SubPolicy(Policy);
   SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
   Node->getTypeAsWritten().print(OS, SubPolicy);
   OS << ')';
   PrintExpr(Node->getSubExpr());


Index: test/Sema/ast-print.c
===================================================================
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -57,6 +57,12 @@
 void cast() {
 // CHECK: int *x = ((void *)0), *y = ((void *)0);
   int *x = ((void *)0), *y = ((void *)0);
+
+  enum Foo { FOO = 0 };
+  struct {
+    enum Foo foo;
+// CHECK: } anon = {(enum Foo)0};
+  } anon = {(enum Foo)0};
 }
 
 void stmtExpr() {
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1480,6 +1480,7 @@
   OS << '(';
   PrintingPolicy SubPolicy(Policy);
   SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
   Node->getTypeAsWritten().print(OS, SubPolicy);
   OS << ')';
   PrintExpr(Node->getSubExpr());
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to