From 893ea8d0a6420591d966fa0e7135e510b1523b57 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 31 Jul 2013 23:55:15 +0000 Subject: [PATCH] clang-format: Make alignment of trailing comments optional .. .. in order to support WebKit style properly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187549 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 4 ++++ lib/Format/Format.cpp | 4 ++++ lib/Format/WhitespaceManager.cpp | 6 +++--- unittests/Format/FormatTest.cpp | 7 ++++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 7d5d66b7da..9eb7afba2c 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -131,6 +131,9 @@ struct FormatStyle { /// Foo instead of Foo. bool ObjCSpaceBeforeProtocolList; + /// \brief If \c true, aligns trailing comments. + bool AlignTrailingComments; + /// \brief If \c true, aligns escaped newlines as far left as possible. /// Otherwise puts them into the right-most column. bool AlignEscapedNewlinesLeft; @@ -188,6 +191,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft && + AlignTrailingComments == R.AlignTrailingComments && AllowAllParametersOfDeclarationOnNextLine == R.AllowAllParametersOfDeclarationOnNextLine && AllowShortIfStatementsOnASingleLine == diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 65b1a265e5..290f0597f5 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -91,6 +91,7 @@ template <> struct MappingTraits { IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); + IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", Style.AllowAllParametersOfDeclarationOnNextLine); IO.mapOptional("AllowShortIfStatementsOnASingleLine", @@ -153,6 +154,7 @@ FormatStyle getLLVMStyle() { FormatStyle LLVMStyle; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignEscapedNewlinesLeft = false; + LLVMStyle.AlignTrailingComments = true; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.AllowShortLoopsOnASingleLine = false; @@ -188,6 +190,7 @@ FormatStyle getGoogleStyle() { FormatStyle GoogleStyle; GoogleStyle.AccessModifierOffset = -1; GoogleStyle.AlignEscapedNewlinesLeft = true; + GoogleStyle.AlignTrailingComments = true; GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; GoogleStyle.AllowShortIfStatementsOnASingleLine = true; GoogleStyle.AllowShortLoopsOnASingleLine = true; @@ -245,6 +248,7 @@ FormatStyle getMozillaStyle() { FormatStyle getWebKitStyle() { FormatStyle Style = getLLVMStyle(); Style.AccessModifierOffset = -4; + Style.AlignTrailingComments = false; Style.BreakBeforeBinaryOperators = true; Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; Style.BreakConstructorInitializersBeforeComma = true; diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 3805278de7..94aca027b8 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -124,6 +124,8 @@ void WhitespaceManager::alignTrailingComments() { unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; Newlines += Changes[i].NewlinesBefore; if (Changes[i].IsTrailingComment) { + // If this comment follows an } in column 0, it probably documents the + // closing of a namespace and we don't want to align it. bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 && Changes[i - 1].Kind == tok::r_brace && Changes[i - 1].StartOfTokenColumn == 0; @@ -140,9 +142,7 @@ void WhitespaceManager::alignTrailingComments() { Changes[i + 1].OriginalWhitespaceRange.getEnd())) && // Which is not a comment itself. Changes[i + 1].Kind != tok::comment; - if (FollowsRBraceInColumn0) { - // If this comment follows an } in column 0, it probably documents the - // closing of a namespace and we don't want to align it. + if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) { alignTrailingComments(StartOfSequence, i, MinColumn); MinColumn = ChangeMinColumn; MaxColumn = ChangeMinColumn; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 61d0614595..d5b9f27699 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5472,6 +5472,7 @@ TEST_F(FormatTest, ParsesConfiguration) { EXPECT_FALSE(Style.FIELD); CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); + CHECK_PARSE_BOOL(AlignTrailingComments); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); @@ -5679,9 +5680,9 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { Style); // Do not align comments. - // FIXME: Implement option to suppress comment alignment. - // verifyFormat("int a; // Do not\n" - // "double b; // align comments."); + verifyFormat("int a; // Do not\n" + "double b; // align comments.", + Style); // Accept input's line breaks. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" -- 2.40.0