From: Daniel Jasper Date: Mon, 18 Feb 2013 13:08:03 +0000 (+0000) Subject: Reformat lines if they were "moved around". X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=55d7ba6b661974ae69b8a3012e5bb02d03b6735c;p=platform%2Fupstream%2Fllvm.git Reformat lines if they were "moved around". An unwrapped line can get moved around if there is no newline before it and the previous line was formatted. Example: template // Cursor is on this line when hitting "format" T *getFETokenInfo() const { return static_cast(FETokenInfo); } "return .." is the second unwrapped line in this scenario. I does not touch any reformatted region. Thus, the result of formatting is: template T *getFETokenInfo() const { return static_cast(FETokenInfo); } After second format (and arguably desired end-result): template T *getFETokenInfo() const { return static_cast(FETokenInfo); } This fixes: llvm.org/PR15060. llvm-svn: 175440 --- diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index fa62752..e950fe6 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -999,7 +999,9 @@ public: while (IndentForLevel.size() <= TheLine.Level) IndentForLevel.push_back(-1); IndentForLevel.resize(TheLine.Level + 1); - if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) { + bool WasMoved = + PreviousLineWasTouched && TheLine.First.FormatTok.NewlinesBefore == 0; + if (TheLine.Type != LT_Invalid && (WasMoved || touchesRanges(TheLine))) { unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); unsigned Indent = LevelIndent; if (static_cast(Indent) + Offset >= 0) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c63d756..90196af 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -168,6 +168,19 @@ TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); } +TEST_F(FormatTest, ReformatsMovedLines) { + EXPECT_EQ( + "template T *getFETokenInfo() const {\n" + " return static_cast(FETokenInfo);\n" + "}\n" + " int a; // <- Should not be formatted", + format( + "template\n" + "T *getFETokenInfo() const { return static_cast(FETokenInfo); }\n" + " int a; // <- Should not be formatted", + 9, 5, getLLVMStyle())); +} + //===----------------------------------------------------------------------===// // Tests for control statements. //===----------------------------------------------------------------------===//