[PATCH] D16437: Fix printing of enum casts with suppressed tags

Nick Sumner via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 21 17:39:11 PST 2016


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16437.45627.patch
Type: text/x-patch
Size: 822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160122/77c2f791/attachment.bin>


More information about the cfe-commits mailing list