summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-01-13 08:01:36 +0000
committerDaniel Jasper <djasper@google.com>2013-01-13 08:01:36 +0000
commitef906a9c67a009a4c94b2162157a34f3458a915b (patch)
tree4a4b1edea8780136b3b9b0bf4fe03a1c918bfbf3
parent40e45eeae2da45903a73d4fb5c4bfd4cb9c1e936 (diff)
downloadbcm5719-llvm-ef906a9c67a009a4c94b2162157a34f3458a915b.tar.gz
bcm5719-llvm-ef906a9c67a009a4c94b2162157a34f3458a915b.zip
Improve identification of c-style casts.
A ")" before any of "=", "{" or ";" won't be a cast. This fixes issues with the formatting of unnamed parameters. Before: void f(int *){} After: void f(int *) {} llvm-svn: 172349
-rw-r--r--clang/lib/Format/Format.cpp6
-rw-r--r--clang/unittests/Format/FormatTest.cpp22
2 files changed, 24 insertions, 4 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 29debe58b6b..5de17179118 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -976,7 +976,11 @@ private:
Current.Type = TT_BlockComment;
} else if (Current.is(tok::r_paren) &&
(Current.Parent->Type == TT_PointerOrReference ||
- Current.Parent->Type == TT_TemplateCloser)) {
+ Current.Parent->Type == TT_TemplateCloser) &&
+ (Current.Children.empty() ||
+ (Current.Children[0].isNot(tok::equal) &&
+ Current.Children[0].isNot(tok::semi) &&
+ Current.Children[0].isNot(tok::l_brace)))) {
// FIXME: We need to get smarter and understand more cases of casts.
Current.Type = TT_CastRParen;
} else if (Current.is(tok::at) && Current.Children.size()) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5ace0f82f91..765d16f99ae 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1045,9 +1045,6 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("A<int **> a;");
verifyFormat("A<int *, int *> a;");
verifyFormat("A<int **, int **> a;");
- verifyFormat("Type *A = static_cast<Type *>(P);");
- verifyFormat("Type *A = (Type *)P;");
- verifyFormat("Type *A = (vector<Type *, int *>)P;");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -1062,6 +1059,25 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyGoogleFormat("int a = b ? *c : *d;");
}
+TEST_F(FormatTest, FormatsCasts) {
+ verifyFormat("Type *A = static_cast<Type *>(P);");
+ verifyFormat("Type *A = (Type *)P;");
+ verifyFormat("Type *A = (vector<Type *, int *>)P;");
+ verifyFormat("int a = (int)(2.0f);");
+
+ // FIXME: These also need to be identified.
+ verifyFormat("int a = (int) 2.0f;");
+ verifyFormat("int a = (int) * b;");
+
+ // These are not casts.
+ verifyFormat("void f(int *) {}");
+ verifyFormat("void f(int *);");
+ verifyFormat("void f(int *) = 0;");
+ verifyFormat("void f(SmallVector<int>) {}");
+ verifyFormat("void f(SmallVector<int>);");
+ verifyFormat("void f(SmallVector<int>) = 0;");
+}
+
TEST_F(FormatTest, FormatsFunctionTypes) {
// FIXME: Determine the cases that need a space after the return type and fix.
verifyFormat("A<bool()> a;");
OpenPOWER on IntegriCloud