]> granicus.if.org Git - clang/commitdiff
Improve formatting of braced lists.
authorDaniel Jasper <djasper@google.com>
Thu, 23 May 2013 18:05:18 +0000 (18:05 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 23 May 2013 18:05:18 +0000 (18:05 +0000)
Before: vector<int> v{ -1};
After:  vector<int> v{-1};

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182597 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 1cda3c711cac0c0c188916177353e7e8ec1b10c2..41932666293a2cc8671e1e629b62512ce6583e9d 100644 (file)
@@ -100,8 +100,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
     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;
index 3f3374435c17e13449034018243b5bd1e4f9ffd3..4d346bd05130cf82f0384748ac685d8b4612f8f2 100644 (file)
@@ -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 &&
index e51a8fe8cd3f7a4df3adfbb74b027ee077a274a9..e3d5c25002e06067e13c24e01a78d7fd8ecaeaac 100644 (file)
@@ -3137,7 +3137,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
     verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
     verifyFormat("vector<T> 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);
 }