summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-05-14 09:30:02 +0000
committerDaniel Jasper <djasper@google.com>2013-05-14 09:30:02 +0000
commit1fb8d8814ae28a4ebe94652faafad8545a115bfe (patch)
tree16180a3abc0b42737856707915195e4a22a7f697
parentf7f295f321fd434e1e542844a71f538a56f2f8fb (diff)
downloadclang-1fb8d8814ae28a4ebe94652faafad8545a115bfe.tar.gz
clang-1fb8d8814ae28a4ebe94652faafad8545a115bfe.tar.bz2
clang-1fb8d8814ae28a4ebe94652faafad8545a115bfe.tar.xz
Fix clang-format bug in unwrapped-line merging.
Before (in styles that allow it), clang-format would not merge an if statement onto a single line, if only the second line was format (e.g. in an editor integration): if (a) return; // clang-format invoked on this line. With this patch, this gets properly merged to: if (a) return; // ... git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181770 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Format/Format.cpp28
-rw-r--r--unittests/Format/FormatTest.cpp10
2 files changed, 19 insertions, 19 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index b8e0455c71..2bab6e7353 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -44,7 +44,7 @@ struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
}
};
-template<>
+template <>
struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
static void
enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
@@ -1193,9 +1193,15 @@ public:
(touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
FormatPPDirective = true;
+ // Determine indent and try to merge multiple unwrapped lines.
while (IndentForLevel.size() <= TheLine.Level)
IndentForLevel.push_back(-1);
IndentForLevel.resize(TheLine.Level + 1);
+ unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
+ if (static_cast<int>(Indent) + Offset >= 0)
+ Indent += Offset;
+ tryFitMultipleLinesInOne(Indent, I, E);
+
bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
if (TheLine.First.is(tok::eof)) {
if (PreviousLineWasTouched) {
@@ -1206,9 +1212,6 @@ public:
} else if (TheLine.Type != LT_Invalid &&
(WasMoved || FormatPPDirective || touchesLine(TheLine))) {
unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
- unsigned Indent = LevelIndent;
- if (static_cast<int>(Indent) + Offset >= 0)
- Indent += Offset;
if (FirstTok.WhiteSpaceStart.isValid() &&
// Insert a break even if there is a structural error in case where
// we break apart a line consisting of multiple unwrapped lines.
@@ -1219,7 +1222,6 @@ public:
Indent = LevelIndent =
SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
}
- tryFitMultipleLinesInOne(Indent, I, E);
UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
TheLine.First, Whitespaces);
PreviousEndOfLineColumn =
@@ -1228,18 +1230,17 @@ public:
PreviousLineWasTouched = true;
} else {
if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
- unsigned Indent =
+ unsigned LevelIndent =
SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
- unsigned LevelIndent = Indent;
+ // Remove trailing whitespace of the previous line if it was touched.
+ if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
+ formatFirstToken(TheLine.First, PreviousLineLastToken, LevelIndent,
+ TheLine.InPPDirective, PreviousEndOfLineColumn);
+
if (static_cast<int>(LevelIndent) - Offset >= 0)
LevelIndent -= Offset;
if (TheLine.First.isNot(tok::comment))
IndentForLevel[TheLine.Level] = LevelIndent;
-
- // Remove trailing whitespace of the previous line if it was touched.
- if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
- formatFirstToken(TheLine.First, PreviousLineLastToken, Indent,
- TheLine.InPPDirective, PreviousEndOfLineColumn);
}
// If we did not reformat this unwrapped line, the column at the end of
// the last token is unchanged - thus, we can calculate the end of the
@@ -1327,8 +1328,6 @@ private:
///
/// This will change \c Line and \c AnnotatedLine to contain the merged line,
/// if possible; note that \c I will be incremented when lines are merged.
- ///
- /// Returns whether the resulting \c Line can fit in a single line.
void tryFitMultipleLinesInOne(unsigned Indent,
std::vector<AnnotatedLine>::iterator &I,
std::vector<AnnotatedLine>::iterator E) {
@@ -1352,7 +1351,6 @@ private:
I->First.FormatTok.IsFirst)) {
tryMergeSimplePPDirective(I, E, Limit);
}
- return;
}
void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index b7f544e8a1..19c5215a3f 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -245,6 +245,8 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
"}",
AllowsMergedIf);
+ EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
+
AllowsMergedIf.ColumnLimit = 14;
verifyFormat("if (a) return;", AllowsMergedIf);
verifyFormat("if (aaaaaaaaa)\n"
@@ -3810,7 +3812,7 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
"a;\n"
"}\n"
"{\n"
- " b;\n"
+ " b; //\n"
"}\n"
"}",
format("{\n"
@@ -3818,15 +3820,15 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
"a;\n"
"}\n"
"{\n"
- " b;\n"
+ " b; //\n"
"}\n"
"}",
22, 2, getLLVMStyle()));
EXPECT_EQ(" {\n"
- " a;\n"
+ " a; //\n"
" }",
format(" {\n"
- "a;\n"
+ "a; //\n"
" }",
4, 2, getLLVMStyle()));
EXPECT_EQ("void f() {}\n"