r175440 - Reformat lines if they were "moved around".
Daniel Jasper
djasper at google.com
Mon Feb 18 05:08:03 PST 2013
Author: djasper
Date: Mon Feb 18 07:08:03 2013
New Revision: 175440
URL: http://llvm.org/viewvc/llvm-project?rev=175440&view=rev
Log:
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<typename T> // Cursor is on this line when hitting "format"
T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
"return .." is the second unwrapped line in this scenario. I does not
touch any reformatted region. Thus, the result of formatting is:
template <typename T> T *getFETokenInfo() const { return static_cast<T *>(FETokenInfo); }
After second format (and arguably desired end-result):
template <typename T> T *getFETokenInfo() const {
return static_cast<T *>(FETokenInfo);
}
This fixes: llvm.org/PR15060.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=175440&r1=175439&r2=175440&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Feb 18 07:08:03 2013
@@ -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<int>(Indent) + Offset >= 0)
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175440&r1=175439&r2=175440&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb 18 07:08:03 2013
@@ -168,6 +168,19 @@ TEST_F(FormatTest, RemovesTrailingWhites
EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
}
+TEST_F(FormatTest, ReformatsMovedLines) {
+ EXPECT_EQ(
+ "template <typename T> T *getFETokenInfo() const {\n"
+ " return static_cast<T *>(FETokenInfo);\n"
+ "}\n"
+ " int a; // <- Should not be formatted",
+ format(
+ "template<typename T>\n"
+ "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
+ " int a; // <- Should not be formatted",
+ 9, 5, getLLVMStyle()));
+}
+
//===----------------------------------------------------------------------===//
// Tests for control statements.
//===----------------------------------------------------------------------===//
More information about the cfe-commits
mailing list