r188891 - clang-format: Indent relative to unary operators.

Daniel Jasper djasper at google.com
Wed Aug 21 01:39:02 PDT 2013


Author: djasper
Date: Wed Aug 21 03:39:01 2013
New Revision: 188891

URL: http://llvm.org/viewvc/llvm-project?rev=188891&view=rev
Log:
clang-format: Indent relative to unary operators.

Before:
  if (!aaaaaaaaaa(  // break
          aaaaa)) {
  }

After:
  if (!aaaaaaaaaa(  // break
           aaaaa)) {
  }

Also cleaned up formatting using clang-format.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=188891&r1=188890&r2=188891&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed Aug 21 03:39:01 2013
@@ -365,11 +365,13 @@ unsigned ContinuationIndenter::addTokenT
       State.Stack.back().LastSpace = State.Column;
     else if ((Previous.Type == TT_BinaryOperator ||
               Previous.Type == TT_ConditionalExpr ||
+              Previous.Type == TT_UnaryOperator ||
               Previous.Type == TT_CtorInitializerColon) &&
              !(Previous.getPrecedence() == prec::Assignment &&
                Current.FakeLParens.empty()))
       // Always indent relative to the RHS of the expression unless this is a
-      // simple assignment without binary expression on the RHS.
+      // simple assignment without binary expression on the RHS. Also indent
+      // relative to unary operators and the colons of constructor initializers.
       State.Stack.back().LastSpace = State.Column;
     else if (Previous.Type == TT_InheritanceColon)
       State.Stack.back().Indent = State.Column;

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=188891&r1=188890&r2=188891&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Aug 21 03:39:01 2013
@@ -138,8 +138,7 @@ template <> struct MappingTraits<clang::
     IO.mapOptional("IndentFunctionDeclarationAfterType",
                    Style.IndentFunctionDeclarationAfterType);
     IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
-    IO.mapOptional("SpaceInEmptyParentheses",
-                   Style.SpaceInEmptyParentheses);
+    IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
     IO.mapOptional("SpacesInCStyleCastParentheses",
                    Style.SpacesInCStyleCastParentheses);
     IO.mapOptional("SpaceAfterControlStatementKeyword",
@@ -318,8 +317,7 @@ namespace {
 
 class NoColumnLimitFormatter {
 public:
-  NoColumnLimitFormatter(ContinuationIndenter *Indenter)
-      : Indenter(Indenter) {}
+  NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
 
   /// \brief Formats the line starting at \p State, simply keeping all of the
   /// input's line breaking decisions.
@@ -332,6 +330,7 @@ public:
       Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
     }
   }
+
 private:
   ContinuationIndenter *Indenter;
 };

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=188891&r1=188890&r2=188891&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Aug 21 03:39:01 2013
@@ -1123,8 +1123,8 @@ bool TokenAnnotator::spaceRequiredBetwee
   if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
     return Style.SpaceInEmptyParentheses;
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
-    return Right.Type == TT_CastRParen ||
-           (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen)
+    return (Right.Type == TT_CastRParen ||
+            (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
                ? Style.SpacesInCStyleCastParentheses
                : Style.SpacesInParentheses;
   if (Right.isOneOf(tok::semi, tok::comma))
@@ -1181,11 +1181,11 @@ bool TokenAnnotator::spaceRequiredBetwee
         Left.MatchingParen->Previous->is(tok::kw___attribute))
       return true;
     return Line.Type == LT_ObjCDecl ||
-        Left.isOneOf(tok::kw_return, tok::kw_new,
-                     tok::kw_delete, tok::semi) ||
-        (Style.SpaceAfterControlStatementKeyword &&
-         Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
-                      tok::kw_catch));
+           Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
+                        tok::semi) ||
+           (Style.SpaceAfterControlStatementKeyword &&
+            Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
+                         tok::kw_catch));
   }
   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
     return false;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=188891&r1=188890&r2=188891&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Aug 21 03:39:01 2013
@@ -3528,6 +3528,14 @@ TEST_F(FormatTest, UnderstandsUnaryOpera
   verifyFormat("int a = i /* confusing comment */++;");
 }
 
+TEST_F(FormatTest, IndentsRelativeToUnaryOperators) {
+  verifyFormat("if (!aaaaaaaaaa( // break\n"
+               "         aaaaa)) {\n"
+               "}");
+  verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
+               "                aaaaa));");
+}
+
 TEST_F(FormatTest, UndestandsOverloadedOperators) {
   verifyFormat("bool operator<();");
   verifyFormat("bool operator>();");





More information about the cfe-commits mailing list