From 1bee0738b67b784f08d5e2f8351920260c9cfb1d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 23 May 2013 18:05:18 +0000 Subject: [PATCH] Improve formatting of braced lists. Before: vector v{ -1}; After: vector v{-1}; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182597 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 5 ++--- lib/Format/TokenAnnotator.cpp | 7 +++---- unittests/Format/FormatTest.cpp | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 1cda3c711c..4193266629 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -100,8 +100,7 @@ template <> struct MappingTraits { IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); IO.mapOptional("SpacesBeforeTrailingComments", Style.SpacesBeforeTrailingComments); - IO.mapOptional("SpacesInBracedLists", - Style.SpacesInBracedLists); + IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists); IO.mapOptional("Standard", Style.Standard); IO.mapOptional("IndentWidth", Style.IndentWidth); IO.mapOptional("UseTab", Style.UseTab); @@ -146,7 +145,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.AlignEscapedNewlinesLeft = true; GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; GoogleStyle.AllowShortIfStatementsOnASingleLine = true; - GoogleStyle.AllowShortLoopsOnASingleLine= true; + GoogleStyle.AllowShortLoopsOnASingleLine = true; GoogleStyle.BinPackParameters = true; GoogleStyle.ColumnLimit = 80; GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 3f3374435c..4d346bd051 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1092,6 +1092,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, return false; // No spaces in "{}". if (Left.is(tok::l_brace) || Right.is(tok::r_brace)) return Style.SpacesInBracedLists; + if (Right.Type == TT_UnaryOperator) + return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && + (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr); if (Left.is(tok::identifier) && Right.is(tok::l_brace) && Right.getNextNoneComment()) return false; @@ -1131,10 +1134,6 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Tok.getNextNoneComment() != NULL && Tok.Type != TT_ObjCMethodExpr; if (Tok.Parent->Type == TT_UnaryOperator || Tok.Parent->Type == TT_CastRParen) return false; - if (Tok.Type == TT_UnaryOperator) - return !Tok.Parent->isOneOf(tok::l_paren, tok::l_square, tok::at) && - (Tok.Parent->isNot(tok::colon) || - Tok.Parent->Type != TT_ObjCMethodExpr); if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) { return Tok.Type == TT_TemplateCloser && Tok.Parent->Type == TT_TemplateCloser && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e51a8fe8cd..e3d5c25002 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3137,7 +3137,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { verifyFormat("vector x{1, 2, 3, 4};", NoSpaces); verifyFormat("vector x{{}, {}, {}, {}};", NoSpaces); verifyFormat("f({1, 2});", NoSpaces); - verifyFormat("auto v = Foo{1};", NoSpaces); + verifyFormat("auto v = Foo{-1};", NoSpaces); verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces); } -- 2.40.0