Skip to content

Commit 3cd9df8

Browse files
mkurdejtstellar
authored andcommitted
[clang-format] Fix PointerAlignment: Right not working with tab indentation.
Fixes llvm#55407. Given configuration: ``` UseTab: Always PointerAlignment: Right AlignConsecutiveDeclarations: true ``` Before, the pointer was misaligned in this code: ``` void f() { unsigned long long big; char *ptr; // misaligned int i; } ``` That was due to the fact that when handling right-aligned pointers, the Spaces were changed but StartOfTokenColumn was not. Also, a tab was used not only for indentation but for spacing too when using `UseTab: ForIndentation` config option: ``` void f() { unsigned long long big; char *ptr; // \t after char int i; } ``` Reviewed By: owenpan Differential Revision: https://reviews.llvm.org/D125528
1 parent d350783 commit 3cd9df8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
414414
--Previous) {
415415
Changes[Previous + 1].Spaces -= Shift;
416416
Changes[Previous].Spaces += Shift;
417+
Changes[Previous].StartOfTokenColumn += Shift;
417418
}
418419
}
419420
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13687,6 +13687,21 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
1368713687
"int bbbbbbbb; // x\n",
1368813688
Tab);
1368913689

13690+
FormatStyle TabAlignment = Tab;
13691+
TabAlignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13692+
TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
13693+
verifyFormat("unsigned long long big;\n"
13694+
"char*\t\t ptr;",
13695+
TabAlignment);
13696+
TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
13697+
verifyFormat("unsigned long long big;\n"
13698+
"char *\t\t ptr;",
13699+
TabAlignment);
13700+
TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
13701+
verifyFormat("unsigned long long big;\n"
13702+
"char\t\t *ptr;",
13703+
TabAlignment);
13704+
1369013705
Tab.TabWidth = 4;
1369113706
Tab.IndentWidth = 8;
1369213707
verifyFormat("class TabWidth4Indent8 {\n"
@@ -13729,6 +13744,26 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
1372913744
" \t */",
1373013745
Tab));
1373113746

13747+
TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
13748+
TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
13749+
verifyFormat("void f() {\n"
13750+
"\tunsigned long long big;\n"
13751+
"\tchar* ptr;\n"
13752+
"}",
13753+
TabAlignment);
13754+
TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
13755+
verifyFormat("void f() {\n"
13756+
"\tunsigned long long big;\n"
13757+
"\tchar * ptr;\n"
13758+
"}",
13759+
TabAlignment);
13760+
TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
13761+
verifyFormat("void f() {\n"
13762+
"\tunsigned long long big;\n"
13763+
"\tchar *ptr;\n"
13764+
"}",
13765+
TabAlignment);
13766+
1373213767
Tab.UseTab = FormatStyle::UT_ForIndentation;
1373313768
verifyFormat("{\n"
1373413769
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"

0 commit comments

Comments
 (0)