Author: rmarker Date: 2024-01-14T20:55:44+01:00 New Revision: 60a9874f54922a0fd9bfca9a028c32473f7ef85f
URL: https://github.com/llvm/llvm-project/commit/60a9874f54922a0fd9bfca9a028c32473f7ef85f DIFF: https://github.com/llvm/llvm-project/commit/60a9874f54922a0fd9bfca9a028c32473f7ef85f.diff LOG: [clang-format] Add PenaltyBreakScopeResolution option. (#78015) Resolves #78014 Added: Modified: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/ConfigParseTest.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index ac9a0b70ed5daa4..8bc13e45bf2f5fa 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4432,6 +4432,11 @@ the configuration (without a prefix: ``Auto``). **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` :ref:`¶ <PenaltyBreakOpenParenthesis>` The penalty for breaking after ``(``. +.. _PenaltyBreakScopeResolution: + +**PenaltyBreakScopeResolution** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ <PenaltyBreakScopeResolution>` + The penalty for breaking after ``::``. + .. _PenaltyBreakString: **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ <PenaltyBreakString>` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 3cbce1be1594376..dc8a6fe506bce65 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1131,6 +1131,7 @@ clang-format - Add ``BreakAdjacentStringLiterals`` option. - Add ``ObjCPropertyAttributeOrder`` which can be used to sort ObjC property attributes (like ``nonatomic, strong, nullable``). +- Add ``PenaltyBreakScopeResolution`` option. - Add ``.clang-format-ignore`` files. - Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 5ffd63ee73fc361..6fd7947bd217911 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3398,6 +3398,10 @@ struct FormatStyle { /// \version 14 unsigned PenaltyBreakOpenParenthesis; + /// The penalty for breaking after ``::``. + /// \version 18 + unsigned PenaltyBreakScopeResolution; + /// The penalty for each line break introduced inside a string literal. /// \version 3.7 unsigned PenaltyBreakString; @@ -4873,6 +4877,7 @@ struct FormatStyle { PenaltyBreakComment == R.PenaltyBreakComment && PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess && PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis && + PenaltyBreakScopeResolution == R.PenaltyBreakScopeResolution && PenaltyBreakString == R.PenaltyBreakString && PenaltyBreakTemplateDeclaration == R.PenaltyBreakTemplateDeclaration && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index ff5ed6c306f383b..d3e62c41098437c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1054,6 +1054,8 @@ template <> struct MappingTraits<FormatStyle> { Style.PenaltyBreakFirstLessLess); IO.mapOptional("PenaltyBreakOpenParenthesis", Style.PenaltyBreakOpenParenthesis); + IO.mapOptional("PenaltyBreakScopeResolution", + Style.PenaltyBreakScopeResolution); IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); IO.mapOptional("PenaltyBreakTemplateDeclaration", Style.PenaltyBreakTemplateDeclaration); @@ -1602,6 +1604,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; LLVMStyle.PenaltyBreakOpenParenthesis = 0; + LLVMStyle.PenaltyBreakScopeResolution = 500; LLVMStyle.PenaltyBreakTemplateDeclaration = prec::Relational; LLVMStyle.PenaltyIndentedWhitespace = 0; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 24ce18a64348c1d..01a599db2e83c3b 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3765,7 +3765,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, } if (Left.is(tok::coloncolon)) - return 500; + return Style.PenaltyBreakScopeResolution; if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || Right.is(tok::kw_operator)) { if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 18ecba270e3455a..6c0f9ddac5ac8fd 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -255,6 +255,8 @@ TEST(ConfigParseTest, ParsesConfiguration) { PenaltyBreakTemplateDeclaration, 1234u); CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 1234u); + CHECK_PARSE("PenaltyBreakScopeResolution: 1234", PenaltyBreakScopeResolution, + 1234u); CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", PenaltyReturnTypeOnItsOwnLine, 1234u); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 340ae39cb22b036..263bbed3db13ff0 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -21567,6 +21567,19 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { Style); } +TEST_F(FormatTest, BreakPenaltyScopeResolution) { + FormatStyle Style = getLLVMStyle(); + Style.ColumnLimit = 20; + Style.PenaltyExcessCharacter = 100; + verifyFormat("unsigned long\n" + "foo::bar();", + Style); + Style.PenaltyBreakScopeResolution = 10; + verifyFormat("unsigned long foo::\n" + " bar();", + Style); +} + TEST_F(FormatTest, WorksFor8bitEncodings) { // FIXME: unstable test case EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits