From 6561f6a13b79ed752748ede590792191edf78ce8 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 9 Jul 2013 07:43:55 +0000 Subject: [PATCH] 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); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185908 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 9 ++++++--- lib/Format/TokenAnnotator.cpp | 4 ++-- unittests/Format/FormatTest.cpp | 7 +++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index e4004a649b..23f6f5b3e8 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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; } diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 3c2ae52c99..c7034734d4 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -996,7 +996,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 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(const AnnotatedLine &Line, 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)) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 2afd91e482..6c70ebb11a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2456,6 +2456,13 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { " 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) { -- 2.40.0