summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-01-07 14:00:11 +0000
committerDaniel Jasper <djasper@google.com>2015-01-07 14:00:11 +0000
commit495321047851f182d7d97e62e07bb63932fe2185 (patch)
tree0dad028af055e6c29a03ff868f8c198d120f7103
parentfcf0cba88cf6b466264f1644536e9491920ae295 (diff)
downloadbcm5719-llvm-495321047851f182d7d97e62e07bb63932fe2185.tar.gz
bcm5719-llvm-495321047851f182d7d97e62e07bb63932fe2185.zip
clang-format: Understand single-line comments at the end of blocks.
This prevents clang-format from moving/aligning the comment in the snippet: void f() { int i; // some comment // some unrelated comment } llvm-svn: 225352
-rw-r--r--clang/lib/Format/WhitespaceManager.cpp10
-rw-r--r--clang/unittests/Format/FormatTest.cpp8
2 files changed, 14 insertions, 4 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index a2f599142df..bf1207e59c9 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -163,15 +163,17 @@ void WhitespaceManager::alignTrailingComments() {
Changes[i - 1].StartOfTokenColumn == 0;
bool WasAlignedWithStartOfNextLine = false;
if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
+ unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
+ Changes[i].OriginalWhitespaceRange.getEnd());
for (unsigned j = i + 1; j != e; ++j) {
if (Changes[j].Kind != tok::comment) { // Skip over comments.
+ unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
+ Changes[j].OriginalWhitespaceRange.getEnd());
// The start of the next token was previously aligned with the
// start of this comment.
WasAlignedWithStartOfNextLine =
- (SourceMgr.getSpellingColumnNumber(
- Changes[i].OriginalWhitespaceRange.getEnd()) ==
- SourceMgr.getSpellingColumnNumber(
- Changes[j].OriginalWhitespaceRange.getEnd()));
+ CommentColumn == NextColumn ||
+ CommentColumn == NextColumn + Style.IndentWidth;
break;
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a0f450338b1..fc49bde7140 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -998,6 +998,14 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
" // first\n"
"// at start\n"
"otherLine();"));
+ EXPECT_EQ("void f() {\n"
+ " lineWith(); // comment\n"
+ " // at start\n"
+ "}",
+ format("void f() {\n"
+ " lineWith(); // comment\n"
+ " // at start\n"
+ "}"));
verifyFormat(
"#define A \\\n"
OpenPOWER on IntegriCloud