From: Daniel Jasper Date: Wed, 29 Oct 2014 23:40:50 +0000 (+0000) Subject: clang-format: Format line if invoked on the trailing newline. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a02d78fcea0fc03629a7e228245abf929f2ca0c;p=clang clang-format: Format line if invoked on the trailing newline. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220883 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index bd0dc6eb73..4f3b86a4a7 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1962,8 +1962,7 @@ private: if (!IncludeLeadingNewlines) Start = Start.getLocWithOffset(First.LastNewlineOffset); SourceLocation End = Last.getStartOfNonWhitespace(); - if (Last.TokenText.size() > 0) - End = End.getLocWithOffset(Last.TokenText.size() - 1); + End = End.getLocWithOffset(Last.TokenText.size()); CharSourceRange Range = CharSourceRange::getCharRange(Start, End); return affectsCharSourceRange(Range); } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e7c9402837..83b74216fb 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -159,9 +159,21 @@ TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 25, 0, getLLVMStyleWithColumns(12))); } +TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) { + EXPECT_EQ("int b;\n\nint a;", + format("int b;\n\nint a;", 8, 0, getLLVMStyle())); + EXPECT_EQ("int b;\n\nint a;", + format("int b;\n\nint a;", 7, 0, getLLVMStyle())); + + // This might not strictly be correct, but is likely good in all practical + // cases. + EXPECT_EQ("int b;\nint a;", + format("int b;int a;", 7, 0, getLLVMStyle())); +} + TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { EXPECT_EQ("int a;\n\n int b;", - format("int a;\n \n\n int b;", 7, 0, getLLVMStyle())); + format("int a;\n \n\n int b;", 8, 0, getLLVMStyle())); EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); }