nick.sumner created this revision. nick.sumner added reviewers: bkramer, rsmith. nick.sumner added a subscriber: cfe-commits.
Allow C style casts to be printed correctly even when the incoming PrintingPolicy suppresses specifiers. This can happen, for instance, when casts occur during the initialization of variables inside a DeclGroup. Given the code: void foo() { int *x = ((void *)0), *y = ((void *)0); } The casts are printed as: int *x = ((void *)0), *y = ((*)0); Note that the second cast lacks 'void' because specifiers are suppressed when printing members of the declgroup (after the first). With the patch, the casts are printed as: int *x = ((void *)0), *y = ((void *)0); http://reviews.llvm.org/D16352 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 @@ -53,3 +53,8 @@ // CHECK: struct pair_t p = {a: 3, .b = 4}; struct pair_t p = {a: 3, .b = 4}; + +void cast() { +// CHECK: int *x = ((void *)0), *y = ((void *)0); + int *x = ((void *)0), *y = ((void *)0); +} Index: lib/AST/StmtPrinter.cpp =================================================================== --- lib/AST/StmtPrinter.cpp +++ lib/AST/StmtPrinter.cpp @@ -1476,7 +1476,9 @@ } void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { OS << '('; - Node->getTypeAsWritten().print(OS, Policy); + PrintingPolicy SubPolicy(Policy); + SubPolicy.SuppressSpecifiers = 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 @@ -53,3 +53,8 @@ // CHECK: struct pair_t p = {a: 3, .b = 4}; struct pair_t p = {a: 3, .b = 4}; + +void cast() { +// CHECK: int *x = ((void *)0), *y = ((void *)0); + int *x = ((void *)0), *y = ((void *)0); +} Index: lib/AST/StmtPrinter.cpp =================================================================== --- lib/AST/StmtPrinter.cpp +++ lib/AST/StmtPrinter.cpp @@ -1476,7 +1476,9 @@ } void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { OS << '('; - Node->getTypeAsWritten().print(OS, Policy); + PrintingPolicy SubPolicy(Policy); + SubPolicy.SuppressSpecifiers = 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