r176650 - Remove unncessary whitespace when triggered on empty line.
Daniel Jasper
djasper at google.com
Thu Mar 7 12:50:00 PST 2013
Author: djasper
Date: Thu Mar 7 14:50:00 2013
New Revision: 176650
URL: http://llvm.org/viewvc/llvm-project?rev=176650&view=rev
Log:
Remove unncessary whitespace when triggered on empty line.
With the cursor located at "I", clang-format would not do anything to:
int a;
I
int b;
With this patch, it reduces the number of empty lines as necessary, and
removes unnecessary whitespace. It does not change/reformat "int a;" or
"int b;".
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=176650&r1=176649&r2=176650&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Mar 7 14:50:00 2013
@@ -1143,7 +1143,7 @@ public:
/*WhitespaceStartColumn*/ 0, Style);
}
} else if (TheLine.Type != LT_Invalid &&
- (WasMoved || touchesRanges(TheLine))) {
+ (WasMoved || touchesLine(TheLine))) {
unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
unsigned Indent = LevelIndent;
if (static_cast<int>(Indent) + Offset >= 0)
@@ -1175,7 +1175,7 @@ public:
IndentForLevel[TheLine.Level] = LevelIndent;
// Remove trailing whitespace of the previous line if it was touched.
- if (PreviousLineWasTouched)
+ if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
PreviousEndOfLineColumn);
}
@@ -1370,22 +1370,34 @@ private:
}
}
- bool touchesRanges(const AnnotatedLine &TheLine) {
- const FormatToken *First = &TheLine.First.FormatTok;
- const FormatToken *Last = &TheLine.Last->FormatTok;
- CharSourceRange LineRange = CharSourceRange::getTokenRange(
- First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
- Last->Tok.getLocation());
+ bool touchesRanges(const CharSourceRange& Range) {
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
- if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
+ if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
Ranges[i].getBegin()) &&
!SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
- LineRange.getBegin()))
+ Range.getBegin()))
return true;
}
return false;
}
+ bool touchesLine(const AnnotatedLine &TheLine) {
+ const FormatToken *First = &TheLine.First.FormatTok;
+ const FormatToken *Last = &TheLine.Last->FormatTok;
+ CharSourceRange LineRange = CharSourceRange::getTokenRange(
+ First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
+ Last->Tok.getLocation());
+ return touchesRanges(LineRange);
+ }
+
+ bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
+ const FormatToken *First = &TheLine.First.FormatTok;
+ CharSourceRange LineRange = CharSourceRange::getCharRange(
+ First->WhiteSpaceStart,
+ First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
+ return touchesRanges(LineRange);
+ }
+
virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
AnnotatedLines.push_back(AnnotatedLine(TheLine));
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176650&r1=176649&r2=176650&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 7 14:50:00 2013
@@ -194,6 +194,13 @@ TEST_F(FormatTest, FormatsCorrectRegionF
25, 0, getLLVMStyleWithColumns(12)));
}
+TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
+ EXPECT_EQ("int a;\n\n int b;",
+ format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
+ EXPECT_EQ("int a;\n\n int b;",
+ format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
+}
+
TEST_F(FormatTest, ReformatsMovedLines) {
EXPECT_EQ(
"template <typename T> T *getFETokenInfo() const {\n"
More information about the cfe-commits
mailing list