r249552 - [clang-format] Stop alignment sequences on open braces and parens when
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 7 08:03:26 PDT 2015
Author: djasper
Date: Wed Oct 7 10:03:26 2015
New Revision: 249552
URL: http://llvm.org/viewvc/llvm-project?rev=249552&view=rev
Log:
[clang-format] Stop alignment sequences on open braces and parens when
aligning assignments.
This was done correctly when aligning the declarations, but not when
aligning assignments.
FIXME: The code between assignments and declarations alignment is
roughly duplicated and
would benefit from factorization.
Bug 25090: https://llvm.org/bugs/show_bug.cgi?id=25090
Patch by Beren Minor. Thank you.
Modified:
cfe/trunk/lib/Format/WhitespaceManager.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=249552&r1=249551&r2=249552&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Wed Oct 7 10:03:26 2015
@@ -153,6 +153,9 @@ void WhitespaceManager::calculateLineBre
// 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;
@@ -162,6 +165,7 @@ void WhitespaceManager::alignConsecutive
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.
@@ -181,11 +185,13 @@ void WhitespaceManager::alignConsecutive
};
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
- if (Changes[i].NewlinesBefore > 0) {
+ if (Changes[i].NewlinesBefore != 0) {
EndOfSequence = i;
- // If there is a blank line or if the last line didn't contain any
- // assignment, the sequence ends here.
- if (Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine) {
+ // 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
@@ -193,6 +199,7 @@ void WhitespaceManager::alignConsecutive
}
FoundAssignmentOnLine = false;
+ FoundLeftBraceOnLine = false;
FoundLeftParenOnLine = false;
}
@@ -202,14 +209,24 @@ void WhitespaceManager::alignConsecutive
(FoundAssignmentOnLine || Changes[i].NewlinesBefore > 0 ||
Changes[i + 1].NewlinesBefore > 0)) {
AlignSequence();
- } else if (!FoundLeftParenOnLine && Changes[i].Kind == tok::r_paren) {
- AlignSequence();
+ } else if (Changes[i].Kind == tok::r_brace) {
+ if (!FoundLeftBraceOnLine)
+ AlignSequence();
+ FoundLeftBraceOnLine = false;
+ } else if (Changes[i].Kind == tok::l_brace) {
+ FoundLeftBraceOnLine = true;
+ if (!FoundAssignmentOnLine)
+ 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 (!FoundAssignmentOnLine)
AlignSequence();
- } else if (!FoundAssignmentOnLine && !FoundLeftParenOnLine &&
- Changes[i].Kind == tok::equal) {
+ } else if (!FoundAssignmentOnLine && !FoundLeftBraceOnLine &&
+ !FoundLeftParenOnLine && Changes[i].Kind == tok::equal) {
FoundAssignmentOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = i;
@@ -267,6 +284,9 @@ void WhitespaceManager::alignConsecutive
// 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)
return;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=249552&r1=249551&r2=249552&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Oct 7 10:03:26 2015
@@ -8649,6 +8649,19 @@ TEST_F(FormatTest, AlignConsecutiveAssig
" someLooooooooooooooooongFunction();\n"
"int j = 2;",
Alignment);
+
+ verifyFormat("auto lambda = []() {\n"
+ " auto i = 0;\n"
+ " return 0;\n"
+ "};\n"
+ "int i = 0;\n"
+ "auto v = type{\n"
+ " i = 1, //\n"
+ " (i = 2), //\n"
+ " i = 3 //\n"
+ "};",
+ Alignment);
+
// FIXME: Should align all three assignments
verifyFormat(
"int i = 1;\n"
@@ -8817,6 +8830,23 @@ TEST_F(FormatTest, AlignConsecutiveDecla
" someLooooooooooooooooongFunction();\n"
"int j = 2;",
Alignment);
+
+ Alignment.AlignConsecutiveAssignments = true;
+ verifyFormat("auto lambda = []() {\n"
+ " auto ii = 0;\n"
+ " float j = 0;\n"
+ " return 0;\n"
+ "};\n"
+ "int i = 0;\n"
+ "float i2 = 0;\n"
+ "auto v = type{\n"
+ " i = 1, //\n"
+ " (i = 2), //\n"
+ " i = 3 //\n"
+ "};",
+ Alignment);
+ Alignment.AlignConsecutiveAssignments = false;
+
// FIXME: Should align all three declarations
verifyFormat(
"int i = 1;\n"
More information about the cfe-commits
mailing list