r176356 - Correctly format arrays of pointers and function types.
Daniel Jasper
djasper at google.com
Fri Mar 1 09:13:29 PST 2013
Author: djasper
Date: Fri Mar 1 11:13:29 2013
New Revision: 176356
URL: http://llvm.org/viewvc/llvm-project?rev=176356&view=rev
Log:
Correctly format arrays of pointers and function types.
Before:
void f(Type(*parameter)[10]) {}
int(*func)(void *);
After:
void f(Type (*parameter)[10]) {}
int (*func)(void *);
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=176356&r1=176355&r2=176356&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Mar 1 11:13:29 2013
@@ -645,6 +645,9 @@ private:
if (NextToken == NULL)
return TT_Unknown;
+ if (PrevToken->is(tok::l_paren) && !IsExpression)
+ return TT_PointerOrReference;
+
if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) ||
PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) ||
PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) ||
@@ -1041,6 +1044,11 @@ bool TokenAnnotator::spaceRequiredBefore
if (Tok.is(tok::colon))
return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() &&
Tok.Type != TT_ObjCMethodExpr;
+ if (Tok.is(tok::l_paren) && !Tok.Children.empty() &&
+ Tok.Children[0].Type == TT_PointerOrReference &&
+ !Tok.Children[0].Children.empty() &&
+ Tok.Children[0].Children[0].isNot(tok::r_paren))
+ return true;
if (Tok.Parent->Type == TT_UnaryOperator || Tok.Parent->Type == TT_CastRParen)
return false;
if (Tok.Type == TT_UnaryOperator)
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176356&r1=176355&r2=176356&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 1 11:13:29 2013
@@ -1812,7 +1812,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStar
verifyFormat("int main(int argc, char **argv) {}");
verifyFormat("Test::Test(int b) : a(b * b) {}");
verifyIndependentOfContext("f(a, *a);");
- verifyIndependentOfContext("f(*a);");
+ verifyFormat("void g() { f(*a); }");
verifyIndependentOfContext("int a = b * 10;");
verifyIndependentOfContext("int a = 10 * b;");
verifyIndependentOfContext("int a = b * c;");
@@ -1845,6 +1845,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStar
verifyIndependentOfContext("return sizeof(int **);");
verifyIndependentOfContext("return sizeof(int ******);");
verifyIndependentOfContext("return (int **&)a;");
+ verifyFormat("void f(Type (*parameter)[10]) {}");
verifyGoogleFormat("return sizeof(int**);");
verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
@@ -1884,7 +1885,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStar
verifyIndependentOfContext("a = &(x + y);");
verifyIndependentOfContext("*(x + y).call();");
verifyIndependentOfContext("&(x + y)->call();");
- verifyIndependentOfContext("&(*I).first");
+ verifyFormat("void f() { &(*I).first; }");
verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
verifyFormat(
@@ -1909,7 +1910,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStar
verifyIndependentOfContext("A = new SomeType *[Length]();");
verifyGoogleFormat("A = new SomeType* [Length]();");
+}
+TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
EXPECT_EQ("int *a;\n"
"int *a;\n"
"int *a;",
@@ -1977,12 +1980,13 @@ TEST_F(FormatTest, FormatsCasts) {
}
TEST_F(FormatTest, FormatsFunctionTypes) {
- // FIXME: Determine the cases that need a space after the return type and fix.
verifyFormat("A<bool()> a;");
verifyFormat("A<SomeType()> a;");
verifyFormat("A<void(*)(int, std::string)> a;");
- verifyFormat("int(*func)(void *);");
+ // FIXME: Inconsistent.
+ verifyFormat("int (*func)(void *);");
+ verifyFormat("void f() { int(*func)(void *); }");
}
TEST_F(FormatTest, BreaksLongDeclarations) {
More information about the cfe-commits
mailing list