Skip to content

Commit

Permalink
[clang-format] Fix PointerAlignment: Right not working with tab inden…
Browse files Browse the repository at this point in the history
…tation.

Fixes llvm/llvm-project#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
  • Loading branch information
mkurdej authored and tstellar committed Jun 9, 2022
1 parent d350783 commit 3cd9df8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
--Previous) {
Changes[Previous + 1].Spaces -= Shift;
Changes[Previous].Spaces += Shift;
Changes[Previous].StartOfTokenColumn += Shift;
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13687,6 +13687,21 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"int bbbbbbbb; // x\n",
Tab);

FormatStyle TabAlignment = Tab;
TabAlignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
verifyFormat("unsigned long long big;\n"
"char*\t\t ptr;",
TabAlignment);
TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
verifyFormat("unsigned long long big;\n"
"char *\t\t ptr;",
TabAlignment);
TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
verifyFormat("unsigned long long big;\n"
"char\t\t *ptr;",
TabAlignment);

Tab.TabWidth = 4;
Tab.IndentWidth = 8;
verifyFormat("class TabWidth4Indent8 {\n"
Expand Down Expand Up @@ -13729,6 +13744,26 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
" \t */",
Tab));

TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
verifyFormat("void f() {\n"
"\tunsigned long long big;\n"
"\tchar* ptr;\n"
"}",
TabAlignment);
TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
verifyFormat("void f() {\n"
"\tunsigned long long big;\n"
"\tchar * ptr;\n"
"}",
TabAlignment);
TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
verifyFormat("void f() {\n"
"\tunsigned long long big;\n"
"\tchar *ptr;\n"
"}",
TabAlignment);

Tab.UseTab = FormatStyle::UT_ForIndentation;
verifyFormat("{\n"
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
Expand Down

0 comments on commit 3cd9df8

Please sign in to comment.