From 84f5ddfacc59c5d4878cd47b7794e53dc8e2e9f9 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 14 May 2013 10:31:09 +0000 Subject: [PATCH] Correctly determine ranges for clang-format. We have been assuming that CharSourceRange::getTokenRange() by itself expands a range until the end of a token, but in fact it only sets IsTokenRange to true. Thus, we have so far only considered the first character of the last token to belong to an unwrapped line. This did not really manifest in symptoms as all edit integrations expand ranges to fully lines. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181778 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 4 ++-- unittests/Format/FormatTest.cpp | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 2bab6e7353..5cb5fef9d5 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1473,9 +1473,9 @@ private: bool touchesLine(const AnnotatedLine &TheLine) { const FormatToken *First = &TheLine.First.FormatTok; const FormatToken *Last = &TheLine.Last->FormatTok; - CharSourceRange LineRange = CharSourceRange::getTokenRange( + CharSourceRange LineRange = CharSourceRange::getCharRange( First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset), - Last->Tok.getLocation()); + Last->Tok.getLocation().getLocWithOffset(Last->TokenLength - 1)); return touchesRanges(LineRange); } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 19c5215a3f..665f20322e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -218,7 +218,7 @@ TEST_F(FormatTest, ReformatsMovedLines) { // Tests for control statements. //===----------------------------------------------------------------------===// -TEST_F(FormatTest, FormatIfWithoutCompountStatement) { +TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { verifyFormat("if (true)\n f();\ng();"); verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); @@ -246,6 +246,8 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) { AllowsMergedIf); EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); + EXPECT_EQ("if (a) return; // comment", + format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); AllowsMergedIf.ColumnLimit = 14; verifyFormat("if (a) return;", AllowsMergedIf); -- 2.50.1