diff options
author | Daniel Jasper <djasper@google.com> | 2013-03-22 16:25:51 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-03-22 16:25:51 +0000 |
commit | c363dbb204b6c77b67dfed030436643947b37cbd (patch) | |
tree | aab1eaab38760ea4f89372155d4319a3c614eb24 | |
parent | 92e44d911c748f2ef0d578bbf7b0703fb2ed4d9c (diff) |
Align comments to surrounding unformatted comments.
Before:
int a; // not formatted
// formatting this line only
After:
int a; // not formatted
// formatting this line only
This makes clang-format stable independent of whether the whole
file or single lines are formatted in most cases.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177739 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/Format.cpp | 21 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 15 |
2 files changed, 33 insertions, 3 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 40e980f67d..4e2298071c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -141,6 +141,7 @@ public: Comment.MinColumn = NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces; Comment.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength; + Comment.Untouchable = false; Comments.push_back(Comment); return; } @@ -199,6 +200,14 @@ public: return Replaces; } + void addUntouchableComment(unsigned Column) { + StoredComment Comment; + Comment.MinColumn = Column; + Comment.MaxColumn = Column; + Comment.Untouchable = true; + Comments.push_back(Comment); + } + private: /// \brief Finds a common prefix of lines of a block comment to properly /// indent (and possibly decorate with '*'s) added lines. @@ -350,6 +359,7 @@ private: unsigned MaxColumn; unsigned NewLines; unsigned Spaces; + bool Untouchable; }; SmallVector<StoredComment, 16> Comments; typedef SmallVector<StoredComment, 16>::iterator comment_iterator; @@ -377,9 +387,11 @@ private: /// \brief Put all the comments between \p I and \p E into \p Column. void alignComments(comment_iterator I, comment_iterator E, unsigned Column) { while (I != E) { - unsigned Spaces = I->Spaces + Column - I->MinColumn; - storeReplacement( - I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' ')); + if (!I->Untouchable) { + unsigned Spaces = I->Spaces + Column - I->MinColumn; + storeReplacement( + I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' ')); + } ++I; } } @@ -1345,6 +1357,9 @@ public: SourceMgr.getSpellingColumnNumber(LastLoc) + Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1; PreviousLineWasTouched = false; + if (TheLine.Last->is(tok::comment)) + Whitespaces.addUntouchableComment(SourceMgr.getSpellingColumnNumber( + TheLine.Last->FormatTok.Tok.getLocation()) - 1); } } return Whitespaces.generateReplacements(); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 051deebc09..0840302754 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -594,6 +594,21 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); } +TEST_F(FormatTest, CanFormatCommentsLocally) { + EXPECT_EQ("int a; // comment\n" + "int b; // comment", + format("int a; // comment\n" + "int b; // comment", + 0, 0, getLLVMStyle())); + EXPECT_EQ("int a; // comment\n" + " // line 2\n" + "int b;", + format("int a; // comment\n" + " // line 2\n" + "int b;", + 28, 0, getLLVMStyle())); +} + TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { EXPECT_EQ("// comment", format("// comment ")); EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", |