r185908 - Format overloaded operators like other functions.

Daniel Jasper djasper at google.com
Tue Jul 9 00:43:56 PDT 2013


Author: djasper
Date: Tue Jul  9 02:43:55 2013
New Revision: 185908

URL: http://llvm.org/viewvc/llvm-project?rev=185908&view=rev
Log:
Format overloaded operators like other functions.

This fixes llvm.org/PR16328 (at least partially).

Before:
SomeLoooooooooooooooooooooooooooooogType operator<<(
    const SomeLooooooooogType &a, const SomeLooooooooogType &b);

After:
SomeLoooooooooooooooooooooooooooooogType
operator<<(const SomeLooooooooogType &a, const SomeLooooooooogType &b);

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=185908&r1=185907&r2=185908&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Jul  9 02:43:55 2013
@@ -536,7 +536,9 @@ private:
                  State.Stack.back().VariablePos != 0) {
         State.Column = State.Stack.back().VariablePos;
       } else if (Previous.ClosesTemplateDeclaration ||
-                 (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
+                 ((Current.Type == TT_StartOfName ||
+                   Current.is(tok::kw_operator)) &&
+                  State.ParenLevel == 0 &&
                   (!Style.IndentFunctionDeclarationAfterType ||
                    Line.StartsDefinition))) {
         State.Column = State.Stack.back().Indent;
@@ -1111,8 +1113,9 @@ private:
          (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
       return true;
 
-    if (Current.Type == TT_StartOfName && Line.MightBeFunctionDecl &&
-        State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
+    if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
+        Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter &&
+        State.ParenLevel == 0)
       return true;
     return false;
   }

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=185908&r1=185907&r2=185908&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Jul  9 02:43:55 2013
@@ -996,7 +996,7 @@ unsigned TokenAnnotator::splitPenalty(co
   if (Left.is(tok::comma))
     return 1;
 
-  if (Right.Type == TT_StartOfName) {
+  if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
     if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
       return 3;
     else if (Line.MightBeFunctionDecl && Right.BindingStrength == 1)
@@ -1203,7 +1203,7 @@ bool TokenAnnotator::spaceRequiredBefore
 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
                                     const FormatToken &Right) {
   const FormatToken &Left = *Right.Previous;
-  if (Right.Type == TT_StartOfName)
+  if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
     return true;
   if (Right.is(tok::colon) &&
       (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=185908&r1=185907&r2=185908&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jul  9 02:43:55 2013
@@ -2456,6 +2456,13 @@ TEST_F(FormatTest, BreaksFunctionDeclara
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
                "    bbbb bbbb);");
+
+  // Treat overloaded operators like other functions.
+  verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
+               "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
+  verifyGoogleFormat(
+      "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
+      "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
 }
 
 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {





More information about the cfe-commits mailing list