Skip to content

Commit 6b8a9ff

Browse files
gedareowenca
authored andcommitted
[clang-format] Fix RAS reference alignment when PAS is left or middle
Fixes a bug with the handling of right aligned references with left/middle alignment pointers. Fixes #63452. Differential Revision: https://reviews.llvm.org/D153579
1 parent fd89299 commit 6b8a9ff

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,24 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
445445
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
446446

447447
// If PointerAlignment is PAS_Right, keep *s or &s next to the token
448-
if (Style.PointerAlignment == FormatStyle::PAS_Right &&
448+
if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
449+
Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
449450
Changes[i].Spaces != 0) {
451+
const bool ReferenceNotRightAligned =
452+
Style.ReferenceAlignment != FormatStyle::RAS_Right &&
453+
Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
450454
for (int Previous = i - 1;
451455
Previous >= 0 &&
452456
Changes[Previous].Tok->getType() == TT_PointerOrReference;
453457
--Previous) {
458+
assert(
459+
Changes[Previous].Tok->isOneOf(tok::star, tok::amp, tok::ampamp));
460+
if (Changes[Previous].Tok->isNot(tok::star)) {
461+
if (ReferenceNotRightAligned)
462+
continue;
463+
} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
464+
continue;
465+
}
454466
Changes[Previous + 1].Spaces -= Shift;
455467
Changes[Previous].Spaces += Shift;
456468
Changes[Previous].StartOfTokenColumn += Shift;

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,15 @@ TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
21892189
verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
21902190
verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
21912191

2192+
Style.AlignConsecutiveDeclarations.Enabled = true;
2193+
verifyFormat("Const unsigned int * c;\n"
2194+
"const unsigned int * d;\n"
2195+
"Const unsigned int &e;\n"
2196+
"const unsigned int &f;\n"
2197+
"const unsigned &&g;\n"
2198+
"Const unsigned h;",
2199+
Style);
2200+
21922201
// FIXME: we don't handle this yet, so output may be arbitrary until it's
21932202
// specifically handled
21942203
// verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);

0 commit comments

Comments
 (0)