summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2013-05-23 11:42:52 +0000
committerManuel Klimek <klimek@google.com>2013-05-23 11:42:52 +0000
commitc573080d450a5d96776cb6bdb039ba4b1a1fd9b8 (patch)
treee4a8f520d999a949b5b4d1ffe9fd52e5010d411a
parent5415c9c352716b8f00cb524114b0a583c85aab1f (diff)
downloadbcm5719-llvm-c573080d450a5d96776cb6bdb039ba4b1a1fd9b8.tar.gz
bcm5719-llvm-c573080d450a5d96776cb6bdb039ba4b1a1fd9b8.zip
Stop aligning trailing comments which are aligned with the next line.
Previously we would align: f(); // comment // other comment g(); Even if // other comment was at the start of the line. Now we do not align trailing comments if they have been already aligned correctly with the next line. Thus, f(); // comment // other comment g(); will not be changed, while: f(); // comment // other commment g(); will lead to the two trailing comments being aligned. llvm-svn: 182577
-rw-r--r--clang/lib/Format/WhitespaceManager.cpp16
-rw-r--r--clang/unittests/Format/FormatTest.cpp13
2 files changed, 28 insertions, 1 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index a75514ffe75..d4be86e5861 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -142,12 +142,26 @@ void WhitespaceManager::alignTrailingComments() {
unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
Newlines += Changes[i].NewlinesBefore;
if (Changes[i].IsTrailingComment) {
+ bool WasAlignedWithStartOfNextLine =
+ // A comment on its own line.
+ Changes[i].NewlinesBefore == 1 &&
+ // Not the last line.
+ i + 1 != e &&
+ // The start of the next token was previously aligned with
+ // the start of this comment.
+ (SourceMgr.getSpellingColumnNumber(
+ Changes[i].OriginalWhitespaceRange.getEnd()) ==
+ SourceMgr.getSpellingColumnNumber(
+ Changes[i + 1].OriginalWhitespaceRange.getEnd())) &&
+ // Which is not a comment itself.
+ Changes[i + 1].Kind != tok::comment;
if (BreakBeforeNext || Newlines > 1 ||
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
// Break the comment sequence if the previous line did not end
// in a trailing comment.
(Changes[i].NewlinesBefore == 1 && i > 0 &&
- !Changes[i - 1].IsTrailingComment)) {
+ !Changes[i - 1].IsTrailingComment) ||
+ WasAlignedWithStartOfNextLine) {
alignTrailingComments(StartOfSequence, i, MinColumn);
MinColumn = ChangeMinColumn;
MaxColumn = ChangeMaxColumn;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4919a142a83..e51a8fe8cd3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -637,6 +637,19 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
"// test\n"
"int a;\n"
"});"));
+
+ EXPECT_EQ("lineWith(); // comment\n"
+ "// at start\n"
+ "otherLine();",
+ format("lineWith(); // comment\n"
+ "// at start\n"
+ "otherLine();"));
+ EXPECT_EQ("lineWith(); // comment\n"
+ " // at start\n"
+ "otherLine();",
+ format("lineWith(); // comment\n"
+ " // at start\n"
+ "otherLine();"));
}
TEST_F(FormatTest, CanFormatCommentsLocally) {
OpenPOWER on IntegriCloud