diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index a963c6369aa9cebfda3ed6fbea693436ae98a691..2405f8319c6106c72fcab7314d45a0a40578b5a7 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -1133,6 +1133,9 @@ struct FormatStyle { /// ``Foo <Protocol>`` instead of ``Foo<Protocol>``. bool ObjCSpaceBeforeProtocolList; + /// \brief The penalty for breaking around an assignment operator. + unsigned PenaltyBreakAssignment; + /// \brief The penalty for breaking a function call after ``call(``. unsigned PenaltyBreakBeforeFirstCallParameter; @@ -1420,6 +1423,8 @@ struct FormatStyle { ObjCBlockIndentWidth == R.ObjCBlockIndentWidth && ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty && ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList && + PenaltyBreakAssignment == + R.PenaltyBreakAssignment && PenaltyBreakBeforeFirstCallParameter == R.PenaltyBreakBeforeFirstCallParameter && PenaltyBreakComment == R.PenaltyBreakComment && diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index c8677e805179ca3975ae25167de8beeaa389c0c5..ac83379e6b15df951f45cc06a9db46b8af607a2b 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -343,6 +343,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); IO.mapOptional("ObjCSpaceBeforeProtocolList", Style.ObjCSpaceBeforeProtocolList); + IO.mapOptional("PenaltyBreakAssignment", + Style.PenaltyBreakAssignment); IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", Style.PenaltyBreakBeforeFirstCallParameter); IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); @@ -582,6 +584,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpacesInAngles = false; + LLVMStyle.PenaltyBreakAssignment = prec::Assignment; LLVMStyle.PenaltyBreakComment = 300; LLVMStyle.PenaltyBreakFirstLessLess = 120; LLVMStyle.PenaltyBreakString = 1000; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 50583a95055da7aa49a0a8a75605eaa0c6604f6b..69e2d43014ef4a5c5da4e35ec051076feaf2869a 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2093,9 +2093,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Left.is(TT_ConditionalExpr)) return prec::Conditional; prec::Level Level = Left.getPrecedence(); - if (Level != prec::Unknown) - return Level; - Level = Right.getPrecedence(); + if (Level == prec::Unknown) + Level = Right.getPrecedence(); + if (Level == prec::Assignment) + return Style.PenaltyBreakAssignment; if (Level != prec::Unknown) return Level; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 076041406c87ac83117dc001c056ca655788ec8c..0935c1bed204cc381557a19b854762a63988a1f8 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3457,6 +3457,18 @@ TEST_F(FormatTest, BreaksAfterAssignments) { " 1;"); } +TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", + Style); + + Style.PenaltyBreakAssignment = 20; + verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" + " cccccccccccccccccccccccccc;", + Style); +} + TEST_F(FormatTest, AlignsAfterAssignments) { verifyFormat( "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" @@ -8802,6 +8814,8 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); + CHECK_PARSE("PenaltyBreakAssignment: 1234", + PenaltyBreakAssignment, 1234u); CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", PenaltyBreakBeforeFirstCallParameter, 1234u); CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);