From: Daniel Jasper Date: Tue, 1 Dec 2015 13:28:53 +0000 (+0000) Subject: clang-format: Make it possible to turn off comment reflowing. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c5e6fe85ed11629997e92029812d48489bcfb1db;p=clang clang-format: Make it possible to turn off comment reflowing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@254414 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 02d4274a32..1e5393440e 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -466,9 +466,12 @@ struct FormatStyle { PAS_Middle }; - /// Pointer and reference alignment style. + /// \brief Pointer and reference alignment style. PointerAlignmentStyle PointerAlignment; + /// \brief If true, clang-format will attempt to re-flow comments. + bool ReflowComments; + /// \brief If true, clang-format will sort #includes. bool SortIncludes; diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index e41ae9769a..3b6e193837 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -1076,7 +1076,8 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, return 0; } } else if (Current.is(TT_BlockComment) && Current.isTrailingComment()) { - if (CommentPragmasRegex.match(Current.TokenText.substr(2))) + if (!Style.ReflowComments || + CommentPragmasRegex.match(Current.TokenText.substr(2))) return 0; Token.reset(new BreakableBlockComment( Current, State.Line->Level, StartColumn, Current.OriginalColumn, @@ -1084,7 +1085,8 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, } else if (Current.is(TT_LineComment) && (Current.Previous == nullptr || Current.Previous->isNot(TT_ImplicitStringLiteral))) { - if (CommentPragmasRegex.match(Current.TokenText.substr(2))) + if (!Style.ReflowComments || + CommentPragmasRegex.match(Current.TokenText.substr(2))) return 0; Token.reset(new BreakableLineComment(Current, State.Line->Level, StartColumn, /*InPPDirective=*/false, diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 0b6c9f5965..d66145fd88 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -284,8 +284,9 @@ template <> struct MappingTraits { IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", Style.PenaltyReturnTypeOnItsOwnLine); - IO.mapOptional("SortIncludes", Style.SortIncludes); IO.mapOptional("PointerAlignment", Style.PointerAlignment); + IO.mapOptional("ReflowComments", Style.ReflowComments); + IO.mapOptional("SortIncludes", Style.SortIncludes); IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); @@ -490,6 +491,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.Standard = FormatStyle::LS_Cpp11; LLVMStyle.UseTab = FormatStyle::UT_Never; + LLVMStyle.ReflowComments = true; LLVMStyle.SpacesInParentheses = false; LLVMStyle.SpacesInSquareBrackets = false; LLVMStyle.SpaceInEmptyParentheses = false; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 084ffc9442..94d456c059 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1197,6 +1197,13 @@ TEST_F(FormatTest, AlignsBlockComments) { "comment */")); } +TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { + FormatStyle Style = getLLVMStyleWithColumns(20); + Style.ReflowComments = false; + verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); + verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); +} + TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", @@ -9639,6 +9646,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); CHECK_PARSE_BOOL(Cpp11BracedListStyle); + CHECK_PARSE_BOOL(ReflowComments); CHECK_PARSE_BOOL(SortIncludes); CHECK_PARSE_BOOL(SpacesInParentheses); CHECK_PARSE_BOOL(SpacesInSquareBrackets);