From: Daniel Jasper Date: Wed, 2 Jan 2013 08:57:10 +0000 (+0000) Subject: Understand * and & in ternary expressions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d334407e68e45e3e7baa0cd140d4fd75e7483d6;p=clang Understand * and & in ternary expressions. Before: "int a = b ? *c : * d;" After: "int a = b ? *c : *d; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171358 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a6c11dcd7a..b304e2bf3d 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -806,6 +806,7 @@ private: if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || Line.Tokens[Index - 1].Tok.is(tok::comma) || Line.Tokens[Index - 1].Tok.is(tok::kw_return) || + Line.Tokens[Index - 1].Tok.is(tok::colon) || Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) return TokenAnnotation::TT_UnaryOperator; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 99204f56f5..5dcd2d1bad 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -650,7 +650,7 @@ TEST_F(FormatTest, UndestandsOverloadedOperators) { verifyFormat("void operator delete[](void *ptr);"); } -TEST_F(FormatTest, UnderstandsUsesOfStar) { +TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("int *f(int *a) {\n}"); verifyFormat("f(a, *a);"); verifyFormat("f(*a);"); @@ -668,6 +668,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStar) { verifyFormat("return 10 * b;"); verifyFormat("return *b * *c;"); verifyFormat("return a & ~b;"); + verifyFormat("f(b ? *c : *d);"); + verifyFormat("int a = b ? *c : *d;"); // FIXME: Is this desired for LLVM? Fix if not. verifyFormat("A a;"); @@ -680,6 +682,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStar) { verifyGoogleFormat("A a;"); verifyGoogleFormat("A a;"); verifyGoogleFormat("A a;"); + verifyGoogleFormat("f(b ? *c : *d);"); + verifyGoogleFormat("int a = b ? *c : *d;"); } TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {