berenm updated this revision to Diff 39232.
berenm added a comment.
[clang-format] Alignment code factorization.
http://reviews.llvm.org/D14325
Files:
lib/Format/WhitespaceManager.cpp
unittests/Format/FormatTest.cpp
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -8703,6 +8703,19 @@
" loooooooooooooooooooooongParameterB);\n"
"int j = 2;",
Alignment);
+
+ verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
+ " typename B = very_long_type_name_1,\n"
+ " typename T_2 = very_long_type_name_2>\n"
+ "auto foo() {}\n",
+ Alignment);
+ verifyFormat("int a, b = 1;\n"
+ "int c = 2;\n"
+ "int dd = 3;\n",
+ Alignment);
+ verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
+ "float b[1][] = {{3.f}};\n",
+ Alignment);
}
TEST_F(FormatTest, AlignConsecutiveDeclarations) {
@@ -8903,6 +8916,29 @@
"int myvar = 1;",
Alignment);
Alignment.ColumnLimit = 80;
+ Alignment.AlignConsecutiveAssignments = false;
+
+ verifyFormat(
+ "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
+ " typename LongType, typename B>\n"
+ "auto foo() {}\n",
+ Alignment);
+ verifyFormat("float a, b = 1;\n"
+ "int c = 2;\n"
+ "int dd = 3;\n",
+ Alignment);
+ verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
+ "float b[1][] = {{3.f}};\n",
+ Alignment);
+ Alignment.AlignConsecutiveAssignments = true;
+ verifyFormat("float a, b = 1;\n"
+ "int c = 2;\n"
+ "int dd = 3;\n",
+ Alignment);
+ verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
+ "float b[1][] = {{3.f}};\n",
+ Alignment);
+ Alignment.AlignConsecutiveAssignments = false;
}
TEST_F(FormatTest, LinuxBraceBreaking) {
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -148,86 +148,108 @@
}
}
-// Walk through all of the changes and find sequences of "=" to align. To do
-// so, keep track of the lines and whether or not an "=" was found on align. If
-// a "=" is found on a line, extend the current sequence. If the current line
-// cannot be part of a sequence, e.g. because there is an empty line before it
-// or it contains non-assignments, finalize the previous sequence.
-//
-// FIXME: The code between assignment and declaration alignment is mostly
-// duplicated and would benefit from factorization.
-void WhitespaceManager::alignConsecutiveAssignments() {
- if (!Style.AlignConsecutiveAssignments)
- return;
-
+struct TokenSequenceAligner {
+ template <typename F>
+ void align(const FormatStyle &Style, F &&Matches,
+ SmallVector<WhitespaceManager::Change, 16> &Changes);
+
+ template <typename F>
+ void alignSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
+ SmallVector<WhitespaceManager::Change, 16> &Changes);
+};
+
+// Walk through all of the changes and find sequences of matching tokens to
+// align. To do so, keep track of the lines and whether or not a matching token
+// was found on a line. If a matching token is found, extend the current
+// sequence. If the current line cannot be part of a sequence, e.g. because
+// there is an empty line before it or it contains only non-matching tokens,
+// finalize the previous sequence.
+template <typename F>
+void TokenSequenceAligner::align(
+ const FormatStyle &Style, F &&Matches,
+ SmallVector<WhitespaceManager::Change, 16> &Changes) {
unsigned MinColumn = 0;
unsigned MaxColumn = UINT_MAX;
+
+ // Line number of the start and the end of the current token sequence.
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
- bool FoundAssignmentOnLine = false;
- bool FoundLeftBraceOnLine = false;
- bool FoundLeftParenOnLine = false;
- // Aligns a sequence of assignment tokens, on the MinColumn column.
+ // Keep track of the number of braces and parentheses on the current line. If
+ // they aren't balanced, we will end the sequence.
+ unsigned LeftBracesOnLine = 0;
+ unsigned LeftParensOnLine = 0;
+
+ // Keep track of the number of commas on the line, we will only align a
+ // sequence of matching tokens if they are preceded by the same number of
+ // commas.
+ unsigned CommasOnPrevLine = 0;
+ unsigned CommasOnLine = 0;
+
+ // Whether a matching token has been found on the current line.
+ bool FoundOnLine = false;
+
+ // Aligns a sequence of matching tokens, on the MinColumn column.
//
- // Sequences start from the first assignment token to align, and end at the
+ // Sequences start from the first matching token to align, and end at the
// first token of the first line that doesn't need to be aligned.
//
// We need to adjust the StartOfTokenColumn of each Change that is on a line
- // containing any assignment to be aligned and located after such assignment
- auto AlignSequence = [&] {
+ // containing any matching token to be aligned and located after such token.
+ auto AlignCurrentSequence = [&] {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
- alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn);
+ alignSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
+ Changes);
MinColumn = 0;
MaxColumn = UINT_MAX;
StartOfSequence = 0;
EndOfSequence = 0;
};
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
if (Changes[i].NewlinesBefore != 0) {
+ CommasOnPrevLine = CommasOnLine;
+ CommasOnLine = 0;
EndOfSequence = i;
// If there is a blank line, if the last line didn't contain any
- // assignment, or if we found an open brace or paren, the sequence ends
- // here.
- if (Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine ||
- FoundLeftBraceOnLine || FoundLeftParenOnLine) {
- // NB: In the latter case, the sequence should end at the beggining of
- // the previous line, but it doesn't really matter as there is no
- // assignment on it
- AlignSequence();
- }
-
- FoundAssignmentOnLine = false;
- FoundLeftBraceOnLine = false;
- FoundLeftParenOnLine = false;
+ // matching token, or if we found unbalanced braces or parentheses, the
+ // sequence ends here.
+ if (Changes[i].NewlinesBefore > 1 || !FoundOnLine ||
+ LeftBracesOnLine > 0 || LeftParensOnLine > 0)
+ AlignCurrentSequence();
+
+ FoundOnLine = false;
+ LeftBracesOnLine = 0;
+ LeftParensOnLine = 0;
}
- // If there is more than one "=" per line, or if the "=" appears first on
- // the line of if it appears last, end the sequence
- if (Changes[i].Kind == tok::equal &&
- (FoundAssignmentOnLine || Changes[i].NewlinesBefore > 0 ||
- Changes[i + 1].NewlinesBefore > 0)) {
- AlignSequence();
+ // If there is more than one matching token per line, end the sequence.
+ if (Matches(Changes[i]) && FoundOnLine) {
+ AlignCurrentSequence();
} else if (Changes[i].Kind == tok::r_brace) {
- if (!FoundLeftBraceOnLine)
- AlignSequence();
- FoundLeftBraceOnLine = false;
+ if (LeftBracesOnLine == 0)
+ AlignCurrentSequence();
+ else
+ --LeftBracesOnLine;
} else if (Changes[i].Kind == tok::l_brace) {
- FoundLeftBraceOnLine = true;
- if (!FoundAssignmentOnLine)
- AlignSequence();
+ ++LeftBracesOnLine;
+ if (!FoundOnLine)
+ AlignCurrentSequence();
} else if (Changes[i].Kind == tok::r_paren) {
- if (!FoundLeftParenOnLine)
- AlignSequence();
- FoundLeftParenOnLine = false;
+ if (LeftParensOnLine == 0)
+ AlignCurrentSequence();
+ else
+ --LeftParensOnLine;
} else if (Changes[i].Kind == tok::l_paren) {
- FoundLeftParenOnLine = true;
- if (!FoundAssignmentOnLine)
- AlignSequence();
- } else if (!FoundAssignmentOnLine && !FoundLeftBraceOnLine &&
- !FoundLeftParenOnLine && Changes[i].Kind == tok::equal) {
- FoundAssignmentOnLine = true;
+ ++LeftParensOnLine;
+ if (!FoundOnLine)
+ AlignCurrentSequence();
+ } else if (Changes[i].Kind == tok::comma) {
+ if (!FoundOnLine)
+ ++CommasOnLine;
+ } else if (!FoundOnLine && LeftBracesOnLine == 0 && LeftParensOnLine == 0 &&
+ Matches(Changes[i])) {
+ FoundOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = i;
@@ -237,8 +259,9 @@
LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
- if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) {
- AlignSequence();
+ if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
+ CommasOnPrevLine != CommasOnLine) {
+ AlignCurrentSequence();
StartOfSequence = i;
}
@@ -248,25 +271,26 @@
}
EndOfSequence = Changes.size();
- AlignSequence();
+ AlignCurrentSequence();
}
-void WhitespaceManager::alignConsecutiveAssignments(unsigned Start,
- unsigned End,
- unsigned Column) {
- bool FoundAssignmentOnLine = false;
+template <typename F>
+void TokenSequenceAligner::alignSequence(
+ unsigned Start, unsigned End, unsigned Column, F &&Matches,
+ SmallVector<WhitespaceManager::Change, 16> &Changes) {
+ bool FoundOnLine = false;
int Shift = 0;
for (unsigned i = Start; i != End; ++i) {
if (Changes[i].NewlinesBefore > 0) {
- FoundAssignmentOnLine = false;
+ FoundOnLine = false;
Shift = 0;
}
- // If this is the first assignment to be aligned, remember by how many
+ // If this is the first matching token to be aligned, remember by how many
// spaces it has to be shifted, so the rest of the changes on the line are
// shifted by the same amount
- if (!FoundAssignmentOnLine && Changes[i].Kind == tok::equal) {
- FoundAssignmentOnLine = true;
+ if (!FoundOnLine && Matches(Changes[i])) {
+ FoundOnLine = true;
Shift = Column - Changes[i].StartOfTokenColumn;
Changes[i].Spaces += Shift;
}
@@ -278,111 +302,33 @@
}
}
-// Walk through all of the changes and find sequences of declaration names to
-// align. To do so, keep track of the lines and whether or not a name was found
-// on align. If a name is found on a line, extend the current sequence. If the
-// current line cannot be part of a sequence, e.g. because there is an empty
-// line before it or it contains non-declarations, finalize the previous
-// sequence.
-//
-// FIXME: The code between assignment and declaration alignment is mostly
-// duplicated and would benefit from factorization.
-void WhitespaceManager::alignConsecutiveDeclarations() {
- if (!Style.AlignConsecutiveDeclarations)
+void WhitespaceManager::alignConsecutiveAssignments() {
+ if (!Style.AlignConsecutiveAssignments)
return;
- unsigned MinColumn = 0;
- unsigned MaxColumn = UINT_MAX;
- unsigned StartOfSequence = 0;
- unsigned EndOfSequence = 0;
- bool FoundDeclarationOnLine = false;
- bool FoundLeftBraceOnLine = false;
- bool FoundLeftParenOnLine = false;
-
- auto AlignSequence = [&] {
- if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
- alignConsecutiveDeclarations(StartOfSequence, EndOfSequence, MinColumn);
- MinColumn = 0;
- MaxColumn = UINT_MAX;
- StartOfSequence = 0;
- EndOfSequence = 0;
- };
+ TokenSequenceAligner aligner;
+ aligner.align(Style,
+ [&](const Change &C) {
+ // Do not align on equal signs that are first on a line.
+ if (C.NewlinesBefore > 0)
+ return false;
- for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
- if (Changes[i].NewlinesBefore != 0) {
- EndOfSequence = i;
- if (Changes[i].NewlinesBefore > 1 || !FoundDeclarationOnLine ||
- FoundLeftBraceOnLine || FoundLeftParenOnLine)
- AlignSequence();
- FoundDeclarationOnLine = false;
- FoundLeftBraceOnLine = false;
- FoundLeftParenOnLine = false;
- }
+ // Do not align on equal signs that are last on a line.
+ if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
+ return false;
- if (Changes[i].Kind == tok::r_brace) {
- if (!FoundLeftBraceOnLine)
- AlignSequence();
- FoundLeftBraceOnLine = false;
- } else if (Changes[i].Kind == tok::l_brace) {
- FoundLeftBraceOnLine = true;
- if (!FoundDeclarationOnLine)
- AlignSequence();
- } else if (Changes[i].Kind == tok::r_paren) {
- if (!FoundLeftParenOnLine)
- AlignSequence();
- FoundLeftParenOnLine = false;
- } else if (Changes[i].Kind == tok::l_paren) {
- FoundLeftParenOnLine = true;
- if (!FoundDeclarationOnLine)
- AlignSequence();
- } else if (!FoundDeclarationOnLine && !FoundLeftBraceOnLine &&
- !FoundLeftParenOnLine && Changes[i].IsStartOfDeclName) {
- FoundDeclarationOnLine = true;
- if (StartOfSequence == 0)
- StartOfSequence = i;
-
- unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
- int LineLengthAfter = -Changes[i].Spaces;
- for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
- LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
- unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
-
- if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) {
- AlignSequence();
- StartOfSequence = i;
- }
-
- MinColumn = std::max(MinColumn, ChangeMinColumn);
- MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
- }
- }
-
- EndOfSequence = Changes.size();
- AlignSequence();
+ return C.Kind == tok::equal;
+ },
+ Changes);
}
-void WhitespaceManager::alignConsecutiveDeclarations(unsigned Start,
- unsigned End,
- unsigned Column) {
- bool FoundDeclarationOnLine = false;
- int Shift = 0;
- for (unsigned i = Start; i != End; ++i) {
- if (Changes[i].NewlinesBefore != 0) {
- FoundDeclarationOnLine = false;
- Shift = 0;
- }
-
- if (!FoundDeclarationOnLine && Changes[i].IsStartOfDeclName) {
- FoundDeclarationOnLine = true;
- Shift = Column - Changes[i].StartOfTokenColumn;
- Changes[i].Spaces += Shift;
- }
+void WhitespaceManager::alignConsecutiveDeclarations() {
+ if (!Style.AlignConsecutiveDeclarations)
+ return;
- assert(Shift >= 0);
- Changes[i].StartOfTokenColumn += Shift;
- if (i + 1 != Changes.size())
- Changes[i + 1].PreviousEndOfTokenColumn += Shift;
- }
+ TokenSequenceAligner aligner;
+ aligner.align(Style, [](Change const &C) { return C.IsStartOfDeclName; },
+ Changes);
}
void WhitespaceManager::alignTrailingComments() {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits