summaryrefslogtreecommitdiffstats
path: root/lib/Format/WhitespaceManager.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-10-07 15:03:26 +0000
committerDaniel Jasper <djasper@google.com>2015-10-07 15:03:26 +0000
commit304dc5d7802b8cb863335bcc4b535dc2ba1ee7d5 (patch)
tree47164ba07963ef5942ccbdce841fa72d1b821b4c /lib/Format/WhitespaceManager.cpp
parent895dbe3435d8428161e45e1c11d61b50126b8ecb (diff)
[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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@249552 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/WhitespaceManager.cpp')
-rw-r--r--lib/Format/WhitespaceManager.cpp36
1 files changed, 28 insertions, 8 deletions
diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp
index 1b11e73c53..3536132e9e 100644
--- a/lib/Format/WhitespaceManager.cpp
+++ b/lib/Format/WhitespaceManager.cpp
@@ -153,6 +153,9 @@ void WhitespaceManager::calculateLineBreakInformation() {
// 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::alignConsecutiveAssignments() {
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::alignConsecutiveAssignments() {
};
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::alignConsecutiveAssignments() {
}
FoundAssignmentOnLine = false;
+ FoundLeftBraceOnLine = false;
FoundLeftParenOnLine = false;
}
@@ -202,14 +209,24 @@ void WhitespaceManager::alignConsecutiveAssignments() {
(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::alignConsecutiveAssignments(unsigned Start,
// 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;