[PATCH] D16352: Fix printing of C style casts with suppressed specifiers

2016-01-20 Thread Nick Sumner via cfe-commits
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


[PATCH] D16430: Fix printing of nested variable declarations with suppressed specifiers

2016-01-21 Thread Nick Sumner via cfe-commits
nick.sumner created this revision.
nick.sumner added reviewers: bkramer, rsmith.
nick.sumner added a subscriber: cfe-commits.

Allow nested variable declarations to have their types printed correctly even
when the incoming PrintingPolicy suppresses specifiers. This can happen when
initializing a variable with a statement expression. Given the C code:

int a, b = ({
int c = 1;
c;
});

The declaration of c is printed with a missing type specifier:

int a, b = ({
c = 1;
c;
});

With the patch, the declaration of c is properly printed:

int a, b = ({
int c = 1;
c;
});

This is related to the previous patch: http://reviews.llvm.org/D16352

http://reviews.llvm.org/D16430

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
@@ -58,3 +58,11 @@
 // CHECK: int *x = ((void *)0), *y = ((void *)0);
   int *x = ((void *)0), *y = ((void *)0);
 }
+
+void stmtExpr() {
+  int a, b = ({
+// CHECK: int c = 1;
+int c = 1;
+c;
+  });
+}
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -127,7 +127,9 @@
 
 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
   SmallVector Decls(S->decls());
-  Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
+  PrintingPolicy SubPolicy(Policy);
+  SubPolicy.SuppressSpecifiers = false;
+  Decl::printGroup(Decls.data(), Decls.size(), OS, SubPolicy, IndentLevel);
 }
 
 void StmtPrinter::VisitNullStmt(NullStmt *Node) {


Index: test/Sema/ast-print.c
===
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -58,3 +58,11 @@
 // CHECK: int *x = ((void *)0), *y = ((void *)0);
   int *x = ((void *)0), *y = ((void *)0);
 }
+
+void stmtExpr() {
+  int a, b = ({
+// CHECK: int c = 1;
+int c = 1;
+c;
+  });
+}
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -127,7 +127,9 @@
 
 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
   SmallVector Decls(S->decls());
-  Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
+  PrintingPolicy SubPolicy(Policy);
+  SubPolicy.SuppressSpecifiers = false;
+  Decl::printGroup(Decls.data(), Decls.size(), OS, SubPolicy, IndentLevel);
 }
 
 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D16433: Fix printing signed character literals

2016-01-21 Thread Nick Sumner via cfe-commits
nick.sumner created this revision.
nick.sumner added reviewers: bkramer, rsmith.
nick.sumner added a subscriber: cfe-commits.

Allow StmtPrinter to print signed character literals. Given the code:
char c = '\200';

The character literal is presently printed as:
char c = '\Uff80';

The original literal has the signed value -128 (or unsigned value 128)
and only has any meaning when clamped to the extended ASCII range.
'\Uff80' isn't a valid character.

With the patch, the literal is printed as:
char c = '\x80';

As a side note, this doesn't handle multicharacter literals, but I
don't think there is enough information in the AST to make that
possible. They are implementation defined, anyway.

http://reviews.llvm.org/D16433

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
@@ -66,3 +66,6 @@
 c;
   });
 }
+
+// CHECK: char c = '\x80';
+char c = '\200';
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1230,8 +1230,8 @@
   default:
 if (value < 256 && isPrintable((unsigned char)value))
   OS << "'" << (char)value << "'";
-else if (value < 256)
-  OS << "'\\x" << llvm::format("%02x", value) << "'";
+else if (value < 256 || CharacterLiteral::Ascii == Node->getKind())
+  OS << "'\\x" << llvm::format("%02x", (unsigned char)value) << "'";
 else if (value <= 0x)
   OS << "'\\u" << llvm::format("%04x", value) << "'";
 else


Index: test/Sema/ast-print.c
===
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -66,3 +66,6 @@
 c;
   });
 }
+
+// CHECK: char c = '\x80';
+char c = '\200';
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -1230,8 +1230,8 @@
   default:
 if (value < 256 && isPrintable((unsigned char)value))
   OS << "'" << (char)value << "'";
-else if (value < 256)
-  OS << "'\\x" << llvm::format("%02x", value) << "'";
+else if (value < 256 || CharacterLiteral::Ascii == Node->getKind())
+  OS << "'\\x" << llvm::format("%02x", (unsigned char)value) << "'";
 else if (value <= 0x)
   OS << "'\\u" << llvm::format("%04x", value) << "'";
 else
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-01-21 Thread Nick Sumner via cfe-commits
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


[PATCH] D16438: Fix printing of nested variable declarations with suppressed tags

2016-01-21 Thread Nick Sumner via cfe-commits
nick.sumner created this revision.
nick.sumner added reviewers: bkramer, rsmith.
nick.sumner added a subscriber: cfe-commits.

Allow nested variable declarations using suppressed tags.
This is the logical conclusion of a handful of patches needed to correctly 
print types in nested declarations and casts:
http://reviews.llvm.org/D16352
http://reviews.llvm.org/D16430
http://reviews.llvm.org/D16437

Currently, for the code:
enum Foo { FOO = 0 };
struct { enum Foo foo; } anon = {({
enum Foo foo2 = FOO;
foo2;
})};

The nested declaration of foo2 omits the tagged type name when printed:
foo2 = FOO;

With this patch, the tagged type name is included:
enum Foo foo2 = FOO;




http://reviews.llvm.org/D16438

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
@@ -71,6 +71,15 @@
 int c = 1;
 c;
   });
+
+  enum Foo { FOO = 0 };
+  struct {
+enum Foo foo;
+  } anon = {({
+// CHECK: enum Foo foo2 = FOO;
+enum Foo foo2 = FOO;
+foo2;
+  })};
 }
 
 // CHECK: char c = '\x80';
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -129,6 +129,7 @@
   SmallVector Decls(S->decls());
   PrintingPolicy SubPolicy(Policy);
   SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
   Decl::printGroup(Decls.data(), Decls.size(), OS, SubPolicy, IndentLevel);
 }
 


Index: test/Sema/ast-print.c
===
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -71,6 +71,15 @@
 int c = 1;
 c;
   });
+
+  enum Foo { FOO = 0 };
+  struct {
+enum Foo foo;
+  } anon = {({
+// CHECK: enum Foo foo2 = FOO;
+enum Foo foo2 = FOO;
+foo2;
+  })};
 }
 
 // CHECK: char c = '\x80';
Index: lib/AST/StmtPrinter.cpp
===
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -129,6 +129,7 @@
   SmallVector Decls(S->decls());
   PrintingPolicy SubPolicy(Policy);
   SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
   Decl::printGroup(Decls.data(), Decls.size(), OS, SubPolicy, IndentLevel);
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D16438: Fix printing of types in initializers with suppressed tags.

2016-01-22 Thread Nick Sumner via cfe-commits
nick.sumner retitled this revision from "Fix printing of nested variable 
declarations with suppressed tags" to "Fix printing of types in initializers 
with suppressed tags.".
nick.sumner updated the summary for this revision.
nick.sumner updated this revision to Diff 45658.
nick.sumner added a comment.

Updated to reset tag and specifier suppression precisely when printing 
initializers.


http://reviews.llvm.org/D16438

Files:
  lib/AST/DeclPrinter.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,13 @@
 
 // CHECK: struct pair_t p = {a: 3, .b = 4};
 struct pair_t p = {a: 3, .b = 4};
+
+void initializers() {
+  // CHECK: int *x = ((void *)0), *y = ((void *)0);
+  int *x = ((void *)0), *y = ((void *)0);
+  struct Z{};
+  struct {
+struct Z z;
+  // CHECK: } z = {(struct Z){}};
+  } z = {(struct Z){}};
+}
Index: lib/AST/DeclPrinter.cpp
===
--- lib/AST/DeclPrinter.cpp
+++ lib/AST/DeclPrinter.cpp
@@ -751,7 +751,10 @@
   else if (D->getInitStyle() == VarDecl::CInit) {
 Out << " = ";
   }
-  Init->printPretty(Out, nullptr, Policy, Indentation);
+  PrintingPolicy SubPolicy(Policy);
+  SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
+  Init->printPretty(Out, nullptr, SubPolicy, Indentation);
   if ((D->getInitStyle() == VarDecl::CallInit) && 
!isa(Init))
 Out << ")";
 }


Index: test/Sema/ast-print.c
===
--- test/Sema/ast-print.c
+++ test/Sema/ast-print.c
@@ -53,3 +53,13 @@
 
 // CHECK: struct pair_t p = {a: 3, .b = 4};
 struct pair_t p = {a: 3, .b = 4};
+
+void initializers() {
+  // CHECK: int *x = ((void *)0), *y = ((void *)0);
+  int *x = ((void *)0), *y = ((void *)0);
+  struct Z{};
+  struct {
+struct Z z;
+  // CHECK: } z = {(struct Z){}};
+  } z = {(struct Z){}};
+}
Index: lib/AST/DeclPrinter.cpp
===
--- lib/AST/DeclPrinter.cpp
+++ lib/AST/DeclPrinter.cpp
@@ -751,7 +751,10 @@
   else if (D->getInitStyle() == VarDecl::CInit) {
 Out << " = ";
   }
-  Init->printPretty(Out, nullptr, Policy, Indentation);
+  PrintingPolicy SubPolicy(Policy);
+  SubPolicy.SuppressSpecifiers = false;
+  SubPolicy.SuppressTag = false;
+  Init->printPretty(Out, nullptr, SubPolicy, Indentation);
   if ((D->getInitStyle() == VarDecl::CallInit) && !isa(Init))
 Out << ")";
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D16438: Fix printing of types in initializers with suppressed tags.

2016-01-23 Thread Nick Sumner via cfe-commits
nick.sumner added a comment.

Thanks for your help!

I don't have SVN commit access.


http://reviews.llvm.org/D16438



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits