Skip to content

Commit dd6a6ba

Browse files
[clang-format] Reuse AlignTokens for aligning macros (#164120)
Fixes #52985. This leaves aligning short case statements with its own logic. But that is harder to port, because it aligns even with no content of the right hand side of the :.
1 parent e457097 commit dd6a6ba

File tree

2 files changed

+26
-67
lines changed

2 files changed

+26
-67
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,11 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
432432
// right-justified. It is used to align compound assignments like `+=` and `=`.
433433
// When RightJustify and ACS.PadOperators are true, operators in each block to
434434
// be aligned will be padded on the left to the same length before aligning.
435-
template <typename F>
435+
//
436+
// The simple check will not look at the indentaion and nesting level to recurse
437+
// into the line for alignment. It will also not count the commas. This is e.g.
438+
// for aligning macro definitions.
439+
template <typename F, bool SimpleCheck = false>
436440
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
437441
SmallVector<WhitespaceManager::Change, 16> &Changes,
438442
unsigned StartAt,
@@ -465,9 +469,9 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
465469

466470
// Measure the scope level (i.e. depth of (), [], {}) of the first token, and
467471
// abort when we hit any token in a higher scope than the starting one.
468-
auto IndentAndNestingLevel = StartAt < Changes.size()
469-
? Changes[StartAt].indentAndNestingLevel()
470-
: std::tuple<unsigned, unsigned, unsigned>();
472+
const auto IndentAndNestingLevel =
473+
StartAt < Changes.size() ? Changes[StartAt].indentAndNestingLevel()
474+
: std::tuple<unsigned, unsigned, unsigned>();
471475

472476
// Keep track of the number of commas before the matching tokens, we will only
473477
// align a sequence of matching tokens if they are preceded by the same number
@@ -536,14 +540,17 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
536540
if (CurrentChange.Tok->isNot(tok::comment))
537541
LineIsComment = false;
538542

539-
if (CurrentChange.Tok->is(tok::comma)) {
540-
++CommasBeforeMatch;
541-
} else if (CurrentChange.indentAndNestingLevel() > IndentAndNestingLevel) {
542-
// Call AlignTokens recursively, skipping over this scope block.
543-
unsigned StoppedAt =
544-
AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
545-
i = StoppedAt - 1;
546-
continue;
543+
if (!SimpleCheck) {
544+
if (CurrentChange.Tok->is(tok::comma)) {
545+
++CommasBeforeMatch;
546+
} else if (CurrentChange.indentAndNestingLevel() >
547+
IndentAndNestingLevel) {
548+
// Call AlignTokens recursively, skipping over this scope block.
549+
const auto StoppedAt =
550+
AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
551+
i = StoppedAt - 1;
552+
continue;
553+
}
547554
}
548555

549556
if (!Matches(CurrentChange))
@@ -683,61 +690,8 @@ void WhitespaceManager::alignConsecutiveMacros() {
683690
return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
684691
};
685692

686-
unsigned MinColumn = 0;
687-
688-
// Start and end of the token sequence we're processing.
689-
unsigned StartOfSequence = 0;
690-
unsigned EndOfSequence = 0;
691-
692-
// Whether a matching token has been found on the current line.
693-
bool FoundMatchOnLine = false;
694-
695-
// Whether the current line consists only of comments
696-
bool LineIsComment = true;
697-
698-
unsigned I = 0;
699-
for (unsigned E = Changes.size(); I != E; ++I) {
700-
if (Changes[I].NewlinesBefore != 0) {
701-
EndOfSequence = I;
702-
703-
// Whether to break the alignment sequence because of an empty line.
704-
bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
705-
!Style.AlignConsecutiveMacros.AcrossEmptyLines;
706-
707-
// Whether to break the alignment sequence because of a line without a
708-
// match.
709-
bool NoMatchBreak =
710-
!FoundMatchOnLine &&
711-
!(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
712-
713-
if (EmptyLineBreak || NoMatchBreak) {
714-
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
715-
AlignMacrosMatches, Changes);
716-
}
717-
718-
// A new line starts, re-initialize line status tracking bools.
719-
FoundMatchOnLine = false;
720-
LineIsComment = true;
721-
}
722-
723-
if (Changes[I].Tok->isNot(tok::comment))
724-
LineIsComment = false;
725-
726-
if (!AlignMacrosMatches(Changes[I]))
727-
continue;
728-
729-
FoundMatchOnLine = true;
730-
731-
if (StartOfSequence == 0)
732-
StartOfSequence = I;
733-
734-
unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
735-
MinColumn = std::max(MinColumn, ChangeMinColumn);
736-
}
737-
738-
EndOfSequence = I;
739-
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
740-
AlignMacrosMatches, Changes);
693+
AlignTokens<decltype(AlignMacrosMatches) &, /*SimpleCheck=*/true>(
694+
Style, AlignMacrosMatches, Changes, 0, Style.AlignConsecutiveMacros);
741695
}
742696

743697
void WhitespaceManager::alignConsecutiveAssignments() {

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18559,6 +18559,11 @@ TEST_F(FormatTest, AlignConsecutiveMacros) {
1855918559
"#define bbbb 4\n"
1856018560
"#define ccc (5)",
1856118561
Style);
18562+
18563+
Style.ColumnLimit = 30;
18564+
verifyFormat("#define MY_FUNC(x) callMe(X)\n"
18565+
"#define MY_LONG_CONSTANT 17",
18566+
Style);
1856218567
}
1856318568

1856418569
TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {

0 commit comments

Comments
 (0)