summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-03-07 20:50:00 +0000
committerDaniel Jasper <djasper@google.com>2013-03-07 20:50:00 +0000
commitf71cf3bd3e08d72e99d7b988da4e2f1078009ff8 (patch)
treea32b6623bd9d804962b0a1fede831378cbbdf081 /clang/lib
parent9c1796f8775ec64e42bf845703cc7b06e1804924 (diff)
downloadbcm5719-llvm-f71cf3bd3e08d72e99d7b988da4e2f1078009ff8.tar.gz
bcm5719-llvm-f71cf3bd3e08d72e99d7b988da4e2f1078009ff8.zip
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;". llvm-svn: 176650
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Format/Format.cpp32
1 files changed, 22 insertions, 10 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2b82410cf87..e738a5c3f3f 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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));
}
OpenPOWER on IntegriCloud