diff options
author | rmarker <37921131+rmarker@users.noreply.github.com> | 2024-01-15 06:25:44 +1030 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-14 20:55:44 +0100 |
commit | 60a9874f54922a0fd9bfca9a028c32473f7ef85f (patch) | |
tree | fb675245fae942260694fe2162d944c859560b19 /clang | |
parent | 7f1d757fb40f06cc1c6b134d770987b340286996 (diff) | |
download | llvm-60a9874f54922a0fd9bfca9a028c32473f7ef85f.zip llvm-60a9874f54922a0fd9bfca9a028c32473f7ef85f.tar.gz llvm-60a9874f54922a0fd9bfca9a028c32473f7ef85f.tar.bz2 |
[clang-format] Add PenaltyBreakScopeResolution option. (#78015)
Resolves #78014
Diffstat (limited to 'clang')
-rw-r--r-- | clang/docs/ClangFormatStyleOptions.rst | 5 | ||||
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 1 | ||||
-rw-r--r-- | clang/include/clang/Format/Format.h | 5 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/ConfigParseTest.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 13 |
7 files changed, 30 insertions, 1 deletions
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index ac9a0b7..8bc13e4 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 3cbce1be..dc8a6fe 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 5ffd63e..6fd7947 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 ff5ed6c..d3e62c4 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 24ce18a..01a599d 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 18ecba2..6c0f9dd 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 340ae39..263bbed 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" |