]> granicus.if.org Git - clang/commitdiff
Correctly determine ranges for clang-format.
authorDaniel Jasper <djasper@google.com>
Tue, 14 May 2013 10:31:09 +0000 (10:31 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 14 May 2013 10:31:09 +0000 (10:31 +0000)
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
unittests/Format/FormatTest.cpp

index 2bab6e7353826ed21c0b6e6b3753a9f3a4ccad5a..5cb5fef9d545e95f1abc8779cc1daded86486fea 100644 (file)
@@ -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);
   }
 
index 19c5215a3fad63310b42a3c51b2b44d26b810fea..665f20322eb1bc3253fb6c025e1d457c68fb62f8 100644 (file)
@@ -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);